]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/keybinder.qc
Header police
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / keybinder.qc
1 #include "keybinder.qh"
2 #ifndef KEYBINDER_H
3 #define KEYBINDER_H
4 #include "listbox.qc"
5 CLASS(XonoticKeyBinder, XonoticListBox)
6         METHOD(XonoticKeyBinder, configureXonoticKeyBinder, void(entity));
7         ATTRIB(XonoticKeyBinder, rowsPerItem, int, 1)
8         METHOD(XonoticKeyBinder, drawListBoxItem, void(entity, int, vector, bool, bool));
9         METHOD(XonoticKeyBinder, doubleClickListBoxItem, void(entity, float, vector));
10         METHOD(XonoticKeyBinder, resizeNotify, void(entity, vector, vector, vector, vector));
11         METHOD(XonoticKeyBinder, showNotify, void(entity));
12         METHOD(XonoticKeyBinder, setSelected, void(entity, float));
13         METHOD(XonoticKeyBinder, keyDown, float(entity, float, float, float));
14         METHOD(XonoticKeyBinder, keyGrabbed, void(entity, float, float));
15         METHOD(XonoticKeyBinder, destroy, void(entity));
16
17         ATTRIB(XonoticKeyBinder, realFontSize, vector, '0 0 0')
18         ATTRIB(XonoticKeyBinder, realUpperMargin, float, 0)
19         ATTRIB(XonoticKeyBinder, columnFunctionOrigin, float, 0)
20         ATTRIB(XonoticKeyBinder, columnFunctionSize, float, 0)
21         ATTRIB(XonoticKeyBinder, columnKeysOrigin, float, 0)
22         ATTRIB(XonoticKeyBinder, columnKeysSize, float, 0)
23
24         METHOD(XonoticKeyBinder, loadKeyBinds, void(entity));
25         ATTRIB(XonoticKeyBinder, previouslySelected, int, -1)
26         ATTRIB(XonoticKeyBinder, inMouseHandler, float, 0)
27         ATTRIB(XonoticKeyBinder, userbindEditButton, entity, NULL)
28         ATTRIB(XonoticKeyBinder, keyGrabButton, entity, NULL)
29         ATTRIB(XonoticKeyBinder, clearButton, entity, NULL)
30         ATTRIB(XonoticKeyBinder, userbindEditDialog, entity, NULL)
31         METHOD(XonoticKeyBinder, editUserbind, void(entity, string, string, string));
32 ENDCLASS(XonoticKeyBinder)
33 entity makeXonoticKeyBinder();
34 void KeyBinder_Bind_Change(entity btn, entity me);
35 void KeyBinder_Bind_Clear(entity btn, entity me);
36 void KeyBinder_Bind_Edit(entity btn, entity me);
37 void KeyBinder_Bind_Reset_All(entity btn, entity me);
38 #endif
39
40 #ifdef IMPLEMENTATION
41
42 const string KEY_NOT_BOUND_CMD = "// not bound";
43
44 const int MAX_KEYS_PER_FUNCTION = 2;
45 const int MAX_KEYBINDS = 256;
46 string Xonotic_KeyBinds_Functions[MAX_KEYBINDS];
47 string Xonotic_KeyBinds_Descriptions[MAX_KEYBINDS];
48 int Xonotic_KeyBinds_Count = -1;
49
50 void Xonotic_KeyBinds_Read()
51 {
52         Xonotic_KeyBinds_Count = 0;
53
54         #define KEYBIND_DEF(func, desc) MACRO_BEGIN { \
55                 if((Xonotic_KeyBinds_Count < MAX_KEYBINDS)) { \
56                         Xonotic_KeyBinds_Functions[Xonotic_KeyBinds_Count] = strzone(func); \
57                         Xonotic_KeyBinds_Descriptions[Xonotic_KeyBinds_Count] = strzone(desc); \
58                         ++Xonotic_KeyBinds_Count; \
59                 } \
60         } MACRO_END
61
62         KEYBIND_DEF(""                                      , _("Moving"));
63         KEYBIND_DEF("+forward"                              , _("forward"));
64         KEYBIND_DEF("+back"                                 , _("backpedal"));
65         KEYBIND_DEF("+moveleft"                             , _("strafe left"));
66         KEYBIND_DEF("+moveright"                            , _("strafe right"));
67         KEYBIND_DEF("+jump"                                 , _("jump / swim"));
68         KEYBIND_DEF("+crouch"                               , _("crouch / sink"));
69         KEYBIND_DEF("+hook"                                 , _("off-hand hook"));
70         KEYBIND_DEF("+jetpack"                              , _("jet pack"));
71         KEYBIND_DEF(""                                      , "");
72         KEYBIND_DEF(""                                      , _("Attacking"));
73         KEYBIND_DEF("+fire"                                 , _("primary fire"));
74         KEYBIND_DEF("+fire2"                                , _("secondary fire"));
75         KEYBIND_DEF(""                                      , "");
76         KEYBIND_DEF(""                                      , _("Weapon switching"));
77         KEYBIND_DEF("weapprev"                              , _("previous"));
78         KEYBIND_DEF("weapnext"                              , _("next"));
79         KEYBIND_DEF("weaplast"                              , _("previously used"));
80         KEYBIND_DEF("weapbest"                              , _("best"));
81         KEYBIND_DEF("reload"                                , _("reload"));
82
83         int i;
84
85         #define ADD_TO_W_LIST(pred) \
86                 FOREACH(Weapons, it != WEP_Null, LAMBDA( \
87                         if (it.impulse != imp) continue; \
88                         if (!(pred)) continue; \
89                         w_list = strcat(w_list, it.m_name, " / "); \
90                 ))
91
92         for(int imp = 1; imp <= 9; ++imp)
93         {
94         string w_list = "";
95                 ADD_TO_W_LIST(!(it.flags & WEP_FLAG_MUTATORBLOCKED) && !(it.flags & WEP_FLAG_SUPERWEAPON));
96                 ADD_TO_W_LIST(it.flags & WEP_FLAG_SUPERWEAPON);
97                 ADD_TO_W_LIST(it.flags & WEP_FLAG_MUTATORBLOCKED);
98                 if(w_list)
99                         KEYBIND_DEF(strcat("weapon_group_", itos(imp)), substring(w_list, 0, -4));
100                 if(imp == 0)
101                         break;
102                 if(imp == 9)
103                         imp = -1;
104         }
105         #undef ADD_TO_W_LIST
106
107         KEYBIND_DEF(""                                      , "");
108         KEYBIND_DEF(""                                      , _("View"));
109         KEYBIND_DEF("+zoom"                                 , _("hold zoom"));
110         KEYBIND_DEF("togglezoom"                            , _("toggle zoom"));
111         KEYBIND_DEF("+showscores"                           , _("show scores"));
112         KEYBIND_DEF("screenshot"                            , _("screen shot"));
113         KEYBIND_DEF("+hud_panel_radar_maximized"            , _("maximize radar"));
114         KEYBIND_DEF(""                                      , "");
115         KEYBIND_DEF(""                                      , _("Communicate"));
116         KEYBIND_DEF("messagemode"                           , _("public chat"));
117         KEYBIND_DEF("messagemode2"                          , _("team chat"));
118         KEYBIND_DEF("+con_chat_maximize"                    , _("show chat history"));
119         KEYBIND_DEF("vyes"                                  , _("vote YES"));
120         KEYBIND_DEF("vno"                                   , _("vote NO"));
121         KEYBIND_DEF("ready"                                 , _("ready"));
122         KEYBIND_DEF(""                                      , "");
123         KEYBIND_DEF(""                                      , _("Client"));
124         KEYBIND_DEF("+show_info"                            , _("server info"));
125         KEYBIND_DEF("toggleconsole"                         , _("enter console"));
126         KEYBIND_DEF("disconnect"                            , _("disconnect"));
127         KEYBIND_DEF("menu_showquitdialog"                   , _("quit"));
128         KEYBIND_DEF(""                                      , "");
129         KEYBIND_DEF(""                                      , _("Teamplay"));
130         KEYBIND_DEF("messagemode2"                          , _("team chat"));
131         KEYBIND_DEF("team_auto"                             , _("auto-join team"));
132         KEYBIND_DEF("menu_showteamselect"                   , _("team menu"));
133         KEYBIND_DEF("menu_showsandboxtools"                 , _("sandbox menu"));
134         KEYBIND_DEF("spec"                                  , _("enter spectator mode"));
135         KEYBIND_DEF("dropweapon"                            , _("drop weapon"));
136         KEYBIND_DEF("+use"                                  , _("drop key / drop flag"));
137         KEYBIND_DEF("+button8"                              , _("drag object"));
138         KEYBIND_DEF("toggle chase_active"                   , _("3rd person view"));
139         KEYBIND_DEF(""                                      , "");
140         KEYBIND_DEF(""                                      , _("User defined"));
141
142         for(i = 1; i <= 32; ++i)
143                 KEYBIND_DEF(strcat("+userbind ", itos(i)), strcat("$userbind", itos(i)));
144
145         #undef KEYBIND_DEF
146 }
147
148 entity makeXonoticKeyBinder()
149 {
150         entity me;
151         me = NEW(XonoticKeyBinder);
152         me.configureXonoticKeyBinder(me);
153         return me;
154 }
155 void replace_bind(string from, string to)
156 {
157         int n, j;
158         float k; // not sure if float or int
159         n = tokenize(findkeysforcommand(from, 0)); // uses '...' strings
160         for(j = 0; j < n; ++j)
161         {
162                 k = stof(argv(j));
163                 if(k != -1)
164                         localcmd("\nbind \"", keynumtostring(k), "\" \"", to, "\"\n");
165         }
166         if(n)
167                 cvar_set("_hud_showbinds_reload", "1");
168 }
169 void XonoticKeyBinder_configureXonoticKeyBinder(entity me)
170 {
171         me.configureXonoticListBox(me);
172         me.nItems = 0;
173
174         // TEMP: Xonotic 0.1 to later
175         replace_bind("impulse 1", "weapon_group_1");
176         replace_bind("impulse 2", "weapon_group_2");
177         replace_bind("impulse 3", "weapon_group_3");
178         replace_bind("impulse 4", "weapon_group_4");
179         replace_bind("impulse 5", "weapon_group_5");
180         replace_bind("impulse 6", "weapon_group_6");
181         replace_bind("impulse 7", "weapon_group_7");
182         replace_bind("impulse 8", "weapon_group_8");
183         replace_bind("impulse 9", "weapon_group_9");
184         replace_bind("impulse 14", "weapon_group_0");
185 }
186 void XonoticKeyBinder_loadKeyBinds(entity me)
187 {
188         bool force_initial_selection = false;
189         if(Xonotic_KeyBinds_Count < 0) // me.handle not loaded yet?
190                 force_initial_selection = true;
191         Xonotic_KeyBinds_Read();
192         me.nItems = Xonotic_KeyBinds_Count;
193         if(force_initial_selection)
194                 me.setSelected(me, 0);
195 }
196 void XonoticKeyBinder_showNotify(entity me)
197 {
198         me.destroy(me);
199         me.loadKeyBinds(me);
200 }
201 void XonoticKeyBinder_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
202 {
203         SUPER(XonoticKeyBinder).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
204
205         me.realFontSize_y = me.fontSize / (absSize.y * me.itemHeight);
206         me.realFontSize_x = me.fontSize / (absSize.x * (1 - me.controlWidth));
207         me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
208
209         me.columnFunctionOrigin = 0;
210         me.columnKeysSize = me.realFontSize.x * 12;
211         me.columnFunctionSize = 1 - me.columnKeysSize - 2 * me.realFontSize.x;
212         me.columnKeysOrigin = me.columnFunctionOrigin + me.columnFunctionSize + me.realFontSize.x;
213 }
214 void KeyBinder_Bind_Change(entity btn, entity me)
215 {
216         string func;
217
218         func = Xonotic_KeyBinds_Functions[me.selectedItem];
219         if(func == "")
220                 return;
221
222         me.keyGrabButton.forcePressed = 1;
223         me.clearButton.disabled = 1;
224         keyGrabber = me;
225 }
226 void XonoticKeyBinder_keyGrabbed(entity me, int key, bool ascii)
227 {
228         int n, j, nvalid;
229         float k;
230         string func;
231
232         me.keyGrabButton.forcePressed = 0;
233         me.clearButton.disabled = 0;
234
235         if(key == K_ESCAPE)
236                 return;
237
238         // forbid these keys from being bound in the menu
239         if(key == K_CAPSLOCK || key == K_NUMLOCK)
240         {
241                 KeyBinder_Bind_Change(me, me);
242                 return;
243         }
244
245         func = Xonotic_KeyBinds_Functions[me.selectedItem];
246         if(func == "")
247                 return;
248
249         n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
250         nvalid = 0;
251         for(j = 0; j < n; ++j)
252         {
253                 k = stof(argv(j));
254                 if(k != -1)
255                         ++nvalid;
256         }
257         if(nvalid >= MAX_KEYS_PER_FUNCTION)
258         {
259                 for(j = 0; j < n; ++j)
260                 {
261                         k = stof(argv(j));
262                         if(k != -1)
263                                 //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
264                                 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
265                 }
266         }
267         m_play_click_sound(MENU_SOUND_SELECT);
268         localcmd("\nbind \"", keynumtostring(key), "\" \"", func, "\"\n");
269         localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
270         cvar_set("_hud_showbinds_reload", "1");
271 }
272 void XonoticKeyBinder_destroy(entity me)
273 {
274         if(Xonotic_KeyBinds_Count < 0)
275                 return;
276
277         for(int i = 0; i < MAX_KEYBINDS; ++i)
278         {
279                 if(Xonotic_KeyBinds_Functions[i])
280                         strunzone(Xonotic_KeyBinds_Functions[i]);
281                 Xonotic_KeyBinds_Functions[i] = string_null;
282                 if(Xonotic_KeyBinds_Descriptions[i])
283                         strunzone(Xonotic_KeyBinds_Descriptions[i]);
284                 Xonotic_KeyBinds_Descriptions[i] = string_null;
285         }
286         Xonotic_KeyBinds_Count = 0;
287 }
288 void XonoticKeyBinder_editUserbind(entity me, string theName, string theCommandPress, string theCommandRelease)
289 {
290         string func, descr;
291
292         if(!me.userbindEditDialog)
293                 return;
294
295         func = Xonotic_KeyBinds_Functions[me.selectedItem];
296         if(func == "")
297                 return;
298
299         descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
300         if(substring(descr, 0, 1) != "$")
301                 return;
302         descr = substring(descr, 1, strlen(descr) - 1);
303
304         // Hooray! It IS a user bind!
305         cvar_set(strcat(descr, "_description"), theName);
306         cvar_set(strcat(descr, "_press"), theCommandPress);
307         cvar_set(strcat(descr, "_release"), theCommandRelease);
308 }
309 void KeyBinder_Bind_Edit(entity btn, entity me)
310 {
311         string func, descr;
312
313         if(!me.userbindEditDialog)
314                 return;
315
316         func = Xonotic_KeyBinds_Functions[me.selectedItem];
317         if(func == "")
318                 return;
319
320         descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
321         if(substring(descr, 0, 1) != "$")
322                 return;
323         descr = substring(descr, 1, strlen(descr) - 1);
324
325         // Hooray! It IS a user bind!
326         me.userbindEditDialog.loadUserBind(me.userbindEditDialog, cvar_string(strcat(descr, "_description")), cvar_string(strcat(descr, "_press")), cvar_string(strcat(descr, "_release")));
327
328         DialogOpenButton_Click(btn, me.userbindEditDialog);
329 }
330 void KeyBinder_Bind_Clear(entity btn, entity me)
331 {
332         float n, j, k;
333         string func;
334
335         func = Xonotic_KeyBinds_Functions[me.selectedItem];
336         if(func == "")
337                 return;
338
339         n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
340         for(j = 0; j < n; ++j)
341         {
342                 k = stof(argv(j));
343                 if(k != -1)
344                         //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
345                         localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
346         }
347         m_play_click_sound(MENU_SOUND_CLEAR);
348         localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
349         cvar_set("_hud_showbinds_reload", "1");
350 }
351 void KeyBinder_Bind_Reset_All(entity btn, entity me)
352 {
353         localcmd("unbindall\n");
354         localcmd("exec binds-xonotic.cfg\n");
355         localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
356         cvar_set("_hud_showbinds_reload", "1");
357 }
358 void XonoticKeyBinder_doubleClickListBoxItem(entity me, float i, vector where)
359 {
360         KeyBinder_Bind_Change(NULL, me);
361 }
362 void XonoticKeyBinder_setSelected(entity me, int i)
363 {
364         // handling of "unselectable" items
365         i = floor(0.5 + bound(0, i, me.nItems - 1));
366         if(me.pressed == 0 || me.pressed == 1) // keyboard or scrolling - skip unselectable items
367         {
368                 if(i > me.previouslySelected)
369                 {
370                         while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
371                                 ++i;
372                 }
373                 while((i > 0) && (Xonotic_KeyBinds_Functions[i] == ""))
374                         --i;
375                 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
376                         ++i;
377         }
378         if(me.pressed == 3) // released the mouse - fall back to last valid item
379         {
380                 if(Xonotic_KeyBinds_Functions[i] == "")
381                         i = me.previouslySelected;
382         }
383         if(Xonotic_KeyBinds_Functions[i] != "")
384                 me.previouslySelected = i;
385         if(me.userbindEditButton)
386                 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[i], 0, 1) != "$");
387         SUPER(XonoticKeyBinder).setSelected(me, i);
388 }
389 float XonoticKeyBinder_keyDown(entity me, int key, bool ascii, float shift)
390 {
391         bool r = true;
392         switch(key)
393         {
394                 case K_ENTER:
395                 case K_KP_ENTER:
396                 case K_SPACE:
397                         KeyBinder_Bind_Change(me, me);
398                         break;
399                 case K_DEL:
400                 case K_KP_DEL:
401                 case K_BACKSPACE:
402                         KeyBinder_Bind_Clear(me, me);
403                         break;
404                 default:
405                         r = SUPER(XonoticKeyBinder).keyDown(me, key, ascii, shift);
406                         break;
407         }
408         return r;
409 }
410 void XonoticKeyBinder_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
411 {
412         string s;
413         int j, n;
414         float k;
415         vector theColor;
416         float theAlpha;
417         string func, descr;
418         float extraMargin;
419
420         descr = Xonotic_KeyBinds_Descriptions[i];
421         func = Xonotic_KeyBinds_Functions[i];
422
423         if(func == "")
424         {
425                 theAlpha = 1;
426                 theColor = SKINCOLOR_KEYGRABBER_TITLES;
427                 theAlpha = SKINALPHA_KEYGRABBER_TITLES;
428                 extraMargin = 0;
429         }
430         else
431         {
432                 if(isSelected)
433                 {
434                         if(keyGrabber == me)
435                                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_WAITING, SKINALPHA_LISTBOX_WAITING);
436                         else
437                                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
438                 }
439                 else if(isFocused)
440                 {
441                         me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
442                         draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
443                 }
444
445                 theAlpha = SKINALPHA_KEYGRABBER_KEYS;
446                 theColor = SKINCOLOR_KEYGRABBER_KEYS;
447                 extraMargin = me.realFontSize.x * 0.5;
448         }
449
450         if(substring(descr, 0, 1) == "$")
451         {
452                 s = substring(descr, 1, strlen(descr) - 1);
453                 descr = cvar_string(strcat(s, "_description"));
454                 if(descr == "")
455                         descr = s;
456                 if(cvar_string(strcat(s, "_press")) == "")
457                         if(cvar_string(strcat(s, "_release")) == "")
458                                 theAlpha *= SKINALPHA_DISABLED;
459         }
460
461         s = draw_TextShortenToWidth(descr, me.columnFunctionSize, 0, me.realFontSize);
462         draw_Text(me.realUpperMargin * eY + extraMargin * eX, s, me.realFontSize, theColor, theAlpha, 0);
463         if(func != "")
464         {
465                 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
466                 s = "";
467                 for(j = 0; j < n; ++j)
468                 {
469                         k = stof(argv(j));
470                         if(k != -1)
471                         {
472                                 if(s != "")
473                                         s = strcat(s, ", ");
474                                 s = strcat(s, keynumtostring(k));
475                         }
476                 }
477                 s = draw_TextShortenToWidth(s, me.columnKeysSize, 0, me.realFontSize);
478                 draw_CenterText(me.realUpperMargin * eY + (me.columnKeysOrigin + 0.5 * me.columnKeysSize) * eX, s, me.realFontSize, theColor, theAlpha, 0);
479         }
480 }
481 #endif