summaryrefslogtreecommitdiff
path: root/src/player-input.cpp
blob: 148eb22f607ba0cab85088be2236f7837ae97211 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

#include "player-input.hpp"
#include "window.hpp"
#include "game-settings.hpp"

TypeSafeInt_B(InputEventHash);
TypeSafeInt_B(InputEventType);
TypeSafeInt_B(InputCursorEntered);
TypeSafeInt_B(InputKeyboardKeyAction);
TypeSafeInt_B(InputMouseButtonAction);
TypeSafeInt_B(InputWindowFocused);

DynArray<PkeInputEvent> UnhandledPkeInputEvents{0};

void PkeInput_Tick() { }

void CursorEnterCallback(GLFWwindow *window, int entered) {
	auto &ev = UnhandledPkeInputEvents.Push();
	ev.type = PKE_INPUT_EVENT_TYPE_CURSOR_ENTER;
	ev.timePoint = pkeSettings.steadyClock.now();
	ev.cursorEntered = InputCursorEntered{static_cast<InputCursorEntered_T>(entered)};
	ev.keyboardKeyAction = PKE_INPUT_KEYBOARD_KEY_ACTION_UNSET;
	ev.mouseButtonAction = PKE_INPUT_MOUSE_BUTTON_ACTION_UNSET;
	ev.windowFocused = PKE_INPUT_WINDOW_FOCUSED_UNSET;
	ev.x = 0;
	ev.y = 0;
	ev.button = 0;
	ev.scanCode = 0;
	ev.mods = 0;
}

void CursorPosCallback(GLFWwindow *window, double xPos, double yPos) {
	auto &ev = UnhandledPkeInputEvents.Push();
	ev.type = PKE_INPUT_EVENT_TYPE_CURSOR_POS;
	ev.timePoint = pkeSettings.steadyClock.now();
	ev.cursorEntered = PKE_INPUT_CURSOR_ENTERED_UNSET;
	ev.keyboardKeyAction = PKE_INPUT_KEYBOARD_KEY_ACTION_UNSET;
	ev.mouseButtonAction = PKE_INPUT_MOUSE_BUTTON_ACTION_UNSET;
	ev.windowFocused = PKE_INPUT_WINDOW_FOCUSED_UNSET;
	ev.x = xPos;
	ev.y = yPos;
	ev.button = 0;
	ev.scanCode = 0;
	ev.mods = 0;
}

void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
	auto &ev = UnhandledPkeInputEvents.Push();
	ev.type = PKE_INPUT_EVENT_TYPE_KEY;
	ev.timePoint = pkeSettings.steadyClock.now();
	ev.cursorEntered = PKE_INPUT_CURSOR_ENTERED_UNSET;
	ev.keyboardKeyAction = InputKeyboardKeyAction{static_cast<InputKeyboardKeyAction_T>(action)};
	ev.mouseButtonAction = PKE_INPUT_MOUSE_BUTTON_ACTION_UNSET;
	ev.windowFocused = PKE_INPUT_WINDOW_FOCUSED_UNSET;
	ev.x = 0;
	ev.y = 0;
	ev.button = key;
	ev.scanCode = scancode;
	ev.mods = mods;
}

void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods) {
	auto &ev = UnhandledPkeInputEvents.Push();
	ev.type = PKE_INPUT_EVENT_TYPE_MOUSE_BUTTON;
	ev.timePoint = pkeSettings.steadyClock.now();
	ev.cursorEntered = PKE_INPUT_CURSOR_ENTERED_UNSET;
	ev.keyboardKeyAction = PKE_INPUT_KEYBOARD_KEY_ACTION_UNSET;
	ev.mouseButtonAction = InputMouseButtonAction{static_cast<InputMouseButtonAction_T>(action)};
	ev.windowFocused = PKE_INPUT_WINDOW_FOCUSED_UNSET;
	ev.x = 0;
	ev.y = 0;
	ev.button = button;
	ev.scanCode = 0;
	ev.mods = mods;
}

void ScrollCallback(GLFWwindow *window, double xOffset, double yOffset) {
	auto &ev = UnhandledPkeInputEvents.Push();
	ev.type = PKE_INPUT_EVENT_TYPE_SCROLL;
	ev.timePoint = pkeSettings.steadyClock.now();
	ev.cursorEntered = PKE_INPUT_CURSOR_ENTERED_UNSET;
	ev.keyboardKeyAction = PKE_INPUT_KEYBOARD_KEY_ACTION_UNSET;
	ev.mouseButtonAction = PKE_INPUT_MOUSE_BUTTON_ACTION_UNSET;
	ev.windowFocused = PKE_INPUT_WINDOW_FOCUSED_UNSET;
	ev.x = xOffset;
	ev.y = yOffset;
	ev.button = 0;
	ev.scanCode = 0;
	ev.mods = 0;
}

void WindowFocusCallback(GLFWwindow *window, int focused) {
	auto &ev = UnhandledPkeInputEvents.Push();
	ev.type = PKE_INPUT_EVENT_TYPE_WINDOW_FOCUS;
	ev.timePoint = pkeSettings.steadyClock.now();
	ev.cursorEntered = PKE_INPUT_CURSOR_ENTERED_UNSET;
	ev.keyboardKeyAction = PKE_INPUT_KEYBOARD_KEY_ACTION_UNSET;
	ev.mouseButtonAction = PKE_INPUT_MOUSE_BUTTON_ACTION_UNSET;
	ev.windowFocused = InputWindowFocused{static_cast<InputWindowFocused_T>(focused)};
	ev.x = 0;
	ev.y = 0;
	ev.button = 0;
	ev.scanCode = 0;
	ev.mods = 0;
}

void PkeInput_Init() {
	glfwSetCursorEnterCallback(window, CursorEnterCallback);
	glfwSetCursorPosCallback(window, CursorPosCallback);
	glfwSetKeyCallback(window, KeyCallback);
	glfwSetMouseButtonCallback(window, MouseButtonCallback);
	glfwSetScrollCallback(window, ScrollCallback);
	glfwSetWindowFocusCallback(window, WindowFocusCallback);
}

void PkeInput_Teardown() {
	UnhandledPkeInputEvents.~DynArray();
}