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