KLUIChatBagScrollViewItem.lua 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. ---@class KLUIChatBagScrollViewItem:UIKmlLuaPanelBase
  2. ---@field view KLUIChatBagScrollViewItemView
  3. ---@field itemList CommonProtos.Item[]
  4. ---@field indexToItem table<number,KLBagItemItem>
  5. local KLUIChatBagScrollViewItem = class(UIKmlLuaPanelBase)
  6. local this = KLUIChatBagScrollViewItem
  7. this.idToIns = {}
  8. ---创建时调用一次
  9. function this:Init()
  10. if not self.args then
  11. self.args = {}
  12. end
  13. self.hor = self.args.hor or 25
  14. self.vet = self.args.vet or 8
  15. self.itemList = self.args.itemList or {}
  16. self.page = self.args.page or 1
  17. self.forbidCount = self.args.forbidCount or 0
  18. self.id = self.args.id or EBagTileScrollViewId.Bag
  19. self.forbidClickCallback = self.args.forbidClickCallback
  20. self.dragEndCallback = self.args.dragEndCallback
  21. self.parent = self.args.parent
  22. self.itemClick=self.args.itemClick
  23. self.indexToItem = {}
  24. self.indexToForbidGrid = {}
  25. self.indexForbidList = {}
  26. self.selectList=self.args.selectList or {}
  27. if this.idToIns[self.id] == nil then
  28. this.idToIns[self.id] = self
  29. end
  30. end
  31. ---创建或者刷新界面数据时调用
  32. function this:Refresh()
  33. local x = self.args.x or 0
  34. local y = self.args.y or 0
  35. GUI:setPosition(self.view.root, x, y)
  36. local height = self.hor*45
  37. local width = self.vet*45
  38. GUI:setContentSize(self.view.BagTileScrollViewPrefab,370, 184)
  39. GUI:setPosition(self.view.BagTileScrollViewPrefab,2, 0)
  40. GUI:setContentSize(self.view.prefabBagTileScrollView_,370, 184)
  41. GUI:setContentSize(self.view.BagTileContent,width, height)
  42. self:RefreshItem()
  43. self:RefreshForbidGrid(self.forbidCount)
  44. end
  45. ---注册UI事件和服务器消息
  46. function this:RegistEvents()
  47. end
  48. function this:Close()
  49. for id, ins in pairs(this.idToIns) do
  50. if ins == self then
  51. this.idToIns[id] = nil
  52. end
  53. end
  54. end
  55. function this:RefreshItem()
  56. local canDrag=true
  57. if self.args and self.args.canDrag~=nil then
  58. canDrag=self.args.canDrag
  59. end
  60. --local item = GUI:UIPanel_Open("dev/ui/Common/Item/KLBagItem/KLBagItemItem",self.view.BagTileContent,self,{x=0, y=0},true)
  61. for index, item in pairs(self.itemList) do
  62. --local hor,vet = BagInfo.GetHorAndVetWithInt(index)
  63. local hor,vet = SL.Bag:GetHorAndVetWithInt(index)
  64. GUI:UIPanel_Open("dev/ui/Common/Item/KLBagItem/KLBagItemItem",self.view.BagTileContent,self,
  65. {BagTileScrollViewId=self.id,item=item, x=(vet-1)*45, y=(hor-1)*-45, z=-100, canDrag=canDrag, beginDrag=self.beginDrag,
  66. onDrag=self.onDrag, endDrag=self.endDrag, ins=self, index=index,click=self.itemClick,isSelect=self.selectList[item.id],callBackUI=self.args.callBackUI},true
  67. , function(bagItem)
  68. self.indexToItem[index] = bagItem
  69. bagItem:Updatenoclip(0)
  70. bagItem:SetItemPutonChatPanel(item)
  71. end)
  72. end
  73. end
  74. function this:RefreshItemByItem(itemList)
  75. self.itemList = itemList
  76. self:RefreshItem()
  77. end
  78. function this:RefreshInfo()
  79. for index, klBagItem in pairs(self.indexToItem) do
  80. klBagItem:RefreshInfo()
  81. end
  82. end
  83. function this:onDrag(eventData, systemData)
  84. -- 如果没在拖拽装备图素,则移动滑动条
  85. if eventData.dragItem == false then
  86. self:MoveScrollView(Time.deltaTime * 80 * systemData.delta.y)
  87. return
  88. end
  89. -- 如果拖到了滑动条上下方区域 滑动条自动上下
  90. self:JudgeMoveScrollView(systemData)
  91. -- 判断占位情况
  92. self:JudgeMoveUseGrid(eventData, systemData)
  93. end
  94. local redGridList
  95. local storageRedGridList
  96. local blueGridList
  97. local storageBlueGridList
  98. function this:JudgeMoveUseGrid(eventData, systemData)
  99. redGridList={}
  100. blueGridList={}
  101. storageRedGridList={}
  102. storageBlueGridList={}
  103. ---@type KLBagItemItem
  104. local dragItem = eventData.temp
  105. local bagTileScrollView = this.idToIns[EBagTileScrollViewId.Bag]
  106. if bagTileScrollView then
  107. local bool, pos = GUI:ScreenPointToLocalPointInRectangle(bagTileScrollView.view.BagTileContent, systemData["position"])
  108. local vet = pos.x // 45 + 1
  109. local hor = -pos.y // 45 + 1
  110. local holdGrid = SL:GetConfig('cfg_item',dragItem.item.cfgId).holdGrid
  111. local holdHor = holdGrid[1]
  112. local holdVet = holdGrid[2]
  113. local startVet = vet - (holdHor-1)//2
  114. local startHor = hor - (holdVet-1)//2
  115. local maxHor, maxVet = SL:GetMetaValue(EMetaVarGetKey.GET_BAG_MAX_HOR_VERT)
  116. local useGridTable = SL:GetMetaValue(EMetaVarGetKey.GET_BAG_USE_GRID_TABLE)
  117. local useTable = useGridTable[1]
  118. local index = dragItem.index
  119. local itemHor,itemVet = SL.Bag:GetHorAndVetWithInt(index)--BagInfo.GetHorAndVetWithInt(index)
  120. -- 在背包里
  121. if hor>0 and vet>0 and hor<=maxHor and vet<=maxVet then
  122. -- 边界情况
  123. if startHor<=0 or startHor+holdVet-1>maxHor or startVet<=0 or startVet+holdHor-1>maxVet then
  124. for nowHor=startHor, startHor+holdVet-1 do
  125. for nowVet=startVet, startVet+holdHor-1 do
  126. table.insert(redGridList, BagInfo.GetIntWithHorAndVet(nowHor,nowVet))
  127. end
  128. end
  129. -- 通常情况
  130. else
  131. for nowHor=startHor, startHor+holdVet-1 do
  132. for nowVet=startVet, startVet+holdHor-1 do
  133. if useTable[BagInfo.GetIntWithHorAndVet(nowHor,nowVet)] == true then
  134. -- 还要排除下移动到拖拽的几个格子是红色的情况
  135. if nowHor<itemHor or nowHor>itemHor+holdVet-1 or nowVet<itemVet or nowVet >itemVet+holdHor-1 then
  136. table.insert(redGridList, BagInfo.GetIntWithHorAndVet(nowHor,nowVet))
  137. else
  138. table.insert(blueGridList, BagInfo.GetIntWithHorAndVet(nowHor,nowVet))
  139. end
  140. else
  141. table.insert(blueGridList, BagInfo.GetIntWithHorAndVet(nowHor,nowVet))
  142. end
  143. end
  144. end
  145. end
  146. end
  147. GUI:DataListInitData(bagTileScrollView.view.red_grid_,function()
  148. return table.count(redGridList)
  149. end,function()
  150. end,function(realIndex)
  151. end,function(realIndex)
  152. local root = GUI:GetChildControl(bagTileScrollView.view.red_grid_,realIndex,'red_grid_item')
  153. local index = redGridList[realIndex+1]
  154. --local rootRectTrans = GUI:GetRectTransform(root)
  155. --rootRectTrans.anchoredPosition = Vector2((index%100-1)*45, -(index//100-1)*45)
  156. --rootRectTrans:SetAsLastSibling()
  157. GUI:SetAnchoredPosition(root,Vector2((index%100-1)*45, -(index//100-1)*45))
  158. GUI:SetAsLastSibling(root)
  159. end)
  160. GUI:DataListUpdateData(bagTileScrollView.view.red_grid_)
  161. GUI:DataListInitData(bagTileScrollView.view.green_grid_,function()
  162. return table.count(blueGridList)
  163. end,function()
  164. end,function(realIndex)
  165. end,function(realIndex)
  166. local root = GUI:GetChildControl(bagTileScrollView.view.green_grid_,realIndex,'green_grid_item')
  167. local index = blueGridList[realIndex+1]
  168. --local rootRectTrans = GUI:GetRectTransform(root)
  169. --rootRectTrans.anchoredPosition = Vector2((index%100-1)*45, -(index//100-1)*45)
  170. --rootRectTrans:SetAsLastSibling()
  171. GUI:SetAnchoredPosition(root,Vector2((index%100-1)*45, -(index//100-1)*45))
  172. GUI:SetAsLastSibling(root)
  173. end)
  174. GUI:DataListUpdateData(bagTileScrollView.view.green_grid_)
  175. end
  176. local storageTileScrollView = this.idToIns[EBagTileScrollViewId.Storage]
  177. if storageTileScrollView then
  178. local bool, pos = GUI:ScreenPointToLocalPointInRectangle(storageTileScrollView.view.BagTileContent, systemData["position"])
  179. local vet = pos.x // 45 + 1
  180. local hor = -pos.y // 45 + 1
  181. local holdGrid = SL:GetConfig('cfg_item',dragItem.item.cfgId).holdGrid
  182. local holdHor = holdGrid[1]
  183. local holdVet = holdGrid[2]
  184. local startVet = vet - (holdHor-1)//2
  185. local startHor = hor - (holdVet-1)//2
  186. local maxHor, maxVet = SL:GetMetaValue(EMetaVarGetKey.GET_STORE_HOUSE_MAX_HV)
  187. local panel = GUI:GetUI("dev/ui/Bag/Panel/KLDepots/KLDepotsPanel")
  188. local useGridTable = SL:GetMetaValue(EMetaVarGetKey.GET_STORE_HOUSE_USE_GRID_TABLE)
  189. local useTable = useGridTable[panel.CurPage]
  190. if useTable == nil then
  191. useTable = {}
  192. end
  193. local index = dragItem.index
  194. local itemHor,itemVet =SL.Bag:GetHorAndVetWithInt(index) --BagInfo.GetHorAndVetWithInt(index)
  195. -- 在仓库里
  196. if hor>0 and vet>0 and hor<=maxHor and vet<=maxVet then
  197. -- 边界情况
  198. if startHor<=0 or startHor+holdVet-1>maxHor or startVet<=0 or startVet+holdHor-1>maxVet then
  199. for nowHor=startHor, startHor+holdVet-1 do
  200. for nowVet=startVet, startVet+holdHor-1 do
  201. table.insert(storageRedGridList, BagInfo.GetIntWithHorAndVet(nowHor,nowVet))
  202. end
  203. end
  204. -- 通常情况
  205. else
  206. for nowHor=startHor, startHor+holdVet-1 do
  207. for nowVet=startVet, startVet+holdHor-1 do
  208. if useTable[BagInfo.GetIntWithHorAndVet(nowHor,nowVet)] == true then
  209. -- 还要排除下移动到拖拽的几个格子是红色的情况
  210. if nowHor<itemHor or nowHor>itemHor+holdVet-1 or nowVet<itemVet or nowVet >itemVet+holdHor-1 then
  211. table.insert(storageRedGridList, BagInfo.GetIntWithHorAndVet(nowHor,nowVet))
  212. else
  213. table.insert(storageBlueGridList, BagInfo.GetIntWithHorAndVet(nowHor,nowVet))
  214. end
  215. else
  216. table.insert(storageBlueGridList, BagInfo.GetIntWithHorAndVet(nowHor,nowVet))
  217. end
  218. end
  219. end
  220. end
  221. end
  222. GUI:DataListInitData(storageTileScrollView.view.red_grid_,function()
  223. return table.count(storageRedGridList)
  224. end,function()
  225. end,function(realIndex)
  226. end,function(realIndex)
  227. local root = GUI:GetChildControl(storageTileScrollView.view.red_grid_,realIndex,'red_grid_item')
  228. local index = storageRedGridList[realIndex+1]
  229. --local rootRectTrans = GUI:GetRectTransform(root)
  230. --rootRectTrans.anchoredPosition = Vector2((index%100-1)*45, -(index//100-1)*45)
  231. --rootRectTrans:SetAsLastSibling()
  232. GUI:SetAnchoredPosition(root,Vector2((index%100-1)*45, -(index//100-1)*45))
  233. GUI:SetAsLastSibling(root)
  234. end)
  235. GUI:DataListUpdateData(storageTileScrollView.view.red_grid_)
  236. GUI:DataListInitData(storageTileScrollView.view.green_grid_,function()
  237. return table.count(storageBlueGridList)
  238. end,function()
  239. end,function(realIndex)
  240. end,function(realIndex)
  241. local root = GUI:GetChildControl(storageTileScrollView.view.green_grid_,realIndex,'green_grid_item')
  242. local index = storageBlueGridList[realIndex+1]
  243. --local rootRectTrans = GUI:GetRectTransform(root)
  244. --rootRectTrans.anchoredPosition = Vector2((index%100-1)*45, -(index//100-1)*45)
  245. --rootRectTrans:SetAsLastSibling()
  246. GUI:SetAnchoredPosition(root,Vector2((index%100-1)*45, -(index//100-1)*45))
  247. GUI:SetAsLastSibling(root)
  248. end)
  249. GUI:DataListUpdateData(storageTileScrollView.view.green_grid_)
  250. end
  251. end
  252. this.InsToCo = {}
  253. function this:JudgeMoveScrollView(systemData)
  254. for _, bagTileIns in pairs(this.idToIns) do
  255. local rectTransform = GUI:GetRectTransform(bagTileIns.view.prefabBagTileScrollView_)
  256. local width = rectTransform.rect.width
  257. local height = rectTransform.rect.height
  258. local bool, pos = GUI:ScreenPointToLocalPointInRectangle(bagTileIns.view.prefabBagTileScrollView_,
  259. systemData["position"])
  260. if pos.x > 0 and pos.x < width then
  261. if pos.y > -45 then
  262. if this.InsToCo[bagTileIns] == nil then
  263. this.InsToCo[bagTileIns] = Coroutine.Start(
  264. function()
  265. while(true) do
  266. Coroutine.WaitForEndOfFrame()
  267. bagTileIns:MoveScrollView(Time.deltaTime * -150)
  268. end
  269. end
  270. )
  271. end
  272. elseif pos.y < -height+45 then
  273. if this.InsToCo[bagTileIns] == nil then
  274. this.InsToCo[bagTileIns] = Coroutine.Start(
  275. function()
  276. while(true) do
  277. Coroutine.WaitForEndOfFrame()
  278. bagTileIns:MoveScrollView(Time.deltaTime * 150)
  279. end
  280. end
  281. )
  282. end
  283. else
  284. if this.InsToCo[bagTileIns] ~= nil then
  285. Coroutine.Stop(this.InsToCo[bagTileIns])
  286. this.InsToCo[bagTileIns] = nil
  287. end
  288. end
  289. else
  290. if this.InsToCo[bagTileIns] ~= nil then
  291. Coroutine.Stop(this.InsToCo[bagTileIns])
  292. this.InsToCo[bagTileIns] = nil
  293. end
  294. end
  295. end
  296. end
  297. function this:MoveScrollView(moveDelta)
  298. local position = GUI:GetRectTransform(self.view.BagTileContent).anchoredPosition
  299. position.y = position.y + moveDelta
  300. GUI:GetRectTransform(self.view.BagTileContent).anchoredPosition = position
  301. end
  302. function this:beginDrag()
  303. end
  304. function this:endDrag(eventData, systemData)
  305. if self.dragEndCallback then
  306. local mousePosition = systemData["position"]
  307. ---@type KLBagItemItem
  308. local dragItem = eventData.dragItem
  309. local panel = GUI:GetUI("dev/ui/Bag/Panel/KLUIBag/KLUIBagPanel")
  310. if panel then
  311. local bool, pos = GUI:ScreenPointToLocalPointInRectangle(panel.bagTileScrollView.view.BagTileContent, mousePosition)
  312. local vet = pos.x // 45 + 1
  313. local hor = -pos.y // 45 + 1
  314. local holdGrid = SL:GetConfig('cfg_item',dragItem.item.cfgId).holdGrid
  315. local holdHor = holdGrid[1]
  316. local holdVet = holdGrid[2]
  317. vet = vet - (holdHor-1)//2
  318. hor = hor - (holdVet-1)//2
  319. local storageScrollView = this.idToIns[EBagTileScrollViewId.Storage]
  320. local storageVet, storageHor
  321. if storageScrollView then
  322. bool, pos = GUI:ScreenPointToLocalPointInRectangle(storageScrollView.view.BagTileContent, mousePosition)
  323. storageVet = pos.x // 45 + 1
  324. storageHor = -pos.y // 45 + 1
  325. holdGrid = SL:GetConfig('cfg_item',dragItem.item.cfgId).holdGrid
  326. holdHor = holdGrid[1]
  327. holdVet = holdGrid[2]
  328. storageVet = storageVet - (holdHor-1)//2
  329. storageHor = storageHor - (holdVet-1)//2
  330. end
  331. self.dragEndCallback(self.parent, {dragItem=dragItem, bagPutHor=hor, bagPutVet=vet, storageHor=storageHor, storageVet=storageVet, index=eventData.index,raycastObj=eventData.raycastObj})
  332. end
  333. end
  334. for _, co in pairs(this.InsToCo) do
  335. Coroutine.Stop(co)
  336. this.InsToCo[_] = nil
  337. end
  338. local bagTileScrollView = this.idToIns[EBagTileScrollViewId.Bag]
  339. if bagTileScrollView then
  340. redGridList = {}
  341. blueGridList = {}
  342. GUI:DataListUpdateData(bagTileScrollView.view.red_grid_)
  343. GUI:DataListUpdateData(bagTileScrollView.view.green_grid_)
  344. end
  345. local storageTileScrollView = this.idToIns[EBagTileScrollViewId.Storage]
  346. if storageTileScrollView then
  347. storageRedGridList = {}
  348. storageBlueGridList = {}
  349. GUI:DataListUpdateData(storageTileScrollView.view.red_grid_)
  350. GUI:DataListUpdateData(storageTileScrollView.view.green_grid_)
  351. end
  352. end
  353. ---刷新选中
  354. ---@param data table<number,boolean>
  355. function this:RefreshSelect(data)
  356. for k,v in pairs(self.indexToItem) do
  357. GUI:setVisible(v.view.selectImg,data[v.item.id])
  358. end
  359. end
  360. function this:RefreshForbidGrid(count)
  361. self.indexForbidList = {}
  362. self.forbidCount = count
  363. local remainCount = count
  364. for nowHor=self.hor,1,-1 do
  365. for nowVet = self.vet, 1, -1 do
  366. if remainCount == 0 then
  367. break
  368. end
  369. remainCount = remainCount - 1
  370. table.insert(self.indexForbidList, nowHor*100+nowVet)
  371. end
  372. end
  373. GUI:DataListInitData(self.view.forbid_list_,function()
  374. return table.count(self.indexForbidList)
  375. end,function()
  376. end,
  377. function() end,
  378. function(realIndex)
  379. local index = self.indexForbidList[realIndex+1]
  380. local hor = index//100
  381. local vet = index%100
  382. local forbid_prefab = GUI:GetChildControl(self.view.forbid_list_,realIndex,'forbid_item')
  383. GUI:setPosition(forbid_prefab, (vet-1)*45,(hor-1)*-45)
  384. GUI:SetOnPrefabControlLoaded(forbid_prefab, function()
  385. local forbid_btn = GUI:GetChildControl(self.view.forbid_list_,realIndex,'UIBagForbidButton')
  386. if self.forbidClickCallback then
  387. GUI:AddOnClickEvent(forbid_btn, self.parent, self.forbidClickCallback,{index=index})
  388. end
  389. end)
  390. end)
  391. GUI:DataListUpdateData(self.view.forbid_list_)
  392. end
  393. function this:Item_Change(eventData)
  394. local ksItemList={}
  395. for _, itemInfo in pairs(eventData) do
  396. local index = itemInfo.index%10000
  397. local item = itemInfo.item
  398. if item == nil then
  399. local bagItem = self.indexToItem[index]
  400. GUI:UIPanel_Close(nil, bagItem)
  401. self.indexToItem[index] = nil
  402. else
  403. if self.indexToItem[index] then
  404. local bagItem = self.indexToItem[index]
  405. bagItem:SetItem(itemInfo.item)
  406. else
  407. local canDrag=true
  408. if self.args and self.args.canDrag~=nil then
  409. canDrag=self.args.canDrag
  410. end
  411. local hor,vet = SL.Bag:GetHorAndVetWithInt(index)--BagInfo.GetHorAndVetWithInt(index)
  412. local bagItem = GUI:UIPanel_Open("dev/ui/Common/Item/KLBagItem/KLBagItemItem",self.view.BagTileContent,self,{item=item, x=(vet-1)*45, y=(hor-1)*-45, z=-100, canDrag= canDrag,
  413. beginDrag=self.beginDrag, onDrag=self.onDrag, endDrag=self.endDrag, ins=self, index=index,click=self.itemClick,callBackUI=self.args.callBackUI},true)
  414. self.indexToItem[index] = bagItem
  415. table.insert(ksItemList, bagItem)
  416. end
  417. end
  418. end
  419. return ksItemList
  420. end
  421. ----清除页面item
  422. function this:ClearBagItem()
  423. for index,v in pairs(self.indexToItem) do
  424. GUI:UIPanel_Close(nil, v)
  425. self.indexToItem[index] = nil
  426. end
  427. end
  428. return this