SUIManager.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. local DebugMediator = requireMediator("DebugMediator")
  2. local SUIManager = class("SUIManager", DebugMediator)
  3. SUIManager.NAME = "SUIManager"
  4. function SUIManager:ctor()
  5. SUIManager.super.ctor(self, self.NAME)
  6. self._allITEMBOXWidgets = {}
  7. self._ITEMBOXItems = {}
  8. self._allItemShowWidgets = {}
  9. self._itemShowElements = {}
  10. self._itemShowItems = {}
  11. end
  12. function SUIManager:destory()
  13. if SUIManager.instance then
  14. global.Facade:removeMediator( SUIManager.NAME )
  15. SUIManager.instance = nil
  16. end
  17. end
  18. function SUIManager:Inst()
  19. if not SUIManager.instance then
  20. SUIManager.instance = SUIManager.new()
  21. global.Facade:registerMediator(SUIManager.instance)
  22. end
  23. return SUIManager.instance
  24. end
  25. function SUIManager:listNotificationInterests()
  26. local noticeTable = global.NoticeTable
  27. return
  28. {
  29. noticeTable.SUIITEMBOXWidgetAdd,
  30. noticeTable.SUIITEMBOXPutoutAll,
  31. noticeTable.ReleaseMemory,
  32. }
  33. end
  34. function SUIManager:handleNotification(notification)
  35. local noticeTable = global.NoticeTable
  36. local noticeID = notification:getName()
  37. local data = notification:getBody()
  38. if noticeTable.SUIITEMBOXWidgetAdd == noticeID then
  39. self:OnSUIITEMBOXWidgetAdd(data)
  40. elseif noticeTable.SUIITEMBOXPutoutAll == noticeID then
  41. self:OnSUIITEMBOXPutoutAll(data)
  42. elseif noticeTable.ReleaseMemory == noticeID then
  43. self:OnReleaseMemory(data)
  44. end
  45. end
  46. function SUIManager:OnReleaseMemory(data)
  47. self._ITEMBOXItems = {}
  48. end
  49. ----------------------------
  50. function SUIManager:OnSUIITEMBOXWidgetAdd(data)
  51. local boxindex = data.boxindex
  52. local widget = data.widget
  53. -- 监听事件;
  54. widget:registerScriptHandler( function(state)
  55. if state == "enter" then
  56. if boxindex then
  57. self._allITEMBOXWidgets[boxindex] = widget
  58. self:ReloadITEMBOXWidget(boxindex, false)
  59. end
  60. elseif state == "exit" then
  61. if boxindex then
  62. self._allITEMBOXWidgets[boxindex] = nil
  63. end
  64. end
  65. end )
  66. end
  67. function SUIManager:OnSUIITEMBOXPutoutAll()
  68. --
  69. for boxindex, itembox in pairs(self._ITEMBOXItems) do
  70. local npcProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
  71. local submitData =
  72. {
  73. index = boxindex,
  74. npcid = npcProxy:GetCurrentNPCID()
  75. }
  76. global.SUIManager:RequestITEMBOXPutout(submitData)
  77. end
  78. end
  79. function SUIManager:ReloadAllITEMBOXWidgets()
  80. for boxindex, itemboxWidget in pairs(self._allITEMBOXWidgets) do
  81. self:ReloadITEMBOXWidget(boxindex)
  82. end
  83. end
  84. function SUIManager:ReloadITEMBOXWidget(boxindex,notCheckHave)
  85. local itemboxWidget = self._allITEMBOXWidgets[boxindex]
  86. if nil == itemboxWidget then
  87. return nil
  88. end
  89. local itemData = self._ITEMBOXItems[boxindex]
  90. local MakeIndex = itemData and itemData.MakeIndex
  91. if notCheckHave==false and MakeIndex then
  92. local BagProxy = global.Facade:retrieveProxy( global.ProxyTable.Bag )
  93. itemData = BagProxy:GetItemDataByMakeIndex(MakeIndex)
  94. if not itemData then
  95. local ItemManagerProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemManagerProxy)
  96. local itemType = ItemManagerProxy:GetItemType(itemData)
  97. if itemType == ItemManagerProxy.ItemType.Equip then
  98. local EquipProxy = global.Facade:retrieveProxy( global.ProxyTable.Equip )
  99. itemData = EquipProxy:GetEquipDataByMakeIndex(MakeIndex)
  100. end
  101. end
  102. end
  103. if nil == itemData then
  104. itemboxWidget:removeChildByName("item")
  105. return nil
  106. end
  107. local contentSize = itemboxWidget:getContentSize()
  108. local ItemMoveProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemMoveProxy)
  109. --
  110. local info = {}
  111. info.index = itemData.Index
  112. info.itemData = itemData
  113. info.movable = true
  114. info.look = true
  115. info.from = ItemMoveProxy.ItemFrom.ITEMBOX
  116. local goodItem = GoodsItem:create(info)
  117. itemboxWidget:removeChildByName("item")
  118. itemboxWidget:addChild(goodItem)
  119. goodItem:setName("item")
  120. goodItem:setPosition(cc.p(contentSize.width/2, contentSize.height/2))
  121. end
  122. function SUIManager:GetITEMBOXIndexByMakeIndex(makeindex)
  123. for k, v in pairs(self._ITEMBOXItems) do
  124. if v.MakeIndex == makeindex then
  125. return k
  126. end
  127. end
  128. return nil
  129. end
  130. function SUIManager:LoadItemShowWidget(itemid)
  131. if nil == self._itemShowItems[itemid] then
  132. self:RequestItemShowInfo(itemid)
  133. return nil
  134. end
  135. for k, element in pairs(self._itemShowElements) do
  136. if element.itemid == itemid then
  137. local itemData = self._itemShowItems[itemid]
  138. --
  139. local info = {}
  140. info.index = itemData.Index
  141. info.itemData = itemData
  142. info.look = element.showtips == 1
  143. info.count = element.itemcount
  144. info.bgVisible = element.bgtype == 1
  145. local goodItem = GoodsItem:create(info)
  146. element.widget:removeChildByName("item")
  147. element.widget:addChild(goodItem)
  148. goodItem:setName("item")
  149. goodItem:setPosition(cc.p(35, 35))
  150. end
  151. end
  152. end
  153. function SUIManager:onRegister()
  154. SUIManager.super.onRegister(self)
  155. end
  156. function SUIManager:RegisterMsgHandlerAfterEnterWorld()
  157. local msgType = global.MsgType
  158. LuaRegisterMsgHandler(msgType.MSG_SC_ITEMBOX_UPDATE, handler(self, self.RespITEMBOXUpdate))
  159. LuaRegisterMsgHandler(msgType.MSG_SC_ITEMBOX_REMOVE, handler(self, self.RespITEMBOXRemove))
  160. LuaRegisterMsgHandler(msgType.MSG_SC_ItemShow_INFO, handler(self, self.RespItemShowInfo))
  161. end
  162. function SUIManager:RespITEMBOXUpdate(msg)
  163. local jsonData = ParseRawMsgToJson(msg)
  164. if not jsonData then
  165. return nil
  166. end
  167. local header = msg:GetHeader()
  168. local boxindex = header.recog
  169. local itemData = ChangeItemServersSendDatas(jsonData)
  170. -- dump(header)
  171. -- dump(jsonData)
  172. self._ITEMBOXItems[boxindex] = itemData
  173. --
  174. self:ReloadITEMBOXWidget(boxindex)
  175. end
  176. function SUIManager:RespITEMBOXRemove(msg)
  177. local header = msg:GetHeader()
  178. local boxindex = header.recog
  179. self._ITEMBOXItems[boxindex] = nil
  180. -- dump(boxindex)
  181. --
  182. self:ReloadITEMBOXWidget(boxindex)
  183. end
  184. function SUIManager:RequestITEMBOXPutin(data)
  185. -- dump(data)
  186. SendTableToServer(global.MsgType.MSG_CS_ITEMBOX_PUTIN, data)
  187. end
  188. function SUIManager:RequestITEMBOXPutout(data)
  189. -- dump(data)
  190. SendTableToServer(global.MsgType.MSG_CS_ITEMBOX_PUTOUT, data)
  191. end
  192. function SUIManager:RespItemShowInfo(msg)
  193. local jsonData = ParseRawMsgToJson(msg)
  194. if not jsonData then
  195. return nil
  196. end
  197. local itemData = ChangeItemServersSendDatas(jsonData)
  198. self._itemShowItems[itemData.Index] = itemData
  199. --
  200. self:LoadItemShowWidget(itemData.Index)
  201. end
  202. function SUIManager:RequestItemShowInfo(itemid)
  203. -- dump(itemid)
  204. LuaSendMsg(global.MsgType.MSG_CS_ItemShow_INFO, itemid)
  205. end
  206. return SUIManager