SimpleGL  1.1.0
A framework for platform independent rendering
glfw_callback.cpp
Go to the documentation of this file.
1 
9 
10 #include "SimpleGL/logging.hpp"
11 #include "SimpleGL/event.hpp"
12 
13 using namespace std;
14 
15 namespace sgl {
16  ErrorCallback* ErrorCallback::current = nullptr;
17 
18  ErrorCallback* ErrorCallback::getCurrent() {
19  return current;
20  }
21 
22  void ErrorCallback::setCurrent(ErrorCallback* callback) {
23  /* Only set if callback is not a nullptr */
24  if (callback != nullptr) {
25  current = callback;
26  }
27  }
28 
29  void ErrorCallback::dispatch(int error, const char* description) {
30  /* Instance shouldn't be null when calling this function */
31  if (current) {
32  current->invoke(error, string(description));
33  }
34  }
35 
36  ErrorCallback::ErrorCallback(void) {
37  /* Nothing to do here */
38  }
39 
40  ErrorCallback::~ErrorCallback(void) {
41  /* Set instance to nullptr if this was the current error callback */
42  if (ErrorCallback::current == this) {
43  ErrorCallback::current = nullptr;
44  }
45  }
46 
48  void ErrorCallback::invoke(int error, std::string description) {
49  Logger::logError("GLFW error " + to_string(error) + ": " + description);
50  }
51 
52  KeyCallback* KeyCallback::current = nullptr;
53 
54  KeyCallback* KeyCallback::getCurrent() {
55  return current;
56  }
57 
58  void KeyCallback::setCurrent(KeyCallback* callback) {
59  /* Only set if callback is not a nullptr */
60  if (callback != nullptr) {
61  current = callback;
62  }
63  }
64 
65  void KeyCallback::dispatch(GLFWwindow* window, int key, int scancode, int action, int mods) {
66  /* Instance shouldn't be null when calling this function */
67  if (current) {
68  current->invoke(window, static_cast<Key> (key), scancode, static_cast<Action> (action), static_cast<Modifier> (mods));
69  }
70  }
71 
72  KeyCallback::KeyCallback(void) {
73  /* Nothing to do here */
74  }
75 
76  KeyCallback::~KeyCallback(void) {
77  /* Set instance to nullptr if this was the current key callback */
78  if (KeyCallback::current == this) {
79  KeyCallback::current = nullptr;
80  }
81  }
82 
84  void KeyCallback::invoke(GLFWwindow* window, Key key, int scancode, Action action, Modifier mods) {
85  KeyEvent* event = new KeyEvent(key, scancode, action, mods);
86  Event::addEvent(event);
87  }
88 
89  MouseButtonCallback* MouseButtonCallback::current = nullptr;
90 
91  MouseButtonCallback* MouseButtonCallback::getCurrent() {
92  return current;
93  }
94 
95  void MouseButtonCallback::setCurrent(MouseButtonCallback* callback) {
96  /* Only set if callback is not a nullptr */
97  if (callback != nullptr) {
98  current = callback;
99  }
100  }
101 
102  void MouseButtonCallback::dispatch(GLFWwindow* window, int button, int action, int mods) {
103  /* Instance shouldn't be null when calling this function */
104  if (current) {
105  current->invoke(window, static_cast<MouseButton> (button), static_cast<Action> (action), static_cast<Modifier> (mods));
106  }
107  }
108 
109  MouseButtonCallback::MouseButtonCallback(void) {
110  /* Nothing to do here */
111  }
112 
113  MouseButtonCallback::~MouseButtonCallback(void) {
114  /* Set instance to nullptr if this was the current mouse button callback */
115  if (MouseButtonCallback::current == this) {
116  MouseButtonCallback::current = nullptr;
117  }
118  }
119 
121  void MouseButtonCallback::invoke(GLFWwindow* window, MouseButton button, Action action, Modifier mods) {
122  MouseButtonEvent* event = new MouseButtonEvent(button, action, mods);
123  Event::addEvent(event);
124  }
125 }
virtual void invoke(int error, std::string description)
This function gets called whenever a GLFW error occurs.
MouseButton
This enum wraps the GLFW mouse buttons.
Definition: input.hpp:396
Key
This enum wraps the GLFW keys.
Definition: input.hpp:22
This class wraps a GLFW error callback.
Modifier
This enum wraps the GLFW modifier bits.
Definition: input.hpp:457
Generic namespace for the SimpleGL framework.
Definition: application.hpp:18
This class wraps a GLFW mouse button callback.
This class wraps a GLFW key callback.
virtual void invoke(GLFWwindow *window, MouseButton button, Action action, Modifier mods)
This function gets called whenever a mouse button is pressed or released.
Action
This enum wraps the GLFW actions.
Definition: input.hpp:440
This class defines a mouse button event, created by the MouseButtonCallback.
Definition: event.hpp:155
virtual void invoke(GLFWwindow *window, Key key, int scancode, Action action, Modifier mods)
This function gets called whenever a key is pressed or released.
This class defines a key event, created by the KeyCallback.
Definition: event.hpp:92