KLJoyStickPanel.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. ---@class KLJoyStickPanel:UIKmlLuaPanelBase
  2. ---@field view KLJoyStickPanelView
  3. local KLJoyStickPanel = class(UIKmlLuaPanelBase)
  4. local this = KLJoyStickPanel
  5. local maxRandius = 70
  6. local basePos = Vector3.zero
  7. local direction = Vector2.zero
  8. local tickModel = ETickModel.fixed
  9. local operateTimer = 0
  10. local interruptHookTime = 0
  11. local isReleaseSkill = nil
  12. local isFirst = true
  13. local isReset = false
  14. local Direction = {
  15. Up = enum(0),
  16. RightUp = enum(),
  17. Right = enum(),
  18. RightDown = enum(),
  19. Down = enum(),
  20. LeftDown = enum(),
  21. Left = enum(),
  22. LeftUp = enum(),
  23. Max = enum(),
  24. }
  25. local DirectionOffset = {
  26. [Direction.Up] = Vector2(-1, 1),
  27. [Direction.RightUp] = Vector2(0, 1),
  28. [Direction.Right] = Vector2(1, 1),
  29. [Direction.RightDown] = Vector2(1, 0),
  30. [Direction.Down] = Vector2(1, -1),
  31. [Direction.LeftDown] = Vector2(0, -1),
  32. [Direction.Left] = Vector2(-1, -1),
  33. [Direction.LeftUp] = Vector2(-1, 0),
  34. [Direction.Max] = Vector2(0, 0),
  35. }
  36. ---创建时调用一次
  37. function this:Init()
  38. end
  39. ---创建或者刷新界面数据时调用
  40. function this:Refresh()
  41. tickModel = SL:GetMetaValue(EMetaVarGetKey.GET_ROCKER_TYPE)
  42. end
  43. ---注册UI事件和服务器消息
  44. function this:RegistEvents()
  45. GUI:AddOnTouchEvent(self.view.JoyStickBg, self, self.OnTouchEvent)
  46. --self.schedule = SL:Schedule(self.schedule, 0, Time.deltaTime, -1, function()
  47. -- return self:OnUpdate()
  48. --end)
  49. SL:RegisterLUAEvent(Event.RoleSetting_JoystickSetChange, self.JoyStickModelChange, self)
  50. end
  51. function this:JoyStickModelChange()
  52. tickModel = SL:GetMetaValue(EMetaVarGetKey.GET_ROCKER_TYPE)
  53. end
  54. function this:OnUpdate()
  55. if SL:GetIsXtra() then
  56. if Xtra_UIToolsManager.IsEdiorOpen then
  57. return
  58. end
  59. end
  60. if SL:GetIsPcPlatform() then
  61. return
  62. end
  63. local key = UtilityLua.GetKeyWSAD()
  64. if key ~= 0 then
  65. operateTimer = Time.time
  66. end
  67. if key ~= 0 then
  68. direction:Set(0, 0)
  69. if bit.band(key, EGetKeyCodeWSAD.EKeyCodeW) ~= 0 then
  70. direction:AddXY(0, 1)
  71. elseif bit.band(key, EGetKeyCodeWSAD.EKeyCodeS) ~= 0 then
  72. direction:AddXY(0, -1)
  73. end
  74. if bit.band(key, EGetKeyCodeWSAD.EKeyCodeD) ~= 0 then
  75. direction:AddXY(1, 0)
  76. elseif bit.band(key, EGetKeyCodeWSAD.EKeyCodeA) ~= 0 then
  77. direction:AddXY(-1, 0)
  78. end
  79. local distance = Vector2.Magnitude(direction)
  80. local pos = direction * maxRandius
  81. GUI:setPosition(self.view.JoyStick, pos.x, pos.y)
  82. local dir = self:GetJoyStickNormalOffset(direction.x, direction.y)
  83. direction:Copy(dir)
  84. SL:SetMetaValue(EMetaVarSetKey.SET_IS_CAN_SCENE_TOUCH, false)
  85. GUI:setScale(self.view.JoyStickBg, 1)
  86. GUI:Image_setAlpha(self.view.JoyStickBgSmall, 0.8)
  87. GUI:Image_setAlpha(self.view.JoyStick, 0.8)
  88. self:MoveWithDirection()
  89. isReset = false
  90. end
  91. local keyUp = UtilityLua.GetKeyUpWSAD()
  92. if keyUp ~= 0 or not SL:GetIsInGame() then
  93. isReset = true
  94. end
  95. if not Application.isFocused then
  96. if isFirst then
  97. isReset = true
  98. isFirst = false
  99. end
  100. else
  101. isFirst = true
  102. end
  103. if isReset then
  104. self:ResetStick()
  105. isReset = false
  106. end
  107. end
  108. ---@param kmlCtrl UIKmlLuaControl
  109. ---@param eventType EUIEventName
  110. function this:OnTouchEvent(kmlCtrl, eventType, args)
  111. if eventType == EUIEventName.OnBeginDrag then
  112. operateTimer = Time.time
  113. elseif eventType == EUIEventName.OnDrag then
  114. local basePos1 = args.pressEventCamera:WorldToScreenPoint(basePos)
  115. direction:Set(args.position.x - basePos1.x, args.position.y - basePos1.y)
  116. local distance = Vector2.Magnitude(direction)
  117. local radius = Mathf.Clamp(distance, 0, maxRandius)
  118. local pos = direction.normalized * radius
  119. GUI:setPosition(self.view.JoyStick, pos.x, pos.y)
  120. local dir = self:GetJoyStickNormalOffset(direction.x, direction.y)
  121. direction:Copy(dir)
  122. SL:SetMetaValue(EMetaVarSetKey.SET_IS_CAN_SCENE_TOUCH, false)
  123. GUI:setScale(self.view.JoyStickBg, 1)
  124. GUI:Image_setAlpha(self.view.JoyStickBgSmall, 0.8)
  125. GUI:Image_setAlpha(self.view.JoyStick, 0.8)
  126. self:MoveWithDirection()
  127. elseif eventType == EUIEventName.OnPointerDown then
  128. if tickModel == ETickModel.Move then
  129. local pointPos = nil
  130. if args.position.z == nil then
  131. local x, y, z = GUI:GetWorldPosition(self.view.JoyStickBg)
  132. pointPos = Vector3.New(args.position.x, args.position.y, z)
  133. else
  134. pointPos = Vector3.New(args.position.x, args.position.y, args.position.z)
  135. end
  136. local pos = args.pressEventCamera:ScreenToWorldPoint(pointPos)
  137. GUI:SetWorldPosition(self.view.JoyStickBg, pos.x, pos.y, pos.z)
  138. else
  139. GUI:setPosition(self.view.JoyStickBg, 0, 0)
  140. end
  141. local x, y, z = GUI:GetWorldPosition(self.view.JoyStickBg)
  142. basePos:Set(x, y, z)
  143. GUI:setScale(self.view.JoyStickBg, 1)
  144. GUI:Image_setAlpha(self.view.JoyStickBgSmall, 0.8)
  145. GUI:Image_setAlpha(self.view.JoyStick, 0.8)
  146. SL:SetMetaValue(EMetaVarSetKey.SET_IS_CAN_SCENE_TOUCH, false)
  147. elseif eventType == EUIEventName.OnPointerUp then
  148. self:ResetStick()
  149. end
  150. end
  151. function this:ResetStick(control, eventData)
  152. GUI:setPosition(self.view.JoyStickBg, 0, 0)
  153. GUI:setPosition(self.view.JoyStick, 0, 0)
  154. SL:LineMoveTo(Dot2(0, 0), false, false)
  155. GUI:setScale(self.view.JoyStickBg, 0.9)
  156. GUI:Image_setAlpha(self.view.JoyStickBgSmall, 0.5)
  157. GUI:Image_setAlpha(self.view.JoyStick, 0.5)
  158. SL:SetMetaValue(EMetaVarSetKey.SET_IS_CAN_SCENE_TOUCH, true)
  159. --EventManager.Dispatch(Event.StickMoveEvent, false)
  160. SL:onLUAEvent(Event.StickMoveEvent, false)
  161. SL:onLUAEvent(LUA_EVENT_JOYSTICK_MOVE, false)
  162. end
  163. ---移动
  164. function this:MoveWithDirection()
  165. if direction == Vector2Int.zero then return end
  166. ---暂停自动挂机
  167. if Time.time - operateTimer > interruptHookTime then
  168. SL:PauseOnHook(false, 2)
  169. operateTimer = Time.time
  170. else
  171. SL:onLUAEvent(Event.StickMoveEvent, true)
  172. end
  173. SL:ClearDelaySkill()
  174. SL:LineMoveTo(Dot2.New(direction.x, direction.y), false, false)
  175. SL:StopScriptAutoMove()
  176. if SL:GetMetaValue(EMetaVarGetKey.IS_FIND_PATH_SHOW_FOOT) then
  177. SL:onLUAEvent(Event.ShowPathFindFootSteps)
  178. end
  179. SL:onLUAEvent(LUA_EVENT_JOYSTICK_MOVE, true)
  180. end
  181. ---获取方向偏移
  182. function this:GetJoyStickNormalOffset(x, y)
  183. local angle = Mathf.Atan2(y, x) * Mathf.Rad2Deg
  184. if SL:GetMetaValue(EMetaVarGetKey.ENGINE_IS_LOCK_CAMERA) then
  185. angle = angle + 22.5
  186. else
  187. local yAngle = SL:GetMetaValue(EMetaVarGetKey.MAIN_CAMERA_Y_ORBIT_ANGLE_Y)
  188. angle = angle - yAngle - 22.5
  189. end
  190. angle = angle % 360
  191. if angle >= 0 and angle < 45 then
  192. return DirectionOffset[Direction.Right]
  193. end
  194. if angle >= 45 and angle < 90 then
  195. return DirectionOffset[Direction.RightUp]
  196. end
  197. if angle >= 90 and angle < 135 then
  198. return DirectionOffset[Direction.Up]
  199. end
  200. if angle >= 135 and angle < 180 then
  201. return DirectionOffset[Direction.LeftUp]
  202. end
  203. if angle >= 180 and angle < 225 then
  204. return DirectionOffset[Direction.Left]
  205. end
  206. if angle >= 225 and angle < 270 then
  207. return DirectionOffset[Direction.LeftDown]
  208. end
  209. if angle >= 270 and angle < 315 then
  210. return DirectionOffset[Direction.Down]
  211. end
  212. return DirectionOffset[Direction.RightDown]
  213. end
  214. function this:Close()
  215. if self.schedule then
  216. SL:UnSchedule(self.schedule)
  217. self.schedule = nil
  218. end
  219. end
  220. return this