Skip to content

Commit

Permalink
Support multi-touch in FPS camera
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhonnyg committed Mar 19, 2024
1 parent 52e409d commit d1272c7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 21 deletions.
4 changes: 4 additions & 0 deletions vantage/vantage.input_binding
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ mouse_trigger {
input: MOUSE_WHEEL_DOWN
action: "VANTAGE_WHEEL_DOWN"
}
touch_trigger {
input: TOUCH_MULTI
action: "VANTAGE_TOUCH_MULTI"
}
7 changes: 7 additions & 0 deletions vantage/vantage_component.script
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ local function make_camera(id, type)
camera.on_update = vantage.orbital.on_update
end

local sys_info = sys.get_sys_info()
camera.use_touch_controls = sys_info.system_name == "Android" or sys_info.system_name == "iPhone OS"

local window_width, window_height = window.get_size()
local project_width = sys.get_config_int("display.width")
camera.window_scale = window_width / project_width

return camera
end

Expand Down
82 changes: 61 additions & 21 deletions vantage/vantage_core.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
local M = {}

M.VANTAGE_W = hash("VANTAGE_W")
M.VANTAGE_S = hash("VANTAGE_S")
M.VANTAGE_A = hash("VANTAGE_A")
M.VANTAGE_D = hash("VANTAGE_D")
M.VANTAGE_Q = hash("VANTAGE_Q")
M.VANTAGE_E = hash("VANTAGE_E")
M.VANTAGE_MOUSE_1 = hash("VANTAGE_MOUSE_1")
M.VANTAGE_MOUSE_2 = hash("VANTAGE_MOUSE_2")
M.VANTAGE_MOUSE_3 = hash("VANTAGE_MOUSE_3")
M.VANTAGE_WHEEL_UP = hash("VANTAGE_WHEEL_UP")
M.VANTAGE_WHEEL_DOWN = hash("VANTAGE_WHEEL_DOWN")
M.VANTAGE_W = hash("VANTAGE_W")
M.VANTAGE_S = hash("VANTAGE_S")
M.VANTAGE_A = hash("VANTAGE_A")
M.VANTAGE_D = hash("VANTAGE_D")
M.VANTAGE_Q = hash("VANTAGE_Q")
M.VANTAGE_E = hash("VANTAGE_E")
M.VANTAGE_MOUSE_1 = hash("VANTAGE_MOUSE_1")
M.VANTAGE_MOUSE_2 = hash("VANTAGE_MOUSE_2")
M.VANTAGE_MOUSE_3 = hash("VANTAGE_MOUSE_3")
M.VANTAGE_WHEEL_UP = hash("VANTAGE_WHEEL_UP")
M.VANTAGE_WHEEL_DOWN = hash("VANTAGE_WHEEL_DOWN")
M.VANTAGE_TOUCH_MULTI = hash("VANTAGE_TOUCH_MULTI")

M.VANTAGE_TYPE_WASD = 0
M.VANTAGE_TYPE_ORBITAL = 1

local function move_forward(camera)
M.WINDOW_MODE_PORTRAIT = 0
M.WINDOW_MODE_LANDSCAPE = 1

local function move_z(camera, dir)
camera.forward = camera.forward + dir * camera.movement_speed
end

local function move_x(camera, dir)
camera.sidestep = camera.sidestep + dir * camera.movement_speed
end

local function move_forward(camera, value)
camera.forward = camera.forward - camera.movement_speed
end

Expand Down Expand Up @@ -72,22 +84,48 @@ M.wasd = {
self.forward = 0
self.sidestep = 0
self.up = 0

self.window_width, self.window_height = window.get_size()
if self.window_width > self.window_height then
self.window_mode = M.WINDOW_MODE_LANDSCAPE
else
self.window_mode = M.WINDOW_MODE_PORTRAIT
end
end,
on_input = function(self, action_id, action)
if action_id == hash(M.VANTAGE_W) then
if action_id == M.VANTAGE_W then
move_forward(self)
elseif action_id == hash(M.VANTAGE_S) then
elseif action_id == M.VANTAGE_S then
move_backward(self)
elseif action_id == hash(M.VANTAGE_A) then
elseif action_id == M.VANTAGE_A then
strafe_left(self)
elseif action_id == hash(M.VANTAGE_D) then
elseif action_id == M.VANTAGE_D then
strafe_right(self)
elseif action_id == hash(M.VANTAGE_Q) then
elseif action_id == M.VANTAGE_Q then
move_down(self)
elseif action_id == hash(M.VANTAGE_E) then
elseif action_id == M.VANTAGE_E then
move_up(self)
elseif action_id == hash(M.VANTAGE_MOUSE_1) then
if not action.pressed then
elseif action_id == M.VANTAGE_TOUCH_MULTI then
for k, v in pairs(action.touch) do
if self.use_touch_controls then
if not action.pressed then
local x_scaled = v.x * self.window_scale
local y_scaled = v.y * self.window_scale
if x_scaled < self.window_width / 2 then
if v.dy ~= 0 then
move_z(self, -v.dy)
end
if v.dx ~= 0 then
move_x(self, v.dx)
end
else
rotate(self, v.dx, v.dy)
end
end
end
end
elseif action_id == M.VANTAGE_MOUSE_1 then
if not self.use_touch_controls then
rotate(self, action.dx, action.dy)
end
end
Expand All @@ -104,7 +142,9 @@ M.orbital = {
end,
on_input = function(self, action_id, action)
if action_id == hash(M.VANTAGE_MOUSE_1) then
rotate(self, action.dx, action.dy)
if not action.pressed then
rotate(self, action.dx, action.dy)
end
elseif action_id == hash(M.VANTAGE_WHEEL_UP) then
self.zoom_offset = self.zoom_offset - self.movement_speed
elseif action_id == hash(M.VANTAGE_WHEEL_DOWN) then
Expand Down

0 comments on commit d1272c7

Please sign in to comment.