Skip to content

Commit

Permalink
Core: Allow moving the camera target via arrow keys
Browse files Browse the repository at this point in the history
It's not as versatile as a full panning mode, but also much simpler.
  • Loading branch information
rdw-software committed Nov 23, 2023
1 parent b82a828 commit 5f0c05c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
1 change: 1 addition & 0 deletions Core/NativeClient/C_Camera.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ local C_Camera = {
MIN_ORBIT_DISTANCE = 45,
MAX_ORBIT_DISTANCE = 80,
targetWorldPosition = Vector3D(0, 0, 0),
TARGET_DEBUG_STEPSIZE_IN_WORLD_UNITS = 12,
}

function C_Camera.CreatePerspectiveProjection(verticalFieldOfViewInDegrees, aspectRatio, zNearDistance, zFarDistance)
Expand Down
35 changes: 34 additions & 1 deletion Core/NativeClient/NativeClient.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local bit = require("bit")
local ffi = require("ffi")
local glfw = require("glfw")
local interop = require("interop")
Expand All @@ -6,6 +7,7 @@ local uv = require("uv")
local C_Camera = require("Core.NativeClient.C_Camera")
local C_Cursor = require("Core.NativeClient.C_Cursor")
local Renderer = require("Core.NativeClient.Renderer")
local Vector3D = require("Core.VectorMath.Vector3D")

local Box = require("Core.NativeClient.DebugDraw.Box")
local Cone = require("Core.NativeClient.DebugDraw.Cone")
Expand Down Expand Up @@ -261,7 +263,38 @@ function NativeClient:SCROLL_STATUS_CHANGED(eventID, payload)
end

function NativeClient:KEYPRESS_STATUS_CHANGED(eventID, payload)
print("KEYPRESS_STATUS_CHANGED")
local GLFW_KEY_LEFT = glfw.bindings.glfw_find_constant("GLFW_KEY_LEFT")
local GLFW_KEY_RIGHT = glfw.bindings.glfw_find_constant("GLFW_KEY_RIGHT")
local GLFW_KEY_DOWN = glfw.bindings.glfw_find_constant("GLFW_KEY_DOWN")
local GLFW_KEY_UP = glfw.bindings.glfw_find_constant("GLFW_KEY_UP")
local GLFW_MOD_SHIFT = glfw.bindings.glfw_find_constant("GLFW_MOD_SHIFT")
local GLFW_PRESS = glfw.bindings.glfw_find_constant("GLFW_PRESS")
local wasKeyPressed = tonumber(payload.key_details.action) == GLFW_PRESS
if not wasKeyPressed then
return
end

local isModifiedBySHIFT = bit.band(payload.key_details.mods, GLFW_MOD_SHIFT) == 1
if not isModifiedBySHIFT then
return
end

local wasLeftKey = payload.key_details.key == GLFW_KEY_LEFT
local wasRightKey = payload.key_details.key == GLFW_KEY_RIGHT
local wasUpKey = payload.key_details.key == GLFW_KEY_UP
local wasDownKey = payload.key_details.key == GLFW_KEY_DOWN
local movementDirectionX = wasLeftKey and -1 or 0
movementDirectionX = wasRightKey and 1 or movementDirectionX
local movementDirectionZ = wasUpKey and 1 or 0
movementDirectionZ = wasDownKey and -1 or movementDirectionZ

local movementDistanceInWorldUnits = C_Camera.TARGET_DEBUG_STEPSIZE_IN_WORLD_UNITS
local translation = Vector3D(
movementDirectionX * movementDistanceInWorldUnits,
0,
movementDirectionZ * movementDistanceInWorldUnits
)
C_Camera.targetWorldPosition = C_Camera.targetWorldPosition:Add(translation)
end

function NativeClient:UNICODE_INPUT_RECEIVED(eventID, payload)
Expand Down
76 changes: 76 additions & 0 deletions Tests/NativeClient/NativeClient.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local glfw = require("glfw")
local C_Camera = require("Core.NativeClient.C_Camera")
local C_Cursor = require("Core.NativeClient.C_Cursor")
local NativeClient = require("Core.NativeClient.NativeClient")
local Vector3D = require("Core.VectorMath.Vector3D")

describe("NativeClient", function()
describe("CURSOR_MOVED", function()
Expand Down Expand Up @@ -479,4 +480,79 @@ describe("NativeClient", function()
end
)
end)

describe("KEYPRESS_STATUS_CHANGED", function()
local originalCameraTarget = C_Camera.GetTargetPosition()
after(function()
C_Camera.SetTargetPosition(originalCameraTarget)
end)

it("should adjust the camera target if SHIFT + LEFT was pressed", function()
local event = ffi.new("deferred_event_t")
event.key_details.key = glfw.bindings.glfw_find_constant("GLFW_KEY_LEFT")
event.key_details.action = glfw.bindings.glfw_find_constant("GLFW_PRESS")
event.key_details.mods = glfw.bindings.glfw_find_constant("GLFW_MOD_SHIFT")

NativeClient:KEYPRESS_STATUS_CHANGED("KEYPRESS_STATUS_CHANGED", event)

local newCameraTarget = C_Camera.GetTargetPosition()
local expectedTranslation = Vector3D(-C_Camera.TARGET_DEBUG_STEPSIZE_IN_WORLD_UNITS, 0, 0)
local expectedCameraTarget = originalCameraTarget:Add(expectedTranslation)

assertEquals(newCameraTarget.x, expectedCameraTarget.x)
assertEquals(newCameraTarget.y, expectedCameraTarget.y)
assertEquals(newCameraTarget.z, expectedCameraTarget.z)
end)

it("should adjust the camera target if SHIFT + RIGHT was pressed", function()
local event = ffi.new("deferred_event_t")
event.key_details.key = glfw.bindings.glfw_find_constant("GLFW_KEY_RIGHT")
event.key_details.action = glfw.bindings.glfw_find_constant("GLFW_PRESS")
event.key_details.mods = glfw.bindings.glfw_find_constant("GLFW_MOD_SHIFT")

NativeClient:KEYPRESS_STATUS_CHANGED("KEYPRESS_STATUS_CHANGED", event)

local newCameraTarget = C_Camera.GetTargetPosition()
local expectedTranslation = Vector3D(C_Camera.TARGET_DEBUG_STEPSIZE_IN_WORLD_UNITS, 0, 0)
local expectedCameraTarget = originalCameraTarget:Add(expectedTranslation)

assertEquals(newCameraTarget.x, expectedCameraTarget.x)
assertEquals(newCameraTarget.y, expectedCameraTarget.y)
assertEquals(newCameraTarget.z, expectedCameraTarget.z)
end)

it("should adjust the camera target if SHIFT + UP was pressed", function()
local event = ffi.new("deferred_event_t")
event.key_details.key = glfw.bindings.glfw_find_constant("GLFW_KEY_UP")
event.key_details.action = glfw.bindings.glfw_find_constant("GLFW_PRESS")
event.key_details.mods = glfw.bindings.glfw_find_constant("GLFW_MOD_SHIFT")

NativeClient:KEYPRESS_STATUS_CHANGED("KEYPRESS_STATUS_CHANGED", event)

local newCameraTarget = C_Camera.GetTargetPosition()
local expectedTranslation = Vector3D(0, 0, C_Camera.TARGET_DEBUG_STEPSIZE_IN_WORLD_UNITS)
local expectedCameraTarget = originalCameraTarget:Add(expectedTranslation)

assertEquals(newCameraTarget.x, expectedCameraTarget.x)
assertEquals(newCameraTarget.y, expectedCameraTarget.y)
assertEquals(newCameraTarget.z, expectedCameraTarget.z)
end)

it("should adjust the camera target if SHIFT + DOWN was pressed", function()
local event = ffi.new("deferred_event_t")
event.key_details.key = glfw.bindings.glfw_find_constant("GLFW_KEY_DOWN")
event.key_details.action = glfw.bindings.glfw_find_constant("GLFW_PRESS")
event.key_details.mods = glfw.bindings.glfw_find_constant("GLFW_MOD_SHIFT")

NativeClient:KEYPRESS_STATUS_CHANGED("KEYPRESS_STATUS_CHANGED", event)

local newCameraTarget = C_Camera.GetTargetPosition()
local expectedTranslation = Vector3D(0, 0, -C_Camera.TARGET_DEBUG_STEPSIZE_IN_WORLD_UNITS)
local expectedCameraTarget = originalCameraTarget:Add(expectedTranslation)

assertEquals(newCameraTarget.x, expectedCameraTarget.x)
assertEquals(newCameraTarget.y, expectedCameraTarget.y)
assertEquals(newCameraTarget.z, expectedCameraTarget.z)
end)
end)
end)

0 comments on commit 5f0c05c

Please sign in to comment.