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