feat: add scene view fly controls
This commit is contained in:
@@ -33,9 +33,14 @@ struct SceneViewportInput {
|
||||
ImVec2 viewportSize = ImVec2(0.0f, 0.0f);
|
||||
ImVec2 mouseDelta = ImVec2(0.0f, 0.0f);
|
||||
float mouseWheel = 0.0f;
|
||||
float deltaTime = 0.0f;
|
||||
float moveForward = 0.0f;
|
||||
float moveRight = 0.0f;
|
||||
float moveUp = 0.0f;
|
||||
bool hovered = false;
|
||||
bool focused = false;
|
||||
bool looking = false;
|
||||
bool fastMove = false;
|
||||
bool orbiting = false;
|
||||
bool panning = false;
|
||||
bool focusSelectionRequested = false;
|
||||
|
||||
@@ -19,7 +19,12 @@ struct SceneViewportCameraInputState {
|
||||
float panDeltaX = 0.0f;
|
||||
float panDeltaY = 0.0f;
|
||||
float zoomDelta = 0.0f;
|
||||
float deltaTime = 0.0f;
|
||||
float moveForward = 0.0f;
|
||||
float moveRight = 0.0f;
|
||||
float moveUp = 0.0f;
|
||||
float viewportHeight = 0.0f;
|
||||
bool fastMove = false;
|
||||
};
|
||||
|
||||
class SceneViewportCameraController {
|
||||
@@ -93,6 +98,23 @@ public:
|
||||
m_position += ((right * -input.panDeltaX) + (up * input.panDeltaY)) * worldUnitsPerPixel;
|
||||
}
|
||||
|
||||
if (input.deltaTime > 0.0f &&
|
||||
(std::abs(input.moveForward) > Math::EPSILON ||
|
||||
std::abs(input.moveRight) > Math::EPSILON ||
|
||||
std::abs(input.moveUp) > Math::EPSILON)) {
|
||||
const Math::Vector3 movement =
|
||||
GetForward() * input.moveForward +
|
||||
GetRight() * input.moveRight +
|
||||
Math::Vector3::Up() * input.moveUp;
|
||||
if (movement.SqrMagnitude() > Math::EPSILON) {
|
||||
const float speedMultiplier = input.fastMove ? 4.0f : 1.0f;
|
||||
const float flySpeed = (std::max)(5.0f, m_distance * 2.0f) * speedMultiplier;
|
||||
const Math::Vector3 delta = Math::Vector3::Normalize(movement) * flySpeed * input.deltaTime;
|
||||
m_position += delta;
|
||||
m_focalPoint += delta;
|
||||
}
|
||||
}
|
||||
|
||||
if (std::abs(input.zoomDelta) > Math::EPSILON) {
|
||||
const float zoomFactor = std::pow(0.85f, input.zoomDelta);
|
||||
m_distance = std::clamp(m_distance * zoomFactor, 0.5f, 500.0f);
|
||||
@@ -111,7 +133,7 @@ public:
|
||||
private:
|
||||
void ApplyRotationDelta(float deltaX, float deltaY) {
|
||||
m_yawDegrees += deltaX * 0.30f;
|
||||
m_pitchDegrees = std::clamp(m_pitchDegrees - deltaY * 0.20f, -89.0f, 89.0f);
|
||||
m_pitchDegrees = std::clamp(m_pitchDegrees + deltaY * 0.20f, -89.0f, 89.0f);
|
||||
}
|
||||
|
||||
Math::Vector3 GetRight() const {
|
||||
|
||||
@@ -91,6 +91,11 @@ public:
|
||||
SceneViewportCameraInputState controllerInput = {};
|
||||
controllerInput.viewportHeight = input.viewportSize.y;
|
||||
controllerInput.zoomDelta = input.hovered ? input.mouseWheel : 0.0f;
|
||||
controllerInput.deltaTime = input.deltaTime;
|
||||
controllerInput.moveForward = input.moveForward;
|
||||
controllerInput.moveRight = input.moveRight;
|
||||
controllerInput.moveUp = input.moveUp;
|
||||
controllerInput.fastMove = input.fastMove;
|
||||
|
||||
if (input.looking) {
|
||||
controllerInput.lookDeltaX = input.mouseDelta.x;
|
||||
|
||||
@@ -263,15 +263,29 @@ void SceneViewPanel::Render() {
|
||||
|
||||
SceneViewportInput input = {};
|
||||
input.viewportSize = content.availableSize;
|
||||
input.deltaTime = io.DeltaTime;
|
||||
input.hovered = content.hovered;
|
||||
input.focused = content.focused;
|
||||
input.mouseWheel = content.hovered ? io.MouseWheel : 0.0f;
|
||||
input.mouseWheel = content.hovered ? -io.MouseWheel : 0.0f;
|
||||
input.looking = m_lookDragging;
|
||||
input.orbiting = m_orbitDragging;
|
||||
input.panning = m_panDragging;
|
||||
input.fastMove = io.KeyShift;
|
||||
input.focusSelectionRequested =
|
||||
content.focused && !io.WantTextInput && ImGui::IsKeyPressed(ImGuiKey_F, false);
|
||||
|
||||
if (m_lookDragging && content.focused && !io.WantTextInput) {
|
||||
input.moveForward =
|
||||
(ImGui::IsKeyDown(ImGuiKey_W) ? 1.0f : 0.0f) -
|
||||
(ImGui::IsKeyDown(ImGuiKey_S) ? 1.0f : 0.0f);
|
||||
input.moveRight =
|
||||
(ImGui::IsKeyDown(ImGuiKey_D) ? 1.0f : 0.0f) -
|
||||
(ImGui::IsKeyDown(ImGuiKey_A) ? 1.0f : 0.0f);
|
||||
input.moveUp =
|
||||
(ImGui::IsKeyDown(ImGuiKey_E) ? 1.0f : 0.0f) -
|
||||
(ImGui::IsKeyDown(ImGuiKey_Q) ? 1.0f : 0.0f);
|
||||
}
|
||||
|
||||
if (m_lookDragging || m_orbitDragging || m_panDragging) {
|
||||
input.mouseDelta = io.MouseDelta;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user