KLJoyStickPanel.lua 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. local key = UtilityLua.GetKeyWSAD()
  61. if key ~= 0 then
  62. operateTimer = Time.time
  63. end
  64. if key ~= 0 then
  65. direction:Set(0, 0)
  66. if bit.band(key, EGetKeyCodeWSAD.EKeyCodeW) ~= 0 then
  67. direction:AddXY(0, 1)
  68. elseif bit.band(key, EGetKeyCodeWSAD.EKeyCodeS) ~= 0 then
  69. direction:AddXY(0, -1)
  70. end
  71. if bit.band(key, EGetKeyCodeWSAD.EKeyCodeD) ~= 0 then
  72. direction:AddXY(1, 0)
  73. elseif bit.band(key, EGetKeyCodeWSAD.EKeyCodeA) ~= 0 then
  74. direction:AddXY(-1, 0)
  75. end
  76. local distance = Vector2.Magnitude(direction)
  77. local pos = direction * maxRandius
  78. GUI:setPosition(self.view.JoyStick, pos.x, pos.y)
  79. local dir = self:GetJoyStickNormalOffset(direction.x, direction.y)
  80. direction:Copy(dir)
  81. SL:SetMetaValue(EMetaVarSetKey.SET_IS_CAN_SCENE_TOUCH, false)
  82. GUI:setScale(self.view.JoyStickBg, 1)
  83. GUI:Image_setAlpha(self.view.JoyStickBgSmall, 0.8)
  84. GUI:Image_setAlpha(self.view.JoyStick, 0.8)
  85. self:MoveWithDirection()
  86. isReset = false
  87. end
  88. local keyUp = UtilityLua.GetKeyUpWSAD()
  89. if keyUp ~= 0 or not SL:GetIsInGame() then
  90. isReset = true
  91. end
  92. if not Application.isFocused then
  93. if isFirst then
  94. isReset = true
  95. isFirst = false
  96. end
  97. else
  98. isFirst = true
  99. end
  100. if isReset then
  101. self:ResetStick()
  102. isReset = false
  103. end
  104. end
  105. ---@param kmlCtrl UIKmlLuaControl
  106. ---@param eventType EUIEventName
  107. function this:OnTouchEvent(kmlCtrl, eventType, args)
  108. if eventType == EUIEventName.OnBeginDrag then
  109. operateTimer = Time.time
  110. elseif eventType == EUIEventName.OnDrag then
  111. local basePos1 = args.pressEventCamera:WorldToScreenPoint(basePos)
  112. direction:Set(args.position.x - basePos1.x, args.position.y - basePos1.y)
  113. local distance = Vector2.Magnitude(direction)
  114. local radius = Mathf.Clamp(distance, 0, maxRandius)
  115. local pos = direction.normalized * radius
  116. GUI:setPosition(self.view.JoyStick, pos.x, pos.y)
  117. local dir = self:GetJoyStickNormalOffset(direction.x, direction.y)
  118. direction:Copy(dir)
  119. SL:SetMetaValue(EMetaVarSetKey.SET_IS_CAN_SCENE_TOUCH, false)
  120. GUI:setScale(self.view.JoyStickBg, 1)
  121. GUI:Image_setAlpha(self.view.JoyStickBgSmall, 0.8)
  122. GUI:Image_setAlpha(self.view.JoyStick, 0.8)
  123. self:MoveWithDirection()
  124. elseif eventType == EUIEventName.OnPointerDown then
  125. if tickModel == ETickModel.Move then
  126. local pointPos = nil
  127. if args.position.z == nil then
  128. local x, y, z = GUI:GetWorldPosition(self.view.JoyStickBg)
  129. pointPos = Vector3.New(args.position.x, args.position.y, z)
  130. else
  131. pointPos = Vector3.New(args.position.x, args.position.y, args.position.z)
  132. end
  133. local pos = args.pressEventCamera:ScreenToWorldPoint(pointPos)
  134. GUI:SetWorldPosition(self.view.JoyStickBg, pos.x, pos.y, pos.z)
  135. else
  136. GUI:setPosition(self.view.JoyStickBg, 0, 0)
  137. end
  138. local x, y, z = GUI:GetWorldPosition(self.view.JoyStickBg)
  139. basePos:Set(x, y, z)
  140. GUI:setScale(self.view.JoyStickBg, 1)
  141. GUI:Image_setAlpha(self.view.JoyStickBgSmall, 0.8)
  142. GUI:Image_setAlpha(self.view.JoyStick, 0.8)
  143. SL:SetMetaValue(EMetaVarSetKey.SET_IS_CAN_SCENE_TOUCH, false)
  144. elseif eventType == EUIEventName.OnPointerUp then
  145. self:ResetStick()
  146. end
  147. end
  148. function this:ResetStick(control, eventData)
  149. GUI:setPosition(self.view.JoyStickBg, 0, 0)
  150. GUI:setPosition(self.view.JoyStick, 0, 0)
  151. SL:LineMoveTo(Dot2(0, 0), false, false)
  152. GUI:setScale(self.view.JoyStickBg, 0.9)
  153. GUI:Image_setAlpha(self.view.JoyStickBgSmall, 0.5)
  154. GUI:Image_setAlpha(self.view.JoyStick, 0.5)
  155. SL:SetMetaValue(EMetaVarSetKey.SET_IS_CAN_SCENE_TOUCH, true)
  156. --EventManager.Dispatch(Event.StickMoveEvent, false)
  157. SL:onLUAEvent(Event.StickMoveEvent, false)
  158. SL:onLUAEvent(LUA_EVENT_JOYSTICK_MOVE, false)
  159. end
  160. ---移动
  161. function this:MoveWithDirection()
  162. if direction == Vector2Int.zero then return end
  163. ---暂停自动挂机
  164. if Time.time - operateTimer > interruptHookTime then
  165. SL:PauseOnHook(false, 2)
  166. operateTimer = Time.time
  167. else
  168. SL:onLUAEvent(Event.StickMoveEvent, true)
  169. end
  170. SL:ClearDelaySkill()
  171. SL:LineMoveTo(Dot2.New(direction.x, direction.y), false, false)
  172. SL:StopScriptAutoMove()
  173. if SL:GetMetaValue(EMetaVarGetKey.IS_FIND_PATH_SHOW_FOOT) then
  174. SL:onLUAEvent(Event.ShowPathFindFootSteps)
  175. end
  176. SL:onLUAEvent(LUA_EVENT_JOYSTICK_MOVE, true)
  177. end
  178. ---获取方向偏移
  179. function this:GetJoyStickNormalOffset(x, y)
  180. local angle = Mathf.Atan2(y, x) * Mathf.Rad2Deg
  181. if SL:GetMetaValue(EMetaVarGetKey.ENGINE_IS_LOCK_CAMERA) then
  182. angle = angle + 22.5
  183. else
  184. local yAngle = SL:GetMetaValue(EMetaVarGetKey.MAIN_CAMERA_Y_ORBIT_ANGLE_Y)
  185. angle = angle - yAngle - 22.5
  186. end
  187. angle = angle % 360
  188. if angle >= 0 and angle < 45 then
  189. return DirectionOffset[Direction.Right]
  190. end
  191. if angle >= 45 and angle < 90 then
  192. return DirectionOffset[Direction.RightUp]
  193. end
  194. if angle >= 90 and angle < 135 then
  195. return DirectionOffset[Direction.Up]
  196. end
  197. if angle >= 135 and angle < 180 then
  198. return DirectionOffset[Direction.LeftUp]
  199. end
  200. if angle >= 180 and angle < 225 then
  201. return DirectionOffset[Direction.Left]
  202. end
  203. if angle >= 225 and angle < 270 then
  204. return DirectionOffset[Direction.LeftDown]
  205. end
  206. if angle >= 270 and angle < 315 then
  207. return DirectionOffset[Direction.Down]
  208. end
  209. return DirectionOffset[Direction.RightDown]
  210. end
  211. function this:Close()
  212. if self.schedule then
  213. SL:UnSchedule(self.schedule)
  214. self.schedule = nil
  215. end
  216. end
  217. return this