MainGamePadLayout.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. local MainGamePadLayout = class("MainGamePadLayout", function()
  2. return cc.Layer:create()
  3. end)
  4. local enableSizeWalk = cc.size(150, 160) -- 双摇杆 走
  5. local enableSizeRun = cc.size(370, 370) -- 双摇杆 跑
  6. local enableSizeAll = cc.size(290, 335) -- 单摇杆 走 跑
  7. local originPosWalk = cc.p(100, 105) -- 双摇杆 走
  8. local originPosRun = cc.p(200, 205) -- 双摇杆 跑
  9. local originPosAll = cc.p(150, 155) -- 双摇杆 跑
  10. function MainGamePadLayout:ctor()
  11. self._path = global.MMO.PATH_RES_PRIVATE .. "main/Rocker/"
  12. self._angle = 0xff -- joystick 方向(度数)
  13. self._joystickType = 1 -- 1走摇杆 2跑摇杆
  14. self._curTouchId = nil
  15. end
  16. function MainGamePadLayout.create(...)
  17. local layout = MainGamePadLayout.new()
  18. if layout:Init(...) then
  19. return layout
  20. end
  21. return nil
  22. end
  23. function MainGamePadLayout:Init(pType)
  24. self._joystickType = pType
  25. self._enableSize = (self._joystickType == 1 and enableSizeWalk or enableSizeRun) -- 触摸有效区
  26. -- 背景
  27. local path = (self._joystickType == 1 and self._path .. "1900012073.png" or self._path .. "1900012073.png")
  28. self._bg = cc.Sprite:create(path) -- 背景
  29. local bgSize = self._bg:getContentSize()
  30. self._bgSize = bgSize
  31. self._bgOrigin = (self._joystickType == 1 and originPosWalk or originPosRun) -- 背景默认位置
  32. self._limitRadius = bgSize.width * 0.3 -- joystick限制半径
  33. self._limitRadiusSq = self._limitRadius * self._limitRadius -- 半径的平方
  34. self._joystickOrigin = cc.p(bgSize.width * 0.5, bgSize.height * 0.5) -- joystick 原点
  35. self:setContentSize(self._enableSize)
  36. self._bg:setPosition(self._bgOrigin)
  37. self._bg:setCascadeOpacityEnabled(true)
  38. self:addChild(self._bg)
  39. -- 摇杆
  40. self._gamePad = cc.Sprite:create(self._path .. "1900012070.png")
  41. self:addChild(self._gamePad)
  42. self._gamePad:setPosition(cc.p(self._bgSize.width / 2, self._bgSize.height / 2))
  43. -- 走跑提醒
  44. local contentSize = self._gamePad:getContentSize()
  45. local fileName = self._joystickType == 1 and "1900012076.png" or "1900012075.png"
  46. self._joystickTips = cc.Sprite:create(self._path .. fileName)
  47. self._gamePad:addChild(self._joystickTips)
  48. self._joystickTips:setPosition(cc.p(contentSize.width/2, contentSize.height/2))
  49. -- 箭头
  50. self._arrowTips = cc.Sprite:create(self._path .. "1900012074.png")
  51. self._bg:addChild(self._arrowTips)
  52. -- 走/跑切换
  53. self._buttonMode = ccui.Button:create()
  54. self:addChild(self._buttonMode)
  55. self._buttonMode:setAnchorPoint(cc.p(0, 0))
  56. self._buttonMode:setPosition(cc.p(0, 28))
  57. self._buttonMode:loadTextureNormal(self._path .. "bg_jindutiao_16.png")
  58. self._buttonMode:addClickEventListener(function()
  59. local joystickMode = GET_CLOUD_DATA("joystick_mode") or 2
  60. SET_CLOUD_DATA("joystick_mode", 3-joystickMode)
  61. global.Facade:sendNotification(global.NoticeTable.Layer_Main_JoystickUpdate)
  62. end)
  63. self:gamePadVisible(false)
  64. self:gamePadTipsVisible(false)
  65. self:registerTouchListener()
  66. self:UpdateJoystickMode()
  67. -- self:InitTouchLayout() -- for debug
  68. self:OnRocker_Show_Distance_Change()
  69. return true
  70. end
  71. function MainGamePadLayout:onTouchBegan(touch, event)
  72. if not self:isVisible() then
  73. return false
  74. end
  75. if not self:CheckTouch(touch) then -- 是否是当前触摸对象
  76. return false
  77. end
  78. if not self:checkGamePadRect(touch) then -- 是否在点击rect
  79. return false
  80. end
  81. self._curTouchId = touch:getId()
  82. self:gamePadVisible(true)
  83. self:gamePadTipsVisible(false)
  84. self:JoystickLogic(touch)
  85. return true
  86. end
  87. function MainGamePadLayout:onTouchMoved(touch, event)
  88. if not self:CheckTouch(touch) then
  89. return
  90. end
  91. self:JoystickLogic(touch)
  92. end
  93. function MainGamePadLayout:onTouchEnded(touch, event)
  94. if not self:CheckTouch(touch) then
  95. return
  96. end
  97. self:Ray2Scene(touch)
  98. self:ResetGamePad()
  99. end
  100. function MainGamePadLayout:Ray2Scene(touch)
  101. -- 只响应在盲区的
  102. if self._angle ~= 0xFF then
  103. return
  104. end
  105. local worldPos = self:convertTouchToNodeSpace(touch)
  106. local touchPos = worldPos
  107. local touchWay = global.MMO.MOVE_EVENT_TOUCH_AND_MOUSE_L
  108. local eventType = cc.Handler.EVENT_TOUCH_BEGAN
  109. global.gamePlayerController:HandleTouchEndEvent(touchPos, touchWay, eventType)
  110. end
  111. function MainGamePadLayout:CheckTouch(touch)
  112. if (self._curTouchId ~= nil and self._curTouchId ~= touch:getId()) then
  113. return false
  114. end
  115. return true
  116. end
  117. function MainGamePadLayout:checkGamePadRect(touch)
  118. local bOutSide = true
  119. local touchPoint = self:convertTouchToNodeSpace(touch)
  120. local gamePadRect = cc.rect(0, 0, self._enableSize.width, self._enableSize.height)
  121. if (cc.rectContainsPoint(gamePadRect, touchPoint)) then
  122. self._bg:setPosition(touchPoint)
  123. bOutSide = false
  124. end
  125. return not bOutSide
  126. end
  127. function MainGamePadLayout:gamePadVisible(visible)
  128. self._bg:setVisible(visible)
  129. end
  130. function MainGamePadLayout:gamePadTipsVisible(visible)
  131. self._arrowTips:setVisible(visible)
  132. end
  133. function MainGamePadLayout:JoystickLogic(touch)
  134. -- swallow touch
  135. self._listener:setSwallowTouches(true)
  136. local touchPoint = self._bg:convertTouchToNodeSpace(touch)
  137. local targPos, step = self:calcTargPos(touchPoint)
  138. local bgOriginPos = cc.p(self._bg:getPositionX()-self._bgSize.width/2, self._bg:getPositionY()-self._bgSize.height/2)
  139. self._gamePad:setPosition(cc.pAdd(bgOriginPos, targPos))
  140. self._gamePad:stopAllActions()
  141. self:SetGamePadDir(cc.pSub(targPos, self._joystickOrigin), step)
  142. end
  143. function MainGamePadLayout:calcTargPos(movePos)
  144. local diff = cc.pSub(movePos, self._joystickOrigin)
  145. local distSq = cc.pLengthSQ(diff)
  146. if (distSq <= self._limitRadiusSq) then
  147. return movePos, 1
  148. end
  149. -- out of bound, clamp it.
  150. diff = cc.pNormalize(diff)
  151. diff = cc.pMul(diff, self._limitRadius)
  152. return cc.pAdd(self._joystickOrigin, diff), 2
  153. end
  154. function MainGamePadLayout:SetGamePadDir(vec, step)
  155. local dis = cc.pLengthSQ(vec)
  156. if (dis <= 50) then --// 触摸盲区
  157. self._angle = 0xff
  158. self._step = 1
  159. self:gamePadTipsVisible(false)
  160. else
  161. self._angle = math.deg(cc.pToAngleSelf(vec))
  162. self._step = step
  163. self:checkArrowTips()
  164. end
  165. end
  166. function MainGamePadLayout:checkArrowTips()
  167. local angle = 90 - self._angle
  168. self._arrowTips:setRotation(angle)
  169. local radius = self._bgSize.width/2 + 10
  170. local sinValue = math.sin(self._angle/57.29577951)
  171. local cosValue = math.cos(self._angle/57.29577951)
  172. local offsetX = radius * cosValue
  173. local offsetY = radius * sinValue
  174. self._arrowTips:setPosition(cc.p(self._bgSize.width / 2 + offsetX, self._bgSize.height / 2 + offsetY))
  175. self:gamePadTipsVisible(true)
  176. end
  177. function MainGamePadLayout:ResetGamePad()
  178. if (self._listener) then
  179. self._listener:setSwallowTouches(false)
  180. end
  181. self._angle = 0xFF
  182. self._move = 1
  183. self._curTouchId = nil
  184. self:gamePadVisible(false)
  185. self:gamePadTipsVisible(false)
  186. self._bg:setPosition(self._bgOrigin)
  187. self._gamePad:stopAllActions()
  188. self._gamePad:runAction(cc.EaseBackOut:create(cc.MoveTo:create(0.3, self._bgOrigin)))
  189. end
  190. function MainGamePadLayout:GetGamePadAngle()
  191. return self._angle
  192. end
  193. function MainGamePadLayout:GetGamePadStep()
  194. return self._step
  195. end
  196. function MainGamePadLayout:InitTouchLayout()
  197. -- init touch layer
  198. -- init touch layer
  199. if self._touchLayout then
  200. self._touchLayout:removeFromParent()
  201. self._touchLayout = nil
  202. end
  203. self._touchLayout = ccui.Layout:create()
  204. local viewSize = global.Director:getVisibleSize()
  205. self._touchLayout:setContentSize(self._enableSize.width, self._enableSize.height)
  206. self._touchLayout:setBackGroundColorType(ccui.LayoutBackGroundColorType.solid)
  207. self._touchLayout:setBackGroundColor(self._ctrlMode == 2 and cc.Color3B.GREEN or cc.Color3B.RED)
  208. self._touchLayout:setAnchorPoint(0, 0)
  209. self._touchLayout:setOpacity(100)
  210. self:addChild(self._touchLayout, 0)
  211. end
  212. function MainGamePadLayout:registerTouchListener()
  213. local function began(touch, event)
  214. return self:onTouchBegan(touch, event)
  215. end
  216. local function moved(touch, event)
  217. return self:onTouchMoved(touch, event)
  218. end
  219. local function ended(touch, event)
  220. return self:onTouchEnded(touch, event)
  221. end
  222. local function cancelled(touch, event)
  223. return self:onTouchEnded(touch, event)
  224. end
  225. self:setTouchEnabled(true)
  226. local listener = cc.EventListenerTouchOneByOne:create()
  227. local eventDispatcher = global.Director:getEventDispatcher()
  228. listener:registerScriptHandler(began, cc.Handler.EVENT_TOUCH_BEGAN)
  229. listener:registerScriptHandler(moved, cc.Handler.EVENT_TOUCH_MOVED)
  230. listener:registerScriptHandler(ended, cc.Handler.EVENT_TOUCH_ENDED)
  231. listener:registerScriptHandler(cancelled, cc.Handler.EVENT_TOUCH_CANCELLED)
  232. eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self)
  233. self._listener = listener
  234. end
  235. function MainGamePadLayout:SetDir(dir)
  236. if not dir or not dir.x or not dir.y then
  237. return
  238. end
  239. local targPos1 = cc.p((dir.x + 1) * self._bgSize.width * 0.5, (dir.y + 1) * self._bgSize.height * 0.5)
  240. local targPos = self:calcTargPos(targPos1)
  241. local bgOriginPos = cc.p(self._bg:getPositionX()-self._bgSize.width/2, self._bg:getPositionY()-self._bgSize.height/2)
  242. self._gamePad:stopAllActions()
  243. self._gamePad:setPosition(cc.pAdd(bgOriginPos, targPos))
  244. self:SetGamePadDir(cc.pSub(targPos, self._joystickOrigin))
  245. self:checkArrowTips()
  246. if 0 == dir.x and 0 == dir.y then
  247. self:ResetGamePad()
  248. else
  249. self:gamePadVisible(true)
  250. self:gamePadTipsVisible(true)
  251. end
  252. end
  253. function MainGamePadLayout:UpdateJoystickMode()
  254. if CHECK_SETTING(6) ~= 1 or (SL:GetMetaValue("GAME_DATA","gameOption_WalkOnly") == 1) then
  255. -- 单摇杆
  256. -------------------------
  257. self._enableSize = enableSizeAll -- 触摸有效区
  258. self._bgOrigin = originPosAll -- 背景默认位置
  259. self:setContentSize(self._enableSize)
  260. self._bg:setPosition(self._bgOrigin)
  261. -- 摇杆背景
  262. self._bg:setTexture(self._path .. "1900012073.png")
  263. local bgSize = self._bg:getContentSize()
  264. self._limitRadius = bgSize.width * 0.3 -- joystick限制半径
  265. self._limitRadiusSq = self._limitRadius * self._limitRadius -- 半径的平方
  266. self._joystickOrigin = cc.p(bgSize.width * 0.5, bgSize.height * 0.5) -- joystick 原点
  267. self._bgSize = bgSize
  268. -- 摇杆中间球
  269. self._gamePad:setTexture(self._path .. "1900012070.png")
  270. self._gamePad:setPosition(cc.p(0,0))
  271. local gamePadSize = self._gamePad:getContentSize()
  272. self._joystickTips:setPosition(cc.p(gamePadSize.width/2, gamePadSize.height/2))
  273. -------------------------
  274. -- 摇杆
  275. local joystickMode = GET_CLOUD_DATA("joystick_mode") or 2
  276. if joystickMode == 1 then
  277. -- 走
  278. self:setVisible(self._joystickType == 1)
  279. self._buttonMode:loadTextureNormal(self._path .. "bg_jindutiao_17.png")
  280. self._buttonMode:setVisible(true)
  281. else
  282. -- 跑
  283. self:setVisible(self._joystickType == 2)
  284. self._buttonMode:loadTextureNormal(self._path .. "bg_jindutiao_16.png")
  285. self._buttonMode:setVisible(true)
  286. end
  287. -- 走 -> 跑
  288. if (SL:GetMetaValue("GAME_DATA","gameOption_WalkOnly") == 1) then
  289. self:setVisible(self._joystickType == 2)
  290. self._buttonMode:setVisible(false)
  291. end
  292. else
  293. -- 双摇杆
  294. -------------------------
  295. -- 响应区域
  296. self._enableSize = (self._joystickType == 1 and enableSizeWalk or enableSizeRun) -- 触摸有效区
  297. self._bgOrigin = (self._joystickType == 1 and originPosWalk or originPosRun) -- 背景默认位置
  298. self:setContentSize(self._enableSize)
  299. self._bg:setPosition(self._bgOrigin)
  300. -- 背景
  301. self._bg:setTexture(self._path .. (self._joystickType == 1 and "1900012072.png" or "1900012073.png"))
  302. local bgSize = self._bg:getContentSize()
  303. self._bgSize = bgSize
  304. self._limitRadius = bgSize.width * 0.5 -- joystick限制半径
  305. self._limitRadiusSq = self._limitRadius * self._limitRadius -- 半径的平方
  306. self._joystickOrigin = cc.p(bgSize.width * 0.5, bgSize.height * 0.5) -- joystick 原点
  307. -- 摇杆中间球
  308. self._gamePad:setTexture(self._path .. (self._joystickType == 1 and "1900012070.png" or "1900012071.png"))
  309. self._gamePad:setPosition(cc.p(self._bgSize.width / 2, self._bgSize.height / 2))
  310. local gamePadSize = self._gamePad:getContentSize()
  311. self._joystickTips:setPosition(cc.p(gamePadSize.width/2, gamePadSize.height/2))
  312. -------------------------
  313. -- 遥感显示隐藏
  314. self:setVisible(true)
  315. self._buttonMode:setVisible(false)
  316. end
  317. self:ResetGamePad()
  318. -- self:InitTouchLayout()
  319. end
  320. function MainGamePadLayout:OnRocker_Show_Distance_Change()
  321. local distance = CHECK_SETTING(global.MMO.SETTING_IDX_ROCKER_SHOW_DISTANCE) or 0
  322. originPosWalk = cc.p(100+distance, 105) -- 双摇杆 走
  323. originPosRun = cc.p(200+distance, 205) -- 双摇杆 跑
  324. originPosAll = cc.p(150+distance, 155) -- 双摇杆 跑
  325. self._bgOrigin = (self._joystickType == 1 and originPosWalk or originPosRun) -- 背景默认位置
  326. if CHECK_SETTING(6) ~= 1 or (SL:GetMetaValue("GAME_DATA","gameOption_WalkOnly") == 1) then
  327. self._bgOrigin = originPosAll
  328. end
  329. self._bg:setPosition(self._bgOrigin)
  330. self:ResetGamePad()
  331. end
  332. return MainGamePadLayout