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