Doxygen Samurai Engine 0.0.1
Doxygen Samurai Engine Documentation
Loading...
Searching...
No Matches
callbacks.cpp
Go to the documentation of this file.
1#include "callbacks.h"
2#include <string.h>
3
4#include "input.h"
6
7#if !SAMURAI_SDL
8
9void addTypedInput(samurai::Input &input, unsigned int c)
10{
11 if (c < 127)
12 {
13 auto l = strlen(input.typedInput);
14 if (l < sizeof(input.typedInput) - 1)
15 {
16 input.typedInput[l++] = c;
17 input.typedInput[l] = 0;
18 }
19 }
20}
21
22void characterCallback(GLFWwindow *window, unsigned int codepoint)
23{
24 auto ptr = glfwGetWindowUserPointer(window);
25 samurai::Window &pikaWindow = *(samurai::Window *)ptr;
26
27 addTypedInput(pikaWindow.input, codepoint);
28}
29
30void windowFocusCallback(GLFWwindow *window, int focused)
31{
32 auto ptr = glfwGetWindowUserPointer(window);
33 samurai::Window &pikaWindow = *(samurai::Window *)ptr;
34
35 if (focused)
36 {
37 pikaWindow.input.hasFocus = 1;
38 }
39 else
40 {
41 pikaWindow.input.hasFocus = 0;
42 }
43}
44
45static void processAButton(samurai::Button &b, int action)
46{
47 if (action == GLFW_PRESS)
48 {
49 b.setHeld(true);
50
51 }
52 else if (action == GLFW_RELEASE)
53 {
54 b.setHeld(false);
55 }
56 else if (action == GLFW_REPEAT)
57 {
58 b.setHeld(true);
59 b.setTyped(true);
60 }
61
62};
63
64void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
65{
66 auto ptr = glfwGetWindowUserPointer(window);
67 samurai::Window &pikaWindow = *(samurai::Window *)ptr;
68
69 if ((action == GLFW_REPEAT || action == GLFW_PRESS) && key == GLFW_KEY_BACKSPACE)
70 {
71 addTypedInput(pikaWindow.input, 8);
72 }
73
74 if(key >= GLFW_KEY_A && key <= GLFW_KEY_Z)
75 {
76 int index = key - GLFW_KEY_A;
77 processAButton(pikaWindow.input.buttons[samurai::Button::A + index], action);
78 }else if (key >= GLFW_KEY_0 && key <= GLFW_KEY_9)
79 {
80 int index = key - GLFW_KEY_0;
81 processAButton(pikaWindow.input.buttons[samurai::Button::NR0 + index], action);
82 }else
83 {
84
85 if (key == GLFW_KEY_SPACE)
86 {
88 }
89 else
90 if (key == GLFW_KEY_ENTER)
91 {
93 }
94 else
95 if (key == GLFW_KEY_ESCAPE)
96 {
98 }
99 else
100 if (key == GLFW_KEY_UP)
101 {
102 processAButton(pikaWindow.input.buttons[samurai::Button::Up], action);
103 }
104 else
105 if (key == GLFW_KEY_DOWN)
106 {
108 }
109 else
110 if (key == GLFW_KEY_LEFT)
111 {
113 }
114 else
115 if (key == GLFW_KEY_RIGHT)
116 {
118 }
119 else
120 if (key == GLFW_KEY_LEFT_CONTROL)
121 {
123 }else
124 if (key == GLFW_KEY_TAB)
125 {
127 }else
128 if (key == GLFW_KEY_LEFT_ALT)
129 {
131 }
132 }
133
134}
135
136void mouseCallback(GLFWwindow *window, int key, int action, int mods)
137{
138
139 auto ptr = glfwGetWindowUserPointer(window);
140 samurai::Window &pikaWindow = *(samurai::Window *)ptr;
141
142 if (key == GLFW_MOUSE_BUTTON_LEFT)
143 {
144 processAButton(pikaWindow.input.lMouse, action);
145 }
146 else if(key == GLFW_MOUSE_BUTTON_RIGHT)
147 {
148 processAButton(pikaWindow.input.rMouse, action);
149 }
150
151
152}
153#endif
void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition callbacks.cpp:64
void mouseCallback(GLFWwindow *window, int key, int action, int mods)
void addTypedInput(samurai::Input &input, unsigned int c)
Definition callbacks.cpp:9
void characterCallback(GLFWwindow *window, unsigned int codepoint)
Definition callbacks.cpp:22
void windowFocusCallback(GLFWwindow *window, int focused)
Definition callbacks.cpp:30
static void processAButton(samurai::Button &b, int action)
Definition callbacks.cpp:45
bool hasFocus
Definition input.h:74
Button lMouse
Definition input.h:62
Button rMouse
Definition input.h:63
char typedInput[20]
Definition input.h:71
Button buttons[Button::BUTTONS_COUNT]
Definition input.h:69
Input input
Definition window.h:31