summaryrefslogtreecommitdiff
path: root/ui/manualCameraController.cpp
blob: 53905ebda297bdb2bb9d466197f08331ac12e14b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "manualCameraController.h"
#include <algorithm>
#include <cmath>
#include <gfx/gl/camera.h>
#include <maths.h>

bool
ManualCameraController::handleInput(const SDL_Event & e, const Position &)
{
	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;
				case SDLK_KP_8:
					direction = 0;
					break;
				case SDLK_KP_4:
					direction = -half_pi;
					break;
				case SDLK_KP_6:
					direction = half_pi;
					break;
				case SDLK_KP_2:
					direction = pi;
					break;
			}
			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)
							* (RelativePosition2D {-e.motion.xrel, e.motion.yrel} * dist / 2.0F);
				}
			}
			return true;
		case SDL_MOUSEWHEEL:
			dist = std::clamp(dist - static_cast<float>(e.wheel.y) * 400.F, 5.F, 200000.F);
			break;
	}
	return false;
}

void
ManualCameraController::render(const UIShader &, const Position &) const
{
}

void
ManualCameraController::updateCamera(Camera * camera) const
{
	const auto forward = glm::normalize(sincos(direction) || -sin(pitch));
	camera->setView((focus || 0) - (forward * 3.F * std::pow(dist, 1.3F)), forward);
}