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