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