TDME2 1.9.121
CameraRotationInputHandler.cpp
Go to the documentation of this file.
2
3#include <tdme/tdme.h>
11#include <tdme/gui/GUI.h>
12#include <tdme/math/Matrix4x4.h>
14#include <tdme/math/Vector3.h>
17
23using tdme::gui::GUI;
30
31CameraRotationInputHandler::CameraRotationInputHandler(Engine* engine, CameraRotationInputHandlerEventHandler* eventHandler)
32{
33 this->engine = engine;
34 this->eventHandler = eventHandler;
35 keyLeft = false;
36 keyRight = false;
37 keyUp = false;
38 keyDown = false;
39 keyPeriod = false;
40 keyComma = false;
41 keyPlus = false;
42 keyMinus = false;
43 keyR = false;
44 mouseDragging = false;
45 defaultScale = 1.0f;
46 scale = 1.0f;
47 lookFromRotations.addRotation(Vector3(0.0f, 1.0f, 0.0f), -45.0f);
48 lookFromRotations.addRotation(Vector3(1.0f, 0.0f, 0.0f), -45.0f);
49 lookFromRotations.addRotation(Vector3(0.0f, 0.0f, 1.0f), 0.0f);
51 resetRequested = false;
52 mouseLastX = 0;
53 mouseLastY = 0;
54}
55
57}
58
60{
61 return lookFromRotations;
62}
63
65{
66 resetRequested = true;
67}
68
70{
71 auto scaling = false;
72 for (auto i = 0; i < engine->getGUI()->getMouseEvents().size(); i++) {
73 auto& event = engine->getGUI()->getMouseEvents()[i];
74 if (event.isProcessed() == true) continue;
75 if (mouseDragging == true) {
76 if (event.getButton() == MOUSE_BUTTON_MIDDLE) {
77 auto xMoved = (event.getX() - mouseLastX) / 5.0f;
78 auto yMoved = (event.getY() - mouseLastY) / 5.0f;
79 mouseLastX = event.getX();
80 mouseLastY = event.getY();
81 auto& xRotation = lookFromRotations.getRotation(0);
82 auto& yRotation = lookFromRotations.getRotation(1);
83 auto xRotationAngle = xRotation.getAngle() + xMoved;
84 auto yRotationAngle = yRotation.getAngle() + yMoved;
85 xRotation.setAngle(xRotationAngle);
86 yRotation.setAngle(yRotationAngle);
88 } else {
89 mouseDragging = false;
90 }
91 } else {
92 if (event.getButton() == MOUSE_BUTTON_MIDDLE) {
93 mouseDragging = true;
94 mouseLastX = event.getX();
95 mouseLastY = event.getY();
96 }
97 }
98 auto mouseWheel = event.getWheelY();
99 if (mouseWheel != 0) {
100 scale += -mouseWheel * 0.1f;
101 if (scale < 0.0001f) scale = 0.0001f;
102 scaling = true;
103 }
104 }
105 for (auto i = 0; i < engine->getGUI()->getKeyboardEvents().size(); i++) {
106 auto& event = engine->getGUI()->getKeyboardEvents()[i];
107 if (event.isProcessed() == true) continue;
108 if (event.getType() == GUIKeyboardEvent::KEYBOARDEVENT_KEY_TYPED) continue;
109 auto isKeyDown = event.getType() == GUIKeyboardEvent::KEYBOARDEVENT_KEY_PRESSED;
110 if (event.getKeyCode() == GUIKeyboardEvent::KEYCODE_LEFT)
111 keyLeft = isKeyDown;
112
113 if (event.getKeyCode() == GUIKeyboardEvent::KEYCODE_RIGHT)
114 keyRight = isKeyDown;
115
116 if (event.getKeyCode() == GUIKeyboardEvent::KEYCODE_UP)
117 keyUp = isKeyDown;
118
119 if (event.getKeyCode() == GUIKeyboardEvent::KEYCODE_DOWN)
120 keyDown = isKeyDown;
121
122 if (Character::toLowerCase(event.getKeyChar()) == '.')
123 keyPeriod = isKeyDown;
124
125 if (Character::toLowerCase(event.getKeyChar()) == ',')
126 keyComma = isKeyDown;
127
128 if (Character::toLowerCase(event.getKeyChar()) == '+')
129 keyPlus = isKeyDown;
130
131 if (Character::toLowerCase(event.getKeyChar()) == '-')
132 keyMinus = isKeyDown;
133
134 if (Character::toLowerCase(event.getKeyChar()) == 'r')
135 keyR = isKeyDown;
136
137 }
138
139 auto entity = engine->getEntity("model");
140
141 auto& rotationX = lookFromRotations.getRotation(0);
142 auto& rotationY = lookFromRotations.getRotation(1);
143 auto& rotationZ = lookFromRotations.getRotation(2);
144
145 if (keyLeft == true) rotationX.setAngle(rotationX.getAngle() - 1.0f);
146 if (keyRight == true) rotationX.setAngle(rotationX.getAngle() + 1.0f);
147 if (keyUp == true) rotationY.setAngle(rotationY.getAngle() + 1.0f);
148 if (keyDown == true) rotationY.setAngle(rotationY.getAngle() - 1.0f);
149 if (keyComma == true) rotationZ.setAngle(rotationZ.getAngle() - 1.0f);
150 if (keyPeriod == true) rotationZ.setAngle(rotationZ.getAngle() + 1.0f);
151
152 if (keyMinus == true) {
153 scaling = true;
154 scale += 0.05f;
155 }
156 if (keyPlus == true) {
157 scaling = true;
158 scale -= 0.05f;
159 if (scale < 0.0001f) scale = 0.0001f;
160 }
161
162 if (keyR == true || resetRequested == true) {
163 rotationY.setAngle(-45.0f);
164 rotationZ.setAngle(0.0f);
166 }
167 if (keyLeft == true ||
168 keyRight == true ||
169 keyUp == true ||
170 keyDown == true ||
171 keyComma == true ||
172 keyPeriod == true ||
173 keyR == true ||
176 if (entity != nullptr) {
177 boundingBoxTransformed = *entity->getBoundingBoxTransformed();
178 }
179 }
180 auto cam = engine->getCamera();
181 auto lookAt = cam->getLookAt();
182 if (resetRequested == true) {
183 if (entity != nullptr) {
184 auto entityBoundingBoxTransformed = entity->getBoundingBoxTransformed();
185 for (auto i = 0; i < 3; i++) {
186 if (entityBoundingBoxTransformed->getMin()[i] < boundingBoxTransformed.getMin()[i]) boundingBoxTransformed.getMin()[i] = entityBoundingBoxTransformed->getMin()[i];
187 if (entityBoundingBoxTransformed->getMax()[i] > boundingBoxTransformed.getMax()[i]) boundingBoxTransformed.getMax()[i] = entityBoundingBoxTransformed->getMax()[i];
188 }
190 lookAt.set(boundingBoxTransformed.getCenter());
191 } else {
192 lookAt.set(0.0f, 0.0f, 0.0f);
193 }
194 resetRequested = false;
195 }
196 Vector3 forwardVector(0.0f, 0.0f, 1.0f);
197 auto forwardVectorTransformed = lookFromRotations.getTransformationsMatrix().multiply(forwardVector).scale(scale);
198 auto upVector = lookFromRotations.getRotation(2).getQuaternion().multiply(Vector3(0.0f, 1.0f, 0.0f)).normalize();
199 auto lookFrom = lookAt.clone().add(forwardVectorTransformed);
200 cam->setLookFrom(lookFrom);
201 cam->setLookAt(lookAt);
202 cam->setUpVector(upVector);
203
204 if (scaling == true && eventHandler != nullptr) eventHandler->onCameraScale();
205 if ((keyLeft == true ||
206 keyRight == true ||
207 keyUp == true ||
208 keyDown == true ||
209 keyComma == true ||
210 keyPeriod == true ||
211 keyR == true ||
212 resetRequested == true) &&
213 eventHandler != nullptr) {
214 if (eventHandler != nullptr) eventHandler->onCameraRotation();
215 }
216}
#define MOUSE_BUTTON_MIDDLE
const Vector3 & getLookAt() const
Definition: Camera.h:228
Engine main class.
Definition: Engine.h:122
Entity * getEntity(const string &id)
Returns a entity by given id.
Definition: Engine.h:981
Camera * getCamera()
Definition: Engine.h:907
void setAngle(const float angle)
Definition: Rotation.h:63
const Quaternion & getQuaternion() const
Definition: Rotation.h:85
const float getAngle() const
Definition: Rotation.h:56
Transformations which contain scale, rotations and translation.
const Matrix4x4 & getTransformationsMatrix() const
Rotation & getRotation(const int idx)
Get rotation at given index.
virtual void update()
Computes transformation matrix.
void addRotation(const Vector3 &axis, const float angle)
Add rotation.
const Vector3 & getCenter() const
Definition: BoundingBox.h:120
void update()
Updates this bounding box.
GUI module class.
Definition: GUI.h:66
vector< GUIMouseEvent > & getMouseEvents()
Definition: GUI.h:212
vector< GUIKeyboardEvent > & getKeyboardEvents()
Definition: GUI.h:219
4x4 3D Matrix class
Definition: Matrix4x4.h:24
Vector3 multiply(const Vector3 &v) const
Multiplies a vector3 with this matrix into destination vector.
Definition: Matrix4x4.h:351
Quaternion class.
Definition: Quaternion.h:22
Quaternion & multiply(const Quaternion q)
Multiplies this quaternion with quaternion q.
Definition: Quaternion.h:227
Quaternion & normalize()
Normalize quaternion.
Definition: Quaternion.h:213
Quaternion clone() const
Clones the quaternion.
Definition: Quaternion.h:477
Quaternion & add(const Quaternion &q)
Adds given quaternion q to this quaternion.
Definition: Quaternion.h:242
3D vector 3 class
Definition: Vector3.h:22
Vector3 & scale(float scale)
Scale this vector.
Definition: Vector3.h:349
void handleInputEvents() override
Handle input events that have not yet been processed.
Character class.
Definition: Character.h:15
virtual void onCameraRotation()=0
On rotation event to be overloaded.
virtual void onCameraScale()=0
On scale event to be overloaded.