8
8
9
9
#include " Input.h"
10
10
11
- #include < GLFW/glfw3.h>
12
- #include < utils/math/Float.h>
11
+ #include " window/platform/glfw/Input.h"
13
12
14
- static Siege::Window* windowPtr = nullptr ;
15
-
16
- static Siege::Input::MouseCoordinates currentMouseCoordinates;
17
-
18
- static std::map<int , int > keyMap;
19
-
20
- static void GetCursorPositionCallback (GLFWwindow* window, double xpos, double ypos)
13
+ namespace Siege
21
14
{
22
- currentMouseCoordinates.x = static_cast <float >(xpos);
23
- currentMouseCoordinates.y = static_cast <float >(ypos);
24
- }
25
-
26
- static GLFWwindow* AsGlfwWindow (void * window)
15
+ Glfw::Window Input::primaryWindow = nullptr ;
16
+ int Input::latestKey {-1 };
17
+ int Input::latestChar {-1 };
18
+ bool Input::keyUpdated {false };
19
+ bool Input::charUpdated {false };
20
+ std::map<int , int > Input::keyMap;
21
+ void Input::SetInputWindowSource (Glfw::Window window)
27
22
{
28
- return reinterpret_cast <GLFWwindow*>(window);
29
- }
23
+ primaryWindow = window;
30
24
31
- namespace Siege
32
- {
25
+ using namespace Glfw ;
33
26
34
- void Input::SetWindowPointer (Window* window)
35
- {
36
- // TODO: Fail if inputted pointer is nullptr
37
- windowPtr = window;
38
- glfwSetCursorPosCallback (AsGlfwWindow (window->GetWindow ()), GetCursorPositionCallback);
27
+ SetOnTextKeyPressedCallback (window, [](Window, unsigned int key) {
28
+ latestChar = key;
29
+ charUpdated = true ;
30
+ });
31
+
32
+ SetOnKeyPressedCallback (window, [](Window, int key, int scancode, int action, int mod) {
33
+ latestKey = ((action == ACTION_PRESSED || action == ACTION_REPEAT) * key) +
34
+ (action == ACTION_RELEASED * -1 );
35
+ keyUpdated = true ;
36
+ });
39
37
}
38
+
40
39
bool Input::IsKeyDown (int key)
41
40
{
42
- bool hasKey = ( glfwGetKey ( AsGlfwWindow (windowPtr-> GetWindow ()) , key) == GLFW_PRESS );
41
+ bool hasKey = Glfw::IsKeyDown (primaryWindow , key);
43
42
44
43
if (keyMap.find (key) != keyMap.end ())
45
44
{
@@ -54,8 +53,7 @@ bool Input::IsKeyDown(int key)
54
53
55
54
bool Input::IsKeyJustPressed (int key)
56
55
{
57
- bool hasKey =
58
- glfwGetKey (reinterpret_cast <GLFWwindow*>(windowPtr->GetWindow ()), key) == GLFW_PRESS;
56
+ bool hasKey = Glfw::IsKeyDown (primaryWindow, key);
59
57
60
58
bool keyEntryExists = keyMap.find (key) != keyMap.end ();
61
59
if (keyEntryExists && hasKey)
@@ -77,20 +75,22 @@ bool Input::IsKeyJustPressed(int key)
77
75
return false ;
78
76
}
79
77
80
- const Input::MouseCoordinates& Input::GetCursorPosition ()
78
+ const MousePosition Input::GetCursorPosition ()
81
79
{
82
- return currentMouseCoordinates ;
80
+ return Glfw::GetMousePosition (primaryWindow) ;
83
81
}
84
82
85
- Input::MouseCoordinates Input::GetNormalisedMousePosition ()
83
+ int Input::GetLatestKey ()
86
84
{
87
- using namespace Siege ::Float;
88
- return {Float::Clamp (Float::Normalise (currentMouseCoordinates.x , 0 , windowPtr->GetWidth ()),
89
- -1 .f ,
90
- 1 .f ),
91
- Float::Clamp (Float::Normalise (currentMouseCoordinates.y , 0 , windowPtr->GetHeight ()),
92
- -1 .f ,
93
- 1 .f )};
85
+ auto key = (keyUpdated * latestKey) + (!keyUpdated * -1 );
86
+ keyUpdated = false ;
87
+ return key;
94
88
}
95
89
90
+ int Input::GetLatestChar ()
91
+ {
92
+ auto key = (charUpdated * latestChar) + (!charUpdated * -1 );
93
+ charUpdated = false ;
94
+ return key;
95
+ }
96
96
} // namespace Siege
0 commit comments