LayerFacadeMediator.lua 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. local LayerFacadeMediator = class("LayerFacadeMediator", framework.Mediator)
  2. LayerFacadeMediator.NAME = "LayerFacadeMediator"
  3. local mmax = math.max
  4. local tremove = table.remove
  5. function LayerFacadeMediator:ctor()
  6. LayerFacadeMediator.super.ctor(self, self.NAME)
  7. self._normalMediators = {}
  8. self._normalLayerLocalZ = {}
  9. self._allMediators = {}
  10. self._hideMainCounting = 0
  11. end
  12. function LayerFacadeMediator:Cleanup()
  13. self._normalMediators = {}
  14. self._normalLayerLocalZ = {}
  15. self._allMediators = {}
  16. self._hideMainCounting = 0
  17. end
  18. function LayerFacadeMediator:listNotificationInterests()
  19. local noticeTable = global.NoticeTable
  20. return
  21. {
  22. noticeTable.Layer_Open,
  23. noticeTable.Layer_Close,
  24. noticeTable.Layer_Close_Current,
  25. noticeTable.Layer_Enter_Current,
  26. noticeTable.ReleaseMemory,
  27. noticeTable.ResolutionSizeChange,
  28. noticeTable.Layer_SetMoveEnable,
  29. noticeTable.Layer_HideMainEvent,
  30. noticeTable.Layer_SetZOrderPanel,
  31. }
  32. end
  33. function LayerFacadeMediator:handleNotification(notification)
  34. local noticeTable = global.NoticeTable
  35. local noticeName = notification:getName()
  36. local noticeData = notification:getBody()
  37. if noticeTable.Layer_Open == noticeName then
  38. self:OpenLayer(noticeData)
  39. elseif noticeTable.Layer_Close == noticeName then
  40. self:CloseLayer(noticeData)
  41. elseif noticeTable.Layer_Close_Current == noticeName then
  42. self:CloseCurrentLayer()
  43. elseif noticeTable.Layer_Enter_Current == noticeName then
  44. self:EnterCurrentLayer()
  45. elseif noticeTable.Layer_SetMoveEnable == noticeName then
  46. self:SetLayerMoveEnable(noticeData)
  47. elseif noticeTable.ReleaseMemory == noticeName then
  48. self:Cleanup()
  49. elseif noticeTable.ResolutionSizeChange == noticeName then
  50. self:OnResolutionSizeChange()
  51. elseif noticeTable.Layer_HideMainEvent == noticeName then
  52. self:OnHideMainEvent(noticeData)
  53. elseif noticeTable.Layer_SetZOrderPanel == noticeName then
  54. self:SetLayerZPanel(noticeData)
  55. end
  56. end
  57. function LayerFacadeMediator:OnResolutionSizeChange()
  58. dispatchCocosCustomEvent(global.NoticeTable.ResolutionSizeChange)
  59. end
  60. function LayerFacadeMediator:resetLayerPos(mediator)
  61. local moveWidget = mediator._responseMoved or mediator._moveWidget
  62. if not moveWidget or tolua.isnull(moveWidget) or not mediator._layer then
  63. return false
  64. end
  65. local touchNodeSize = moveWidget:getContentSize()
  66. local touchNodeAnchor = moveWidget:getAnchorPoint()
  67. local worldPos = moveWidget:getWorldPosition()
  68. local winSize = global.Director:getWinSize()
  69. local offx = 0
  70. local offy = 0
  71. if winSize.width - worldPos.x < touchNodeSize.width * (1 - touchNodeAnchor.x) then --超右边
  72. offx = -(touchNodeSize.width * (1 - touchNodeAnchor.x) - winSize.width + worldPos.x)
  73. end
  74. if worldPos.x < touchNodeSize.width * touchNodeAnchor.x then --超左边
  75. offx = touchNodeSize.width * touchNodeAnchor.x - worldPos.x
  76. end
  77. if winSize.height - worldPos.y < touchNodeSize.height * (1 - touchNodeAnchor.y) then --超上边
  78. offy = -(touchNodeSize.height * (1 - touchNodeAnchor.y) - winSize.height + worldPos.y)
  79. end
  80. if worldPos.y < touchNodeSize.height * touchNodeAnchor.y then --超下边
  81. offy = touchNodeSize.height * touchNodeAnchor.y - worldPos.y
  82. end
  83. local layer = mediator._layer
  84. local pos = cc.p(layer:getPosition())
  85. layer:setPosition(pos.x + offx, pos.y + offy)
  86. end
  87. function LayerFacadeMediator:checkOutScreen(mediator)
  88. local moveWidget = mediator._responseMoved or mediator._moveWidget
  89. if not moveWidget or not mediator._layer then
  90. return false
  91. end
  92. local touchNodeSize = moveWidget:getContentSize()
  93. local touchNodeAnchor = moveWidget:getAnchorPoint()
  94. local worldPos = moveWidget:getWorldPosition()
  95. local winSize = global.Director:getWinSize()
  96. if worldPos.x - touchNodeSize.width * (touchNodeAnchor.x) < 0 then -- 超左边
  97. return true
  98. end
  99. if worldPos.x + touchNodeSize.width * (1 - touchNodeAnchor.x) > winSize.width then -- 超右边
  100. return true
  101. end
  102. if worldPos.y + touchNodeSize.height * (1 - touchNodeAnchor.y) > winSize.height then -- 超上边
  103. return true
  104. end
  105. if worldPos.y - touchNodeSize.height * (touchNodeAnchor.y) < 0 then --超下边
  106. return true
  107. end
  108. return false
  109. end
  110. function LayerFacadeMediator:InitOnEnterActive(root)
  111. self.UINode = cc.Layer:create()
  112. root:addChild(self.UINode)
  113. self.UINodeNormal = cc.Layer:create()
  114. self.UINode:addChild(self.UINodeNormal, global.UIZ.UI_NORMAL)
  115. end
  116. function LayerFacadeMediator:InitOnEnterWorld()
  117. -- create game world scene
  118. local sceneGraph = require("scene/sceneGraph")
  119. global.sceneGraphCtl = sceneGraph:Inst()
  120. global.sceneGraphCtl:LoadGraph("scene/game_world")
  121. local scene = global.sceneGraphCtl:GetC2dxScene()
  122. local gameWorldController = require("logic/gameWorldController")
  123. local worldLayer = gameWorldController.create()
  124. scene:addChild(worldLayer)
  125. cc.Director:getInstance():replaceScene(scene)
  126. global.gameWorldController = worldLayer
  127. -- ui
  128. self.mainRoot = global.sceneGraphCtl:GetUiNormal() -- 主界面 root
  129. local cppUINode = global.sceneGraphCtl:GetSceneNode(global.MMO.NODE_UI_TOPMOST) -- ui root
  130. self.UINode = cc.Layer:create()
  131. cppUINode:addChild(self.UINode)
  132. self.UINodeNormal = cc.Layer:create()
  133. self.UINode:addChild(self.UINodeNormal, global.UIZ.UI_NORMAL)
  134. -- game world camera
  135. self:InitGameWorldCamera()
  136. global.Facade:sendNotification(global.NoticeTable.InitedWorldTree)
  137. --
  138. local ssrGUINode = cc.Layer:create()
  139. self.UINode:addChild(ssrGUINode, global.UIZ.UI_NOTICE)
  140. ssr.ssrBridge:onInitRootWorld(self.UINodeNormal, self.UINode)
  141. ssr.ssrBridge:setGUIParent(0, self.UINodeNormal)
  142. ssr.ssrBridge:setGUIParent(1, ssrGUINode)
  143. end
  144. function LayerFacadeMediator:InitGameWorldCamera()
  145. local fov = 60
  146. local zEye = global.Director:getZEye()
  147. zEye = (global.isWinPlayMode and (zEye > global.MMO.CAMERA_Z_MAX_WIN)) and global.MMO.CAMERA_Z_MAX_WIN or zEye
  148. local winSize = global.Director:getWinSize()
  149. local up = cc.vec3( 0, 1, 0 )
  150. local center = cc.vec3( winSize.width * 0.5, winSize.height * 0.5, 0 )
  151. local pos = cc.vec3( center.x, center.y, zEye )
  152. local centerOfView = cc.p( winSize.width * 0.5, winSize.height * 0.5 )
  153. local aspectRatio = winSize.width / winSize.height
  154. local cameraFar = math.max(global.MMO.CAMERA_FAR, zEye / global.MMO.VIEW_SCALE_MIN + 1)
  155. local camera = cc.Camera:createPerspective( fov, aspectRatio, global.MMO.CAMERA_NEAR, cameraFar )
  156. camera:setPosition3D( pos )
  157. camera:lookAt( center, up )
  158. camera:setCameraFlag( cc.CameraFlag.USER1 )
  159. local rootNode = global.sceneGraphCtl:GetSceneNode( global.MMO.NODE_ROOT )
  160. local wordRootNode = global.sceneGraphCtl:GetSceneNode( global.MMO.NODE_GAME_WORLD )
  161. rootNode:addChild( camera )
  162. wordRootNode:setCameraMask( cc.CameraFlag.USER1, true )
  163. global.gameMapController:SetViewCamera( camera )
  164. global.MMO.CAMERA_Z_DEFAULT = zEye
  165. end
  166. function LayerFacadeMediator:OpenLayer(data)
  167. local ltype = data.ltype
  168. local layer = data.layer
  169. local mediator = data.mediator
  170. if ltype == global.UIZ.UI_NORMAL then
  171. -- hide last
  172. local mediatorCount = #self._normalMediators
  173. if mediator._hideLast and mediatorCount > 0 then
  174. local lastMediator = self._normalMediators[mediatorCount]
  175. lastMediator._layer:setVisible(false)
  176. end
  177. self._normalMediators[mediatorCount + 1] = mediator
  178. -- hide main
  179. if mediator._hideMain then
  180. if self._hideMainCounting == 0 then
  181. global.Facade:sendNotification(global.NoticeTable.Layer_Main_Hide)
  182. end
  183. self._hideMainCounting = self._hideMainCounting + 1
  184. end
  185. -- audio
  186. if mediator._voice then
  187. global.Facade:sendNotification(global.NoticeTable.Audio_Play, { type = global.MMO.SND_TYPE_LAYER_CLICK })
  188. end
  189. -- 点击浮起
  190. self:CreateSetZPanel(mediator)
  191. -- 设置坐标
  192. mediator._layer:setPosition(mediator:getResetPosition())
  193. mediator._layer:setLocalZOrder(mediatorCount + 1)
  194. -- addChild
  195. self.UINodeNormal:addChild(layer)
  196. elseif ltype == global.UIZ.UI_MAIN then
  197. self.mainRoot:addChild(layer, global.UIZ.UI_MAIN)
  198. if mediator._GUI_ID then
  199. -- 设置坐标
  200. mediator._layer:setPosition(mediator:getResetPosition())
  201. end
  202. elseif ltype == global.UIZ.UI_RTOUCH then
  203. self.mainRoot:addChild(layer, ltype)
  204. if global.mouseEventController and mediator._mouseBtnEvent then
  205. local children = layer:getChildren()
  206. for k, v in pairs(children) do
  207. local target = v:getChildByName("Panel_1")
  208. if target then
  209. global.mouseEventController:registerMouseButtonEvent(target, mediator._mouseBtnParam)
  210. break
  211. end
  212. end
  213. end
  214. elseif ltype then
  215. self.UINode:addChild(layer, ltype)
  216. if ltype == global.UIZ.UI_NOTICE and mediator._GUI_ID then
  217. -- 设置坐标
  218. mediator._layer:setPosition(mediator:getResetPosition())
  219. end
  220. if global.mouseEventController and mediator._mouseBtnEvent then
  221. local children = layer:getChildren()
  222. for k, v in pairs(children) do
  223. local target = v:getChildByName("Panel_1")
  224. if target then
  225. global.mouseEventController:registerMouseButtonEvent(target, mediator._mouseBtnParam)
  226. break
  227. end
  228. end
  229. end
  230. end
  231. -- for GUI
  232. if mediator._GUI_ID then
  233. mediator._layer.ID = mediator._GUI_ID
  234. mediator._layer.mediator = mediator
  235. ssr.GUI.WinLayers[mediator._GUI_ID] = mediator._layer
  236. ssr.GUI.ATTACH_PARENT = mediator._layer
  237. GUI.WinLayers[mediator._GUI_ID] = mediator._layer
  238. GUI.ATTACH_PARENT = mediator._layer
  239. SLBridge:onLUAEvent(LUA_EVENT_OPENWIN, mediator._GUI_ID)
  240. end
  241. self._allMediators[#self._allMediators + 1] = mediator
  242. end
  243. function LayerFacadeMediator:CloseLayer(data)
  244. local ltype = data.ltype
  245. local layer = data.layer
  246. local mediator = data.mediator
  247. -- for GUI
  248. if mediator._GUI_ID then
  249. ssr.GUI.WinLayers[mediator._GUI_ID] = nil
  250. ssr.GUI.ATTACH_PARENT = nil
  251. GUI.WinLayers[mediator._GUI_ID] = nil
  252. GUI.ATTACH_PARENT = nil
  253. end
  254. if ltype == global.UIZ.UI_NORMAL then
  255. -- remove from items
  256. local index = nil
  257. for k, v in pairs(self._normalMediators) do
  258. if mediator._mediatorName == v._mediatorName then
  259. tremove(self._normalMediators, k)
  260. index = k
  261. break
  262. end
  263. end
  264. -- 重新计算界面 Z 轴
  265. for k, v in pairs(self._normalMediators) do
  266. v:SetLocalZOrder(k)
  267. end
  268. -- for GUI
  269. -- 找到最后一个GUI,并设置为当前挂节点
  270. for i = #self._normalMediators, 1, -1 do
  271. local mediator = self._normalMediators[1]
  272. if mediator._GUI_ID then
  273. ssr.GUI.ATTACH_PARENT = mediator._layer
  274. GUI.ATTACH_PARENT = mediator._layer
  275. end
  276. end
  277. -- show last
  278. if mediator._hideLast and index and (index - 1 > 0) then
  279. local lastMediator = self._normalMediators[index - 1]
  280. lastMediator._layer:setVisible(true)
  281. end
  282. -- show main
  283. if mediator._hideMain then
  284. self._hideMainCounting = self._hideMainCounting - 1
  285. self._hideMainCounting = mmax(self._hideMainCounting, 0)
  286. if self._hideMainCounting == 0 then
  287. global.Facade:sendNotification(global.NoticeTable.Layer_Main_Show)
  288. end
  289. end
  290. -- audio
  291. if mediator._voice then
  292. global.Facade:sendNotification(global.NoticeTable.Audio_Play, { type = global.MMO.SND_TYPE_LAYER_CLOSE })
  293. end
  294. end
  295. for k, v in pairs(self._allMediators) do
  296. if mediator._mediatorName == v._mediatorName then
  297. tremove(self._allMediators, k)
  298. break
  299. end
  300. end
  301. if not layer or tolua.isnull(layer) then
  302. if mediator._GUI_ID then
  303. releasePrint("[ERROR] GUI: " .. mediator._GUI_ID .. " Layer is Removed Before CloseLayer! ")
  304. end
  305. return
  306. end
  307. layer:removeFromParent()
  308. end
  309. function LayerFacadeMediator:CloseCurrentLayer()
  310. local mediators = self._normalMediators
  311. if #mediators > 0 then
  312. local mediator = mediators[#mediators]
  313. if mediator._escClose then
  314. self:SetOnMovingOpacity(false)
  315. if mediator._GUI_ID and GUI.WinLayers[mediator._GUI_ID] then
  316. GUI:Win_Close(GUI.WinLayers[mediator._GUI_ID])
  317. else
  318. mediator:CloseLayer()
  319. end
  320. end
  321. end
  322. end
  323. function LayerFacadeMediator:EnterCurrentLayer()
  324. for i = #self._allMediators, 1, -1 do
  325. local mediator = self._allMediators[i]
  326. if mediator:handlePressedEnter() then
  327. break
  328. end
  329. end
  330. end
  331. function LayerFacadeMediator:ResetMediatorOrder(mediator)
  332. local mediatorCount = #self._normalMediators
  333. if mediatorCount == 0 then
  334. return
  335. end
  336. if self._normalMediators[mediatorCount]._mediatorName == mediator._mediatorName then
  337. return
  338. end
  339. -- 放到最后一个
  340. for k, v in pairs(self._normalMediators) do
  341. if mediator._mediatorName == v._mediatorName then
  342. tremove(self._normalMediators, k)
  343. break
  344. end
  345. end
  346. table.insert(self._normalMediators, mediator)
  347. -- 刷新 Z 轴
  348. for k, v in pairs(self._normalMediators) do
  349. v:SetLocalZOrder(k)
  350. end
  351. -- for GUI
  352. -- 找到最后一个GUI,并设置为当前挂节点
  353. for i = #self._normalMediators, 1, -1 do
  354. local mediator = self._normalMediators[1]
  355. if mediator._GUI_ID then
  356. ssr.GUI.ATTACH_PARENT = mediator._layer
  357. GUI.ATTACH_PARENT = mediator._layer
  358. end
  359. end
  360. end
  361. function LayerFacadeMediator:UpdateLayerPosition(mediator)
  362. local historyPos = mediator:getResetPosition()
  363. if mediator._moveWidget then
  364. local touchNodeSize = mediator._moveWidget:getContentSize()
  365. local touchNodeAnchor = mediator._moveWidget:getAnchorPoint()
  366. local worldPos = mediator._moveWidget:getWorldPosition()
  367. local winSize = global.Director:getWinSize()
  368. local overx = false
  369. local overy = false
  370. if winSize.width - (worldPos.x) < touchNodeSize.width * (1 - touchNodeAnchor.x) then --超右边
  371. overx = true
  372. end
  373. if worldPos.x < touchNodeSize.width * touchNodeAnchor.x then --超左边
  374. overx = true
  375. end
  376. if winSize.height - (worldPos.y) < touchNodeSize.height * (1 - touchNodeAnchor.y) then --超上边
  377. overy = true
  378. end
  379. if worldPos.y < touchNodeSize.height * touchNodeAnchor.y then --超下边
  380. overy = true
  381. end
  382. if overx or overy then
  383. mediator._layer:setPosition(0, 0)
  384. end
  385. end
  386. end
  387. function LayerFacadeMediator:InitLayerPostionLimit(mediator, moveWidget)
  388. -- 预留rect 50 50
  389. local minRect = { width = 80, height = 80 }
  390. local maxX = 0
  391. local minX = 0
  392. local maxY = 0
  393. local minY = 0
  394. if moveWidget and not tolua.isnull(moveWidget) then
  395. local touchNodeSize = moveWidget:getContentSize()
  396. local touchNodeAnchor = moveWidget:getAnchorPoint()
  397. local worldPos = moveWidget:getWorldPosition()
  398. local winSize = global.Director:getWinSize()
  399. maxX = winSize.width + touchNodeSize.width * touchNodeAnchor.x - minRect.width - worldPos.x
  400. minX = -(worldPos.x + touchNodeSize.width * (1 - touchNodeAnchor.x) - minRect.width)
  401. maxY = winSize.height + touchNodeSize.height * touchNodeAnchor.y - minRect.height - worldPos.y
  402. minY = -(worldPos.y + touchNodeSize.height * (1 - touchNodeAnchor.y) - minRect.height)
  403. end
  404. mediator.maxX = maxX or 0
  405. mediator.minX = minX or 0
  406. mediator.maxY = maxY or 0
  407. mediator.minY = minY or 0
  408. ----
  409. local originPosX, originPosY
  410. if mediator._layer then
  411. originPosX, originPosY = mediator._layer:getPosition()
  412. end
  413. mediator.originPosX = originPosX or 0
  414. mediator.originPosY = originPosY or 0
  415. end
  416. function LayerFacadeMediator:CreateSetZPanel(mediator)
  417. local layer = mediator._layer
  418. local children = layer:getChildren()
  419. local panel = nil
  420. local parent = nil
  421. for k, v in pairs(children) do
  422. local target = v:getChildByName("Panel_1")
  423. if target then
  424. panel = target
  425. parent = v
  426. break
  427. end
  428. end
  429. if parent and panel and global.mouseEventController then
  430. global.mouseEventController:registerMouseMoveEvent(panel)
  431. if mediator._mouseBtnEvent then
  432. global.mouseEventController:registerMouseButtonEvent(panel, mediator._mouseBtnParam)
  433. end
  434. local size = panel:getContentSize()
  435. local anchor = panel:getAnchorPoint()
  436. local posx = panel:getPositionX()
  437. local posy = panel:getPositionY()
  438. local settingZ = ccui.Widget:create()
  439. settingZ:setAnchorPoint(anchor)
  440. settingZ:setContentSize(size)
  441. settingZ:setPosition(posx, posy)
  442. settingZ:setTouchEnabled(true)
  443. settingZ:addTouchEventListener(
  444. function(sender, event)
  445. if event == ccui.TouchEventType.began then
  446. self:ResetMediatorOrder(mediator)
  447. end
  448. end
  449. )
  450. settingZ:setSwallowTouches(false)
  451. parent:addChild(settingZ)
  452. end
  453. end
  454. function LayerFacadeMediator:SetLayerZPanel(data)
  455. local mediator = data.mediator
  456. local panel = data.zPanel
  457. local parent = panel:getParent()
  458. if parent and panel and global.mouseEventController then
  459. global.mouseEventController:registerMouseMoveEvent(panel)
  460. if mediator._mouseBtnEvent then
  461. global.mouseEventController:registerMouseButtonEvent(panel, mediator._mouseBtnParam)
  462. end
  463. local size = panel:getContentSize()
  464. local anchor = panel:getAnchorPoint()
  465. local posx = panel:getPositionX()
  466. local posy = panel:getPositionY()
  467. local settingZ = ccui.Widget:create()
  468. settingZ:setAnchorPoint(anchor)
  469. settingZ:setContentSize(size)
  470. settingZ:setPosition(posx, posy)
  471. settingZ:setTouchEnabled(true)
  472. settingZ:addTouchEventListener(
  473. function(sender, event)
  474. if event == ccui.TouchEventType.began then
  475. self:ResetMediatorOrder(mediator)
  476. end
  477. end
  478. )
  479. settingZ:setSwallowTouches(false)
  480. parent:addChild(settingZ)
  481. end
  482. end
  483. function LayerFacadeMediator:SetLayerMoveEnable(data)
  484. local layer = data.layer
  485. local mediator = data.mediator
  486. local moveWidget = data.moveWidget
  487. if not layer or not mediator or not moveWidget then
  488. return nil
  489. end
  490. if not data.isMove then
  491. if mediator._moveWidget then
  492. RemoveAllWidgetTouchEventListener(moveWidget)
  493. if not moveWidget._linkClick then
  494. moveWidget:addClickEventListener(function()
  495. end)
  496. end
  497. mediator._moveWidget = nil
  498. end
  499. return
  500. end
  501. mediator._moveWidget = moveWidget
  502. self:UpdateLayerPosition(mediator)
  503. self:InitLayerPostionLimit(mediator, moveWidget)
  504. local function checkPos(x, y)
  505. if x >= mediator.maxX then
  506. x = mediator.maxX
  507. elseif x <= mediator.minX then
  508. x = mediator.minX
  509. end
  510. if y >= mediator.maxY then
  511. y = mediator.maxY
  512. elseif y <= mediator.minY then
  513. y = mediator.minY
  514. end
  515. return x, y
  516. end
  517. local isMoved = false
  518. local isPressed = false
  519. local function delayCallback()
  520. if not moveWidget or isPressed then
  521. return
  522. end
  523. if not isMoved then
  524. self:SetOnMovingOpacity(true)
  525. return
  526. end
  527. local sPos = moveWidget:getTouchBeganPosition()
  528. local ePos = moveWidget:getTouchMovePosition()
  529. local diff = cc.pSub(ePos, sPos)
  530. local distSq = cc.pLengthSQ(diff)
  531. if distSq <= 100 then
  532. isPressed = true
  533. self:SetOnMovingOpacity(true)
  534. end
  535. end
  536. local function ontouch(sender, event)
  537. if event == ccui.TouchEventType.began then
  538. mediator.basePosX, mediator.basePosy = layer:getPosition()
  539. isMoved = false
  540. isPressed = false
  541. sender:stopAllActions()
  542. performWithDelay(sender, delayCallback, 2)
  543. -- 设置当前界面为层级最高
  544. self:ResetMediatorOrder(mediator)
  545. elseif event == ccui.TouchEventType.moved then
  546. local sPos = sender:getTouchBeganPosition()
  547. local ePos = sender:getTouchMovePosition()
  548. local x = mediator.basePosX + ePos.x - sPos.x - mediator.originPosX
  549. local y = mediator.basePosy + ePos.y - sPos.y - mediator.originPosY
  550. x, y = checkPos(x, y)
  551. layer:setPosition(x + mediator.originPosX, y + mediator.originPosY)
  552. local diff = cc.pSub(ePos, sPos)
  553. local distSq = cc.pLengthSQ(diff)
  554. if not isMoved and distSq > 100 then
  555. isMoved = true
  556. self:SetOnMovingOpacity(true)
  557. end
  558. elseif event == ccui.TouchEventType.ended or event == ccui.TouchEventType.canceled then
  559. sender:stopAllActions()
  560. local endPosX, endPosY = layer:getPosition()
  561. endPosX, endPosY = checkPos(endPosX, endPosY)
  562. mediator.__savedPosX = endPosX
  563. mediator.__savedPosY = endPosY
  564. self:SetOnMovingOpacity(false)
  565. end
  566. end
  567. moveWidget:setTouchEnabled(true)
  568. moveWidget:addTouchEventListener(ontouch)
  569. addCocosCustomEventListener(moveWidget, global.NoticeTable.ResolutionSizeChange, function()
  570. self:resetLayerPos(mediator)
  571. end)
  572. end
  573. function LayerFacadeMediator:SetOnMovingOpacity(isBegin)
  574. if CHECK_SETTING and CHECK_SETTING(global.MMO.SETTING_IDX_LAYER_OPACITY) ~= 1 then
  575. return
  576. end
  577. if isBegin == self.OnMovingOpacity then
  578. return
  579. end
  580. self.OnMovingOpacity = isBegin
  581. local node_list = {}
  582. local function seach_child(parente)
  583. for _, child in ipairs(parente:getChildren()) do
  584. seach_child(child)
  585. end
  586. table.insert(node_list, parente)
  587. end
  588. seach_child(self.UINodeNormal)
  589. for i, v in ipairs(node_list) do
  590. v:setCascadeOpacityEnabled(isBegin)
  591. end
  592. self.UINodeNormal:setOpacity(isBegin and 150 or 255)
  593. end
  594. function LayerFacadeMediator:GetLayerLocalZorder(mediator)
  595. local removeKey = nil
  596. local maxn = table.maxn(self._normalLayerLocalZ)
  597. for k, v in pairs(self._normalLayerLocalZ) do
  598. if v == mediator._mediatorName then
  599. removeKey = k
  600. break
  601. end
  602. end
  603. if removeKey and maxn and removeKey == maxn then
  604. return -1
  605. elseif removeKey then
  606. self._normalLayerLocalZ[removeKey] = nil
  607. end
  608. local newLayerZorder = maxn + 1
  609. self._normalLayerLocalZ[newLayerZorder] = mediator._mediatorName
  610. return newLayerZorder
  611. end
  612. function LayerFacadeMediator:AddSSROBJNormalMediator(mediator)
  613. self._normalMediators[#self._normalMediators + 1] = mediator
  614. if mediator and mediator._layer then
  615. mediator._layer:setLocalZOrder(#self._normalMediators)
  616. end
  617. end
  618. function LayerFacadeMediator:RemoveSSROBJNormalMediator(mediator)
  619. if not mediator then
  620. return
  621. end
  622. local index = nil
  623. -- remove from items
  624. for k, v in pairs(self._normalMediators) do
  625. if mediator._mediatorName == v._mediatorName then
  626. tremove(self._normalMediators, k)
  627. index = k
  628. break
  629. end
  630. end
  631. -- 重新计算界面 Z 轴
  632. for k, v in pairs(self._normalMediators) do
  633. v:SetLocalZOrder(k)
  634. end
  635. -- for GUI
  636. -- 找到最后一个GUI,并设置为当前挂节点
  637. for i = #self._normalMediators, 1, -1 do
  638. local mediator = self._normalMediators[1]
  639. if mediator._GUI_ID then
  640. ssr.GUI.ATTACH_PARENT = mediator._layer
  641. GUI.ATTACH_PARENT = mediator._layer
  642. end
  643. end
  644. end
  645. function LayerFacadeMediator:RemoveSSRNormalLayerZOrder(layer)
  646. local removeKey, zOrder, layerKey
  647. if layer and not tolua.isnull(layer) then
  648. layerKey = layer:getName()
  649. zOrder = layer:getLocalZOrder()
  650. end
  651. if not layerKey or not zOrder then
  652. return
  653. end
  654. for k, v in pairs(self._normalLayerLocalZ) do
  655. if v == "__SSR_GUI_MEDIATOR" .. "_" .. layerKey and k == zOrder then
  656. removeKey = k
  657. break
  658. end
  659. end
  660. if removeKey then
  661. self._normalLayerLocalZ[removeKey] = nil
  662. end
  663. end
  664. function LayerFacadeMediator:OnHideMainEvent(mediator)
  665. if mediator._hideMain then
  666. if self._hideMainCounting == 0 then
  667. global.Facade:sendNotification(global.NoticeTable.Layer_Main_Hide)
  668. end
  669. self._hideMainCounting = self._hideMainCounting + 1
  670. else
  671. self._hideMainCounting = self._hideMainCounting - 1
  672. self._hideMainCounting = mmax(self._hideMainCounting, 0)
  673. if self._hideMainCounting == 0 then
  674. global.Facade:sendNotification(global.NoticeTable.Layer_Main_Show)
  675. end
  676. end
  677. end
  678. function LayerFacadeMediator:resetLayerPostion(mediator)
  679. if mediator and mediator._layer then
  680. mediator._layer:setPosition(mediator:getResetPosition())
  681. end
  682. end
  683. return LayerFacadeMediator