-
Notifications
You must be signed in to change notification settings - Fork 5
/
bindings.cpp
305 lines (271 loc) · 6.51 KB
/
bindings.cpp
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
Copyright 2013-2015 Rohit Nirmal
This file is part of Clonepoint.
Clonepoint is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clonepoint is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clonepoint. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bindings.h"
#include "global.h"
BindingsManager::BindingsManager()
{
}
BindingsManager::~BindingsManager()
{
}
eBinding BindingsManager::checkToken(std::string token)
{
if (token == "Bind_MoveRight") return Bind_MoveRight;
if (token == "Bind_MoveLeft") return Bind_MoveLeft;
if (token == "Bind_MoveUp") return Bind_MoveUp;
if (token == "Bind_MoveDown") return Bind_MoveDown;
if (token == "Bind_ToggleCrosslink") return Bind_ToggleCrosslink;
return Bind_Nothing;
}
std::string BindingsManager::bindingToString(eBinding binding)
{
switch (binding)
{
case Bind_MoveRight:
return "Bind_MoveRight";
case Bind_MoveLeft:
return "Bind_MoveLeft";
case Bind_MoveUp:
return "Bind_MoveUp";
case Bind_MoveDown:
return "Bind_MoveDown";
case Bind_ToggleCrosslink:
return "Bind_ToggleCrosslink";
default:
return "";
}
}
SDL_Keycode BindingsManager::getKeyFromToken(char* token)
{
if (!strcmp(token, "A")) return SDLK_a;
if (!strcmp(token, "B")) return SDLK_b;
if (!strcmp(token, "C")) return SDLK_c;
if (!strcmp(token, "D")) return SDLK_d;
if (!strcmp(token, "E")) return SDLK_e;
if (!strcmp(token, "F")) return SDLK_f;
if (!strcmp(token, "G")) return SDLK_g;
if (!strcmp(token, "H")) return SDLK_h;
if (!strcmp(token, "I")) return SDLK_i;
if (!strcmp(token, "J")) return SDLK_j;
if (!strcmp(token, "K")) return SDLK_k;
if (!strcmp(token, "L")) return SDLK_l;
if (!strcmp(token, "M")) return SDLK_m;
if (!strcmp(token, "N")) return SDLK_n;
if (!strcmp(token, "O")) return SDLK_o;
if (!strcmp(token, "P")) return SDLK_p;
if (!strcmp(token, "Q")) return SDLK_q;
if (!strcmp(token, "R")) return SDLK_r;
if (!strcmp(token, "S")) return SDLK_s;
if (!strcmp(token, "T")) return SDLK_t;
if (!strcmp(token, "U")) return SDLK_u;
if (!strcmp(token, "V")) return SDLK_v;
if (!strcmp(token, "W")) return SDLK_w;
if (!strcmp(token, "X")) return SDLK_x;
if (!strcmp(token, "Y")) return SDLK_y;
if (!strcmp(token, "Z")) return SDLK_z;
if (!strcmp(token, "Left")) return SDLK_LEFT;
if (!strcmp(token, "Right")) return SDLK_RIGHT;
if (!strcmp(token, "Up")) return SDLK_UP;
if (!strcmp(token, "Down")) return SDLK_DOWN;
if (!strcmp(token, "RCtrl")) return SDLK_RCTRL;
if (!strcmp(token, "RAlt")) return SDLK_RALT;
if (!strcmp(token, "LCtrl")) return SDLK_LCTRL;
if (!strcmp(token, "LAlt")) return SDLK_LALT;
return SDLK_CLEAR;
}
std::string BindingsManager::getKeyAsString(SDL_Keycode key)
{
switch(key)
{
case SDLK_a:
return "A";
case SDLK_b:
return "B";
case SDLK_c:
return "C";
case SDLK_d:
return "D";
case SDLK_e:
return "E";
case SDLK_f:
return "F";
case SDLK_g:
return "G";
case SDLK_h:
return "H";
case SDLK_i:
return "I";
case SDLK_j:
return "J";
case SDLK_k:
return "K";
case SDLK_l:
return "L";
case SDLK_m:
return "M";
case SDLK_n:
return "N";
case SDLK_o:
return "O";
case SDLK_p:
return "P";
case SDLK_q:
return "Q";
case SDLK_r:
return "R";
case SDLK_s:
return "S";
case SDLK_t:
return "T";
case SDLK_u:
return "U";
case SDLK_v:
return "V";
case SDLK_w:
return "W";
case SDLK_x:
return "X";
case SDLK_y:
return "Y";
case SDLK_z:
return "Z";
case SDLK_LEFT:
return "Left";
case SDLK_RIGHT:
return "Right";
case SDLK_UP:
return "Up";
case SDLK_DOWN:
return "Down";
case SDLK_RCTRL:
return "RCtrl";
case SDLK_RALT:
return "RAlt";
case SDLK_LCTRL:
return "LCtrl";
case SDLK_LALT:
return "LAlt";
default:
return "NONE";
}
}
eBinding BindingsManager::getBindingFromKey(SDL_Keycode key)
{
std::map<SDL_Keycode, eBinding>::iterator it = _bindings.find(key);
if (it != _bindings.end())
{
return it->second;
}
return Bind_Nothing;
}
void BindingsManager::loadBindingsFromConfig(const char* filename)
{
char* text = file_read(filename, NULL);
char* delim = (char*)" =\t\n\r";
char* token = strtok(text, delim);
eBinding binding;
while (token)
{
binding = checkToken(token);
token = strtok(NULL, delim);
if (binding != Bind_Nothing)
{
SDL_Keycode key = getKeyFromToken(token);
addBinding(key, binding);
}
if (token)
token = strtok(NULL, delim);
}
delete [] text;
delete [] token;
text = NULL;
token = NULL;
delim = NULL;
}
std::vector<std::string> BindingsManager::getBindingsToSave()
{
std::vector<std::string> bindings;
std::string line;
std::map<SDL_Keycode, eBinding>::iterator it;
for (it = _bindings.begin(); it != _bindings.end(); it++)
{
line = bindingToString(it->second) + " = " + getKeyAsString(it->first) + "\n";
bindings.push_back(line);
}
return bindings;
}
void BindingsManager::clearBinding(eBinding binding)
{
std::map<SDL_Keycode, eBinding>::iterator it;
for (it = _bindings.begin(); it != _bindings.end(); it++)
{
if (it->second == binding)
{
_bindings.erase(it);
}
}
}
std::string BindingsManager::getKeysBound(eBinding binding)
{
std::string keysBound = "";
std::map<SDL_Keycode, eBinding>::iterator it;
for (it = _bindings.begin(); it != _bindings.end(); it++)
{
if (it->second == binding)
{
if (keysBound != "")
{
keysBound += ";";
}
keysBound += getKeyAsString(it->first);
}
}
if (keysBound == "")
{
keysBound = "NONE";
}
return keysBound;
}
std::string BindingsManager::getFirstKeyBound(eBinding binding)
{
std::string keysBound = "";
std::map<SDL_Keycode, eBinding>::iterator it;
for (it = _bindings.begin(); it != _bindings.end(); it++)
{
if (it->second == binding)
{
return getKeyAsString(it->first);
}
}
return "NONE";
}
void BindingsManager::addBinding(SDL_Keycode key, eBinding binding)
{
//erase previous keys for this binding first.
std::map<SDL_Keycode, eBinding>::iterator it;
std::string keyAsString = getKeyAsString(key);
if (keyAsString == "NONE")
{
return;
}
for (it = _bindings.begin(); it != _bindings.end(); it++)
{
if (it->first == key)
{
_bindings.erase(it);
}
}
_bindings.insert(std::pair<SDL_Keycode, eBinding>(key, binding));
}