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