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