hx3d  1
2D/3D Simple Game Framework
event_manager.cpp
1 /*
2  Event manager.
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/event_manager.hpp"
22 
23 #include "hx3d/core/core.hpp"
24 #include "hx3d/window/game.hpp"
25 #include "hx3d/window/application.hpp"
26 
27 namespace hx3d {
28 namespace window {
29 
30 EventManager::EventManager() {
31  _keysPressed = new bool[static_cast<unsigned int>(KeyEvent::Key::None) + 1]{false};
32  _keysReleased = new bool[static_cast<unsigned int>(KeyEvent::Key::None) + 1]{false};
33 
34  _mouseButtonClicked = new bool[static_cast<unsigned int>(MouseButtonEvent::Button::None) + 1]{false};
35  _mouseButtonReleased = new bool[static_cast<unsigned int>(MouseButtonEvent::Button::None) + 1]{false};
36 
37  _mouseWheelTurned = new bool[static_cast<unsigned int>(MouseWheelEvent::Direction::None) + 1]{false};
38  _windowEvents = new bool[static_cast<unsigned int>(WindowEvent::Type::None) + 1]{false};
39 
40  _screenTouched = false;
41  _screenReleased = false;
42  _touchPressure = 0.f;
43 
44  _touchSimulation = false;
45  _currentHandler = nullptr;
46 }
47 
48 EventManager::~EventManager() {
49  delete[] _keysPressed;
50  delete[] _keysReleased;
51 
52  delete[] _mouseButtonClicked;
53  delete[] _mouseButtonReleased;
54 
55  delete[] _mouseWheelTurned;
56  delete[] _windowEvents;
57 }
58 
60  unsigned int position = static_cast<unsigned int>(type);
61  bool value = _windowEvents[position];
62  if (value)
63  _windowEvents[position] = false;
64 
65  return value;
66 }
67 
69  unsigned int position = static_cast<unsigned int>(button);
70  return _mouseButtonClicked[position];
71 }
72 
74  unsigned int position = static_cast<unsigned int>(button);
75  return _mouseButtonReleased[position];
76 }
77 
79  unsigned int position = static_cast<unsigned int>(button);
80  bool value = _mouseButtonClicked[position];
81  if (value)
82  _mouseButtonClicked[position] = false;
83 
84  return value;
85 }
86 
88  unsigned int position = static_cast<unsigned int>(button);
89  bool value = _mouseButtonReleased[position];
90  if (value)
91  _mouseButtonReleased[position] = false;
92 
93  return value;
94 }
95 
97  unsigned int position = static_cast<unsigned int>(direction);
98  bool value = _mouseWheelTurned[position];
99  if (value)
100  _mouseWheelTurned[position] = false;
101 
102  return value;
103 }
104 
106  return _screenTouched;
107 }
108 
110  return _screenReleased;
111 }
112 
114  bool value = _screenTouched;
115  if (value)
116  _screenTouched = false;
117 
118  return value;
119 }
120 
122  bool value = _screenReleased;
123  if (value)
124  _screenReleased = false;
125 
126  return value;
127 }
128 
130  unsigned int position = static_cast<unsigned int>(key);
131  return _keysPressed[position];
132 }
133 
135  unsigned int position = static_cast<unsigned int>(key);
136  return _keysReleased[position];
137 }
138 
140  unsigned int position = static_cast<unsigned int>(key);
141  bool value = _keysPressed[position];
142  if (value)
143  _keysPressed[position] = false;
144 
145  return value;
146 }
147 
149  unsigned int position = static_cast<unsigned int>(key);
150  bool value = _keysReleased[position];
151  if (value)
152  _keysReleased[position] = false;
153 
154  return value;
155 }
156 
158  return _mousePosition;
159 }
160 
162  return _mouseMovement;
163 }
164 
166  return _mouseWheelMovement;
167 }
168 
170  return _touchPosition;
171 }
172 
174  auto screen_size = Core::App()->getSize();
175  auto viewport = Core::CurrentGame()->getViewport();
176  glm::vec2 pos = {_touchPosition.x * screen_size.x, (1-_touchPosition.y) * screen_size.y};
177 
178  return (viewport ? viewport->screenToWorld(pos) : pos);
179 }
180 
182  return _touchMovement;
183 }
184 
186  return _touchPressure;
187 }
188 
190  _touchSimulation = value;
191 }
192 
194  _currentHandler = handler.get();
195 }
196 
198  _currentHandler = handler;
199 }
200 
201 } /* window */
202 } /* hx3d */
float _touchPressure
Touch pressure.
static window::Game * CurrentGame()
Get the game instance.
Definition: core.cpp:82
glm::vec2 getTouchPosition()
Get the current touch position.
bool * _keysReleased
Keys released.
bool isScreenJustTouched()
Test if the screen have been just touched.
glm::vec2 _mouseMovement
Mouse movement.
bool * _mouseButtonReleased
Mouse buttons released.
bool _touchSimulation
Is touch simulated with mouse ?
glm::vec2 getTouchMovement()
Get the current touch movement.
bool isScreenReleased()
Test if the screen have been released.
glm::vec2 getScreenConvertedTouchPosition()
Get the screen converted touch position.
static window::Application * App()
Get the application instance.
Definition: core.cpp:78
hx3d framework namespace
Definition: audio.hpp:26
bool isMouseButtonJustClicked(MouseButtonEvent::Button button)
Test if a mouse button have just been clicked.
bool isKeyPressed(KeyEvent::Key key)
Test if the key have been pressed.
bool isScreenJustReleased()
Test if the screen have been just released.
glm::vec2 getMouseMovement()
Get the current mouse movement.
glm::ivec2 getSize()
Get the window size (ivec2)
Definition: application.cpp:43
glm::vec2 _mousePosition
Mouse position.
InputHandler * _currentHandler
Current input handler.
glm::vec2 getMouseWheelMovement()
Get the current mouse wheel movement.
bool * _mouseButtonClicked
Mouse buttons clicked.
bool isMouseButtonReleased(MouseButtonEvent::Button button)
Test if a mouse button have been relased.
bool isKeyJustReleased(KeyEvent::Key key)
Test if the key have just been released.
glm::vec2 getMousePosition()
Get the current mouse position.
bool isScreenTouched()
Test if the screen have been touched.
bool * _keysPressed
Keys pressed.
bool isKeyReleased(KeyEvent::Key key)
Test if the key have been released.
Button
Mouse button value.
Definition: events.hpp:101
bool isMouseWheelTurned(MouseWheelEvent::Direction direction)
Test if the mouse wheel have been turned in a direction.
bool _screenTouched
Screen touched ?
bool isMouseButtonClicked(MouseButtonEvent::Button button)
Test if a mouse button have been clicked.
float getTouchPressure()
Get the current touch pressure.
const Ptr< graphics::viewports::Viewport > & getViewport()
Get the current viewport.
Definition: game.cpp:84
void setInputHandler(InputHandler *handler)
Define an input handler.
Direction
Mouse wheel direction.
Definition: events.hpp:124
glm::vec2 _mouseWheelMovement
Mouse wheel movement.
Type
Window event type.
Definition: events.hpp:71
bool * _mouseWheelTurned
Mouse wheels turned.
bool _screenReleased
Screen released ?
bool * _windowEvents
Window events.
bool isWindowState(WindowEvent::Type type)
Test the window state.
glm::vec2 _touchPosition
Touch position.
Input management: use with screens !
void emulateTouchWithMouse(bool value)
Emulate the touch system with the mouse.
bool isKeyJustPressed(KeyEvent::Key key)
Test if the key have just been pressed.
bool isMouseButtonJustReleased(MouseButtonEvent::Button button)
Test if a mouse button have just been released.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34
glm::vec2 _touchMovement
Touch movement.