UIFashionComposeView.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. local UIFashionComposeView = require("UIFashion/UIFashionComposeView_Generate")
  2. local IconItemCtr = require("Common/IconItemCtr")
  3. local PreviewSystem = require('PreviewSystem')
  4. local MultiTypeAssetLoadSystem = require("MultiTypeAssetLoadSystem")
  5. local previewSystem = nil
  6. local AroundSphereMoveType = typeof(AroundSphereMove)
  7. local GameObject = UnityEngine.GameObject
  8. local ConsumePos = {
  9. [1] = {Vector2(0, -220)},
  10. [2] = {Vector2(-200, -220), Vector2(200, -220)},
  11. [3] = {Vector2(-250, -50), Vector2(0, -220), Vector2(250, -50)},
  12. [4] = {Vector2(-300, -50), Vector2(-100, -220), Vector2(100, -220), Vector2(300, -50)},
  13. }
  14. function UIFashionComposeView:OnAwake(data)
  15. self.controller = require("UIFashion/UIFashionComposeCtr"):new()
  16. self.controller:Init(self)
  17. self.controller:SetData(data)
  18. end
  19. function UIFashionComposeView:AddEventListener()
  20. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.COIN_CHANGED, self, self.RefreshMoney)
  21. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.DIAMOND_CHANGED, self, self.RefreshMoney)
  22. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.ITEM_CHANGE, self, self.RefreshItem)
  23. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.FASHION_LIST_CHANGE, self, self.RefreshOwnSign)
  24. end
  25. function UIFashionComposeView:FillContent(data, uiBase)
  26. self.uiBase = uiBase
  27. local gameObject = self.uiBase:GetRoot()
  28. if gameObject ~= nil then
  29. self.gameObject = gameObject
  30. self.transform = gameObject.transform
  31. end
  32. self:InitGenerate(self.transform, data)
  33. self:Init()
  34. end
  35. function UIFashionComposeView:Init()
  36. local cfgId = self.controller:GetCfgId()
  37. local fashionPaperCfgData = ManagerContainer.CfgMgr:GetFashionPaperById(cfgId)
  38. local fashionCfgData = ManagerContainer.CfgMgr:GetFashionById(fashionPaperCfgData.FashionId)
  39. if not previewSystem then
  40. previewSystem = PreviewSystem:new('FashionPaperPreview')
  41. local pos = CommonUtil.TableToVector3(fashionCfgData.ShowCamPos)
  42. local rot = CommonUtil.TableToQuaternion(fashionCfgData.ShowCamRot)
  43. previewSystem:SetView(pos, rot)
  44. end
  45. self.rawImage.rawImage.texture = previewSystem:GetRenderTexture()
  46. if not self.AroundSphereMoveContainer then
  47. self.AroundSphereMoveContainer = GameObject("AroundSphereMoveContainer")
  48. local aroundSphereMove = self.AroundSphereMoveContainer:AddComponent(AroundSphereMoveType)
  49. aroundSphereMove.selfRotateSpeed = 50
  50. if ManagerContainer.LuaModelMgr and ManagerContainer.LuaModelMgr.ModelRoot then
  51. self.AroundSphereMoveContainer.transform:SetParent(ManagerContainer.LuaModelMgr.ModelRoot.transform)
  52. end
  53. end
  54. -- 模型
  55. self:RecycleModel()
  56. if not self.loadSystem then
  57. self.loadSystem = MultiTypeAssetLoadSystem:new()
  58. end
  59. self.loadSystem:SetCompleteCB(self, self.RefreshModel)
  60. self.loadSystem:RemoveLoadAllAsset()
  61. local hasShow = false
  62. local showRoleAvatarIDs = fashionCfgData.ShowRoleAvatarID
  63. if showRoleAvatarIDs then
  64. local num = #showRoleAvatarIDs
  65. if num > 0 then
  66. for i = 1, num do
  67. local roleAvatarID = showRoleAvatarIDs[1]
  68. local roleAvatarCfg = ManagerContainer.CfgMgr:GetRoleAvatarById(roleAvatarID)
  69. if roleAvatarCfg then
  70. self.loadSystem:AddLoadAsset(Enum.ResourceType.GameObject, Constants.ModelPath, roleAvatarCfg.AvatarPrefab)
  71. end
  72. end
  73. hasShow = true
  74. end
  75. end
  76. if not hasShow then
  77. local roleAvatarCfg = ManagerContainer.CfgMgr:GetRoleAvatarById(fashionCfgData.RoleAvatarID)
  78. if roleAvatarCfg then
  79. self.loadSystem:AddLoadAsset(Enum.ResourceType.GameObject, Constants.ModelPath, roleAvatarCfg.AvatarPrefab)
  80. end
  81. end
  82. self.loadSystem:Begin()
  83. -- 名字
  84. self.nameTxt.text.text = I18N.SetLanguageValue(fashionCfgData.FashionName)
  85. -- 显示材料
  86. self:RefreshMakeMaterials()
  87. end
  88. function UIFashionComposeView:RemoveEventListener()
  89. ManagerContainer.LuaEventMgr:Unregister(self.uiData.name)
  90. end
  91. function UIFashionComposeView:AddUIEventListener()
  92. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name)
  93. self.uiBase:AddButtonUniqueEventListener(self.btnClose.button, self, self.OnClickCloseBtn)
  94. self.uiBase:AddButtonUniqueEventListener(self.AnyBtn.button, self, self.OnClickCloseBtn)
  95. self.uiBase:AddButtonEventListener(self.composeBtn.button, self, self.OnClickComposeBtn)
  96. end
  97. function UIFashionComposeView:OnHide()
  98. self:RecycleModel()
  99. if self.loadSystem then
  100. self.loadSystem:Dispose()
  101. self.loadSystem = nil
  102. end
  103. if self.AroundSphereMoveContainer then
  104. GameObject.Destroy(self.AroundSphereMoveContainer)
  105. end
  106. self.AroundSphereMoveContainer = nil
  107. if previewSystem then
  108. previewSystem:Dispose()
  109. previewSystem = nil
  110. end
  111. end
  112. function UIFashionComposeView:OnShow(data)
  113. self.controller:SetData(data)
  114. self:Init()
  115. end
  116. function UIFashionComposeView:OnClose()
  117. end
  118. function UIFashionComposeView:OnDispose()
  119. self:RecycleModel()
  120. if self.loadSystem then
  121. self.loadSystem:Dispose()
  122. self.loadSystem = nil
  123. end
  124. if self.AroundSphereMoveContainer then
  125. GameObject.Destroy(self.AroundSphereMoveContainer)
  126. end
  127. self.AroundSphereMoveContainer = nil
  128. if previewSystem then
  129. previewSystem:Dispose()
  130. previewSystem = nil
  131. end
  132. end
  133. function UIFashionComposeView:OnClickCloseBtn()
  134. self:UIClose()
  135. end
  136. function UIFashionComposeView:OnClickComposeBtn()
  137. local errorCode = self.controller:GetErrorCode()
  138. if errorCode ~= 0 then
  139. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('FashionPaperMakeError' .. errorCode)
  140. return
  141. end
  142. local cfgId = self.controller:GetCfgId()
  143. local fashionPaperCfgData = ManagerContainer.CfgMgr:GetFashionPaperById(cfgId)
  144. local fashionCfgData = ManagerContainer.CfgMgr:GetFashionById(fashionPaperCfgData.FashionId)
  145. local jobCfgId = ManagerContainer.DataMgr.UserData:GetJobCfgId()
  146. if not fashionCfgData.FashionUseJob or IsTableHasValue(fashionCfgData.FashionUseJob, jobCfgId) then
  147. self:SureComposeFashion()
  148. else
  149. local data = {"ComposeFashionSure", nil, nil, self, self.SureComposeFashion}
  150. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UINoticeTips, data)
  151. end
  152. end
  153. function UIFashionComposeView:SureComposeFashion()
  154. local errorCode = self.controller:GetErrorCode()
  155. if errorCode ~= 0 then
  156. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay('FashionPaperMakeError' .. errorCode)
  157. return
  158. end
  159. self.controller:SendCompose()
  160. self:UIClose()
  161. end
  162. function UIFashionComposeView:RefreshMoney()
  163. self:RefreshMakeMaterials()
  164. end
  165. function UIFashionComposeView:RefreshItem()
  166. self:RefreshMakeMaterials()
  167. end
  168. function UIFashionComposeView:RefreshMakeMaterials()
  169. local fashionPaperCfgData = ManagerContainer.CfgMgr:GetFashionPaperById(self.controller:GetCfgId())
  170. local makeMaterial = clone(fashionPaperCfgData.MakeMaterial)
  171. if type(makeMaterial[1]) == 'number' then
  172. makeMaterial = {makeMaterial}
  173. end
  174. local num = #makeMaterial
  175. local materialCfgId = nil
  176. local materialNum = nil
  177. local moneyCfgId = nil
  178. local moneyNum = nil
  179. for i = num, 1, -1 do
  180. materialCfgId = makeMaterial[i][1]
  181. materialNum = makeMaterial[i][2]
  182. local materialItemCfgData = ManagerContainer.CfgMgr:GetItemById(materialCfgId)
  183. if materialItemCfgData.ResType == Enum.ItemType.Coin
  184. or materialItemCfgData.ResType == Enum.ItemType.Diamond then
  185. moneyCfgId = materialCfgId
  186. moneyNum = materialNum
  187. -- 移除一个货币显示到列表,会单独显示
  188. table.remove(makeMaterial, i)
  189. break
  190. end
  191. end
  192. num = #makeMaterial
  193. if num > 4 then num = 4 end
  194. for i = 1, 4 do
  195. local consumeLua = self['consume' .. i]
  196. if i <= num then
  197. materialCfgId = makeMaterial[i][1]
  198. materialNum = makeMaterial[i][2]
  199. consumeLua:SetActive(true)
  200. local itemLua = self["Item"..i]
  201. local curOwnedNum = CommonUtil.GetOwnResCountByItemId(materialCfgId)
  202. if curOwnedNum >= materialNum then
  203. consumeLua.text.text.text = string.format(Constant.DeepGreenColorText, (curOwnedNum .. '/' .. materialNum))
  204. else
  205. consumeLua.text.text.text = string.format(Constant.RedColorText, (curOwnedNum .. '/' .. materialNum))
  206. end
  207. consumeLua.rectTransform.anchoredPosition = ConsumePos[num][i]
  208. local data = {cfgId = materialCfgId, num = curOwnedNum}
  209. IconItemCtr:SetData(self, itemLua, data, nil, self, self.ShowItemTips)
  210. else
  211. consumeLua:SetActive(false)
  212. end
  213. end
  214. if moneyCfgId == nil or moneyNum == nil then
  215. self.coinZ.coinIcon:SetActive(true)
  216. self.coinZ.diamondIcon:SetActive(false)
  217. self.coinZ.text.text.text = '0'
  218. else
  219. local itemCfgData = ManagerContainer.CfgMgr:GetItemById(moneyCfgId)
  220. self.coinZ.coinIcon:SetActive(itemCfgData.ResType == Enum.ItemType.Coin)
  221. self.coinZ.diamondIcon:SetActive(itemCfgData.ResType == Enum.ItemType.Diamond)
  222. local curOwnedNum = CommonUtil.GetOwnResCountByItemId(moneyCfgId)
  223. if curOwnedNum >= moneyNum then
  224. self.coinZ.text.text.text = tostring(moneyNum)
  225. else
  226. self.coinZ.text.text.text = string.format(Constant.RedColorText, moneyNum)
  227. end
  228. end
  229. end
  230. function UIFashionComposeView:RefreshModel()
  231. local cfgId = self.controller:GetCfgId()
  232. local fashionPaperCfgData = ManagerContainer.CfgMgr:GetFashionPaperById(cfgId)
  233. local fashionCfgData = ManagerContainer.CfgMgr:GetFashionById(fashionPaperCfgData.FashionId)
  234. local hasShow = false
  235. local showRoleAvatarIDs = fashionCfgData.ShowRoleAvatarID
  236. local showModelPoss = fashionCfgData.ShowModelPos
  237. local showModelRots = fashionCfgData.ShowModelRot
  238. if showRoleAvatarIDs then
  239. local num = #showRoleAvatarIDs
  240. if num > 0 then
  241. for i = 1, num do
  242. self:CreateFashionModel(showRoleAvatarIDs[i], (showModelPoss and showModelPoss[i] or nil), (showModelRots and showModelRots[i] or nil))
  243. end
  244. hasShow = true
  245. end
  246. end
  247. if not hasShow then
  248. self:CreateFashionModel(fashionCfgData.RoleAvatarID, (showModelPoss and showModelPoss[1] or nil), (showModelRots and showModelRots[1] or nil))
  249. end
  250. end
  251. function UIFashionComposeView:CreateFashionModel(roleAvatarID, modelPos, modelRot)
  252. local roleAvatarCfg = ManagerContainer.CfgMgr:GetRoleAvatarById(roleAvatarID)
  253. if roleAvatarCfg then
  254. local go = ManagerContainer.ResMgr:GetGoFromPool(Constants.ModelPath, roleAvatarCfg.AvatarPrefab)
  255. if go then
  256. if not self.fashionGos then
  257. self.fashionGos = {}
  258. end
  259. local modelPosition = CommonUtil.TableToVector3(modelPos)
  260. local modelRatate = CommonUtil.TableToQuaternion(modelRot)
  261. local fashionTrans = go.transform
  262. local srcPos = fashionTrans.localPosition
  263. local srcRot = fashionTrans.localRotation
  264. fashionTrans:SetParent(self.AroundSphereMoveContainer.transform)
  265. fashionTrans.localPosition = modelPosition
  266. fashionTrans.localRotation = modelRatate
  267. if previewSystem then
  268. previewSystem:AddGo(go)
  269. end
  270. self.fashionGos[#self.fashionGos + 1] = {go, roleAvatarCfg.AvatarPrefab, srcPos, srcRot}
  271. end
  272. end
  273. end
  274. function UIFashionComposeView:RecycleModel()
  275. if self.loadSystem then
  276. self.loadSystem:Cancel()
  277. end
  278. if self.fashionGos then
  279. for _, fashionGo in pairs(self.fashionGos) do
  280. local go = fashionGo[1]
  281. local path = fashionGo[2]
  282. local srcPos = fashionGo[3]
  283. local srcRot = fashionGo[4]
  284. if previewSystem then
  285. previewSystem:RemoveGo(go)
  286. end
  287. go.transform:SetParent(nil)
  288. go.transform.localPosition = srcPos
  289. go.transform.localRotation = srcRot
  290. ManagerContainer.ResMgr:RecycleGO(Constants.ModelPath, path, go)
  291. end
  292. end
  293. self.fashionGos = nil
  294. end
  295. function UIFashionComposeView:ShowItemTips(button, params)
  296. local data = params[0]
  297. --ManagerContainer.LuaUIMgr:OpenItemTips(data.cfgId, nil, data)
  298. ManagerContainer.LuaUIMgr:OpenTips(data)
  299. end
  300. return UIFashionComposeView