summaryrefslogtreecommitdiff
path: root/ui/manualCameraController.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-12-13 23:47:30 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-12-13 23:47:30 +0000
commit1686a01b6ae7467e71eac247078248de4a3b3423 (patch)
tree53716ce767b1b775dc06f658a41a647bddbbeac1 /ui/manualCameraController.cpp
parentMove TickDuration to its own files (diff)
downloadilt-1686a01b6ae7467e71eac247078248de4a3b3423.tar.bz2
ilt-1686a01b6ae7467e71eac247078248de4a3b3423.tar.xz
ilt-1686a01b6ae7467e71eac247078248de4a3b3423.zip
Refactor to start splitting out UI components
Diffstat (limited to 'ui/manualCameraController.cpp')
-rw-r--r--ui/manualCameraController.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/ui/manualCameraController.cpp b/ui/manualCameraController.cpp
new file mode 100644
index 0000000..1d3a067
--- /dev/null
+++ b/ui/manualCameraController.cpp
@@ -0,0 +1,67 @@
+#include "manualCameraController.h"
+#include <algorithm>
+#include <cmath>
+#include <gfx/gl/camera.h>
+#include <maths.h>
+
+bool
+ManualCameraController::handleInput(const SDL_Event & e)
+{
+ switch (e.type) {
+ case SDL_KEYDOWN:
+ switch (e.key.keysym.sym) {
+ case SDLK_LCTRL:
+ case SDLK_RCTRL:
+ ctrl = true;
+ return true;
+ }
+ break;
+ case SDL_KEYUP:
+ switch (e.key.keysym.sym) {
+ case SDLK_LCTRL:
+ case SDLK_RCTRL:
+ ctrl = false;
+ return true;
+ }
+ break;
+ case SDL_MOUSEBUTTONDOWN:
+ switch (e.button.button) {
+ case SDL_BUTTON_RIGHT:
+ SDL_SetRelativeMouseMode(SDL_TRUE);
+ mrb = true;
+ return true;
+ }
+ break;
+ case SDL_MOUSEBUTTONUP:
+ switch (e.button.button) {
+ case SDL_BUTTON_RIGHT:
+ SDL_SetRelativeMouseMode(SDL_FALSE);
+ mrb = false;
+ return true;
+ }
+ break;
+ case SDL_MOUSEMOTION:
+ if (mrb) {
+ if (ctrl) {
+ direction -= 0.01F * static_cast<float>(e.motion.xrel);
+ pitch = std::clamp(pitch - 0.01F * static_cast<float>(e.motion.yrel), 0.1F, half_pi);
+ }
+ else {
+ focus += rotate_flat(-direction) * glm::vec2 {-e.motion.xrel, e.motion.yrel};
+ }
+ }
+ return true;
+ case SDL_MOUSEWHEEL:
+ dist = std::clamp(dist - static_cast<float>(e.wheel.y) * 4.F, 5.F, 200.F);
+ break;
+ }
+ return false;
+}
+
+void
+ManualCameraController::updateCamera(Camera * camera) const
+{
+ camera->forward = glm::normalize(sincosf(direction) ^ -sin(pitch));
+ camera->pos = !focus + up * 3.F - (camera->forward * std::pow(dist, 1.3F));
+ camera->up = up;
+}