Touch.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. -- Mobile framework for Android/iOS
  2. -- Gamepad from Ninja Snow War
  3. -- Gyroscope (activated by default)
  4. -- Touches patterns:
  5. -- - 1 finger touch = pick object through raycast
  6. -- - 1 or 2 fingers drag = rotate camera
  7. -- - 3 fingers touch = switch between first/third person view
  8. -- - 4 fingers touch = switch shadows on/off
  9. -- - 2 fingers sliding in opposite direction (up/down) = zoom in/out
  10. -- 3 fingers touch & 4 fingers touch could be used to switch gyroscope on/off, activate/deactivate secondary viewport, activate a panel GUI, switch debug HUD/geometry, toggle console, switch the gyroscope...
  11. -- Setup:
  12. -- - On init, call this script using 'require "LuaScripts/Utilities/Touch" end' then 'InitTouchInput()' on mobile platforms
  13. -- -> to detect platform, use 'if GetPlatform() == "Android" or GetPlatform() == "iOS"')
  14. -- - Subscribe to touch events (Begin, Move, End) using 'SubscribeToTouchEvents()'
  15. -- - Call the update function 'updateTouches()' from HandleUpdate or equivalent update handler function
  16. TOUCH_SENSITIVITY = 5.0
  17. GYROSCOPE_THRESHOLD = 0.1
  18. CAMERA_MIN_DIST = 1.0
  19. CAMERA_INITIAL_DIST = 5.0
  20. CAMERA_MAX_DIST = 20.0
  21. local moveTouchID = -1
  22. local rotateTouchID = -1
  23. local fireTouchID = -1
  24. local moveButton
  25. local fireButton
  26. local touchButtonSize = 96
  27. local touchButtonBorder = 12
  28. local zoom = false
  29. local newFirstPerson = nil
  30. local debugMode = nil
  31. local shadowMode = true
  32. firstPerson = false
  33. touchEnabled = false
  34. cameraDistance = CAMERA_INITIAL_DIST
  35. cameraNode = nil
  36. -- Create Gamepad Buttons
  37. function InitTouchInput()
  38. moveButton = ui.root:CreateChild("BorderImage")
  39. moveButton.texture = cache:GetResource("Texture2D", "Textures/TouchInput.png")
  40. moveButton.imageRect = IntRect(0, 0, 96, 96) -- Crop right side of the texture
  41. moveButton:SetAlignment(HA_LEFT, VA_BOTTOM)
  42. moveButton:SetPosition(touchButtonBorder, -touchButtonBorder)
  43. moveButton:SetSize(touchButtonSize, touchButtonSize)
  44. moveButton.opacity = 0.25
  45. fireButton = ui.root:CreateChild("BorderImage")
  46. fireButton.texture = cache:GetResource("Texture2D", "Textures/TouchInput.png")
  47. fireButton.imageRect = IntRect(96, 0, 192, 96) -- Crop left side of the texture
  48. fireButton:SetAlignment(HA_RIGHT, VA_BOTTOM)
  49. fireButton:SetPosition(-touchButtonBorder, -touchButtonBorder)
  50. fireButton:SetSize(touchButtonSize, touchButtonSize)
  51. fireButton.opacity = 0.25
  52. touchEnabled = true
  53. end
  54. function SubscribeToTouchEvents()
  55. SubscribeToEvent("TouchBegin", "HandleTouchBegin")
  56. SubscribeToEvent("TouchEnd", "HandleTouchEnd")
  57. end
  58. function updateTouches(controls) -- Called from HandleUpdate
  59. local controls = characterNode:GetScriptObject().controls
  60. local camera = cameraNode:GetComponent("Camera")
  61. zoom = false -- reset bool
  62. -- Touch Inputs
  63. if touchEnabled then
  64. -- Zoom in/out
  65. if input.numTouches == 2 then
  66. local touch1 = input:GetTouch(0)
  67. local touch2 = input:GetTouch(1)
  68. if (touch1.delta.y > 0 and touch2.delta.y < 0) or (touch1.delta.y < 0 and touch2.delta.y > 0) then zoom = true else zoom = false end -- Check for zoom pattern (touches moving in opposite directions)
  69. if zoom then
  70. if Abs(touch1.position.y - touch2.position.y) > Abs(touch1.lastPosition.y - touch2.lastPosition.y) then sens = -1 else sens = 1 end -- Check for zoom direction (in/out)
  71. cameraDistance = cameraDistance + Abs( touch1.delta.y - touch2.delta.y ) * sens * TOUCH_SENSITIVITY / 50
  72. cameraDistance = Clamp(cameraDistance, CAMERA_MIN_DIST, CAMERA_MAX_DIST) -- Restrict zoom range to [1;20]
  73. end
  74. end
  75. -- Switch 1st/3rd person mode
  76. if input.numTouches == 3 then newFirstPerson = not firstPerson end
  77. -- Switch draw debug
  78. if input.numTouches == 4 then shadowMode = not renderer.drawShadows end
  79. -- Rotate and Move
  80. if not zoom then
  81. for i = 0, input.numTouches-1 do -- numtouches=[0;4]
  82. local touch = input:GetTouch(i) -- TouchState
  83. if touch.touchID == rotateTouchID then
  84. controls.yaw = controls.yaw + TOUCH_SENSITIVITY * camera.fov / graphics.height * touch.delta.x
  85. controls.pitch = controls.pitch + TOUCH_SENSITIVITY * camera.fov / graphics.height * touch.delta.y
  86. controls.pitch = Clamp(controls.pitch, -80, 80) -- Limit pitch
  87. end
  88. if touch.touchID == moveTouchID then
  89. local relX = touch.position.x - moveButton.screenPosition.x - touchButtonSize / 2
  90. local relY = touch.position.y - moveButton.screenPosition.y - touchButtonSize / 2
  91. if relY < 0 and Abs(relX * 3 / 2) < Abs(relY) then controls:Set(CTRL_FORWARD, true) end
  92. if relY > 0 and Abs(relX * 3 / 2) < Abs(relY) then controls:Set(CTRL_BACK, true) end
  93. if relX < 0 and Abs(relY * 3 / 2) < Abs(relX) then controls:Set(CTRL_LEFT, true) end
  94. if relX > 0 and Abs(relY * 3 / 2) < Abs(relX) then controls:Set(CTRL_RIGHT, true) end
  95. end
  96. end
  97. end
  98. if fireTouchID >= 0 then controls:Set(CTRL_JUMP, true) end
  99. end
  100. -- Gyroscope (emulated by SDL through a virtual joystick)
  101. if input.numJoysticks > 0 then -- numJoysticks = 1 on iOS & Android
  102. local joystick = input:GetJoystick(0) -- JoystickState
  103. if joystick.numAxes >= 2 then
  104. if joystick:GetAxisPosition(0) < -GYROSCOPE_THRESHOLD then controls:Set(CTRL_LEFT, true) end
  105. if joystick:GetAxisPosition(0) > GYROSCOPE_THRESHOLD then controls:Set(CTRL_RIGHT, true) end
  106. if joystick:GetAxisPosition(1) < -GYROSCOPE_THRESHOLD then controls:Set(CTRL_FORWARD, true) end
  107. if joystick:GetAxisPosition(1) > GYROSCOPE_THRESHOLD then controls:Set(CTRL_BACK, true) end
  108. end
  109. end
  110. end
  111. function HandleTouchBegin(eventType, eventData)
  112. local touchID = eventData:GetInt("TouchID") -- Get #touches or dragging value
  113. local pos = IntVector2(eventData:GetInt("X"), eventData:GetInt("Y")) -- Get touch coordinates
  114. local element = ui:GetElementAt(pos, false) -- Get gamepad UIElement touched (if any)
  115. -- Check for gamepad button touched. If none, rotate
  116. if element == moveButton then moveTouchID = touchID
  117. elseif element == fireButton then fireTouchID = touchID
  118. else rotateTouchID = touchID end
  119. -- Raycast of RigidBodies (for example to acquire a target)
  120. local camera = cameraNode:GetComponent("Camera")
  121. local cameraRay = camera:GetScreenRay(eventData:GetInt("X") / graphics.width, eventData:GetInt("Y") / graphics.height)
  122. local result = scene_:GetComponent("PhysicsWorld"):RaycastSingle(cameraRay, camera.farClip, 2) -- NB: here we restrict targets to layer 2
  123. if result.body ~= nil then print("Physics raycast hit " .. result.body:GetNode().name) end
  124. -- Raycast of drawable components (for targets with or without physics)
  125. local result2 = scene_:GetComponent("Octree"):RaycastSingle(cameraRay, RAY_TRIANGLE, camera.farClip, DRAWABLE_GEOMETRY)
  126. if result2.drawable ~= nil then print("Drawable raycast hit " .. result2.drawable:GetNode().name) end
  127. end
  128. function HandleTouchEnd(eventType, eventData)
  129. local touchID = eventData:GetInt("TouchID")
  130. if touchID == moveTouchID then moveTouchID = -1 end
  131. if touchID == rotateTouchID then rotateTouchID = -1 end
  132. if touchID == fireTouchID then fireTouchID = -1 end
  133. -- On-release Update
  134. firstPerson = newFirstPerson
  135. renderer.drawShadows = shadowMode
  136. end