UIActivityLimitedGiftView.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. local mapdata = ManagerContainer.CfgMgr:GetMapData(levelCfgData.MapId)
  134. param1 = I18N.T(mapdata.Name).." "..levelCfgData.LevelId
  135. end
  136. if cfgData.PlaceholderCount == 0 then
  137. itemlua.levelText.uILocalizeScript:SetContentAndValues("DscGoddessTalk_"..type, {I18N.T(cfgData.Name)})
  138. elseif cfgData.PlaceholderCount == 1 then
  139. itemlua.levelText.uILocalizeScript:SetContentAndValues("DscGoddessTalk_"..type, {param1, I18N.T(cfgData.Name)})
  140. elseif cfgData.PlaceholderCount == 2 then
  141. itemlua.levelText.uILocalizeScript:SetContentAndValues("DscGoddessTalk_"..type, {param1, param2, I18N.T(cfgData.Name)})
  142. end
  143. itemlua.value.text.text = cfgData.Value
  144. itemlua.presentPrice.number.text.text = cfgData.Money
  145. self:RefreshReward(itemlua, cfgData)
  146. self:InitRemainTime(itemlua, data)
  147. self.uiBase:AddButtonUniqueEventListener(itemlua.presentPrice.button, self, self.OnPayClick)
  148. item.CachedRectTransform:SetSizeWithCurrentAnchors(UnityEngine.RectTransform.Axis.Horizontal, self.scrollView.loopListView.ViewPortWidth)
  149. return item
  150. end
  151. function UIActivityLimitedGiftView:RefreshReward(itemlua, cfgData)
  152. local reward = cfgData.Reward
  153. if not reward then return end
  154. if self.rewards == nil then
  155. self.rewards = {}
  156. end
  157. for i = 1, 4 do
  158. local item = itemlua["item"..i]
  159. item:SetActive(reward[i] ~= nil)
  160. if reward[i] then
  161. local trans = item.transform
  162. local data = {cfgId = reward[i][1], num = reward[i][2]}
  163. if trans.childCount > 1 then
  164. local iconItem = item.transform:GetChild(1)
  165. local itemlua1 = CommonUtil.GetBatchItemByGo(self, Enum.PrefabNames.ItemItem, iconItem.gameObject)
  166. IconItemCtr:SetData(self, itemlua1, data, Enum.ItemIEnterType.Bag, self, self.OnItemClick)
  167. else
  168. ManagerContainer.GoPoolMgr:SpawnGo(Enum.PrefabNames.ItemItem, function(itemlua1)
  169. CommonUtil.BatchCreateItems(self, itemlua1, item.transform, data, Enum.ItemIEnterType.Bag, self, self.OnItemClick)
  170. end)
  171. end
  172. end
  173. end
  174. end
  175. function UIActivityLimitedGiftView:InitRemainTime(itemlua, data)
  176. local time = self.controller:GetRemainTime(data.activitiesId)
  177. itemlua.endTimeText.uILocalizeScript:SetContentAndValues("DscLimitedGiftTime", {time})
  178. end
  179. function UIActivityLimitedGiftView:RefreshRemainTime()
  180. local id = self.controller:GetLimitRechargeId()
  181. local time = self.controller:GetRemainTime(id)
  182. self.itemlua.endTimeText.uILocalizeScript:SetContentAndValues("DscLimitedGiftTime", {time})
  183. if not self.controller:IsExist() then
  184. self:OnCloseClick()
  185. end
  186. end
  187. function UIActivityLimitedGiftView:OnItemClick(button, params)
  188. local data = params[0]
  189. ManagerContainer.LuaUIMgr:OpenTips(data)
  190. end
  191. function UIActivityLimitedGiftView:Update()
  192. if not curTime then return end
  193. curTime = curTime + Time.deltaTime
  194. if curTime >= interTime then
  195. curTime = 0
  196. self:RefreshRemainTime()
  197. end
  198. end
  199. function UIActivityLimitedGiftView:RemoveEventListener()
  200. ManagerContainer.LuaEventMgr:Unregister(self.uiData.name)
  201. end
  202. function UIActivityLimitedGiftView:AddUIEventListener()
  203. --self.uiBase:AddButtonEventListener(self.AnyBtn.button, self, self.OnCloseClick)
  204. self.uiBase:AddButtonEventListener(self.BtnClose.button, self, self.OnCloseClick)
  205. self.uiBase:AddButtonEventListener(self.btnArrowL.button, self, self.OnLeftClick)
  206. self.uiBase:AddButtonEventListener(self.btnArrowR.button, self, self.OnRightClick)
  207. self.scrollView.loopListView.mOnBeginDragAction = function()
  208. self:OnBeginDragAction()
  209. end
  210. self.scrollView.loopListView.mOnDragingAction = function()
  211. self:OnDragingAction()
  212. end
  213. self.scrollView.loopListView.mOnSnapItemFinished = function(loopListView, loopListViewItem)
  214. return self:OnSnapItemFinished(loopListView, loopListViewItem)
  215. end
  216. end
  217. function UIActivityLimitedGiftView:OnSnapItemFinished(loopListView, loopListViewItem)
  218. local data = self.controller:GetCurLimitRechargeData(loopListViewItem.ItemIndex)
  219. self.itemlua = CommonUtil.GetBindGridViewItem2Lua(self, "LimitedGiftItem", loopListViewItem.gameObject)
  220. if self.inited then
  221. local idx = self.controller:GetLimitRechargeIdx()
  222. local isAuto = self.controller:GetData()
  223. if isAuto then
  224. else
  225. self.controller:HasOpened(data.activitiesId)
  226. end
  227. self.controller:SetCurId(data.activitiesId)
  228. self.controller:SetSelectedIdx(loopListViewItem.ItemIndex)
  229. end
  230. self:RefreshDots()
  231. end
  232. function UIActivityLimitedGiftView:OnCloseClick()
  233. self:UIClose()
  234. end
  235. function UIActivityLimitedGiftView:OnBeginDragAction()
  236. end
  237. function UIActivityLimitedGiftView:OnDragingAction()
  238. local idx = self.scrollView.loopListView.CurSnapNearestItemIndex
  239. self.curDragIdx = math.abs(idx)
  240. if self.lastDragIdx ~= self.curDragIdx then
  241. local length = self.controller:GetLimitRechargeCount()
  242. local realIdx = math.fmod(idx, length)
  243. self.controller:SetSelectedIdx(realIdx)
  244. self.controller:SetIdBySelectedIdx()
  245. self:RefreshDots()
  246. self.lastDragIdx = self.curDragIdx
  247. end
  248. end
  249. function UIActivityLimitedGiftView:OnLeftClick()
  250. --self.controller:SetData(false)
  251. local idx = self.controller:GetSelectedIdx()
  252. idx = idx - 1
  253. self.scrollView.loopListView:SetSnapTargetItemIndex(idx)
  254. end
  255. function UIActivityLimitedGiftView:OnRightClick()
  256. --self.controller:SetData(false)
  257. local idx = self.controller:GetSelectedIdx()
  258. idx = idx + 1
  259. self.scrollView.loopListView:SetSnapTargetItemIndex(idx)
  260. end
  261. function UIActivityLimitedGiftView:OnPayClick()
  262. local id = self.controller:GetLimitRechargeId()
  263. local errorCode = ManagerContainer.PayMgr:LimitedGiftPay(id)
  264. local errorCodeKey = ManagerContainer.PayMgr:GetInitPayErrorCodeLangKey(errorCode)
  265. if not errorCodeKey then
  266. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(errorCodeKey)
  267. end
  268. end
  269. function UIActivityLimitedGiftView:OnPaySuccess()
  270. --if not self.controller:IsExist() then
  271. -- self:OnCloseClick()
  272. --end
  273. end
  274. function UIActivityLimitedGiftView:OnHide()
  275. end
  276. function UIActivityLimitedGiftView:OnShow(data)
  277. self.controller:SetData(data)
  278. self:Refresh()
  279. curTime = 0
  280. end
  281. function UIActivityLimitedGiftView:OnClose()
  282. if updateHandle ~= nil then
  283. UpdateBeat:RemoveListener(updateHandle)
  284. updateHandle = nil
  285. end
  286. self:DisposeDot()
  287. self.itemlua = nil
  288. self.scrollView.loopListView:Dispose()
  289. self.scrollView.loopListView.mOnSnapItemFinished = nil
  290. self.scrollView.loopListView.mOnBeginDragAction = nil
  291. self.scrollView.loopListView.mOnDragingAction = nil
  292. end
  293. function UIActivityLimitedGiftView:OnDispose()
  294. self.controller:OnDispose()
  295. end
  296. return UIActivityLimitedGiftView