-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputHandler.h
executable file
·203 lines (162 loc) · 3.79 KB
/
InputHandler.h
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#ifndef INPUTHANDLER_H
#define INPUTHANDLER_H
#include <GLFW/glfw3.h>
#include <string>
#include <vector>
#include <iostream>
namespace Engine
{
#define NO_INPUT -1
#define JOYSTICK_BUTTON_1 0
#define JOYSTICK_BUTTON_2 1
#define JOYSTICK_BUTTON_3 2
#define JOYSTICK_BUTTON_4 3
#define JOYSTICK_BUTTON_5 4
#define JOYSTICK_BUTTON_6 5
#define JOYSTICK_BUTTON_7 6
#define JOYSTICK_BUTTON_8 7
#define JOYSTICK_BUTTON_9 8
#define JOYSTICK_BUTTON_10 9
#define JOYSTICK_BUTTON_11 10
#define JOYSTICK_BUTTON_12 11
#define JOYSTICK_BUTTON_13 12
#define JOYSTICK_BUTTON_14 13
#define JOYSTICK_BUTTON_15 14
#define JOYSTICK_BUTTON_16 15
#define JOYSTICK_BUTTON_17 16
#define JOYSTICK_BUTTON_18 17
#define JOYSTICK_BUTTON_19 18
#define JOYSTICK_BUTTON_20 19
#define JOYSTICK_BUTTONS 20
#define MOUSE_AXIS_X 0
#define MOUSE_AXIS_Y 1
#define JOYSTICK_AXIS_1 0
#define JOYSTICK_AXIS_2 1
#define JOYSTICK_AXIS_3 2
#define JOYSTICK_AXIS_4 3
#define JOYSTICK_AXIS_5 4
#define JOYSTICK_AXIS_6 5
#define JOYSTICK_AXIS_7 6
#define JOYSTICK_AXIS_8 7
#define JOYSTICK_AXIS_9 8
#define JOYSTICK_AXIS_10 9
typedef struct KeyBinding
{
KeyBinding( const std::string& name, int key, int mouseBut, int joyBut )
:
name( name ),
key( key ),
mouseBut( mouseBut ),
joyBut( joyBut )
{}
void changeKey( int key )
{
this->key = key;
}
void changeMouseBut( int key )
{
this->key = key;
}
void changeJoyBut( int joyBut )
{
this->joyBut = joyBut;
}
const std::string& getName( )
{
return this->name;
}
int getButton( )
{
return this->key;
}
int getMouseButton( )
{
return this->mouseBut;
}
int getJoystickButton( )
{
return this->joyBut;
}
private:
std::string name;
int key, mouseBut, joyBut;
} KeyBinding;
typedef struct Axis
{
Axis( const std::string& name, int keyPos, int keyNeg, int mouseAxis, int joyAxis )
:
name( name ),
keyPos( keyPos ),
keyNeg( keyNeg ),
mouseAxis( mouseAxis ),
joyAxis( joyAxis )
{}
void changeKeyPositive( int keyPos )
{
this->keyPos = keyPos;
}
void changeKeyNegative( int keyNeg )
{
this->keyNeg = keyNeg;
}
void changeMouseAxis( int mouseAxis )
{
this->mouseAxis = mouseAxis;
}
void changeJoyAxis( int joyAxis )
{
this->joyAxis = joyAxis;
}
const std::string& getName()
{
return this->name;
}
int getKeyPositive()
{
return this->keyPos;
}
int getKeyNegative()
{
return this->keyNeg;
}
int getMouseAxis()
{
return this->mouseAxis;
}
int getJoystickAxis()
{
return this->joyAxis;
}
private:
std::string name;
int keyPos, keyNeg, mouseAxis, joyAxis;
} Axis;
class InputHandler
{
public:
InputHandler();
~InputHandler();
void update();
void addKeyBinding( KeyBinding binding );
void addAxis( Axis axis );
static void refreshJoysticks();
static bool getKey( int key );
static bool getKeyUp( int key );
static bool getKeyDown( int key );
static bool getButton( const std::string& butName );
static bool getButtonUp( const std::string& butName );
static bool getButtonDown( const std::string& butName );
static float getAxis( const std::string& axisName );
static void getMousePos( double* mouseX, double* mouseY );
static bool getMouseButton( int mouseButton );
private:
static std::vector<KeyBinding> keyBindings;
static std::vector<Axis> axes;
static bool joyButtonLast[ GLFW_JOYSTICK_LAST * JOYSTICK_BUTTONS ];
static bool joysticks[ GLFW_JOYSTICK_LAST ];
static bool keyboardLast[ GLFW_KEY_LAST ];
static bool mouseButtonLast[ GLFW_MOUSE_BUTTON_LAST ];
static double lastMouseX, lastMouseY;
};
}
#endif