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