Lugdunum  0.1.0
Window.cpp
Go to the documentation of this file.
1 #include <lug/Window/Window.hpp>
2 
3 #if defined(LUG_SYSTEM_WINDOWS)
5 #elif defined(LUG_SYSTEM_LINUX)
7 #elif defined(LUG_SYSTEM_ANDROID)
9 #else
10  // Theoretically this should never happen since the Config.cmake will
11  // warn the user before, but #error anyway
12  #error "Unsupported operating system or environment"
13 #endif
14 
15 namespace lug {
16 namespace Window {
17 Window::Window() : _impl{new priv::WindowImpl(this)} {
18  initKeyState();
20 }
21 
23  close();
24 }
25 
26 std::unique_ptr<Window> Window::create(const InitInfo& initInfo) {
27  std::unique_ptr<Window> win(new Window());
28 
29  if (!win->init(initInfo)) {
30  return nullptr;
31  }
32 
33  return win;
34 }
35 
36 bool Window::init(const InitInfo& initInfo) {
37  if (_impl != nullptr) {
38 
39  // Specify the width and height of our window, for now it's the only thing we can specify along with the title and style that is
40  _mode.width = initInfo.width;
41  _mode.height = initInfo.height;
42 
43  return _impl->init(initInfo);
44  }
45 
46  return false;
47 }
48 
51  // Basic keys
52  _keyState[Keyboard::Key::A] = false;
53  _keyState[Keyboard::Key::B] = false;
54  _keyState[Keyboard::Key::C] = false;
55  _keyState[Keyboard::Key::D] = false;
56  _keyState[Keyboard::Key::E] = false;
57  _keyState[Keyboard::Key::F] = false;
58  _keyState[Keyboard::Key::G] = false;
59  _keyState[Keyboard::Key::H] = false;
60  _keyState[Keyboard::Key::I] = false;
61  _keyState[Keyboard::Key::J] = false;
62  _keyState[Keyboard::Key::K] = false;
63  _keyState[Keyboard::Key::L] = false;
64  _keyState[Keyboard::Key::M] = false;
65  _keyState[Keyboard::Key::N] = false;
66  _keyState[Keyboard::Key::O] = false;
67  _keyState[Keyboard::Key::P] = false;
68  _keyState[Keyboard::Key::Q] = false;
69  _keyState[Keyboard::Key::R] = false;
70  _keyState[Keyboard::Key::S] = false;
71  _keyState[Keyboard::Key::T] = false;
72  _keyState[Keyboard::Key::U] = false;
73  _keyState[Keyboard::Key::V] = false;
74  _keyState[Keyboard::Key::W] = false;
75  _keyState[Keyboard::Key::X] = false;
76  _keyState[Keyboard::Key::Y] = false;
77  _keyState[Keyboard::Key::Z] = false;
78 
89 
90  // Modifiers
99 
100  // Advanced keys
116  _keyState[Keyboard::Key::Tab] = false;
119  _keyState[Keyboard::Key::End] = false;
123  _keyState[Keyboard::Key::Add] = false;
129  _keyState[Keyboard::Key::Up] = false;
134 
135  // AZERTY Specifics
154 
155  // Numpad
166 
167  // Function keys
168  _keyState[Keyboard::Key::F1] = false;
169  _keyState[Keyboard::Key::F2] = false;
170  _keyState[Keyboard::Key::F3] = false;
171  _keyState[Keyboard::Key::F4] = false;
172  _keyState[Keyboard::Key::F5] = false;
173  _keyState[Keyboard::Key::F6] = false;
174  _keyState[Keyboard::Key::F7] = false;
175  _keyState[Keyboard::Key::F8] = false;
176  _keyState[Keyboard::Key::F9] = false;
177  _keyState[Keyboard::Key::F10] = false;
178  _keyState[Keyboard::Key::F11] = false;
179  _keyState[Keyboard::Key::F12] = false;
180  _keyState[Keyboard::Key::F13] = false;
181  _keyState[Keyboard::Key::F14] = false;
182  _keyState[Keyboard::Key::F15] = false;
183 }
184 
191 }
192 
193 bool Window::isOpen() const {
194  return _impl != nullptr;
195 }
196 
197 bool Window::pollEvent(Event& event) {
198  if (_impl != nullptr) {
199  if (!_impl->pollEvent(event)) {
200  return false;
201  }
202 
203  // If we've receive Destroy just close window without returning the event to the user
204  if (event.type == Event::Type::Destroy) {
205  close();
206  return false;
207  }
208 
209  if (event.type == Event::Type::KeyPressed) {
210  _keyState[event.key.code] = true;
211  } else if (event.type == Event::Type::KeyReleased) {
212  _keyState[event.key.code] = false;
213  }
214 
215  if (event.type == Event::Type::ButtonPressed) {
216  _mouseState[event.mouse.code] = true;
217  _mousePosition.x() = event.mouse.coord.x;
218  _mousePosition.y() = event.mouse.coord.y;
219  } else if (event.type == Event::Type::ButtonReleased) {
220  _mouseState[event.mouse.code] = false;
221  _mousePosition.x() = event.mouse.coord.x;
222  _mousePosition.y() = event.mouse.coord.y;
223  }
224 
225  if (event.type == Event::Type::MouseMoved) {
226  _mousePosition.x() = event.mouse.coord.x;
227  _mousePosition.y() = event.mouse.coord.y;
228  }
229 
230  if (event.type == Event::Type::GamePadChange) {
231  _gamePadState = event.gamePad;
232  }
233 
234  if (event.type == Event::Type::TouchScreenChange) {
235  _touchScreenState = event.touchScreen;
236  }
237 
238  return true;
239  }
240  return false;
241 }
242 
243 void Window::setKeyRepeat(bool enable) {
244  if (_impl != nullptr) {
245  _impl->setKeyRepeat(enable);
246  }
247 }
248 
249 void Window::setMouseCursorVisible(bool visible) {
250  if (_impl != nullptr) {
251  _impl->setMouseCursorVisible(visible);
252  }
253 }
254 
256  if (_impl != nullptr) {
257  _impl->close();
258  delete _impl;
259  _impl = nullptr;
260  }
261 }
262 
264  return _keyState.at(key);
265 }
266 
268  return (_mouseState.at(button));
269 }
270 
271 const Math::Vec2i& Window::getMousePos() const {
272  return _mousePosition;
273 }
274 
275 void Window::setMousePos(const Math::Vec2i& mousePosition) {
276  if (_impl != nullptr) {
277  _impl->setMousePos(mousePosition);
278  }
279  _mousePosition = mousePosition;
280 }
281 
282 Math::Vec2i Window::getWindowSize() const {
283  return {_mode.width, _mode.height};
284 }
285 
286 } // Window
287 } // lug
Represents an event.
Definition: Event.hpp:89
TouchScreenEvent _touchScreenState
Definition: Window.hpp:162
void setMousePos(const Math::Vec2i &mousePosition)
Sets the mouse position.
Definition: Window.cpp:275
std::unordered_map< Keyboard::Key, bool > _keyState
Definition: Window.hpp:190
const Math::Vec2i & getMousePos() const
Retrieves the mouses position.
Definition: Window.cpp:271
void setKeyRepeat(bool state)
Enables or disables key repeat.
Definition: Window.cpp:243
virtual ~Window()
Definition: Window.cpp:22
void close()
Close the window gracefully.
Definition: Window.cpp:255
priv::WindowImpl * _impl
Definition: Window.hpp:180
bool init(const Window::InitInfo &initInfo)
Class for window.
Definition: Window.hpp:59
Key
Abstraction of keyboard keys.
Definition: Keyboard.hpp:23
Button
Abstraction of Mouse buttons.
Definition: Mouse.hpp:26
void setMousePos(const Math::Vec2i &mousePosition)
void initMouseState()
Inits every button in _mouseState to false.
Definition: Window.cpp:185
Type type
The type of the event.
Definition: Event.hpp:110
void initKeyState()
Inits every key in _keyState to false.
Definition: Window.cpp:49
bool isMousePressed(Mouse::Button button) const
Determines if a mouse button pressed.
Definition: Window.cpp:267
void setMouseCursorVisible(bool visible)
bool init(const InitInfo &initInfo)
Definition: Window.cpp:36
Window destroy event.
bool pollEvent(lug::Window::Event &event)
bool isKeyPressed(Keyboard::Key key) const
Determines if a key pressed.
Definition: Window.cpp:263
GamePadEvent _gamePadState
Definition: Window.hpp:161
std::unordered_map< Mouse::Button, bool > _mouseState
Definition: Window.hpp:195
Math::Vec2i _mousePosition
Definition: Window.hpp:200
static std::unique_ptr< Window > create(const InitInfo &initInfo)
Utility to create a window.
Definition: Window.cpp:26
Math::Vec2i getWindowSize() const
Gets the window size.
Definition: Window.cpp:282
VideoMode _mode
Definition: Window.hpp:185
bool isOpen() const
Determines if the window is open.
Definition: Window.cpp:193
virtual bool pollEvent(lug::Window::Event &event)
Checks if an event is available from the window implementation, and fill it in the event parameter...
Definition: Window.cpp:197
void setMouseCursorVisible(bool visible)
Sets the visibility of the mouse cursor (hide/show)
Definition: Window.cpp:249