UIVoyagePrepareView.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. local UIVoyagePrepareView = require("UIVoyage/UIVoyagePrepareView_Generate")
  2. local MaxLv = 5
  3. function UIVoyagePrepareView:OnAwake(data)
  4. self.controller = require("UIVoyage/UIVoyagePrepareCtr"):new()
  5. self.controller:Init(self)
  6. self.controller:SetData(data)
  7. end
  8. function UIVoyagePrepareView:AddEventListener()
  9. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.VOYAGE_AIRSHIP_CHANGED, self, self.OnVoyageAirShipChanged)
  10. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.VOYAGE_NUM_CHANGED, self, self.OnVoyageNumChanged)
  11. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.VOYAGE_SELF_AIRSHIP_DATA_CHANGED, self, self.OnVoyageAirShipDataChanged)
  12. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.CURRENY_CHANGED, self, self.OnCurrenyChanged)
  13. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.ITEM_CHANGE, self, self.OnItemChanged)
  14. end
  15. function UIVoyagePrepareView:FillContent(data, uiBase)
  16. self.uiBase = uiBase
  17. local gameObject = self.uiBase:GetRoot()
  18. if gameObject ~= nil then
  19. self.gameObject = gameObject
  20. self.transform = gameObject.transform
  21. end
  22. self:InitGenerate(self.transform, data)
  23. self:Init()
  24. end
  25. function UIVoyagePrepareView:Init()
  26. self.controller:InitData()
  27. local curSelectId = self.controller:GetCurSelectId()
  28. local consumes = {}
  29. for i = 1, MaxLv do
  30. local shipLua = self['ship' .. tostring(i)]
  31. local cfgData = ManagerContainer.CfgMgr:GetVoyageAirShipCfgById(i)
  32. self:InitShipView(shipLua, cfgData)
  33. self:RefreshSelectItem(shipLua, curSelectId == i)
  34. for _, consume in pairs(cfgData.Consume) do
  35. if not CommonUtil.EleInTable(consume[1], consumes) then
  36. consumes[#consumes + 1] = consume[1]
  37. end
  38. end
  39. end
  40. for i = 1, 2 do
  41. self:RefreshCurrenyItem(self['currency' .. tostring(i)], consumes[i])
  42. end
  43. self.consumes = consumes
  44. self.closeTipsToggle.toggle.isOn = self.controller:GetNotifyAirShipUpTip()
  45. self:RefreshBottomBtn()
  46. end
  47. function UIVoyagePrepareView:RemoveEventListener()
  48. ManagerContainer.LuaEventMgr:Unregister(self.uiData.name)
  49. end
  50. function UIVoyagePrepareView:AddUIEventListener()
  51. self.uiBase:AddButtonUniqueEventListener(self.btnClose.button, self, self.OnClickCloseBtn)
  52. self.uiBase:AddButtonUniqueEventListener(self.btnUpMax.button, self, self.OnClickUpMaxBtn)
  53. self.uiBase:AddButtonUniqueEventListener(self.btnUpOne.button, self, self.OnClickUpOneBtn)
  54. self.uiBase:AddToggleUniqueEventListener(self.closeTipsToggle.toggle, self, self.OnClickCloseTipsToggle)
  55. end
  56. function UIVoyagePrepareView:OnHide()
  57. end
  58. function UIVoyagePrepareView:OnShow(data)
  59. self.controller:SetData(data)
  60. end
  61. function UIVoyagePrepareView:OnClose()
  62. end
  63. function UIVoyagePrepareView:OnDispose()
  64. for i = 1, MaxLv do
  65. self:DisposeShipView(self['ship' .. tostring(i)])
  66. end
  67. self.consumes = nil
  68. self.controller:OnDispose()
  69. end
  70. function UIVoyagePrepareView:OnVoyageAirShipChanged(isChanged)
  71. if not self.controller:RefreshSelectId() then
  72. return
  73. end
  74. local lastSelectId = self.controller:GetLastSelectId()
  75. if lastSelectId then
  76. self:RefreshSelectItem(self['ship' .. tostring(lastSelectId)], false)
  77. end
  78. local curSelectId = self.controller:GetCurSelectId()
  79. if curSelectId then
  80. self:RefreshSelectItem(self['ship' .. tostring(curSelectId)], true)
  81. end
  82. self:RefreshBottomBtn()
  83. end
  84. function UIVoyagePrepareView:OnVoyageNumChanged()
  85. local curSelectId = self.controller:GetCurSelectId()
  86. if curSelectId then
  87. self:RefreshSelectItem(self['ship' .. tostring(curSelectId)], true)
  88. end
  89. end
  90. function UIVoyagePrepareView:OnVoyageAirShipDataChanged()
  91. local data = self.controller:GetSelfAirShipData()
  92. if not data then
  93. return
  94. end
  95. self:UIClose()
  96. end
  97. function UIVoyagePrepareView:OnCurrenyChanged(changeMap)
  98. if not changeMap then return end
  99. local changed = false
  100. if self.consumes then
  101. for i = 1, #self.consumes do
  102. local consume = self.consumes[i]
  103. if changeMap[consume] then
  104. self:RefreshCurrenyItem(self['currency' .. tostring(i)], consume)
  105. changed = true
  106. end
  107. end
  108. end
  109. if changed then
  110. self:RefreshBottomBtn()
  111. end
  112. end
  113. function UIVoyagePrepareView:OnItemChanged(changeMap)
  114. if not changeMap then return end
  115. local changed = false
  116. if self.consumes then
  117. for i = 1, #self.consumes do
  118. local consume = self.consumes[i]
  119. if changeMap[consume] then
  120. self:RefreshCurrenyItem(self['currency' .. tostring(i)], consume)
  121. changed = true
  122. end
  123. end
  124. end
  125. if changed then
  126. self:RefreshBottomBtn()
  127. end
  128. end
  129. function UIVoyagePrepareView:OnClickCloseBtn()
  130. self:UIClose()
  131. end
  132. function UIVoyagePrepareView:OnClickUpMaxBtn()
  133. self:UpLvAirShip(true)
  134. end
  135. function UIVoyagePrepareView:OnClickUpOneBtn()
  136. self:UpLvAirShip(false)
  137. end
  138. function UIVoyagePrepareView:SureUpLvAirShip(upHighest)
  139. local errorCode = self.controller:GetSendRefreshAirShipReqErrorCode(upHighest)
  140. if errorCode == 0 then
  141. errorCode = self.controller:SendRefreshAirShipReq(upHighest)
  142. if errorCode ~= 0 then
  143. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(errorCode)
  144. else
  145. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIVoyageReforming)
  146. end
  147. else
  148. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(errorCode)
  149. end
  150. end
  151. function UIVoyagePrepareView:UpLvAirShip(upHighest)
  152. local errorCode, num, remainNum, validCosts = self.controller:GetSendRefreshAirShipReqErrorCode(upHighest)
  153. if errorCode == 0 then
  154. local len = #validCosts
  155. if len >= 1 then
  156. local vaildCost = validCosts[len]
  157. local cfgId = vaildCost[1]
  158. local itemCfgData = ManagerContainer.CfgMgr:GetItemById(cfgId)
  159. local notifyAirShipUpTip = self.controller:GetNotifyAirShipUpTip()
  160. if not notifyAirShipUpTip and itemCfgData.ResType == Enum.ItemType.Diamond then
  161. local paramData = {}
  162. local itemCfgData2 = ManagerContainer.CfgMgr:GetItemById(cfgId)
  163. paramData[1] = vaildCost[3] * vaildCost[4]
  164. paramData[2] = itemCfgData2 and string.formatbykey(itemCfgData2.Name) or ''
  165. local data = {upHighest and "VoyageUpAirShipTip2" or "VoyageUpAirShipTip1", paramData, upHighest, self, self.SureUpLvAirShip}
  166. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UINoticeTips, data)
  167. else
  168. errorCode = self.controller:SendRefreshAirShipReq(upHighest)
  169. if errorCode ~= 0 then
  170. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(errorCode)
  171. else
  172. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIVoyageReforming)
  173. end
  174. end
  175. else
  176. errorCode = self.controller:SendRefreshAirShipReq(upHighest)
  177. if errorCode ~= 0 then
  178. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(errorCode)
  179. else
  180. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIVoyageReforming)
  181. end
  182. end
  183. elseif errorCode == 1 then
  184. local len = #validCosts
  185. if len >= 1 then
  186. local vaildCost = validCosts[len]
  187. local cfgId = vaildCost[1]
  188. CommonUtil.ItemNotEnoughHandle(cfgId, self.uiData.id)
  189. end
  190. else
  191. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(errorCode)
  192. end
  193. end
  194. function UIVoyagePrepareView:OnClickCloseTipsToggle(_, _, isOn)
  195. self.controller:SetNotifyAirShipUpTip(isOn)
  196. end
  197. function UIVoyagePrepareView:OnClickSetSailBtn()
  198. local curSelectId = self.controller:GetCurSelectId()
  199. local num = self.controller:GetNum()
  200. local numLimit = self.controller:GetNumLimit()
  201. local data = {"VoyageUpAirShipTip3", {curSelectId}, nil, self, self.SureTakeOffAirShip, nil, {(numLimit - num)}}
  202. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UINoticeTips, data)
  203. end
  204. function UIVoyagePrepareView:SureTakeOffAirShip()
  205. local errorCode = self.controller:SendTakeOffAirShipReq()
  206. if errorCode ~= 0 then
  207. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(errorCode)
  208. end
  209. end
  210. function UIVoyagePrepareView:InitShipView(shipLua, cfgData)
  211. if not shipLua then
  212. return
  213. end
  214. if not cfgData then
  215. shipLua:SetActive(false)
  216. return
  217. end
  218. shipLua:SetActive(true)
  219. shipLua.shipTitle.text.text = string.formatbykey(cfgData.Name)
  220. local showDatas = {}
  221. for _, value in pairs(cfgData.TranspoertReward) do
  222. showDatas[#showDatas+1] = {cfgId = value[1], num = value[2]}
  223. end
  224. shipLua.rewardGroup.loopGridView:InitGridView(#showDatas, function (gridView, itemIndex, row, column)
  225. return self:GetItemByRowColumn(gridView, itemIndex, row, column, showDatas)
  226. end, nil)
  227. end
  228. function UIVoyagePrepareView:DisposeShipView(shipLua)
  229. if not shipLua then
  230. return
  231. end
  232. shipLua.rewardGroup.loopGridView:Dispose()
  233. end
  234. function UIVoyagePrepareView:GetItemByRowColumn(gridView, itemIndex, row, column, showDatas)
  235. local showData = showDatas[itemIndex + 1]
  236. local item = nil
  237. if showData then
  238. item = gridView:NewListViewItem('IconSmallItem')
  239. local itemlua = CommonUtil.BindGridViewItem2Lua(self, 'IconItem', item.gameObject)
  240. CommonUtil.UpdateItemPrefab(self, itemlua, showData, nil, self, self.ShowItemTips)
  241. end
  242. return item
  243. end
  244. function UIVoyagePrepareView:ShowItemTips(button, params)
  245. local data = params[0]
  246. ManagerContainer.LuaUIMgr:OpenTips(data)
  247. end
  248. function UIVoyagePrepareView:RefreshSelectItem(shipLua, isOn)
  249. if not shipLua then
  250. return
  251. end
  252. shipLua.off:SetActive(not isOn)
  253. shipLua.on:SetActive(isOn)
  254. if isOn then
  255. local num = self.controller:GetNum()
  256. local numLimit = self.controller:GetNumLimit()
  257. shipLua.time.text.text = tostring(numLimit - num) .. '/' .. tostring(numLimit)
  258. shipLua.airShipItem.animator:Play('AirShipRun')
  259. if num >= numLimit then
  260. shipLua.btnSetSail.button.onClick:RemoveAllListeners()
  261. shipLua.btnSetSail.button.interactable = false
  262. else
  263. self.uiBase:AddButtonUniqueEventListener(shipLua.btnSetSail.button, self, self.OnClickSetSailBtn)
  264. end
  265. else
  266. shipLua.btnSetSail.button.onClick:RemoveAllListeners()
  267. shipLua.airShipItem.animator:Play('AirShipKeep')
  268. end
  269. end
  270. function UIVoyagePrepareView:RefreshCurrenyItem(currency, cfgId)
  271. if not currency then return end
  272. local itemCfgData = ManagerContainer.CfgMgr:GetItemById(cfgId)
  273. if not itemCfgData then
  274. currency:SetActive(false)
  275. return
  276. end
  277. currency:SetActive(true)
  278. currency.icon.image.enabled = false
  279. currency.icon.image.sprite = nil
  280. CommonUtil.LoadIcon(self, itemCfgData.MiniIcon, function (sprite)
  281. if sprite then
  282. currency.icon.image.sprite = sprite
  283. currency.icon.image.enabled = true
  284. end
  285. end)
  286. currency.num.text.text = CommonUtil.FormatNumber(CommonUtil.GetOwnResCountByItemId(cfgId))
  287. end
  288. function UIVoyagePrepareView:RefreshBottomBtn()
  289. local curSelectId = self.controller:GetCurSelectId()
  290. local cfgData = ManagerContainer.CfgMgr:GetVoyageAirShipCfgById(curSelectId)
  291. self:RefreshUpBtn(self.btnUpOne, (curSelectId == MaxLv), cfgData and cfgData.Consume or nil)
  292. self:RefreshUpBtn(self.btnUpMax, (curSelectId == MaxLv), cfgData and cfgData.TopLevel or nil)
  293. end
  294. function UIVoyagePrepareView:RefreshUpBtn(itemLua, isMax, costs)
  295. itemLua.button.interactable = (not isMax)
  296. if not costs then
  297. return
  298. end
  299. local costLength = #costs
  300. if costLength <= 0 then
  301. return
  302. end
  303. local cost, costCfgId, costNum, ownNum, remainNum
  304. for i = 1, costLength do
  305. cost = costs[i]
  306. costCfgId = tonumber(cost[1])
  307. costNum = tonumber(cost[2])
  308. if i ~= costLength then
  309. ownNum = CommonUtil.GetOwnResCountByItemId(costCfgId)
  310. remainNum = Mathf.Floor(ownNum / costNum)
  311. if remainNum >= 1 then
  312. break
  313. end
  314. end
  315. end
  316. if not costCfgId then return end
  317. itemLua.cost.icon.image.enabled = false
  318. itemLua.cost.icon.image.sprite = nil
  319. local itemCfgData = ManagerContainer.CfgMgr:GetItemById(costCfgId)
  320. if itemCfgData then
  321. CommonUtil.LoadIcon(self, itemCfgData.MiniIcon, function (sprite)
  322. if sprite then
  323. itemLua.cost.icon.image.sprite = sprite
  324. itemLua.cost.icon.image.enabled = true
  325. end
  326. end)
  327. end
  328. itemLua.cost.num.text.text = tostring(costNum)
  329. end
  330. return UIVoyagePrepareView