-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.h
103 lines (84 loc) · 3.3 KB
/
controller.h
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
This file is part of OpenGL-StarterPack (GLSP), an open source OpenGL based framework
that facilitates and speeds up demo and project creation by offering an abstraction to
the basic objects of OpenGL as well as incluing the necessary libraries.
MIT License
Copyright (c) 2024 Antonio Espinosa Garcia
*/
#ifndef __CONTROLLER_
#define __CONTROLLER_
#include <GLSP/core.h>
#include <GLSP/object3D.h>
GLSP_NAMESPACE_BEGIN
struct KeyMappings
{
int moveLeft = GLFW_KEY_A;
int moveRight = GLFW_KEY_D;
int moveForward = GLFW_KEY_W;
int moveBackward = GLFW_KEY_S;
int moveUp = GLFW_KEY_E;
int moveDown = GLFW_KEY_Q;
int mouseLeft = GLFW_MOUSE_BUTTON_1;
int mouseMiddle = GLFW_MOUSE_BUTTON_2;
int mouseRight = GLFW_MOUSE_BUTTON_3;
int reset = GLFW_KEY_R;
};
typedef enum ControllerMovementType
{
WASD,
ORBITAL
} ControllerMovementType;
class Controller
{
protected:
Object3D *m_objPtr;
float m_speed;
ControllerMovementType m_type;
KeyMappings m_mappings;
enum KeyActions
{
PRESS = GLFW_PRESS,
RELEASE = GLFW_RELEASE,
REPEAT = GLFW_REPEAT,
};
// MOUSE
float m_mouseSensitivity;
float m_mouseDeltaX;
float m_mouseDeltaY;
float m_mouseLastX;
float m_mouseLastY;
bool m_firstMouse;
bool m_isMouseLeftPressed;
bool m_isMouseMiddlePressed;
bool m_isMouseRightPressed;
glm::vec3 m_orbitalCenter;
Transform m_initialState;
bool m_enabled{true};
public:
Controller(Object3D *obj, ControllerMovementType m = WASD, KeyMappings km = KeyMappings{}) : m_objPtr(obj), m_type(m), m_speed(5.0f),
m_mouseSensitivity(0.4f), m_mouseDeltaX(.0f), m_mouseDeltaY(.0f),
m_mouseLastX(.0f), m_mouseLastY(0.0f), m_firstMouse(true),
m_isMouseLeftPressed(false), m_isMouseMiddlePressed(false), m_isMouseRightPressed(false),
m_initialState(obj->get_transform()), m_mappings(km),
m_orbitalCenter({0.0, 0.0, 0.0}) {}
virtual inline void set_active(const bool s)
{
m_enabled = s;
}
virtual inline bool is_active() const { return m_enabled; }
inline ControllerMovementType get_type() { return m_type; }
inline float get_speed() const { return m_speed; }
inline void set_speed(float s) { m_speed = s; }
inline float get_mouse_sensitivity() const { return m_mouseSensitivity; }
inline void set_mouse_sensitivity(float s) { m_mouseSensitivity = s; }
inline Object3D *get_object() const { return m_objPtr; }
inline void set_object(Object3D *obj) { m_objPtr = obj; }
/*Not insert as GLFW callback!*/
virtual void handle_keyboard(GLFWwindow *window, int key, int action, const float deltaTime);
virtual void handle_mouse(GLFWwindow *window, float xpos, float ypos, bool constrainPitch = true);
virtual void handle_mouse_scroll()
{ /*WIP*/
}
};
GLSP_NAMESPACE_END
#endif