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