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