hx3d  1
2D/3D Simple Game Framework
sdl2_events.cpp
1 /*
2  SDL2 Events Implementation.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/window/sdl2/sdl2_events.hpp"
22 
23 #include "hx3d/core/core.hpp"
24 #include "hx3d/window/application.hpp"
25 #include "hx3d/window/input_handler.hpp"
26 
27 #include <SDL2/SDL_events.h>
28 
29 namespace hx3d {
30 namespace window {
31 
33  SDL_Event event;
34 
35  while (SDL_PollEvent(&event)) {
36  if (event.type == SDL_WINDOWEVENT) {
37  WindowEvent::Type type(WindowEvent::Type::None);
38 
39  switch (event.window.event) {
40  case SDL_WINDOWEVENT_CLOSE:
42  break;
43  case SDL_WINDOWEVENT_SHOWN:
45  break;
46  case SDL_WINDOWEVENT_MOVED:
48  break;
49  }
50 
51  _windowEvents[static_cast<unsigned int>(type)] = true;
52  if (_currentHandler) {
54  }
55  }
56 
57  else if (event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP) {
59  MouseButtonEvent::Button key(MouseButtonEvent::Button::None);
60 
61  switch (event.button.button) {
62  case SDL_BUTTON_LEFT:
64  break;
65  case SDL_BUTTON_RIGHT:
67  break;
68  case SDL_BUTTON_MIDDLE:
70  break;
71  case SDL_BUTTON_X1:
73  break;
74  case SDL_BUTTON_X2:
76  break;
77  }
78 
79  _mousePosition = glm::vec2(event.button.x, event.button.y);
80 
81  unsigned int position = static_cast<unsigned int>(key);
82  if (type == MouseButtonEvent::Type::Released) {
83  _mouseButtonReleased[position] = true;
84  _mouseButtonClicked[position] = false;
85 
86  if (_currentHandler) {
88  }
89 
90  } else {
91  _mouseButtonClicked[position] = true;
92  _mouseButtonReleased[position] = false;
93 
94  if (_currentHandler) {
96  }
97  }
98 
99  if (_touchSimulation) {
100  _touchPosition = glm::vec2(_mousePosition.x / Core::App()->getWidth(),
102  _touchMovement = glm::vec2(0);
103  _touchPressure = 1.f;
104 
105  if (type == MouseButtonEvent::Type::Released) {
106  _screenTouched = false;
107  _screenReleased = true;
108 
109  if (_currentHandler) {
111  }
112 
113  } else {
114  _screenReleased = false;
115  _screenTouched = true;
116 
117  if (_currentHandler) {
119  }
120  }
121  }
122  }
123 
124  else if (event.type == SDL_MOUSEMOTION) {
125  _mousePosition = glm::vec2(event.motion.x, event.motion.y);
126  _mouseMovement = glm::vec2(event.motion.xrel, event.motion.yrel);
127 
128  if (_currentHandler) {
130  }
131 
132  if (_touchSimulation) {
135 
136  if (_currentHandler) {
138  }
139  }
140  }
141 
142  else if (event.type == SDL_MOUSEWHEEL) {
143  MouseWheelEvent::Direction direction(MouseWheelEvent::Direction::None);
144 
145  if (event.wheel.x > 0)
147  else if (event.wheel.x < 0)
149  else if (event.wheel.y > 0)
150  direction = MouseWheelEvent::Direction::Up;
151  else if (event.wheel.y < 0)
153 
154  unsigned int position = static_cast<unsigned int>(direction);
155  _mouseWheelTurned[position] = true;
156 
157  _mouseWheelMovement = glm::vec2(event.wheel.x, event.wheel.y);
158 
159  if (_currentHandler) {
161  }
162  }
163 
164  else if (event.type == SDL_FINGERUP || event.type == SDL_FINGERDOWN) {
165  TouchEvent::Type type(event.type == SDL_FINGERDOWN ? TouchEvent::Type::Touched : TouchEvent::Type::Released);
166 
167  _touchPosition = glm::vec2(event.tfinger.x, event.tfinger.y);
168  _touchPressure = event.tfinger.pressure;
169 
170  if (type == TouchEvent::Type::Released) {
171 
172  if (_screenReleased)
173  return;
174 
175  _touchMovement = glm::vec2(0);
176 
177  _screenTouched = false;
178  _screenReleased = true;
179 
180  if (_currentHandler) {
182  }
183 
184  } else {
185 
186  if (_screenTouched)
187  return;
188 
189  _touchMovement = glm::vec2(0);
190 
191  _screenReleased = false;
192  _screenTouched = true;
193 
194  if (_currentHandler) {
196  }
197  }
198  }
199 
200  else if (event.type == SDL_FINGERMOTION) {
201  _touchPosition = glm::vec2(event.tfinger.x, event.tfinger.y);
202  _touchMovement = glm::vec2(event.tfinger.dx, event.tfinger.dy) * 300.f;
203 
204  _touchPressure = event.tfinger.pressure;
205 
206  if (_currentHandler) {
208  }
209  }
210 
211  else if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) {
212  KeyEvent::Type type(event.type == SDL_KEYDOWN ? KeyEvent::Type::Pressed : KeyEvent::Type::Released);
213  KeyEvent::Key key(KeyEvent::Key::None);
214 
215  if (event.key.repeat) {
216  continue;
217  }
218 
219  switch (event.key.keysym.sym) {
220  case SDLK_a:
221  key = KeyEvent::Key::A;
222  break;
223  case SDLK_b:
224  key = KeyEvent::Key::B;
225  break;
226  case SDLK_c:
227  key = KeyEvent::Key::C;
228  break;
229  case SDLK_d:
230  key = KeyEvent::Key::D;
231  break;
232  case SDLK_e:
233  key = KeyEvent::Key::E;
234  break;
235  case SDLK_q:
236  key = KeyEvent::Key::Q;
237  break;
238  case SDLK_s:
239  key = KeyEvent::Key::S;
240  break;
241  case SDLK_z:
242  key = KeyEvent::Key::Z;
243  break;
244  case SDLK_SPACE:
245  key = KeyEvent::Key::Space;
246  break;
247  case SDLK_ESCAPE:
248  key = KeyEvent::Key::Escape;
249  break;
250  case SDLK_RETURN:
251  key = KeyEvent::Key::Return;
252  break;
253  case SDLK_AC_BACK:
254  key = KeyEvent::Key::AndroidBack;
255  break;
256  case SDLK_MENU:
257  key = KeyEvent::Key::AndroidMenu;
258  break;
259  default:
260  break;
261  }
262 
263  unsigned int position = static_cast<unsigned int>(key);
264 
265  if (type == KeyEvent::Type::Released) {
266  _keysReleased[position] = true;
267  _keysPressed[position] = false;
268 
269  if (_currentHandler) {
271  }
272 
273  } else {
274  _keysPressed[position] = true;
275  _keysReleased[position] = false;
276 
277  if (_currentHandler) {
279  }
280  }
281  }
282  }
283 }
284 
285 } /* window */
286 } /* hx3d */
float _touchPressure
Touch pressure.
Type
Key event type.
Definition: events.hpp:40
bool * _keysReleased
Keys released.
glm::vec2 _mouseMovement
Mouse movement.
virtual void onMouseWheel(MouseWheelEvent::Direction direction, glm::vec2 wheelMovement)
When a mouse wheel event occur.
virtual void onKeyReleased(KeyEvent::Key key)
When a key is released.
bool * _mouseButtonReleased
Mouse buttons released.
bool _touchSimulation
Is touch simulated with mouse ?
virtual void onMouseReleased(MouseButtonEvent::Button button, glm::vec2 mousePosition)
When a mouse click release event occur.
virtual void onMouseMotion(glm::vec2 mousePosition, glm::vec2 mouseMovement)
When a mouse move event occur.
static window::Application * App()
Get the application instance.
Definition: core.cpp:78
hx3d framework namespace
Definition: audio.hpp:26
virtual void onKeyPressed(KeyEvent::Key key)
When a key is pressed.
virtual void onMouseClicked(MouseButtonEvent::Button button, glm::vec2 mousePosition)
When a mouse click event occur.
glm::vec2 _mousePosition
Mouse position.
InputHandler * _currentHandler
Current input handler.
bool * _mouseButtonClicked
Mouse buttons clicked.
bool * _keysPressed
Keys pressed.
virtual void onTouchUp(glm::vec2 touchPosition, float touchPressure)
When a touch up event occur.
Button
Mouse button value.
Definition: events.hpp:101
bool _screenTouched
Screen touched ?
Direction
Mouse wheel direction.
Definition: events.hpp:124
glm::vec2 _mouseWheelMovement
Mouse wheel movement.
virtual void onTouchDown(glm::vec2 touchPosition, float touchPressure)
When a touch down event occur.
Type
Window event type.
Definition: events.hpp:71
bool * _mouseWheelTurned
Mouse wheels turned.
virtual void poll() override
Poll the event queue.
Definition: sdl2_events.cpp:32
bool _screenReleased
Screen released ?
bool * _windowEvents
Window events.
virtual void onTouchMotion(glm::vec2 touchPosition, glm::vec2 touchMovement, float touchPressure)
When a touch move event occur.
glm::vec2 _touchPosition
Touch position.
int getHeight()
Get the window height.
Definition: application.cpp:39
Type
Mouse action type.
Definition: events.hpp:90
glm::vec2 _touchMovement
Touch movement.
int getWidth()
Get the window width.
Definition: application.cpp:35
virtual void onWindowEvent(WindowEvent::Type type)
When a window event occur.