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