UIActivityLimitedGiftView.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. local UIActivityLimitedGiftView = require("UIActivity/UIActivityLimitedGiftView_Generate")
  2. local IconItemCtr = require("Common/IconItemCtr")
  3. local updateHandle
  4. local curTime
  5. local interTime = 1
  6. function UIActivityLimitedGiftView:OnAwake(data)
  7. self.controller = require("UIActivity/UIActivityLimitedGiftCtr"):new()
  8. self.controller:Init(self)
  9. self.controller:SetData(data)
  10. end
  11. function UIActivityLimitedGiftView:AddEventListener()
  12. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name)
  13. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.LIMIT_RECHARGE_PAY_SUCCESS_NTF, self, self.OnPaySuccess)
  14. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.LIMIT_RECHARGE_OPEN_NTF, self, self.RefreshNew)
  15. end
  16. function UIActivityLimitedGiftView:FillContent(data, uiBase)
  17. self.uiBase = uiBase
  18. local gameObject = self.uiBase:GetRoot()
  19. if gameObject ~= nil then
  20. self.gameObject = gameObject
  21. self.transform = gameObject.transform
  22. end
  23. self:InitGenerate(self.transform, data)
  24. self:Init()
  25. end
  26. function UIActivityLimitedGiftView:Init()
  27. self.inited = false
  28. self.dot:SetActive(false)
  29. self.controller:InitLimitedGiftIdx()
  30. self:InitGird()
  31. end
  32. function UIActivityLimitedGiftView:InitUpdate()
  33. if not updateHandle then
  34. updateHandle = UpdateBeat:CreateListener(self.Update, self)
  35. end
  36. UpdateBeat:AddListener(updateHandle)
  37. curTime = 0
  38. end
  39. function UIActivityLimitedGiftView:InitGird()
  40. local initParam = SuperScrollView.LoopListViewInitParam()
  41. initParam.mSmoothDumpRate = 0.05
  42. initParam.mSnapVecThreshold = 9999
  43. self.scrollView.loopListView.OffsetSnapCenterPercent = 0.4
  44. self.scrollView.loopListView.ItemSnapEnable = true
  45. self.scrollView.loopListView:InitListView(-1, function(loopListView, idx)
  46. return self:GetItemByRowColumn(loopListView, idx)
  47. end, nil, initParam)
  48. self.scrollView.scrollRect.horizontal = false
  49. end
  50. function UIActivityLimitedGiftView:OnPageInEnd()
  51. self.super.OnPageInEnd(self)
  52. self:InitUpdate()
  53. self:Refresh()
  54. end
  55. function UIActivityLimitedGiftView:RefreshNew()
  56. if not self.controller:IsExist() then
  57. self:OnCloseClick()
  58. return
  59. end
  60. self.controller:InitLimitedGiftIdx()
  61. self:Refresh()
  62. end
  63. function UIActivityLimitedGiftView:Refresh()
  64. local idx = self.controller:GetLimitRechargeIdx()
  65. local length = self.controller:GetLimitRechargeCount()
  66. self.inited = true
  67. self:DisposeDot()
  68. self:RefreshDots()
  69. self.btnArrowR:SetActive(length > 1)
  70. self.btnArrowL:SetActive(length > 1)
  71. self.scrollView.scrollRect.horizontal = length > 1
  72. self.dotToggleGroup:SetActive(length > 1)
  73. if length == 0 then
  74. return
  75. end
  76. --self.scrollView.loopListView:SetListItemCount(length, false)
  77. self.scrollView.loopListView:MovePanelToItemIndex(idx - 1, 0)
  78. self.scrollView.loopListView:RefreshAllShownItem()
  79. end
  80. function UIActivityLimitedGiftView:RefreshDots()
  81. if not self.dots then self.dots = {} end
  82. local length = self.controller:GetLimitRechargeCount()
  83. local selectedIdx = self.controller:GetLimitRechargeIdx()
  84. local num = #self.dots
  85. local dot = nil
  86. local parent = self.dotToggleGroup.transform
  87. for i = 1, length do
  88. if i <= num then
  89. dot = self.dots[i]
  90. else
  91. dot = CommonUtil.Instantiate(self.dot.gameObject, parent)
  92. if tolua.getpeer(dot) == nil then
  93. tolua.setpeer(dot, {})
  94. end
  95. dot.toggle = dot:GetComponent(Enum.TypeInfo.Toggle)
  96. self.dots[i] = dot
  97. end
  98. dot:SetActive(true)
  99. dot.toggle.isOn = (i == selectedIdx)
  100. end
  101. for i = length + 1, num do
  102. dot = self.dots[i]
  103. dot:SetActive(false)
  104. end
  105. end
  106. function UIActivityLimitedGiftView:DisposeDot()
  107. if not self.dots then return end
  108. for _, dot in pairs(self.dots) do
  109. CommonUtil.DestroyGO(dot)
  110. dot.toggle = nil
  111. if tolua.getpeer(dot) ~= nil then
  112. tolua.setpeer(dot, nil)
  113. end
  114. end
  115. self.dots = nil
  116. end
  117. function UIActivityLimitedGiftView:GetItemByRowColumn(gridView, itemIndex, row, column)
  118. local data = self.controller:GetCurLimitRechargeData(itemIndex)
  119. if not data then return nil end
  120. local cfgData = ManagerContainer.CfgMgr:GetActivitiesDiscountsCfgById(data.activitiesId)
  121. if not data then return nil end
  122. local item = gridView:NewListViewItem('LimitedGiftItem')
  123. if not item then return nil end
  124. local itemlua = CommonUtil.BindGridViewItem2Lua(self, 'LimitedGiftItem', item.gameObject)
  125. local type = cfgData.TriggeringCondition and cfgData.TriggeringCondition[1][1] or 0
  126. local param1 = cfgData.TriggeringCondition and cfgData.TriggeringCondition[1][2] or 0
  127. local param2 = cfgData.TriggeringCondition and cfgData.TriggeringCondition[1][3] or 0
  128. if type == Enum.UnlockChargeType.DrawCardMVP or type == Enum.UnlockChargeType.DrawPetMVP then
  129. local itemCfgData = ManagerContainer.CfgMgr:GetItemById(param1)
  130. param1 = I18N.T(itemCfgData.Name)
  131. elseif type == Enum.UnlockChargeType.BattleLevel then
  132. local levelCfgData = ManagerContainer.CfgMgr:GetLevelDataById(param1)
  133. param1 = I18N.T(levelCfgData.Name)
  134. end
  135. if cfgData.PlaceholderCount == 0 then
  136. itemlua.levelText.uILocalizeScript:SetContentAndValues("DscGoddessTalk_"..type, {I18N.T(cfgData.Name)})
  137. elseif cfgData.PlaceholderCount == 1 then
  138. itemlua.levelText.uILocalizeScript:SetContentAndValues("DscGoddessTalk_"..type, {param1, I18N.T(cfgData.Name)})
  139. elseif cfgData.PlaceholderCount == 2 then
  140. itemlua.levelText.uILocalizeScript:SetContentAndValues("DscGoddessTalk_"..type, {param1, param2, I18N.T(cfgData.Name)})
  141. end
  142. itemlua.value.text.text = cfgData.Value
  143. itemlua.presentPrice.number.text.text = cfgData.Money
  144. self:RefreshReward(itemlua, cfgData)
  145. self:InitRemainTime(itemlua, data)
  146. self.uiBase:AddButtonUniqueEventListener(itemlua.presentPrice.button, self, self.OnPayClick)
  147. item.CachedRectTransform:SetSizeWithCurrentAnchors(UnityEngine.RectTransform.Axis.Horizontal, self.scrollView.loopListView.ViewPortWidth)
  148. return item
  149. end
  150. function UIActivityLimitedGiftView:RefreshReward(itemlua, cfgData)
  151. local reward = cfgData.Reward
  152. if not reward then return end
  153. if self.rewards == nil then
  154. self.rewards = {}
  155. end
  156. for i = 1, 4 do
  157. local item = itemlua["item"..i]
  158. item:SetActive(reward[i] ~= nil)
  159. if reward[i] then
  160. local trans = item.transform
  161. local data = {cfgId = reward[i][1], num = reward[i][2]}
  162. if trans.childCount > 1 then
  163. local iconItem = item.transform:GetChild(1)
  164. local itemlua1 = CommonUtil.GetBatchItemByGo(self, Enum.PrefabNames.ItemItem, iconItem.gameObject)
  165. IconItemCtr:SetData(self, itemlua1, data, Enum.ItemIEnterType.Bag, self, self.OnItemClick)
  166. else
  167. ManagerContainer.GoPoolMgr:SpawnGo(Enum.PrefabNames.ItemItem, function(itemlua1)
  168. CommonUtil.BatchCreateItems(self, itemlua1, item.transform, data, Enum.ItemIEnterType.Bag, self, self.OnItemClick)
  169. end)
  170. end
  171. end
  172. end
  173. end
  174. function UIActivityLimitedGiftView:InitRemainTime(itemlua, data)
  175. local time = self.controller:GetRemainTime(data.activitiesId)
  176. itemlua.endTimeText.uILocalizeScript:SetContentAndValues("DscLimitedGiftTime", {time})
  177. end
  178. function UIActivityLimitedGiftView:RefreshRemainTime()
  179. local id = self.controller:GetLimitRechargeId()
  180. local time = self.controller:GetRemainTime(id)
  181. self.itemlua.endTimeText.uILocalizeScript:SetContentAndValues("DscLimitedGiftTime", {time})
  182. if not self.controller:IsExist() then
  183. self:OnCloseClick()
  184. end
  185. end
  186. function UIActivityLimitedGiftView:OnItemClick(button, params)
  187. local data = params[0]
  188. ManagerContainer.LuaUIMgr:OpenTips(data)
  189. end
  190. function UIActivityLimitedGiftView:Update()
  191. if not curTime then return end
  192. curTime = curTime + Time.deltaTime
  193. if curTime >= interTime then
  194. curTime = 0
  195. self:RefreshRemainTime()
  196. end
  197. end
  198. function UIActivityLimitedGiftView:RemoveEventListener()
  199. ManagerContainer.LuaEventMgr:Unregister(self.uiData.name)
  200. end
  201. function UIActivityLimitedGiftView:AddUIEventListener()
  202. --self.uiBase:AddButtonEventListener(self.AnyBtn.button, self, self.OnCloseClick)
  203. self.uiBase:AddButtonEventListener(self.BtnClose.button, self, self.OnCloseClick)
  204. self.uiBase:AddButtonEventListener(self.btnArrowL.button, self, self.OnLeftClick)
  205. self.uiBase:AddButtonEventListener(self.btnArrowR.button, self, self.OnRightClick)
  206. self.scrollView.loopListView.mOnBeginDragAction = function()
  207. self:OnBeginDragAction()
  208. end
  209. self.scrollView.loopListView.mOnDragingAction = function()
  210. self:OnDragingAction()
  211. end
  212. self.scrollView.loopListView.mOnSnapItemFinished = function(loopListView, loopListViewItem)
  213. return self:OnSnapItemFinished(loopListView, loopListViewItem)
  214. end
  215. end
  216. function UIActivityLimitedGiftView:OnSnapItemFinished(loopListView, loopListViewItem)
  217. local data = self.controller:GetCurLimitRechargeData(loopListViewItem.ItemIndex)
  218. self.itemlua = CommonUtil.GetBindGridViewItem2Lua(self, "LimitedGiftItem", loopListViewItem.gameObject)
  219. if self.inited then
  220. local idx = self.controller:GetLimitRechargeIdx()
  221. local isAuto = self.controller:GetData()
  222. if isAuto then
  223. else
  224. self.controller:HasOpened(data.activitiesId)
  225. end
  226. self.controller:SetCurId(data.activitiesId)
  227. self.controller:SetSelectedIdx(loopListViewItem.ItemIndex)
  228. end
  229. self:RefreshDots()
  230. end
  231. function UIActivityLimitedGiftView:OnCloseClick()
  232. self:UIClose()
  233. end
  234. function UIActivityLimitedGiftView:OnBeginDragAction()
  235. end
  236. function UIActivityLimitedGiftView:OnDragingAction()
  237. local idx = self.scrollView.loopListView.CurSnapNearestItemIndex
  238. self.curDragIdx = math.abs(idx)
  239. if self.lastDragIdx ~= self.curDragIdx then
  240. local length = self.controller:GetLimitRechargeCount()
  241. local realIdx = math.fmod(idx, length)
  242. self.controller:SetSelectedIdx(realIdx)
  243. self.controller:SetIdBySelectedIdx()
  244. self:RefreshDots()
  245. self.lastDragIdx = self.curDragIdx
  246. end
  247. end
  248. function UIActivityLimitedGiftView:OnLeftClick()
  249. --self.controller:SetData(false)
  250. local idx = self.controller:GetSelectedIdx()
  251. idx = idx - 1
  252. self.scrollView.loopListView:SetSnapTargetItemIndex(idx)
  253. end
  254. function UIActivityLimitedGiftView:OnRightClick()
  255. --self.controller:SetData(false)
  256. local idx = self.controller:GetSelectedIdx()
  257. idx = idx + 1
  258. self.scrollView.loopListView:SetSnapTargetItemIndex(idx)
  259. end
  260. function UIActivityLimitedGiftView:OnPayClick()
  261. local id = self.controller:GetLimitRechargeId()
  262. local errorCode = ManagerContainer.PayMgr:LimitedGiftPay(id)
  263. local errorCodeKey = ManagerContainer.PayMgr:GetInitPayErrorCodeLangKey(errorCode)
  264. if not errorCodeKey then
  265. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(errorCodeKey)
  266. end
  267. end
  268. function UIActivityLimitedGiftView:OnPaySuccess()
  269. --if not self.controller:IsExist() then
  270. -- self:OnCloseClick()
  271. --end
  272. end
  273. function UIActivityLimitedGiftView:OnHide()
  274. end
  275. function UIActivityLimitedGiftView:OnShow(data)
  276. self.controller:SetData(data)
  277. self:Refresh()
  278. curTime = 0
  279. end
  280. function UIActivityLimitedGiftView:OnClose()
  281. if updateHandle ~= nil then
  282. UpdateBeat:RemoveListener(updateHandle)
  283. updateHandle = nil
  284. end
  285. self:DisposeDot()
  286. self.itemlua = nil
  287. self.scrollView.loopListView:Dispose()
  288. self.scrollView.loopListView.mOnSnapItemFinished = nil
  289. self.scrollView.loopListView.mOnBeginDragAction = nil
  290. self.scrollView.loopListView.mOnDragingAction = nil
  291. end
  292. function UIActivityLimitedGiftView:OnDispose()
  293. self.controller:OnDispose()
  294. end
  295. return UIActivityLimitedGiftView