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