UserInputController.lua 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. local UserInputController = class('UserInputController', framework.Mediator)
  2. UserInputController.NAME = "UserInputController"
  3. local sformat = string.format
  4. local DEBUG_TOUCH = false
  5. local ignoreShift = {
  6. cc.KeyCode.KEY_SHIFT,
  7. ""
  8. }
  9. local function compareKeyCode(k1, k2, checkFullSort)
  10. if checkFullSort then
  11. if #k1 == #k2 then
  12. for i = 1, #k1 do
  13. local keyCode = k1[i]
  14. local isEqual = false
  15. for j = 1, #k2 do
  16. if keyCode == k2[j] then
  17. isEqual = true
  18. break
  19. end
  20. end
  21. if not isEqual then
  22. return false
  23. end
  24. end
  25. return true
  26. else
  27. return false
  28. end
  29. end
  30. if (table.concat(k1, "+") == table.concat(k2, "+")) then
  31. return true
  32. end
  33. local isIgnoreShift = tonumber(SL:GetMetaValue("GAME_DATA", "is_ignore_shift")) == 1
  34. if isIgnoreShift and (table.concat(k1, "+") == table.concat(ignoreShift, "+") .. table.concat(k2, "+")) then
  35. return true
  36. end
  37. return false
  38. end
  39. function UserInputController:ctor()
  40. UserInputController.super.ctor(self, self.NAME)
  41. self._keyboardListener = nil
  42. self._keyboardEvents = {}
  43. self._keyboardAble = true
  44. self._isPressedShift = false
  45. self._isPressedCtrl = false
  46. self._isPressedAlt = false
  47. self._cursorPosition = {x=0,y=0}
  48. self._touchListener = nil
  49. self._touching = false
  50. end
  51. function UserInputController:destory()
  52. if UserInputController.instance then
  53. global.Facade:removeMediator(UserInputController.NAME)
  54. UserInputController.instance = nil
  55. end
  56. end
  57. function UserInputController:Inst()
  58. if not UserInputController.instance then
  59. UserInputController.instance = UserInputController.new()
  60. global.Facade:registerMediator(UserInputController.instance)
  61. end
  62. return UserInputController.instance
  63. end
  64. function UserInputController:onRegister()
  65. UserInputController.super.onRegister(self)
  66. end
  67. function UserInputController:listNotificationInterests()
  68. local noticeTable = global.NoticeTable
  69. return
  70. {
  71. noticeTable.Layer_Moved_Moving,
  72. noticeTable.PlayerBeDamaged,
  73. noticeTable.MainPlayerActionBegan,
  74. }
  75. end
  76. function UserInputController:handleNotification(notification)
  77. local noticeTable = global.NoticeTable
  78. local noticeID = notification:getName()
  79. local data = notification:getBody()
  80. if noticeID == noticeTable.Layer_Moved_Moving then
  81. self:OnMouseMoved(data)
  82. elseif noticeID == noticeTable.PlayerBeDamaged then
  83. self:OnPlayerBeDamaged()
  84. elseif noticeID == noticeTable.MainPlayerActionBegan then
  85. self:OnMainPlayerActionBegan(data)
  86. end
  87. end
  88. function UserInputController:InitOnEnterLogin()
  89. self:ResetKeyboard()
  90. self:InitKeyCodeLogin()
  91. end
  92. function UserInputController:InitOnEnterRole()
  93. self:ResetKeyboard()
  94. self:InitKeyCodeLogin()
  95. end
  96. function UserInputController:InitOnEnterWorld()
  97. self:ResetKeyboard()
  98. self:InitKeyCodeWorld()
  99. self:ResetTouch()
  100. self:InitTouch()
  101. end
  102. -------------------------------------------------------------------------------
  103. function UserInputController:isTouching()
  104. return self._touching
  105. end
  106. function UserInputController:setIsTouching(t)
  107. self._touching = t
  108. end
  109. function UserInputController:isKeyboardAble()
  110. return self._keyboardAble
  111. end
  112. function UserInputController:setKeyboardAble(able)
  113. self._keyboardAble = able
  114. end
  115. -------------------------------------------------------------------------------
  116. ----------------------------------- listener ----------------------------------
  117. function UserInputController:onEventUserInput()
  118. global.Facade:sendNotification(global.NoticeTable.UserInputEventNotice)
  119. end
  120. ----------------------------------- listener ----------------------------------
  121. ----------------------------------- keycode -----------------------------------
  122. function UserInputController:IsPressedShift()
  123. return self._isPressedShift
  124. end
  125. function UserInputController:IsPressedCtrl()
  126. return self._isPressedCtrl
  127. end
  128. function UserInputController:IsPressedAlt()
  129. return self._isPressedAlt
  130. end
  131. function UserInputController:ResetKeyboard()
  132. if not self._keyboardListener then
  133. return
  134. end
  135. local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
  136. eventDispatcher:removeEventListener(self._keyboardListener)
  137. self._keyboardEvents = {}
  138. self._keyboardListener = nil
  139. self._keyboardAble = true
  140. self._isPressedShift = false
  141. self._isPressedCtrl = false
  142. self._isPressedAlt = false
  143. end
  144. function UserInputController:InitKeyboardListener()
  145. local inputKeyCode = {}
  146. local function onEventPressedCB()
  147. if not self._keyboardAble then
  148. return
  149. end
  150. global.userInputController:onEventUserInput()
  151. local count = #self._keyboardEvents
  152. for i = count, 1, -1 do
  153. local v = self._keyboardEvents[i]
  154. if compareKeyCode(inputKeyCode, v.keyCode, v.checkFullSort) then
  155. if v.pressedCB then
  156. v.pressedCB()
  157. end
  158. -- auto event perssed callback
  159. if v.interval and v.interval > 0 then
  160. if nil == v.timerID then
  161. v.timerID = Schedule(function()
  162. global.userInputController:onEventUserInput()
  163. if v.pressedCB then
  164. v.pressedCB(true)
  165. end
  166. end, v.interval)
  167. end
  168. end
  169. break
  170. end
  171. end
  172. end
  173. local function onEventReleaseCB(keycode)
  174. global.userInputController:onEventUserInput()
  175. --
  176. for _, v in ipairs(self._keyboardEvents) do
  177. if v.timerID then
  178. UnSchedule(v.timerID)
  179. v.timerID = nil
  180. end
  181. end
  182. -- releaseCB
  183. local count = #self._keyboardEvents
  184. for i = count, 1, -1 do
  185. local v = self._keyboardEvents[i]
  186. if compareKeyCode({keycode}, v.keyCode) then
  187. if v.releaseCB then
  188. v.releaseCB()
  189. end
  190. break
  191. end
  192. end
  193. end
  194. local function pressed_callback(keycode, evt)
  195. table.insert(inputKeyCode, keycode)
  196. onEventPressedCB()
  197. if global.gameWorldController then
  198. global.gameWorldController:OnGameActive()
  199. end
  200. end
  201. local function released_callback(keycode, evt)
  202. for k, v in pairs(inputKeyCode) do
  203. if v == keycode then
  204. table.remove(inputKeyCode, k)
  205. end
  206. end
  207. onEventReleaseCB(keycode)
  208. if global.gameWorldController then
  209. global.gameWorldController:OnGameActive()
  210. end
  211. end
  212. self._keyboardListener = cc.EventListenerKeyboard:create()
  213. local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
  214. self._keyboardListener:registerScriptHandler(pressed_callback, cc.Handler.EVENT_KEYBOARD_PRESSED)
  215. self._keyboardListener:registerScriptHandler(released_callback, cc.Handler.EVENT_KEYBOARD_RELEASED)
  216. eventDispatcher:addEventListenerWithFixedPriority(self._keyboardListener, 1)
  217. end
  218. function UserInputController:addKeyboardListener(keyCode, pressedCB, releaseCB, interval, checkFullSort)
  219. interval = interval or 1
  220. if not keyCode then
  221. print("ERROR++++++++++++++++++++++++, keyCode is empty")
  222. return
  223. end
  224. if type(keyCode) == "number" then
  225. local t = {}
  226. table.insert(t, keyCode)
  227. keyCode = t
  228. end
  229. local listener = {}
  230. listener.keyCode = keyCode
  231. listener.pressedCB = pressedCB
  232. listener.releaseCB = releaseCB
  233. listener.interval = interval
  234. listener.checkFullSort = checkFullSort
  235. table.insert(self._keyboardEvents, listener)
  236. end
  237. function UserInputController:rmvKeyboardListener(keyCode)
  238. if type(keyCode) == "number" then
  239. local t = {}
  240. table.insert(t, keyCode)
  241. keyCode = t
  242. end
  243. local count = #self._keyboardEvents
  244. for i = count, 1, -1 do
  245. local v = self._keyboardEvents[i]
  246. if compareKeyCode(v.keyCode, keyCode) then
  247. table.remove(self._keyboardEvents, i)
  248. return true
  249. end
  250. end
  251. return false
  252. end
  253. function UserInputController:updateKeyboardListener( keyCode, pressedCB, releaseCB, interval )
  254. if not keyCode then
  255. print("ERROR++++++++++++++++++++++++, keyCode is empty")
  256. return
  257. end
  258. if type(keyCode) == "number" then
  259. local t = {}
  260. table.insert(t, keyCode)
  261. keyCode = t
  262. end
  263. for k, v in pairs(self._keyboardEvents) do
  264. if compareKeyCode(v.keyCode, keyCode, v.checkFullSort) then
  265. if pressedCB then v.pressedCB = pressedCB end
  266. if releaseCB then v.releaseCB = releaseCB end
  267. if interval then v.interval = interval end
  268. return true
  269. end
  270. end
  271. return false
  272. end
  273. function UserInputController:InitKeyCodeLogin()
  274. self:InitKeyboardListener()
  275. -- ENTER 确认当前界面
  276. local pressedCB = function()
  277. global.Facade:sendNotification(global.NoticeTable.Layer_Enter_Current)
  278. end
  279. local keyCodes = cc.KeyCode.KEY_ENTER
  280. global.userInputController:addKeyboardListener(keyCodes, pressedCB, nil, 0)
  281. local keyCodes = cc.KeyCode.KEY_KP_ENTER
  282. global.userInputController:addKeyboardListener(keyCodes, pressedCB, nil, 0)
  283. end
  284. function UserInputController:InitKeyCodeWorld()
  285. self:InitKeyboardListener()
  286. if not global.isWinPlayMode then
  287. return nil
  288. end
  289. ---------------------------------------------------------------------------
  290. -- F1-F8技能
  291. for i = 1, 8 do
  292. local callback = function(isAuto)
  293. local inputProxy = global.Facade:retrieveProxy(global.ProxyTable.PlayerInputProxy)
  294. local SkillProxy = global.Facade:retrieveProxy(global.ProxyTable.Skill)
  295. local skill = SkillProxy:GetSkillByKey(i)
  296. if not skill then
  297. return false
  298. end
  299. local skillID = skill.MagicID
  300. -- 开关技能
  301. if SkillProxy:IsOnoffSkill(skillID) then
  302. if isAuto then
  303. return
  304. end
  305. SkillProxy:RequestSkillOnoff(skillID)
  306. return
  307. end
  308. local destPos = inputProxy:getCursorMapPosition()
  309. global.Facade:sendNotification(global.NoticeTable.UserInputLaunch, {skillID = skillID, destPos = destPos})
  310. end
  311. local releaseCB = function()
  312. local autoProxy = global.Facade:retrieveProxy( global.ProxyTable.Auto )
  313. autoProxy:ClearLaunchFirstSkill()
  314. end
  315. local keyCodes = cc.KeyCode[string.format("KEY_F%s", i)]
  316. local tag = nil
  317. self:addKeyboardListener(keyCodes, callback,releaseCB,0.001)
  318. end
  319. -- CTRL+F1 - CTRL+F8技能
  320. for i = 1, 8 do
  321. local callback = function(isAuto)
  322. local inptuProxy = global.Facade:retrieveProxy(global.ProxyTable.PlayerInputProxy)
  323. local SkillProxy = global.Facade:retrieveProxy(global.ProxyTable.Skill)
  324. local skill = SkillProxy:GetSkillByKey(i+8)
  325. if not skill then
  326. return false
  327. end
  328. local skillID = skill.MagicID
  329. -- 开关技能
  330. if SkillProxy:IsOnoffSkill(skillID) then
  331. if isAuto then
  332. return
  333. end
  334. SkillProxy:RequestSkillOnoff(skillID)
  335. return
  336. end
  337. -- 技能释放
  338. local destPos = inptuProxy:getCursorMapPosition()
  339. global.Facade:sendNotification(global.NoticeTable.UserInputLaunch, {skillID = skillID, destPos = destPos})
  340. end
  341. local releaseCB = function()
  342. local autoProxy = global.Facade:retrieveProxy( global.ProxyTable.Auto )
  343. autoProxy:ClearLaunchFirstSkill()
  344. end
  345. local keyCodes = {cc.KeyCode.KEY_CTRL, cc.KeyCode[string.format("KEY_F%s", i)]}
  346. local tag = nil
  347. self:addKeyboardListener(keyCodes, callback, releaseCB, 0.001)
  348. end
  349. -- 1-6使用物品
  350. local SkillProxy = global.Facade:retrieveProxy(global.ProxyTable.Skill)
  351. for i = 1, 6 do
  352. local interval = 1
  353. local scheduleID = nil
  354. local function useItemCB()
  355. local QuickUseProxy = global.Facade:retrieveProxy(global.ProxyTable.QuickUseProxy)
  356. local quickUseData = QuickUseProxy:GetQucikUseDataByPos(i)
  357. if quickUseData then
  358. local ItemUseProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemUseProxy)
  359. ItemUseProxy:UseItem(quickUseData)
  360. end
  361. end
  362. local function pressedCB()
  363. local cuiEditorMediator = global.Facade:retrieveMediator("CUIEditorMediator")
  364. if cuiEditorMediator and cuiEditorMediator._layer then
  365. return
  366. end
  367. useItemCB()
  368. if scheduleID then
  369. UnSchedule(scheduleID)
  370. scheduleID = nil
  371. end
  372. scheduleID = Schedule(useItemCB, interval)
  373. end
  374. local function releaseCB()
  375. if scheduleID then
  376. UnSchedule(scheduleID)
  377. scheduleID = nil
  378. end
  379. end
  380. self:addKeyboardListener(cc.KeyCode[string.format("KEY_%s", i)], pressedCB, releaseCB)
  381. end
  382. -- shift+左键持续攻击
  383. local pressedCB = function()
  384. self._isPressedShift = true
  385. global.Facade:sendNotification(global.NoticeTable.UserPressedShift)
  386. end
  387. local releaseCB = function()
  388. self._isPressedShift = false
  389. global.Facade:sendNotification(global.NoticeTable.UserReleaseShift)
  390. end
  391. local keyCodes = cc.KeyCode.KEY_LEFT_SHIFT
  392. self:addKeyboardListener(keyCodes, pressedCB, releaseCB, -1)
  393. -- ctrl
  394. local pressedCB = function()
  395. self._isPressedCtrl = true
  396. end
  397. local releaseCB = function()
  398. self._isPressedCtrl = false
  399. end
  400. local keyCodes = cc.KeyCode.KEY_CTRL
  401. self:addKeyboardListener(keyCodes, pressedCB, releaseCB, -1)
  402. -- F9包裹
  403. local pressedCB = function()
  404. JUMPTO(7)
  405. end
  406. local keyCodes = cc.KeyCode.KEY_F9
  407. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  408. -- F10装备
  409. local pressedCB = function()
  410. SL:OpenMyPlayerUI({extent = 1, isFast = true})
  411. end
  412. local keyCodes = cc.KeyCode.KEY_F10
  413. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  414. -- F11技能
  415. local pressedCB = function()
  416. SL:OpenMyPlayerUI({extent = 4, isFast = true})
  417. end
  418. local keyCodes = cc.KeyCode.KEY_F11
  419. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  420. -- T交易
  421. local pressedCB = function()
  422. if (CHECK_SERVER_OPTION(global.MMO.SERVER_OPTION_TRADE_DEAL) == true) then
  423. local TradeProxy = global.Facade:retrieveProxy(global.ProxyTable.TradeProxy)
  424. TradeProxy:SendTradeRequest()
  425. end
  426. end
  427. local keyCodes = cc.KeyCode.KEY_T
  428. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  429. -- G行会
  430. local pressedCB = function()
  431. JUMPTO(31)
  432. end
  433. local keyCodes = cc.KeyCode.KEY_G
  434. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  435. -- ALT-X返回角色
  436. local pressedCB = function()
  437. local function callback(bType, custom)
  438. if bType == 1 then
  439. global.Facade:sendNotification(global.NoticeTable.RequestLeaveWorld)
  440. end
  441. end
  442. local data = {}
  443. data.str = GET_STRING(30003056)
  444. data.btnDesc = {GET_STRING(1001), GET_STRING(1000)}
  445. data.callback = callback
  446. global.Facade:sendNotification(global.NoticeTable.Layer_CommonTips_Open, data)
  447. end
  448. local keyCodes = {cc.KeyCode.KEY_ALT, cc.KeyCode.KEY_X}
  449. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  450. -- ALT-Q退出游戏
  451. local pressedCB = function()
  452. local function callback(bType, custom)
  453. if bType == 1 then
  454. global.userInputController:RequestToOutGame()
  455. end
  456. end
  457. local data = {}
  458. data.str = GET_STRING(30003036)
  459. data.btnDesc = {GET_STRING(1001), GET_STRING(1000)}
  460. data.callback = callback
  461. global.Facade:sendNotification(global.NoticeTable.Layer_CommonTips_Open, data)
  462. end
  463. local keyCodes = {cc.KeyCode.KEY_ALT, cc.KeyCode.KEY_Q}
  464. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  465. -- CTRL_H 切换攻击模式
  466. local pressedCB = function()
  467. local pkModeTB = {0, 1, 4, 5, 6, 3, 2, 7, 8}
  468. -- 区服模式只能在跨服时可切换
  469. if SL:GetMetaValue("KFSTATE") then
  470. table.insert(pkModeTB, 10)
  471. end
  472. local PlayerPropertyProxy = global.Facade:retrieveProxy(global.ProxyTable.PlayerProperty)
  473. local canMode = {}
  474. for _, v in ipairs(pkModeTB) do
  475. if PlayerPropertyProxy:IsShowCurMode(v) then
  476. table.insert( canMode, v)
  477. end
  478. end
  479. local function getPKModeIndex(pkm)
  480. for k, v in pairs(canMode) do
  481. if pkm == v then
  482. return k
  483. end
  484. end
  485. return 1
  486. end
  487. local PlayerProperty = global.Facade:retrieveProxy(global.ProxyTable.PlayerProperty)
  488. local pkMode = PlayerProperty:GetPKMode()
  489. local index = getPKModeIndex(pkMode)
  490. local nextPKMode = canMode[(index >= #canMode and 1 or index+1)]
  491. PlayerProperty:RequestChangePKMode(nextPKMode)
  492. end
  493. local keyCodes = {cc.KeyCode.KEY_LEFT_CTRL, cc.KeyCode.KEY_H}
  494. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  495. -- CTRL+A 召唤物休息或者攻击
  496. local pressedCB = function()
  497. local SummonsProxy = global.Facade:retrieveProxy(global.ProxyTable.SummonsProxy)
  498. if not SummonsProxy:IsAlived() then
  499. return false
  500. end
  501. local SummonsProxy = global.Facade:retrieveProxy(global.ProxyTable.SummonsProxy)
  502. local currMode = SummonsProxy:GetMode()
  503. local nextMode = currMode == 4 and 2 or 4
  504. SummonsProxy:RequestModeChange(nextMode)
  505. end
  506. local keyCodes = {cc.KeyCode.KEY_LEFT_CTRL, cc.KeyCode.KEY_A}
  507. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  508. -- F12内挂
  509. local pressedCB = function()
  510. global.Facade:sendNotification(global.NoticeTable.Layer_SettingFrame_Open)
  511. end
  512. local keyCodes = cc.KeyCode.KEY_F12
  513. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  514. -- 波浪键 ` ~ 拾取当前位置道具
  515. local pressedCB = function()
  516. global.dropItemController:PickMainPlayerPosItem( )
  517. end
  518. local keyCodes = cc.KeyCode.KEY_GRAVE
  519. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  520. -- ALT
  521. local pressedCB = function()
  522. self._isPressedAlt = true
  523. end
  524. local releaseCB = function()
  525. self._isPressedAlt = false
  526. end
  527. local keyCodes = cc.KeyCode.KEY_ALT
  528. global.userInputController:addKeyboardListener(keyCodes, pressedCB, releaseCB)
  529. -- 拍卖
  530. local pressedCB = function()
  531. if tonumber(SL:GetMetaValue("GAME_DATA","OpenAuctionByP")) == 1 then
  532. return
  533. end
  534. JUMPTO(27)
  535. end
  536. local keyCodes = cc.KeyCode.KEY_P
  537. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  538. -- ESC 关闭当前界面
  539. local pressedCB = function()
  540. local cuiEditorMediator = global.Facade:retrieveMediator("CUIEditorMediator")
  541. if cuiEditorMediator and cuiEditorMediator._layer then
  542. global.Facade:sendNotification(global.NoticeTable.CUIEditorClose)
  543. end
  544. local SUIComponentMediator = global.Facade:retrieveMediator("SUIComponentMediator")
  545. local isClosed = SUIComponentMediator:CloseSUIAddWin()
  546. local isClosedEx = false
  547. if not isClosed then
  548. if ssrGlobal_ESCCloseWinEvent then
  549. isClosedEx = ssrGlobal_ESCCloseWinEvent()
  550. end
  551. end
  552. if not isClosed and not isClosedEx then
  553. global.Facade:sendNotification(global.NoticeTable.Layer_Close_Current)
  554. end
  555. end
  556. local keyCodes = cc.KeyCode.KEY_ESCAPE
  557. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  558. local HeroPropertyProxy = global.Facade:retrieveProxy(global.ProxyTable.HeroPropertyProxy)
  559. --英雄切换攻击模式
  560. local keyCodes = {cc.KeyCode.KEY_CTRL,cc.KeyCode.KEY_E}
  561. local pressedCB = function()
  562. if HeroPropertyProxy:HeroIsLogin() then
  563. HeroPropertyProxy:RequestChangeHeroMode()
  564. end
  565. end
  566. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  567. --切换锁定目标ctrl+w
  568. local keyCodes = {cc.KeyCode.KEY_CTRL,cc.KeyCode.KEY_W}
  569. local pressedCB = function()
  570. GUIFunction:OnSwitchLockTarget_Hero()
  571. end
  572. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  573. --守护位置ctrl+q
  574. local keyCodes = {cc.KeyCode.KEY_CTRL,cc.KeyCode.KEY_Q}
  575. local pressedCB = function()
  576. if HeroPropertyProxy:HeroIsLogin() then
  577. local touchPos = self:GetCursorPos()
  578. local touchPosInWorld = Screen2World(touchPos)
  579. local X,Y = global.sceneManager:WorldPos2MapPos(touchPosInWorld)
  580. HeroPropertyProxy:RequestTargetXY(X,Y)
  581. end
  582. end
  583. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  584. --宠物锁定目标ctrl+r
  585. local keyCodes = {cc.KeyCode.KEY_CTRL,cc.KeyCode.KEY_R}
  586. local pressedCB = function()
  587. local SummonsProxy = global.Facade:retrieveProxy(global.ProxyTable.SummonsProxy)
  588. if not SummonsProxy:IsAlived() then
  589. return
  590. end
  591. local touchPos = self:GetCursorPos()
  592. local touchPosInWorld = Screen2World(touchPos)
  593. local actor = global.actorManager:Pick(touchPosInWorld)
  594. local proxyUtils = requireProxy( "proxyUtils" )
  595. if actor and proxyUtils.checkEnemyTag(actor) == 1 then
  596. local lockID = SummonsProxy:GetLockTargetID()
  597. if lockID == actor:GetID() then --已锁定
  598. SummonsProxy:ReqUnLockActorByID(actor:GetID())
  599. else
  600. SummonsProxy:ReqLockActorByID(actor:GetID())
  601. end
  602. end
  603. end
  604. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  605. --开启合击ctrl+s
  606. local keyCodes = {cc.KeyCode.KEY_CTRL,cc.KeyCode.KEY_S}
  607. local pressedCB = function()
  608. if HeroPropertyProxy:HeroIsLogin() then
  609. if HeroPropertyProxy:getShan() then--在闪的时候才能放合击
  610. HeroPropertyProxy:ReqJointAttack()--请求合击
  611. end
  612. end
  613. end
  614. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  615. --骑马邀请快捷键
  616. local keyCodes = {cc.KeyCode.KEY_CTRL,cc.KeyCode.KEY_L}
  617. local pressedCB = function()
  618. local mainActor = global.gamePlayerController:GetMainPlayer()
  619. if not mainActor then --主玩家不在
  620. return
  621. end
  622. if not mainActor:IsDoubleHorse() then --主玩家不是双人坐骑
  623. ShowSystemTips(GET_STRING(310000801))
  624. return
  625. end
  626. if mainActor:GetHorseCopilotID() then --主玩家的双人坐骑已满
  627. ShowSystemTips(GET_STRING(310000802))
  628. return
  629. end
  630. local inputProxy = global.Facade:retrieveProxy(global.ProxyTable.PlayerInputProxy)
  631. local targetID = inputProxy:GetTargetID()
  632. if not targetID then --没有选中者
  633. return
  634. end
  635. local targetActor = global.actorManager:GetActor(targetID)
  636. if not targetActor then --选中者没找到
  637. ShowSystemTips(GET_STRING(310000803))
  638. return
  639. end
  640. if not targetActor:IsPlayer() then --选择者不是角色玩家
  641. ShowSystemTips(GET_STRING(310000805))
  642. return
  643. end
  644. local mainPos = cc.p( mainActor:GetMapX(), mainActor:GetMapY() )
  645. local targetPos = cc.p( targetActor:GetMapX(), targetActor:GetMapY() )
  646. if cc.pGetDistance( mainPos,targetPos ) > 3 then
  647. ShowSystemTips(GET_STRING(310000804))
  648. return
  649. end
  650. local HorseProxy = global.Facade:retrieveProxy( global.ProxyTable.HorseProxy )
  651. HorseProxy:RequestHorseUpInvite( targetID )
  652. end
  653. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  654. --截图
  655. local keyCodes = {cc.KeyCode.KEY_PAUSE}
  656. local pressedCB = function()
  657. local FileUtils = cc.FileUtils:getInstance()
  658. local WritablePath = FileUtils:getWritablePath()
  659. local saveDir = WritablePath.."Images"
  660. if not FileUtils:isDirectoryExist(saveDir) then
  661. FileUtils:createDirectory(saveDir)
  662. end
  663. local prepath = saveDir.."/capture"
  664. local begin = 1
  665. while(1) do
  666. local filename = string.format("%s%s.png",prepath,begin)
  667. if FileUtils:isFileExist(filename) then
  668. begin = begin + 1
  669. else
  670. cc.utils:captureScreen(function(isOK,path)
  671. if isOK then
  672. ShowSystemTips(string.format(GET_STRING(600000315),path))
  673. end
  674. end, filename)
  675. break
  676. end
  677. end
  678. end
  679. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  680. --商店ctrl+B
  681. local keyCodes = {cc.KeyCode.KEY_CTRL,cc.KeyCode.KEY_B}
  682. local pressedCB = function()
  683. JUMPTO(9)
  684. end
  685. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  686. --快速组队alt+W
  687. local keyCodes = {cc.KeyCode.KEY_ALT,cc.KeyCode.KEY_W}
  688. local pressedCB = function()
  689. local touchPos = self:GetCursorPos()
  690. local touchPosInWorld = Screen2World(touchPos)
  691. local actor = global.actorManager:Pick(touchPosInWorld)
  692. if actor then
  693. local targetID = actor:GetID()
  694. local TeamProxy = global.Facade:retrieveProxy(global.ProxyTable.TeamProxy)
  695. if TeamProxy:IsTeamMember(targetID) then
  696. return
  697. else
  698. TeamProxy:RequestInvite(targetID)
  699. end
  700. end
  701. end
  702. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  703. --自动挂机
  704. local keyCodes = {cc.KeyCode.KEY_CTRL,cc.KeyCode.KEY_ALT, cc.KeyCode.KEY_X}
  705. local pressedCB = function()
  706. local disableKeys = SL:GetMetaValue("GAME_DATA","disableKeys")
  707. if disableKeys and disableKeys ~= "" then
  708. local Keys = string.split(disableKeys,"#")
  709. local key = tonumber(Keys[1]) or 0
  710. if key == 1 then
  711. return
  712. end
  713. end
  714. local AutoProxy = global.Facade:retrieveProxy(global.ProxyTable.Auto)
  715. if AutoProxy:IsAFKState() then
  716. LuaSendMsg(global.MsgType.MSG_CS_AUTOPLAYGAME_REQUEST,2)
  717. global.Facade:sendNotification(global.NoticeTable.AFKEnd)
  718. else
  719. LuaSendMsg(global.MsgType.MSG_CS_AUTOPLAYGAME_REQUEST,1)
  720. global.Facade:sendNotification(global.NoticeTable.AFKBegin)
  721. end
  722. end
  723. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  724. -- CTRL+D 连击技能
  725. local keyCodes = {cc.KeyCode.KEY_CTRL, cc.KeyCode.KEY_D}
  726. local pressedCB = function(isAuto)
  727. local selectSkills = SL:GetMetaValue("SET_COMBO_SKILLS")
  728. if not selectSkills or not selectSkills[1] then
  729. return false
  730. end
  731. local skillID = selectSkills[1]
  732. local num = #selectSkills
  733. local skillProxy = global.Facade:retrieveProxy(global.ProxyTable.Skill)
  734. if skillID then
  735. for i = 1, num do
  736. local id = selectSkills[i]
  737. if id and id ~= 0 then
  738. if skillProxy:IsInCD(id) then
  739. return
  740. end
  741. end
  742. end
  743. end
  744. -- 开关技能
  745. if SkillProxy:IsOnoffSkill(skillID) then
  746. if isAuto then
  747. return
  748. end
  749. SkillProxy:RequestSkillOnoff(skillID)
  750. return
  751. end
  752. -- 技能释放
  753. local inptuProxy = global.Facade:retrieveProxy(global.ProxyTable.PlayerInputProxy)
  754. local destPos = inptuProxy:getCursorMapPosition()
  755. global.Facade:sendNotification(global.NoticeTable.UserInputLaunch, {skillID = skillID, destPos = destPos})
  756. end
  757. local releaseCB = function()
  758. local autoProxy = global.Facade:retrieveProxy( global.ProxyTable.Auto )
  759. autoProxy:ClearLaunchFirstSkill()
  760. end
  761. global.userInputController:addKeyboardListener(keyCodes, pressedCB, releaseCB, 0.1)
  762. -- ENTER 确认当前界面
  763. local pressedCB = function()
  764. global.Facade:sendNotification(global.NoticeTable.Layer_Enter_Current)
  765. end
  766. local keyCodes = cc.KeyCode.KEY_ENTER
  767. global.userInputController:addKeyboardListener(keyCodes, pressedCB, nil, 0)
  768. local keyCodes = cc.KeyCode.KEY_KP_ENTER
  769. global.userInputController:addKeyboardListener(keyCodes, pressedCB, nil, 0)
  770. -- 上下键滚动聊天
  771. local function scrollChatFunc(isUp, isPage)
  772. local MainPropertyMediator = global.Facade:retrieveMediator("MainPropertyMediator")
  773. local layer = MainPropertyMediator and MainPropertyMediator._layer
  774. local list = layer._quickUI.ListView_chat
  775. if not list then return end
  776. local innerSize = list:getInnerContainerSize()
  777. local contentSize = list:getContentSize()
  778. local innerPos = list:getInnerContainerPosition()
  779. if innerSize.height - contentSize.height <= 0 then
  780. return
  781. end
  782. local pageHei = contentSize.height
  783. local scrollY = isPage and pageHei or 14
  784. local mHei = innerSize.height - contentSize.height
  785. local percent = (mHei + innerPos.y + (isUp and -scrollY or scrollY )) / mHei * 100
  786. percent = math.min(math.max(0, percent), 100)
  787. list:scrollToPercentVertical(percent, 0.03, false)
  788. end
  789. local keyCodes = cc.KeyCode.KEY_UP_ARROW
  790. local pressedCB = function()
  791. scrollChatFunc(true)
  792. end
  793. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  794. local keyCodes = cc.KeyCode.KEY_DOWN_ARROW
  795. local pressedCB = function()
  796. scrollChatFunc(false)
  797. end
  798. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  799. --Page UP/DOWN 聊天翻页
  800. local keyCodes = cc.KeyCode.KEY_PG_UP
  801. local pressedCB = function()
  802. scrollChatFunc(true, true)
  803. end
  804. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  805. local keyCodes = cc.KeyCode.KEY_PG_DOWN
  806. local pressedCB = function()
  807. scrollChatFunc(false, true)
  808. end
  809. global.userInputController:addKeyboardListener(keyCodes, pressedCB)
  810. end
  811. ----------------------------------- keycode -----------------------------------
  812. ----------------------------------- touch -----------------------------------
  813. function UserInputController:ResetTouch()
  814. if not self._touchListener then
  815. return nil
  816. end
  817. local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
  818. eventDispatcher:removeEventListener(self._touchListener)
  819. self._cursorPosition = {x=0,y=0}
  820. self._touchListener = nil
  821. self._touching = false
  822. end
  823. function UserInputController:InitTouch()
  824. local labelDebug = nil
  825. if DEBUG_TOUCH then
  826. --********* Debug
  827. local visibleSize = global.Director:getVisibleSize()
  828. local origin = global.Director:getVisibleOrigin()
  829. local label = cc.Label:createWithTTF("", "fonts/font2.ttf", 24)
  830. labelDebug = label
  831. -- position the label on the center of the screen
  832. label:enableOutline(cc.Color4B.BLACK, 1)
  833. label:setAnchorPoint(cc.p(0.5, 1.0))
  834. label:setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height)
  835. global.sceneGraphCtl:GetUiNormal():addChild(label, 9991)
  836. end
  837. local function updateDebugInfoStr(position)
  838. if (labelDebug) then
  839. local touchPos = Screen2World(position)
  840. if not touchPos then
  841. return nil
  842. end
  843. -- [Debug] grid pos
  844. local mapX, mapY = global.sceneManager:WorldPos2MapPos(touchPos)
  845. touchPos = global.sceneManager:MapPos2WorldPos(mapX, mapY, true)
  846. local debugInfo = string.format("[Mx%d,My:%d][tx:%d,ty:%d]", mapX, mapY, touchPos.x, touchPos.y)
  847. labelDebug:setString(debugInfo)
  848. end
  849. end
  850. --********* Input ( touch )
  851. local function began(touch, event)
  852. if not global.gamePlayerController:GetMainPlayer() then
  853. return false
  854. end
  855. local touchPos = touch:getLocation()
  856. local touchWay = global.MMO.MOVE_EVENT_TOUCH_AND_MOUSE_L
  857. local eventType = cc.Handler.EVENT_TOUCH_BEGAN
  858. global.gamePlayerController:HandleTouchEndEvent(touchPos, touchWay, eventType)
  859. local data = {
  860. way = global.MMO.MOVE_EVENT_TOUCH_AND_MOUSE_L,
  861. pos = touch:getLocation()
  862. }
  863. global.Facade:sendNotification(global.NoticeTable.keepMovingBegin, data)
  864. return true
  865. end
  866. local function moved(touch, event)
  867. if (DEBUG_TOUCH) then
  868. updateDebugInfoStr(touch:getLocation())
  869. end
  870. if not global.gamePlayerController:GetMainPlayer() then
  871. return
  872. end
  873. local data = {
  874. way = global.MMO.MOVE_EVENT_TOUCH_AND_MOUSE_L,
  875. pos = touch:getLocation()
  876. }
  877. global.Facade:sendNotification(global.NoticeTable.keepMovingUpdate, data)
  878. end
  879. local function ended(touch, event)
  880. if (DEBUG_TOUCH) then
  881. updateDebugInfoStr(touch:getLocation())
  882. end
  883. if not global.gamePlayerController:GetMainPlayer() then
  884. return
  885. end
  886. global.Facade:sendNotification(global.NoticeTable.keepMovingEnded)
  887. end
  888. local function cancelled(touch, event)
  889. if (DEBUG_TOUCH) then
  890. updateDebugInfoStr(touch:getLocation())
  891. end
  892. if not global.gamePlayerController:GetMainPlayer() then
  893. return
  894. end
  895. global.Facade:sendNotification(global.NoticeTable.keepMovingEnded)
  896. end
  897. local dispatcher = cc.Director:getInstance():getEventDispatcher()
  898. self._touchListener = cc.EventListenerTouchOneByOne:create()
  899. self._touchListener:registerScriptHandler(began, cc.Handler.EVENT_TOUCH_BEGAN)
  900. self._touchListener:registerScriptHandler(moved, cc.Handler.EVENT_TOUCH_MOVED)
  901. self._touchListener:registerScriptHandler(ended, cc.Handler.EVENT_TOUCH_ENDED)
  902. self._touchListener:registerScriptHandler(cancelled, cc.Handler.EVENT_TOUCH_CANCELLED)
  903. dispatcher:addEventListenerWithSceneGraphPriority(self._touchListener, global.gameWorldController)
  904. end
  905. ----------------------------------- touch -----------------------------------
  906. ----------------------------------- mouse event -------------------------------
  907. function UserInputController:OnMouseMoved(data)
  908. self._cursorPosition = data.pos
  909. end
  910. function UserInputController:GetCursorPos()
  911. return self._cursorPosition
  912. end
  913. ----------------------------------- mouse event -------------------------------
  914. -------------------------------------------------------------------------------
  915. function UserInputController:RequestToOutGame( ... )
  916. local function quit( ... )
  917. if self._timerID then
  918. UnSchedule( self._timerID )
  919. self._timerID = nil
  920. global.Facade:sendNotification(global.NoticeTable.Main_Remove_QuitTimeTips)
  921. end
  922. local glview = global.Director:getOpenGLView()
  923. glview:endToLua()
  924. end
  925. if self._timerID then
  926. UnSchedule( self._timerID )
  927. self._timerID = nil
  928. end
  929. if self._leaveID then
  930. UnSchedule( self._leaveID )
  931. self._leaveID = nil
  932. end
  933. if CHECK_SERVER_OPTION("DelayBQuit") then
  934. local delayTime = CHECK_SERVER_OPTION("nBQuitTime")
  935. if delayTime and tonumber(delayTime) then
  936. global.Facade:sendNotification(global.NoticeTable.Main_Add_QuitTimeTips, {time = tonumber(delayTime), type = 2})
  937. self._timerID = Schedule( quit, tonumber(delayTime))
  938. else
  939. quit()
  940. end
  941. else
  942. quit()
  943. end
  944. end
  945. function UserInputController:RequestLeaveWorld( ... )
  946. local function leave( ... )
  947. if self._leaveID then
  948. UnSchedule( self._leaveID )
  949. self._leaveID = nil
  950. global.Facade:sendNotification(global.NoticeTable.Main_Remove_QuitTimeTips)
  951. end
  952. local LoginProxy = global.Facade:retrieveProxy(global.ProxyTable.Login)
  953. LoginProxy:RequestSoftClose()
  954. end
  955. if self._leaveID then
  956. UnSchedule( self._leaveID )
  957. self._leaveID = nil
  958. end
  959. if self._timerID then
  960. UnSchedule( self._timerID )
  961. self._timerID = nil
  962. end
  963. if CHECK_SERVER_OPTION("boDelaySQuit") then
  964. local delayTime = CHECK_SERVER_OPTION("nSQuitTime")
  965. if delayTime and tonumber(delayTime) then
  966. global.Facade:sendNotification(global.NoticeTable.Main_Add_QuitTimeTips, {time = tonumber(delayTime), type = 1})
  967. self._leaveID = Schedule( leave, tonumber(delayTime))
  968. else
  969. leave()
  970. end
  971. else
  972. leave()
  973. end
  974. end
  975. function UserInputController:OnPlayerBeDamaged( ... )
  976. if self._timerID and CHECK_SERVER_OPTION("boBQuitStruckBreak") then
  977. UnSchedule( self._timerID )
  978. self._timerID = nil
  979. global.Facade:sendNotification(global.NoticeTable.Main_Remove_QuitTimeTips)
  980. end
  981. if self._leaveID and CHECK_SERVER_OPTION("boSQuitStruckBreak") then
  982. UnSchedule( self._leaveID )
  983. self._leaveID = nil
  984. global.Facade:sendNotification(global.NoticeTable.Main_Remove_QuitTimeTips)
  985. end
  986. end
  987. function UserInputController:OnMainPlayerActionBegan(act)
  988. if not IsMoveAction(act) then
  989. return
  990. end
  991. if self._timerID and CHECK_SERVER_OPTION("boBQuitMoveBreak") then
  992. UnSchedule( self._timerID )
  993. self._timerID = nil
  994. global.Facade:sendNotification(global.NoticeTable.Main_Remove_QuitTimeTips)
  995. end
  996. if self._leaveID and CHECK_SERVER_OPTION("boSQuitMoveBreak") then
  997. UnSchedule( self._leaveID )
  998. self._leaveID = nil
  999. global.Facade:sendNotification(global.NoticeTable.Main_Remove_QuitTimeTips)
  1000. end
  1001. end
  1002. return UserInputController