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