SLHandlerEvent.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. SLHandlerEvent = {}
  2. SLHandlerEvent.Events = {}
  3. SLHandlerEvent.EventDesc = {}
  4. SLHandlerEvent.PropertyEvents = {}
  5. ------------------------------------------------------------------------------------------------------------------------
  6. -- 窗口事件
  7. WND_EVENT_MOUSE_LB_DOWN = 1 -- 鼠标左键按下事件
  8. WND_EVENT_MOUSE_LB_UP = 2 -- 鼠标左键弹起事件
  9. WND_EVENT_MOUSE_LB_CLICK = 3 -- 鼠标左键点击事件
  10. WND_EVENT_MOUSE_LB_DBCLICK = 4 -- 鼠标左键双击事件
  11. WND_EVENT_MOUSE_RB_DOWN = 5 -- 鼠标右键按下事件
  12. WND_EVENT_MOUSE_RB_UP = 6 -- 鼠标右键弹起事件
  13. WND_EVENT_MOUSE_RB_CLICK = 7 -- 鼠标右键点击事件
  14. WND_EVENT_MOUSE_RB_DBCLICK = 8 -- 鼠标右键双击事件
  15. WND_EVENT_MOUSE_MOVE = 9 -- 鼠标移动事件
  16. WND_EVENT_MOUSE_WHEEL = 10 -- 鼠标滚轮滚动事件
  17. WND_EVENT_MOUSE_IN = 11 -- 鼠标进入控件事件
  18. WND_EVENT_MOUSE_OUT = 12 -- 鼠标离开控件事件
  19. WND_EVENT_WND_VISIBLE = 21 -- 可见状态发生变化事件
  20. WND_EVENT_WND_POS_CHANGE = 22 -- 控件位置发生变化事件
  21. WND_EVENT_WND_SIZECHANGE = 23 -- 窗口大小发生变化事件
  22. WND_EVENT_WND_DESTROY = 24 -- 窗体被销毁事件
  23. WND_EVENT_ITEM_IN = 31 -- 物品框物品移入事件
  24. WND_EVENT_ITEM_OUT = 32 -- 物品框物品移出事件
  25. ------------------------------------------------------------------------------------------------------------------------
  26. local function RegisterMouseClickEvent(widget)
  27. local function __OnMouseDown(sender)
  28. if not widget.__TYPE__ then
  29. return false
  30. end
  31. if not widget._hideTouch then
  32. if not GetWidgetVisible(widget) then
  33. return false
  34. end
  35. end
  36. if not widget:isTouchEnabled() then
  37. return false
  38. end
  39. if not widget:isSwallowTouches() then
  40. return false
  41. end
  42. local call_down_r = SLHandlerEvent:GetCallBack(widget, WND_EVENT_MOUSE_RB_DOWN)
  43. local call_down_l = SLHandlerEvent:GetCallBack(widget, WND_EVENT_MOUSE_LB_DOWN)
  44. local call_click_l = SLHandlerEvent:GetCallBack(widget, WND_EVENT_MOUSE_LB_CLICK)
  45. local call_click_r = SLHandlerEvent:GetCallBack(widget, WND_EVENT_MOUSE_RB_CLICK)
  46. local call_double_l = SLHandlerEvent:GetCallBack(widget, WND_EVENT_MOUSE_LB_DBCLICK)
  47. local call_double_r = SLHandlerEvent:GetCallBack(widget, WND_EVENT_MOUSE_RB_DBCLICK)
  48. local mousePosX = sender:getCursorX()
  49. local mousePosY = sender:getCursorY()
  50. local touchPos = {x = mousePosX, y = mousePosY}
  51. local globalM = global.mouseEventController
  52. local isInSide = globalM:checkNodePos(widget, touchPos)
  53. local mButton = sender:getMouseButton()
  54. if mButton == cc.MouseButton.BUTTON_RIGHT then
  55. globalM:CancelMove(sender, true, true)
  56. if isInSide then
  57. if call_down_r then
  58. SLHandlerEvent.swallowMouse = call_down_r(widget) or false
  59. end
  60. sender:stopPropagation()
  61. end
  62. else
  63. if isInSide then
  64. if call_down_l then
  65. SLHandlerEvent.swallowMouse = call_down_l(widget) or false
  66. widget._DOWN___ = SLHandlerEvent.swallowMouse
  67. else
  68. widget._DOWN___ = nil
  69. end
  70. if widget._DOWN___ then
  71. sender:stopPropagation()
  72. return true
  73. end
  74. if SLHandlerEvent.swallowMouse then
  75. sender:stopPropagation()
  76. return true
  77. end
  78. if call_double_l then
  79. if widget._onDoubleLClickEvent then
  80. widget._onDoubleLClickEvent = false
  81. globalM:CancelMove(sender, false, false)
  82. SLHandlerEvent.swallowMouse = call_double_l({widget = widget, touchPos = touchPos}) or false
  83. if widget._clickDelayLHandler then
  84. UnSchedule(widget._clickDelayLHandler)
  85. widget._clickDelayLHandler = false
  86. end
  87. if SLHandlerEvent.swallowMouse then
  88. sender:stopPropagation()
  89. return true
  90. end
  91. else
  92. widget._onDoubleLClickEvent = true
  93. -- 记录单击触发
  94. widget._clickDelayLHandler =
  95. PerformWithDelayGlobal(
  96. function()
  97. widget._clickDelayLHandler = false
  98. widget._onDoubleLClickEvent = false
  99. end,
  100. 0.5
  101. )
  102. end
  103. end
  104. if call_click_l then
  105. SLHandlerEvent.swallowMouse = call_click_l(widget) or false
  106. end
  107. sender:stopPropagation()
  108. end
  109. end
  110. end
  111. local function __OnMouseUp(sender)
  112. if not widget.__TYPE__ then
  113. return false
  114. end
  115. if not GetWidgetVisible(widget) then
  116. return false
  117. end
  118. if not widget:isTouchEnabled() then
  119. return false
  120. end
  121. if not widget:isSwallowTouches() then
  122. return false
  123. end
  124. local call_up_r = SLHandlerEvent:GetCallBack(widget, WND_EVENT_MOUSE_RB_UP)
  125. local call_up_l = SLHandlerEvent:GetCallBack(widget, WND_EVENT_MOUSE_LB_UP)
  126. local call_click_r = SLHandlerEvent:GetCallBack(widget, WND_EVENT_MOUSE_RB_CLICK)
  127. local call_double_r = SLHandlerEvent:GetCallBack(widget, WND_EVENT_MOUSE_RB_DBCLICK)
  128. local mousePosX = sender:getCursorX()
  129. local mousePosY = sender:getCursorY()
  130. local touchPos = {x = mousePosX, y = mousePosY}
  131. local globalM = global.mouseEventController
  132. local isInSide = global.mouseEventController:checkNodePos(widget, {x = mousePosX, y = mousePosY})
  133. local mButton = sender:getMouseButton()
  134. if mButton == cc.MouseButton.BUTTON_RIGHT then
  135. if isInSide then
  136. if call_up_r then
  137. SLHandlerEvent.swallowMouse = call_up_r(widget) or false
  138. end
  139. if call_double_r then
  140. if widget._onDoubleRClickEvent then
  141. if widget._clickDelayRHandler then
  142. UnSchedule(widget._clickDelayRHandler)
  143. widget._clickDelayRHandler = false
  144. end
  145. widget._onDoubleRClickEvent = false
  146. globalM:CancelMove(sender, false, false)
  147. SLHandlerEvent.swallowMouse = call_double_r({widget = widget, touchPos = touchPos}) or false
  148. else
  149. widget._onDoubleRClickEvent = true
  150. -- 记录单击触发
  151. widget._clickDelayRHandler =
  152. PerformWithDelayGlobal(
  153. function()
  154. if call_click_r then
  155. SLHandlerEvent.swallowMouse = call_click_r({widget = widget, touchPos = touchPos}) or false
  156. end
  157. widget._clickDelayRHandler = false
  158. widget._onDoubleRClickEvent = false
  159. end,
  160. global.MMO.CLICK_DOUBLE_TIME
  161. )
  162. end
  163. elseif call_click_r then
  164. call_click_r(widget)
  165. end
  166. sender:stopPropagation()
  167. end
  168. else
  169. if isInSide then
  170. if call_up_l then
  171. SLHandlerEvent.swallowMouse = call_up_l(widget) or false
  172. end
  173. sender:stopPropagation()
  174. end
  175. end
  176. end
  177. SLHandlerEvent.swallowMouse = false
  178. local listener = cc.EventListenerMouse:create()
  179. listener:registerScriptHandler(__OnMouseDown, cc.Handler.EVENT_MOUSE_DOWN)
  180. listener:registerScriptHandler(__OnMouseUp, cc.Handler.EVENT_MOUSE_UP)
  181. cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(listener, widget)
  182. end
  183. local function RegisterMouseMoveEvent(widget)
  184. local function __OnMouseMoved(sender)
  185. if not widget.__TYPE__ then
  186. return false
  187. end
  188. if not GetWidgetVisible(widget) then
  189. return false
  190. end
  191. local lastTimeNodeState = widget.___wState___
  192. local mousePosX = sender:getCursorX()
  193. local mousePosY = sender:getCursorY()
  194. local touchPos = {x = mousePosX, y = mousePosY}
  195. local isInSide = global.mouseEventController:checkNodePos(widget, touchPos)
  196. if lastTimeNodeState and isInSide then --一直在内部
  197. local insideCallback = SLHandlerEvent:GetCallBack(widget, WND_EVENT_MOUSE_MOVE)
  198. if insideCallback then
  199. insideCallback(widget)
  200. end
  201. widget.___wState___ = true
  202. sender:stopPropagation()
  203. elseif lastTimeNodeState and not isInSide then --移出
  204. local leaveCallback = SLHandlerEvent:GetCallBack(widget, WND_EVENT_MOUSE_OUT)
  205. if leaveCallback then
  206. leaveCallback(widget)
  207. end
  208. widget.___wState___ = false
  209. elseif not lastTimeNodeState and isInSide then --移入
  210. local enterCallback = SLHandlerEvent:GetCallBack(widget, WND_EVENT_MOUSE_IN)
  211. if enterCallback then
  212. enterCallback({widget = widget, touchPos = touchPos})
  213. end
  214. widget.___wState___ = true
  215. sender:stopPropagation()
  216. end
  217. end
  218. SLHandlerEvent.swallowMouse = false
  219. local listener = cc.EventListenerMouse:create()
  220. listener:registerScriptHandler(__OnMouseMoved, cc.Handler.EVENT_MOUSE_MOVE)
  221. cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(listener, widget)
  222. end
  223. local function RegisterMouseWheelEvent(widget)
  224. local function __OnMouseScroll(sender)
  225. if not widget.__TYPE__ then
  226. return false
  227. end
  228. if not GetWidgetVisible(widget) then
  229. return false
  230. end
  231. local call_scrolling = SLHandlerEvent:GetCallBack(widget, WND_EVENT_MOUSE_WHEEL)
  232. if not call_scrolling then
  233. return false
  234. end
  235. local mousePosX = sender:getCursorX()
  236. local mousePosY = sender:getCursorY()
  237. local isInSide = global.mouseEventController:checkNodePos(widget, {x = mousePosX, y = mousePosY})
  238. if not isInSide then
  239. return false
  240. end
  241. local scrollX = sender:getScrollX()
  242. local scrollY = sender:getScrollY()
  243. call_scrolling({widget = widget, x = scrollX, y = scrollY})
  244. end
  245. SLHandlerEvent.swallowMouse = false
  246. local listener = cc.EventListenerMouse:create()
  247. listener:registerScriptHandler(__OnMouseScroll, cc.Handler.EVENT_MOUSE_SCROLL)
  248. cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(listener, widget)
  249. end
  250. -- 添加窗体控件事件
  251. function SLHandlerEvent:RegisterWndEvent(widget, desc, key, callback)
  252. if not CheckObjValid(widget) then
  253. return false
  254. end
  255. SLHandlerEvent.Events = SLHandlerEvent.Events or {}
  256. SLHandlerEvent.Events[widget] = SLHandlerEvent.Events[widget] or {}
  257. SLHandlerEvent.Events[widget][desc] = SLHandlerEvent.Events[widget][desc] or {}
  258. SLHandlerEvent.Events[widget][desc][key] = callback
  259. SLHandlerEvent.EventDesc = SLHandlerEvent.EventDesc or {}
  260. SLHandlerEvent.EventDesc[widget] = desc
  261. widget.__TYPE__ = true
  262. if key == WND_EVENT_MOUSE_MOVE or key == WND_EVENT_MOUSE_IN or key == WND_EVENT_MOUSE_OUT then
  263. RegisterMouseMoveEvent(widget)
  264. elseif key == WND_EVENT_MOUSE_WHEEL then
  265. RegisterMouseWheelEvent(widget)
  266. elseif key > 0 and key < 9 then
  267. RegisterMouseClickEvent(widget)
  268. elseif key == WND_EVENT_WND_DESTROY then
  269. widget:registerScriptHandler(function(state)
  270. if state == "exit" then
  271. SLHandlerEvent:GetCallBack(widget, WND_EVENT_WND_DESTROY, true)
  272. end
  273. end )
  274. end
  275. end
  276. -- 删除窗体控件事件
  277. function SLHandlerEvent:UnRegisterWndEvent(widget, desc, key)
  278. if not CheckObjValid(widget) then
  279. return false
  280. end
  281. SLHandlerEvent.Events = SLHandlerEvent.Events or {}
  282. if desc and SLHandlerEvent.Events[widget] and SLHandlerEvent.Events[widget][desc] then
  283. if key then
  284. SLHandlerEvent.Events[widget][desc][key] = nil
  285. else
  286. SLHandlerEvent.Events[widget][desc] = nil
  287. end
  288. return false
  289. end
  290. SLHandlerEvent.Events[widget] = nil
  291. end
  292. -- 获取窗体控件事件
  293. function SLHandlerEvent:GetCallBack(widget, key, isExecute)
  294. if not CheckObjValid(widget) then
  295. return false
  296. end
  297. local desc = SLHandlerEvent.EventDesc[widget]
  298. if not desc then
  299. return false
  300. end
  301. SLHandlerEvent.Events = SLHandlerEvent.Events or {}
  302. if SLHandlerEvent.Events[widget] and SLHandlerEvent.Events[widget][desc] then
  303. if key then
  304. if SLHandlerEvent.Events[widget][desc][key] then
  305. return isExecute and SLHandlerEvent.Events[widget][desc][key](widget) or SLHandlerEvent.Events[widget][desc][key]
  306. end
  307. end
  308. end
  309. return false
  310. end
  311. -- 添加窗体控件自定义属性
  312. function SLHandlerEvent:AddWndProperty(widget, desc, key, value)
  313. if not CheckObjValid(widget) then
  314. return false
  315. end
  316. SLHandlerEvent.PropertyEvents = SLHandlerEvent.PropertyEvents or {}
  317. SLHandlerEvent.PropertyEvents[widget] = SLHandlerEvent.PropertyEvents[widget] or {}
  318. SLHandlerEvent.PropertyEvents[widget][desc] = SLHandlerEvent.PropertyEvents[widget][desc] or {}
  319. SLHandlerEvent.PropertyEvents[widget][desc][key] = value
  320. end
  321. -- 删除窗体控件自定义属性
  322. function SLHandlerEvent:DelWndProperty(widget, desc, key)
  323. if not CheckObjValid(widget) then
  324. return false
  325. end
  326. SLHandlerEvent.PropertyEvents = SLHandlerEvent.PropertyEvents or {}
  327. if desc and SLHandlerEvent.PropertyEvents[widget] and SLHandlerEvent.PropertyEvents[widget][desc] then
  328. if key then
  329. SLHandlerEvent.PropertyEvents[widget][desc][key] = nil
  330. else
  331. SLHandlerEvent.PropertyEvents[widget][desc] = nil
  332. end
  333. return false
  334. end
  335. SLHandlerEvent.PropertyEvents[widget] = nil
  336. end
  337. -- 获取窗体控件自定义属性
  338. function SLHandlerEvent:GetWndProperty(widget, desc, key)
  339. if not CheckObjValid(widget) then
  340. return nil
  341. end
  342. SLHandlerEvent.PropertyEvents = SLHandlerEvent.PropertyEvents or {}
  343. if SLHandlerEvent.PropertyEvents[widget] and SLHandlerEvent.PropertyEvents[widget][desc] then
  344. if key then
  345. return SLHandlerEvent.PropertyEvents[widget][desc][key]
  346. else
  347. return SLHandlerEvent.PropertyEvents[widget][desc]
  348. end
  349. end
  350. return nil
  351. end