UIPetBattleView.lua 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. local UIPetBattleView = require("UIPet/UIPetBattleView_Generate")
  2. local MultiTypeAssetLoadSystem = require("MultiTypeAssetLoadSystem")
  3. local PetMatsGridView = require("UIPet/PetMatsGridView")
  4. local RoleListItemCtr = require("Common/RoleListItemCtr")
  5. local IconPetItemCtr = require("Common/IconPetItemCtr")
  6. local petMatsGridView
  7. local portraitPath = "UIDojo/Portrait"
  8. local OpsEnum =
  9. {
  10. UpBattle = 1,
  11. Replace = 2,
  12. OnekeyDown = 3,
  13. }
  14. local lastOps = OpsEnum.UpBattle
  15. local deltaVec = Vector2.zero
  16. local curPos = Vector2.zero
  17. local lockIds = {42,43,44,45,46,47}
  18. local updateBeat
  19. local isDragging = false
  20. local tmpPos = Vector3.zero
  21. local pos = Vector2.zero
  22. local edgeX = Vector2.zero
  23. local edgeY = Vector2.zero
  24. function UIPetBattleView:OnAwake(data)
  25. self.controller = require("UIPet/UIPetBattleCtr"):new()
  26. self.controller:Init(self)
  27. self.controller:SetData(data)
  28. end
  29. function UIPetBattleView:AddEventListener()
  30. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name)
  31. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.PET_BATTLE_SUCCESS_NTF, self, self.OnBattleNtf)
  32. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.FILTER_NTF, self, self.RefreshDatasByFilter)
  33. end
  34. function UIPetBattleView:FillContent(data, uiBase)
  35. self.uiBase = uiBase
  36. local gameObject = self.uiBase:GetRoot()
  37. if gameObject ~= nil then
  38. self.gameObject = gameObject
  39. self.transform = gameObject.transform
  40. end
  41. self:InitGenerate(self.transform, data)
  42. self:Init()
  43. end
  44. function UIPetBattleView:Init()
  45. self.mask:SetActive(false)
  46. self.controller:RefreshCurPetBattleLock()
  47. if not updateBeat then
  48. updateBeat = UpdateBeat:CreateListener(self.Update, self)
  49. end
  50. local sizeDelta = self.slotList.slot1.rectTransform.sizeDelta
  51. edgeX.x = sizeDelta.x/2 - UIMgr.SCREEN_WIDTH/2
  52. edgeX.y = UIMgr.SCREEN_WIDTH/2 -sizeDelta.x/2
  53. edgeY.x = sizeDelta.y/2 - UIMgr.SCREEN_HEIGHT/2
  54. edgeY.y = UIMgr.SCREEN_HEIGHT/2 - sizeDelta.y/2
  55. UpdateBeat:AddListener(updateBeat)
  56. lastOps = OpsEnum.UpBattle
  57. deltaVec.x = self.slotList.slot1.rectTransform.sizeDelta.x/2
  58. self.showState = false
  59. self.heroLuaItems = {}
  60. self.petLuaItems = {}
  61. if not petMatsGridView then
  62. petMatsGridView = PetMatsGridView:new()
  63. petMatsGridView:Init(Enum.ItemIEnterType.Pet, self, self.scrollview.loopGridView)
  64. end
  65. self.loadSystem = MultiTypeAssetLoadSystem:new()
  66. self.loadSystem:SetCompleteCB(self, self.PreloadedAssets)
  67. self.slotEmpty:SetActive(false)
  68. for i = 1, 6 do
  69. self.slotBattle["slot"..i]:SetActive(false)
  70. end
  71. self:PetGridOver()
  72. end
  73. function UIPetBattleView:RefreshDatasByFilter(uiId, data, isFilter)
  74. if uiId ~= self.uiData.id then return end
  75. self.controller:SetFilterData(data, isFilter)
  76. self.controller:ClearSelectedInfo()
  77. self:Refresh(true)
  78. self.showState = false
  79. self:RefreshSlotBtns()
  80. end
  81. function UIPetBattleView:PetGridOver()
  82. self:Refresh()
  83. self:PreLoadRes()
  84. end
  85. function UIPetBattleView:PreLoadRes()
  86. self.loadSystem:RemoveLoadAllAsset()
  87. self.loadSystem:AddLoadAsset(Enum.ResourceType.GameObject, Constants.UIPath, portraitPath)
  88. self.loadSystem:Begin()
  89. end
  90. function UIPetBattleView:PreloadedAssets()
  91. self:RefreshTop()
  92. end
  93. function UIPetBattleView:OnBattleNtf(result)
  94. self.controller:InitHeroBattleDatas()
  95. self:Refresh(true)
  96. self:InitBattlePets()
  97. self.showState = false
  98. self:RefreshSlotBtns()
  99. if result then
  100. local content = ""
  101. if lastOps == OpsEnum.UpBattle then
  102. content = "Petfight_003"
  103. elseif lastOps == OpsEnum.Replace then
  104. content = "Petfight_004"
  105. elseif lastOps == OpsEnum.OnekeyDown then
  106. content = "Petfight_005"
  107. end
  108. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(I18N.T(content))
  109. end
  110. if self.mastTimer then
  111. self.mastTimer:Stop()
  112. self.mastTimer = nil
  113. end
  114. self:MaskHide()
  115. end
  116. function UIPetBattleView:MaskHide()
  117. self.mask:SetActive(false)
  118. end
  119. function UIPetBattleView:RefreshTop()
  120. self:InitHerosInfo()
  121. self:InitBattlePets()
  122. self:InitSelectedPet()
  123. local curId = self.controller:GetCurSelectedId()
  124. self:RefreshSlotBtns(curId)
  125. --local unlockCount = self.controller:GetUnlockCount()
  126. --local petBattleCount = self.controller:GetBattlePetCount()
  127. --local remainEmptySlot = unlockCount - petBattleCount
  128. --if remainEmptySlot > 0 then
  129. -- for i = 1, 6 do
  130. -- self.slotTrigger["slot"..i.."_lock"]:SetActive(false)
  131. -- end
  132. --end
  133. self.slotEmpty:SetActive(true)
  134. end
  135. function UIPetBattleView:InitSelectedPet()
  136. local data, idx = self.controller:InitCurSelectedData()
  137. if not data then return end
  138. local offset = math.fmod(idx, 5) + 1
  139. petMatsGridView:MoveToIndex(idx - offset)
  140. local itemlua = petMatsGridView:GetShowItemByIdx(idx)
  141. self:OnItemClick1(data, itemlua)
  142. end
  143. function UIPetBattleView:InitHerosInfo()
  144. local battleHeroList = self.controller:GetList()
  145. for i = 1, 6 do
  146. local heroPerent = self["hero"..i].transform
  147. local hero
  148. if heroPerent.childCount >= 1 then
  149. hero = heroPerent:GetChild(0).gameObject
  150. else
  151. hero = ManagerContainer.ResMgr:GetGoFromPool(Constants.UIPath, portraitPath)
  152. if hero then
  153. hero.transform:SetParent(heroPerent)
  154. hero.transform.localPosition = Vector3.zero
  155. hero.transform.localRotation = Quaternion.identity
  156. hero.transform.localScale = Vector3.one
  157. end
  158. end
  159. if hero then
  160. self.heroLuaItems[i] = CommonUtil.BindGridViewItem2Lua(self, "Portrait", hero)
  161. local heroData = self.controller:GetHeroDataByUid(i)
  162. if heroData then
  163. local cfgData = CommonUtil.GetHeroCfgDataByUid(heroData.id)
  164. RoleListItemCtr:SetData(self, self.heroLuaItems[i].role, {i, heroData, cfgData})
  165. self.heroLuaItems[i].role.plus:SetActive(true)
  166. local quality = heroData.strengthLevel
  167. local frameIcon = quality == 0 and Constant.Quality_Role_PlusFrame_Defaut_Icons or Constant.Quality_Role_PlusFrame_Icons[quality]
  168. CommonUtil.LoadIcon(self, frameIcon, function (sprite)
  169. self.heroLuaItems[i].role.plusFrame.image.sprite = sprite
  170. end)
  171. if battleHeroList then
  172. self.heroLuaItems[i].role.labelUp:SetActive(i ~= 1 and CommonUtil.EleInTable(heroData.id, battleHeroList))
  173. end
  174. end
  175. end
  176. end
  177. end
  178. function UIPetBattleView:InitBattlePets()
  179. for i = 1, 6 do
  180. local petId = self.controller:GetPetBattlePetId(i)
  181. if petId and petId > 0 then
  182. self:AddBattlePet(i, petId)
  183. else
  184. self.slotTrigger["slot"..i].uIEventTriggerListener.onDrag = nil
  185. self.slotTrigger["slot"..i].uIEventTriggerListener.onDrag = nil
  186. self.slotTrigger["slot"..i].uIEventTriggerListener.onPointerUp = nil
  187. self.slotTrigger["slot"..i].uIEventTriggerListener.onClick = function()
  188. self:BattlePetClick(i)
  189. end
  190. end
  191. end
  192. end
  193. function UIPetBattleView:AddBattlePet(idx, petId)
  194. self.controller:SetPetBattlePetId(idx, petId)
  195. if self.petLuaItems[idx] then
  196. local parent = self.slotList["slot"..idx].transform
  197. local pet = self.petLuaItems[idx].gameObject
  198. pet.transform:SetParent(parent)
  199. pet.transform.localPosition = Vector3.zero
  200. pet.transform.localRotation = Quaternion.identity
  201. pet.transform.localScale = Vector3.one
  202. self.petLuaItems[idx].rectTransform.anchoredPosition = Vector2.zero
  203. local petData = self.controller:GetPetDataById(petId)
  204. if not petData then
  205. petData = ManagerContainer.DataMgr.PetDataMgr:GetPetDataById(petId)
  206. end
  207. IconPetItemCtr:SetData(self, self.petLuaItems[idx], petData, Enum.ItemIEnterType.Pet)
  208. else
  209. ManagerContainer.GoPoolMgr:SpawnGo("IconPetItem", function (itemlua)
  210. local parent = self.slotList["slot"..idx].transform
  211. local pet = itemlua.gameObject
  212. pet.transform:SetParent(parent)
  213. pet.transform.localPosition = Vector3.zero
  214. pet.transform.localRotation = Quaternion.identity
  215. pet.transform.localScale = Vector3.one
  216. itemlua.rectTransform.anchoredPosition = Vector2.zero
  217. self.petLuaItems[idx] = itemlua
  218. local petData = self.controller:GetPetDataById(petId)
  219. if not petData then
  220. petData = ManagerContainer.DataMgr.PetDataMgr:GetPetDataById(petId)
  221. end
  222. if petData then
  223. IconPetItemCtr:SetData(self, self.petLuaItems[idx], petData, Enum.ItemIEnterType.Pet)
  224. self.slotTrigger["slot"..idx].uIEventTriggerListener.onBeginDrag = function()
  225. self:BattlePetBeginDrag(idx)
  226. end
  227. self.slotTrigger["slot"..idx].uIEventTriggerListener.onDrag = function()
  228. self:BattlePetOnDrag(idx)
  229. end
  230. self.slotTrigger["slot"..idx].uIEventTriggerListener.onPointerUp = function()
  231. self:BattlePetOnPointerUp(idx)
  232. end
  233. self.slotTrigger["slot"..idx].uIEventTriggerListener.onClick = nil
  234. end
  235. end)
  236. end
  237. end
  238. function UIPetBattleView:RemoveBattlePet(idx, onDispose)
  239. self.controller:SetPetBattlePetId(idx)
  240. self.slotTrigger["slot"..idx].uIEventTriggerListener.onDrag = nil
  241. self.slotTrigger["slot"..idx].uIEventTriggerListener.onPointerUp = nil
  242. self.slotTrigger["slot"..idx].uIEventTriggerListener.onBeginDrag = nil
  243. if not onDispose then
  244. self.slotTrigger["slot"..idx].uIEventTriggerListener.onClick = function()
  245. self:BattlePetClick(idx)
  246. end
  247. else
  248. self.slotTrigger["slot"..idx].uIEventTriggerListener.onClick = nil
  249. end
  250. ManagerContainer.GoPoolMgr:RecycleGo(self.petLuaItems[idx])
  251. self.petLuaItems[idx] = nil
  252. end
  253. function UIPetBattleView:Refresh(resetPos)
  254. if petMatsGridView then
  255. petMatsGridView:Refresh(resetPos)
  256. end
  257. self.btnFilter.check:SetActive(self.controller:GetIsFilter())
  258. end
  259. function UIPetBattleView:OnItemClick(button, params)
  260. local data = params[0]
  261. local itemlua = params[1]
  262. self:OnItemClick1(data, itemlua)
  263. end
  264. function UIPetBattleView:OnItemClick1(data, itemlua)
  265. local lastData = self.controller:GetLastSelectedPetData()
  266. if lastData then
  267. local idx = self.controller:GetPetDataIdxById(lastData.id)
  268. local lastItemlua = petMatsGridView:GetShowItemByIdx(idx)
  269. if lastItemlua then
  270. lastItemlua.selected:SetActive(false)
  271. end
  272. end
  273. if lastData == nil or lastData == data then
  274. self.showState = not self.showState
  275. end
  276. if lastData == data then
  277. self.controller:SetCurSelectedPetData(nil)
  278. if itemlua then
  279. itemlua.selected:SetActive(false)
  280. end
  281. else
  282. self.controller:SetCurSelectedPetData(data)
  283. if itemlua then
  284. itemlua.selected:SetActive(true)
  285. end
  286. end
  287. self:RefreshSlotBtns(data.id)
  288. end
  289. function UIPetBattleView:RefreshSlotBtns(curPetId)
  290. local unlockCount = self.controller:GetUnlockCount()
  291. local lockCount = self.controller:GetLockCount()
  292. local petBattleCount = self.controller:GetBattlePetCount()
  293. local remainEmptySlot = unlockCount - petBattleCount
  294. --self.slotTrigger:SetActive(remainEmptySlot == 0)
  295. self.battlePetCount.text.text = petBattleCount.."/"..unlockCount
  296. for i = 1, 6 do
  297. local heroData = self.controller:GetHeroDataByUid(i)
  298. local petId = self.controller:GetPetBattlePetId(i)
  299. self.slotEmpty["slot"..i.."Btn"]:SetActive(false)
  300. if self.showState then
  301. if remainEmptySlot > 0 then
  302. self.slotEmpty["slot"..i.."Btn"]:SetActive(((not petId or petId == 0) and (heroData.id == 1 or heroData.owned)) or (petId and petId > 0 and petId ~= curPetId))
  303. self.slotBattle["slot"..i]:SetActive(petId and petId > 0 and petId == curPetId)
  304. self.slotTrigger["slot"..i]:SetActive(heroData.id > 1 and not heroData.owned)
  305. if petId and petId > 0 and curPetId ~= petId then
  306. self.slotEmpty["slot"..i.."Btn"].text.uILocalizeScript:SetContent("BtnPetReplace")
  307. else
  308. self.slotEmpty["slot"..i.."Btn"].text.uILocalizeScript:SetContent("BtnPetFollow")
  309. end
  310. self.slotTrigger["slot"..i.."_lock"]:SetActive(false)
  311. if not petId or petId == 0 then
  312. remainEmptySlot = remainEmptySlot - 1
  313. end
  314. else
  315. if unlockCount == petBattleCount then
  316. if petId and petId > 0 then
  317. self.slotEmpty["slot"..i.."Btn"]:SetActive((not petId or petId == 0) or (petId and petId > 0 and petId ~= curPetId))
  318. self.slotBattle["slot"..i]:SetActive(petId and petId > 0 and petId == curPetId)
  319. self.slotTrigger["slot"..i]:SetActive(false)
  320. self.slotTrigger["slot"..i.."_lock"]:SetActive(false)
  321. if petId and petId > 0 and curPetId ~= petId then
  322. self.slotEmpty["slot"..i.."Btn"].text.uILocalizeScript:SetContent("BtnPetReplace")
  323. else
  324. self.slotEmpty["slot"..i.."Btn"].text.uILocalizeScript:SetContent("BtnPetFollow")
  325. end
  326. else
  327. self.slotEmpty["slot"..i.."Btn"]:SetActive(false)
  328. self.slotBattle["slot"..i]:SetActive(false)
  329. self.slotTrigger["slot"..i]:SetActive(false)
  330. end
  331. else
  332. if heroData.id > 1 and not heroData.owned then
  333. self.slotEmpty["slot"..i.."Btn"]:SetActive(false)
  334. self.slotBattle["slot"..i]:SetActive(false)
  335. self.slotTrigger["slot"..i]:SetActive(true)
  336. else
  337. self.slotEmpty["slot"..i.."Btn"]:SetActive((not petId or petId == 0) or (petId and petId > 0 and petId ~= curPetId))
  338. self.slotBattle["slot"..i]:SetActive(petId and petId > 0 and petId == curPetId)
  339. self.slotTrigger["slot"..i]:SetActive(false)
  340. self.slotTrigger["slot"..i.."_lock"]:SetActive(false)
  341. if petId and petId > 0 and curPetId ~= petId then
  342. self.slotEmpty["slot"..i.."Btn"].text.uILocalizeScript:SetContent("BtnPetReplace")
  343. else
  344. self.slotEmpty["slot"..i.."Btn"].text.uILocalizeScript:SetContent("BtnPetFollow")
  345. end
  346. end
  347. end
  348. end
  349. else
  350. self.slotEmpty["slot"..i.."Btn"]:SetActive(false)
  351. self.slotBattle["slot"..i]:SetActive(false)
  352. self.slotTrigger["slot"..i]:SetActive(true)
  353. if not petId or petId == 0 then
  354. if remainEmptySlot > 0 then
  355. self.slotTrigger["slot"..i.."_lock"]:SetActive(false)
  356. self.slotTrigger["slot"..i]:SetActive(false)
  357. --local unlockState = self.controller:GetSlotUnLockState(i)
  358. --local noOwnedLock = heroData.id > 1 and not heroData.owned and not unlockState
  359. --self.slotTrigger["slot"..i.."_lock"]:SetActive(noOwnedLock)
  360. ----remainEmptySlot = remainEmptySlot - 1
  361. else
  362. self.slotTrigger["slot"..i.."_lock"]:SetActive(true)
  363. self.slotTrigger["slot"..i]:SetActive(false)
  364. end
  365. else
  366. self.slotTrigger["slot"..i.."_lock"]:SetActive(false)
  367. end
  368. end
  369. end
  370. end
  371. function UIPetBattleView:BattlePetBeginDrag(idx)
  372. self.beginDrag = true
  373. local itemlua = self.petLuaItems[idx]
  374. if itemlua == nil then return end
  375. self.dragItem = itemlua
  376. self.uIEventTriggerListener = self.slotTrigger["slot"..idx].uIEventTriggerListener
  377. --local root = itemlua.transform.parent.parent
  378. itemlua.transform:SetParent(self.window.transform)
  379. end
  380. function UIPetBattleView:BattlePetOnDrag(idx)
  381. --local itemlua = self.petLuaItems[idx]
  382. --if itemlua == nil then return end
  383. --
  384. --local position = UIEventTriggerListener.currentEventData.delta
  385. ----position = CameraMgr.Instance.UICamera:ScreenToViewportPoint(position)
  386. ----position.x = (position.x - 0.5) * UIMgr.SCREEN_WIDTH
  387. ----position.y = (position.y - 0.5) * UIMgr.SCREEN_HEIGHT
  388. --itemlua.rectTransform.anchoredPosition = itemlua.rectTransform.anchoredPosition + position
  389. end
  390. function UIPetBattleView:Update()
  391. if not self.beginDrag then return end
  392. local position = self.uIEventTriggerListener.currentEventData.position
  393. tmpPos = CameraMgr.Instance.UICamera:ScreenToViewportPoint(position)
  394. pos.x = (tmpPos.x - 0.5) * UIMgr.SCREEN_WIDTH;
  395. pos.y = (tmpPos.y - 0.5) * UIMgr.SCREEN_HEIGHT;
  396. pos.x = CommonUtil.Clamp(edgeX.x, edgeX.y, pos.x)
  397. pos.y = CommonUtil.Clamp(edgeY.x, edgeY.y, pos.y)
  398. --curPos = self.dragItem.rectTransform.anchoredPosition
  399. --self.dragItem.rectTransform.anchoredPosition = Vector2.Lerp(curPos, pos, 20*Time.deltaTime)
  400. self.dragItem.rectTransform.anchoredPosition = pos
  401. end
  402. function UIPetBattleView:BattlePetClick(idx)
  403. local heroData = self.controller:GetHeroDataByUid(idx)
  404. if heroData.id > 1 and not heroData.owned then
  405. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("Petfight_002")
  406. return
  407. end
  408. local itemlua = self.petLuaItems[idx]
  409. if itemlua == nil then
  410. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("Petfight_007")
  411. return
  412. end
  413. end
  414. function UIPetBattleView:BattlePetOnPointerUp(idx)
  415. self.uIEventTriggerListener = nil
  416. self.dragItem = nil
  417. if not self.beginDrag then return end
  418. self.beginDrag = false
  419. local itemlua = self.petLuaItems[idx]
  420. if itemlua == nil then return end
  421. local unlockCount = self.controller:GetUnlockCount()
  422. local petBattleCount = self.controller:GetBattlePetCount()
  423. local remainEmptySlot = unlockCount - petBattleCount
  424. local outArea = true
  425. local relaceIdx = idx
  426. local targetPetId
  427. for i = 1, 6 do
  428. local heroData = self.controller:GetHeroDataByUid(i)
  429. local parent = self.slotTrigger["slot"..i]
  430. local minX = CommonUtil.ConvertUIPos2ScreenPos(parent.transform.position).x - deltaVec.x
  431. local maxX = CommonUtil.ConvertUIPos2ScreenPos(parent.transform.position).x + deltaVec.x
  432. local curX = CommonUtil.ConvertUIPos2ScreenPos(itemlua.transform.position).x
  433. local oriPetId = self.controller:GetPetBattlePetId(idx)
  434. targetPetId = self.controller:GetPetBattlePetId(i)
  435. --if not targetPetId or targetPetId == 0 then
  436. -- remainEmptySlot = remainEmptySlot - 1
  437. --end
  438. if curX >= minX and curX <= maxX then
  439. if remainEmptySlot <= 0 and (not targetPetId or targetPetId == 0) then
  440. break
  441. end
  442. if i == 1 or heroData.owned then
  443. outArea = false
  444. relaceIdx = i
  445. self.controller:SetPetBattlePetId(idx, targetPetId)
  446. self.controller:SetPetBattlePetId(i, oriPetId)
  447. end
  448. break
  449. end
  450. end
  451. if relaceIdx == idx then
  452. local parent = self.slotList["slot"..relaceIdx]
  453. itemlua.transform:SetParent(parent.transform)
  454. itemlua.rectTransform.anchoredPosition = Vector2.zero
  455. return
  456. end
  457. local parent
  458. if targetPetId and targetPetId > 0 then
  459. parent = self.slotList["slot"..idx]
  460. else
  461. parent = self.slotList["slot"..relaceIdx]
  462. self:ResetBattlePetItemlua(idx, relaceIdx, itemlua)
  463. end
  464. itemlua.transform:SetParent(parent.transform)
  465. if outArea then
  466. itemlua.rectTransform.anchoredPosition = Vector2.zero
  467. else
  468. if not targetPetId or targetPetId == 0 then
  469. lastOps = OpsEnum.UpBattle
  470. else
  471. lastOps = OpsEnum.Replace
  472. end
  473. self.controller:SendPetBattle()
  474. end
  475. end
  476. function UIPetBattleView:ResetBattlePetItemlua(oriIdx, repIdx, itemlua)
  477. self.petLuaItems[oriIdx] = nil
  478. self.slotTrigger["slot"..oriIdx].uIEventTriggerListener.onBeginDrag = nil
  479. self.slotTrigger["slot"..oriIdx].uIEventTriggerListener.onDrag = nil
  480. self.slotTrigger["slot"..oriIdx].uIEventTriggerListener.onPointerUp = nil
  481. self.slotTrigger["slot"..oriIdx].uIEventTriggerListener.onClick = function()
  482. self:BattlePetClick(oriIdx)
  483. end
  484. self.petLuaItems[repIdx] = itemlua
  485. self.slotTrigger["slot"..repIdx].uIEventTriggerListener.onBeginDrag = function()
  486. self:BattlePetBeginDrag(repIdx)
  487. end
  488. self.slotTrigger["slot"..repIdx].uIEventTriggerListener.onDrag = function()
  489. self:BattlePetOnDrag(repIdx)
  490. end
  491. self.slotTrigger["slot"..repIdx].uIEventTriggerListener.onPointerUp = function()
  492. self:BattlePetOnPointerUp(repIdx)
  493. end
  494. self.slotTrigger["slot"..repIdx].uIEventTriggerListener.onClick = nil
  495. end
  496. function UIPetBattleView:RemoveEventListener()
  497. ManagerContainer.LuaEventMgr:Unregister(self.uiData.name)
  498. end
  499. function UIPetBattleView:AddUIEventListener()
  500. self.uiBase:AddButtonEventListener(self.btnBack.button, self, self.OnCloseClick)
  501. self.uiBase:AddButtonEventListener(self.slotEmpty.slot1Btn.button, self, self.OnBattleClick, 1)
  502. self.uiBase:AddButtonEventListener(self.slotEmpty.slot2Btn.button, self, self.OnBattleClick, 2)
  503. self.uiBase:AddButtonEventListener(self.slotEmpty.slot3Btn.button, self, self.OnBattleClick, 3)
  504. self.uiBase:AddButtonEventListener(self.slotEmpty.slot4Btn.button, self, self.OnBattleClick, 4)
  505. self.uiBase:AddButtonEventListener(self.slotEmpty.slot5Btn.button, self, self.OnBattleClick, 5)
  506. self.uiBase:AddButtonEventListener(self.slotEmpty.slot6Btn.button, self, self.OnBattleClick, 6)
  507. self.uiBase:AddButtonEventListener(self.btnOneKeyBattle.button, self, self.OnOneKeyBattleClick)
  508. self.uiBase:AddButtonEventListener(self.btnFilter.button, self, self.OnFilterClick)
  509. self.uiBase:AddButtonEventListener(self.btnPlayRule.button, self, self.OnClickPlayRuleBtn)
  510. local lockContent = self.controller:GetLockContent()
  511. if lockContent then
  512. for i = 1, 6 do
  513. self.slotTrigger["slotLock"..i].uIEventTriggerListener.onClick = function()
  514. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(lockContent)
  515. end
  516. end
  517. end
  518. end
  519. function UIPetBattleView:OnFilterClick()
  520. local data = {uiId = self.uiData.id, enter = Enum.ItemIEnterType.Pet, filterData = self.controller:GetFilterData()}
  521. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIFilter, data)
  522. end
  523. function UIPetBattleView:OnCloseClick()
  524. ManagerContainer.LuaUIMgr:ClosePage(self.uiData.id)
  525. if self.uiBase.MSourceUIID > 0 then
  526. ManagerContainer.LuaUIMgr:Open(self.uiBase.MSourceUIID, nil, nil, false)
  527. end
  528. end
  529. function UIPetBattleView:OnClickPlayRuleBtn()
  530. ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIPlayRule, {'PlayExplainTitle', 'PetBattleExplain'})
  531. end
  532. function UIPetBattleView:OnBattleClick(button, params)
  533. local idx = params[0]
  534. local heroData = self.controller:GetHeroDataByUid(idx)
  535. if not heroData then
  536. return
  537. end
  538. if heroData.id > 1 and not heroData.owned then
  539. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("Petfight_002")
  540. return
  541. end
  542. local curSelectedData = self.controller:GetCurSelectedPetData()
  543. if not curSelectedData then
  544. return
  545. end
  546. local curSelectedId = curSelectedData.id
  547. local result = self.controller:HasSamePetInBattle(curSelectedId, idx)
  548. if result then
  549. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("543")
  550. return
  551. end
  552. local petId = self.controller:GetPetBattlePetId(idx)
  553. if petId and petId > 0 then
  554. if petId ~= curSelectedData.id then
  555. local oldPetId = petId
  556. self:ReplacePet(curSelectedData.id, oldPetId)
  557. lastOps = OpsEnum.Replace
  558. else
  559. self:RemoveBattlePet(idx)
  560. end
  561. else
  562. self:ReplacePet(curSelectedData.id)
  563. lastOps = OpsEnum.UpBattle
  564. end
  565. self.controller:SetPetBattlePetId(idx, curSelectedData.id)
  566. --self:AddBattlePet(idx, curSelectedData.id)
  567. self.controller:SetCurSelectedPetData(nil)
  568. --self.controller:SendPetBattle()
  569. self.showState = not self.showState
  570. self:RefreshSlotBtns()
  571. self:FlyShow(curSelectedId, idx)
  572. end
  573. function UIPetBattleView:FlyShow(id, slotIdx)
  574. local idx = self.controller:GetPetDataIdxById(id)
  575. local itemlua = petMatsGridView:GetShowItemByIdx(idx)
  576. if not itemlua then
  577. self.controller:SendPetBattle()
  578. return
  579. end
  580. local target = self.slotEmpty["slot"..slotIdx.."Btn"]
  581. self.flyGo = UnityEngine.GameObject.Instantiate(itemlua.gameObject)
  582. self.flyGo.transform:Find("Selected").gameObject:SetActive(false)
  583. self.flyGo.transform:SetParent(self.window.transform)
  584. self.flyGo.transform.localScale = Vector3.one
  585. self.flyGo.transform.position = itemlua.transform.position
  586. local rectTransform = self.flyGo:GetComponent(Enum.TypeInfo.RectTransform)
  587. local position = CameraMgr.Instance.UICamera:WorldToScreenPoint(self.flyGo.transform.position)
  588. position = CameraMgr.Instance.UICamera:ScreenToViewportPoint(position)
  589. position.x = (position.x - 0.5) * UIMgr.SCREEN_WIDTH
  590. position.y = (position.y - 0.5) * UIMgr.SCREEN_HEIGHT
  591. local vec = Vector2(0.5,0.5)
  592. rectTransform.anchorMin = vec
  593. rectTransform.anchorMax = vec
  594. local oldPivot = rectTransform.pivot
  595. rectTransform.pivot = vec
  596. rectTransform.anchoredPosition = position
  597. oldPivot.x = 0.5 - oldPivot.x
  598. oldPivot.y = 0.5 - oldPivot.y
  599. rectTransform.anchoredPosition = rectTransform.anchoredPosition + Vector2.Scale(rectTransform.sizeDelta, oldPivot)
  600. local pos = target.transform:TransformPoint(self.flyGo.transform.localPosition)
  601. local targetPos = self.flyGo.transform:InverseTransformPoint(pos)
  602. self.mask:SetActive(true)
  603. self.flyGo.transform:DOLocalMove(targetPos, 0.3):OnComplete(function ()
  604. UnityEngine.GameObject.Destroy(self.flyGo)
  605. self.flyGo = nil
  606. self.controller:SendPetBattle()
  607. end)
  608. if not self.mastTimer then
  609. self.mastTimer = Timer.New(slot(self.MaskHide, self), 3, 1)
  610. end
  611. if not self.mastTimer.running then
  612. self.mastTimer:Start()
  613. end
  614. end
  615. function UIPetBattleView:ReplacePet(curSelectedId, oldId)
  616. for i = 1, 6 do
  617. local petId = self.controller:GetPetBattlePetId(i)
  618. if petId == curSelectedId then
  619. if oldId then
  620. self.controller:SetPetBattlePetId(i, oldId)
  621. --self:AddBattlePet(i, oldId)
  622. else
  623. self:RemoveBattlePet(i)
  624. end
  625. return
  626. end
  627. end
  628. end
  629. function UIPetBattleView:OnOneKeyBattleClick()
  630. local length = self.controller:GetBattlePetCount()
  631. if length == 0 then
  632. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("Petfight_005")
  633. return
  634. end
  635. for i = 1, 6 do
  636. local petId = self.controller:GetPetBattlePetId(i)
  637. if petId and petId > 0 then
  638. self:RemoveBattlePet(i)
  639. end
  640. end
  641. lastOps = OpsEnum.OnekeyDown
  642. self.controller:SendPetBattle(true)
  643. end
  644. function UIPetBattleView:GetFirstPetItem()
  645. if petMatsGridView then
  646. return petMatsGridView:GetShowItemByIdx(1)
  647. end
  648. return
  649. end
  650. function UIPetBattleView:PetBattle(owner, ownerCB)
  651. local item = self:GetFirstPetItem()
  652. if not item or not item.gameObject then
  653. if owner and ownerCB then
  654. ownerCB(owner)
  655. end
  656. return
  657. end
  658. if owner and ownerCB then
  659. ownerCB(owner, item.bg)
  660. end
  661. end
  662. function UIPetBattleView:OnHide()
  663. if petMatsGridView then
  664. petMatsGridView:OnHide()
  665. end
  666. end
  667. function UIPetBattleView:OnShow(data)
  668. self.controller:SetData(data)
  669. end
  670. function UIPetBattleView:OnClose()
  671. for i = 1, 6 do
  672. self.slotTrigger["slotLock"..i].uIEventTriggerListener.onClick = nil
  673. end
  674. if self.flyGo then
  675. DG.Tweening.DOTween.Kill(self.flyGo.transform)
  676. UnityEngine.GameObject.Destroy(self.flyGo)
  677. self.flyGo = nil
  678. self.controller:SendPetBattle()
  679. end
  680. if self.mastTimer then
  681. self.mastTimer:Stop()
  682. self.mastTimer = nil
  683. end
  684. if petMatsGridView then
  685. petMatsGridView:OnDispose()
  686. petMatsGridView = nil
  687. end
  688. end
  689. function UIPetBattleView:OnDispose()
  690. self.mask:SetActive(false)
  691. if updateBeat ~= nil then
  692. UpdateBeat:RemoveListener(updateBeat)
  693. updateBeat = nil
  694. end
  695. self.uIEventTriggerListener = nil
  696. for i = 1, 6 do
  697. self:RemoveBattlePet(i, true)
  698. end
  699. self.heroLuaItems = nil
  700. self.petLuaItems = nil
  701. self.controller:OnDispose()
  702. if self.loadSystem then
  703. self.loadSystem:Dispose()
  704. self.loadSystem = nil
  705. end
  706. --ManagerContainer.LuaBattleMgr:SetTeamData(true)
  707. end
  708. return UIPetBattleView