1 #include "keybinder.qh"
3 #include <common/weapons/_all.qh>
7 #include "dialog_settings_input_userbind.qh"
9 const string KEY_NOT_BOUND_CMD = "// not bound";
11 const int MAX_KEYS_PER_FUNCTION = 2;
12 const int MAX_KEYBINDS = 256;
13 string KeyBinds_Functions[MAX_KEYBINDS];
14 string KeyBinds_Descriptions[MAX_KEYBINDS];
15 int KeyBinds_Count = -1;
17 void KeyBinds_BuildList()
21 #define KEYBIND_DEF(func, desc) MACRO_BEGIN \
22 if((KeyBinds_Count < MAX_KEYBINDS)) { \
23 KeyBinds_Functions[KeyBinds_Count] = strzone(func); \
24 KeyBinds_Descriptions[KeyBinds_Count] = strzone(desc); \
29 #define KEYBIND_EMPTY_LINE() KEYBIND_DEF("", "")
30 #define KEYBIND_HEADER(str) KEYBIND_DEF("", str)
32 #define KEYBIND_IS_SPECIAL(func) (substring(func, 0 ,1) == "*")
33 #define KEYBIND_SPECIAL_DEF(key, desc) KEYBIND_DEF(strcat("*", key), desc)
35 KEYBIND_HEADER(_("Moving"));
36 KEYBIND_DEF("+forward" , _("forward"));
37 KEYBIND_DEF("+back" , _("backpedal"));
38 KEYBIND_DEF("+moveleft" , _("strafe left"));
39 KEYBIND_DEF("+moveright" , _("strafe right"));
40 KEYBIND_DEF("+jump" , _("jump / swim"));
41 KEYBIND_DEF("+crouch" , _("crouch / sink"));
42 KEYBIND_DEF("+hook" , _("off-hand hook"));
43 KEYBIND_DEF("+jetpack" , _("jetpack"));
46 KEYBIND_HEADER(_("Attacking"));
47 KEYBIND_DEF("+fire" , _("primary fire"));
48 KEYBIND_DEF("+fire2" , _("secondary fire"));
51 KEYBIND_HEADER(_("Weapons"));
52 KEYBIND_DEF("weapprev" , CTX(_("WEAPON^previous")));
53 KEYBIND_DEF("weapnext" , CTX(_("WEAPON^next")));
54 KEYBIND_DEF("weaplast" , CTX(_("WEAPON^previously used")));
55 KEYBIND_DEF("weapbest" , CTX(_("WEAPON^best")));
56 KEYBIND_DEF("reload" , _("reload"));
57 KEYBIND_DEF("dropweapon" , _("drop weapon / throw nade"));
59 #define ADD_TO_W_LIST(pred) \
60 FOREACH(Weapons, it != WEP_Null, { \
61 if (it.impulse != imp) continue; \
62 if (!(pred)) continue; \
63 w_list = strcat(w_list, it.m_name, " / "); \
66 for(int imp = 1; imp <= 9; ++imp)
69 ADD_TO_W_LIST(!(it.spawnflags & (WEP_FLAG_MUTATORBLOCKED | WEP_FLAG_HIDDEN | WEP_FLAG_SPECIALATTACK | WEP_FLAG_SUPERWEAPON)));
70 ADD_TO_W_LIST((it.spawnflags & WEP_FLAG_SUPERWEAPON) && !(it.spawnflags & WEP_FLAG_HIDDEN));
71 ADD_TO_W_LIST((it.spawnflags & WEP_FLAG_MUTATORBLOCKED) && !(it.spawnflags & (WEP_FLAG_HIDDEN | WEP_FLAG_SPECIALATTACK)));
73 KEYBIND_DEF(strcat("weapon_group_", itos(imp)), substring(w_list, 0, -4));
83 KEYBIND_HEADER(_("View"));
84 KEYBIND_DEF("+zoom" , _("hold zoom"));
85 KEYBIND_DEF("togglezoom" , _("toggle zoom"));
86 KEYBIND_DEF("+showscores" , _("show scores"));
87 KEYBIND_DEF("screenshot" , _("screen shot"));
88 KEYBIND_DEF("+hud_panel_radar_maximized" , _("maximize radar"));
89 KEYBIND_DEF("toggle chase_active" , _("3rd person view"));
90 KEYBIND_DEF("spec" , _("enter spectator mode"));
93 KEYBIND_HEADER(_("Communication"));
94 KEYBIND_DEF("messagemode" , _("public chat"));
95 KEYBIND_DEF("messagemode2" , _("team chat"));
96 KEYBIND_DEF("+con_chat_maximize" , _("show chat history"));
97 KEYBIND_DEF("vyes" , _("vote YES"));
98 KEYBIND_DEF("vno" , _("vote NO"));
99 KEYBIND_DEF("ready" , _("ready"));
100 KEYBIND_EMPTY_LINE();
102 KEYBIND_HEADER(_("Client"));
103 KEYBIND_DEF("+show_info" , _("server info"));
104 // display the hardcoded shortcut to open the console as it works for all
105 // non-English keyboard layouts, unlike default keys (` and ~)
106 KEYBIND_DEF("toggleconsole" , _("enter console"));
107 string console_shortcut = strcat(translate_key("SHIFT"), "+", translate_key("ESCAPE"));
108 KEYBIND_SPECIAL_DEF(console_shortcut, _("enter console"));
109 KEYBIND_DEF("disconnect" , _("disconnect"));
110 KEYBIND_DEF("menu_showquitdialog" , _("quit"));
111 KEYBIND_EMPTY_LINE();
113 KEYBIND_HEADER(_("Teamplay"));
114 KEYBIND_DEF("team_auto" , _("auto-join team"));
115 KEYBIND_DEF("menu_showteamselect" , _("team menu"));
116 KEYBIND_DEF("spec" , _("spectate"));
117 KEYBIND_EMPTY_LINE();
119 KEYBIND_HEADER(_("Misc"));
120 KEYBIND_DEF("+use" , _("drop key/flag, exit vehicle"));
121 KEYBIND_DEF("kill" , _("suicide / respawn"));
122 KEYBIND_DEF("quickmenu" , _("quick menu"));
123 KEYBIND_EMPTY_LINE();
125 KEYBIND_HEADER(_("User defined"));
127 for(int i = 1; i <= 32; ++i)
128 KEYBIND_DEF(strcat("+userbind ", itos(i)), strcat("$userbind", itos(i)));
130 KEYBIND_EMPTY_LINE();
132 KEYBIND_HEADER(_("Development"));
133 KEYBIND_DEF("menu_showsandboxtools" , _("sandbox menu"));
134 KEYBIND_DEF("+button8" , _("drag object (sandbox)"));
135 KEYBIND_DEF("wpeditor_menu" , _("waypoint editor menu"));
140 entity makeXonoticKeyBinder()
143 me = NEW(XonoticKeyBinder);
144 me.configureXonoticKeyBinder(me);
147 void replace_bind(string from, string to)
150 float k; // not sure if float or int
151 n = tokenize(findkeysforcommand(from, 0)); // uses '...' strings
152 for(j = 0; j < n; ++j)
156 localcmd("\nbind \"", keynumtostring(k), "\" \"", to, "\"\n");
159 cvar_set("_hud_showbinds_reload", "1");
161 void XonoticKeyBinder_configureXonoticKeyBinder(entity me)
163 me.configureXonoticListBox(me);
166 // TEMP: Xonotic 0.1 to later
167 replace_bind("impulse 1", "weapon_group_1");
168 replace_bind("impulse 2", "weapon_group_2");
169 replace_bind("impulse 3", "weapon_group_3");
170 replace_bind("impulse 4", "weapon_group_4");
171 replace_bind("impulse 5", "weapon_group_5");
172 replace_bind("impulse 6", "weapon_group_6");
173 replace_bind("impulse 7", "weapon_group_7");
174 replace_bind("impulse 8", "weapon_group_8");
175 replace_bind("impulse 9", "weapon_group_9");
176 replace_bind("impulse 14", "weapon_group_0");
178 void XonoticKeyBinder_loadKeyBinds(entity me)
180 bool force_initial_selection = false;
181 if(KeyBinds_Count < 0) // me.handle not loaded yet?
182 force_initial_selection = true;
183 KeyBinds_BuildList();
184 me.nItems = KeyBinds_Count;
185 if(force_initial_selection)
186 me.setSelected(me, 0);
188 void XonoticKeyBinder_showNotify(entity me)
193 void XonoticKeyBinder_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
195 SUPER(XonoticKeyBinder).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
197 me.realFontSize_y = me.fontSize / (absSize.y * me.itemHeight);
198 me.realFontSize_x = me.fontSize / (absSize.x * (1 - me.controlWidth));
199 me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
201 me.columnFunctionOrigin = 0;
202 me.columnKeysSize = me.realFontSize.x * 12;
203 me.columnFunctionSize = 1 - me.columnKeysSize - 2 * me.realFontSize.x;
204 me.columnKeysOrigin = me.columnFunctionOrigin + me.columnFunctionSize + me.realFontSize.x;
206 void KeyBinder_Bind_Change(entity btn, entity me)
208 string func = KeyBinds_Functions[me.selectedItem];
209 if(func == "" || KEYBIND_IS_SPECIAL(func))
212 me.keyGrabButton.forcePressed = 1;
213 me.clearButton.disabled = 1;
216 void XonoticKeyBinder_keyGrabbed(entity me, int key, bool ascii)
221 me.keyGrabButton.forcePressed = 0;
222 me.clearButton.disabled = 0;
227 // forbid these keys from being bound in the menu
228 if(key == K_CAPSLOCK || key == K_NUMLOCK)
230 KeyBinder_Bind_Change(me, me);
234 string func = KeyBinds_Functions[me.selectedItem];
235 if(func == "" || KEYBIND_IS_SPECIAL(func))
238 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
240 for(j = 0; j < n; ++j)
246 if(nvalid >= MAX_KEYS_PER_FUNCTION)
248 for(j = 0; j < n; ++j)
253 // bind to empty cmd instead of using unbind so it gets saved in config and overrides any default binds
254 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
258 m_play_click_sound(MENU_SOUND_SELECT);
259 localcmd("\nbind \"", keynumtostring(key), "\" \"", func, "\"\n");
260 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
261 cvar_set("_hud_showbinds_reload", "1");
263 void XonoticKeyBinder_destroy(entity me)
265 if(KeyBinds_Count < 0)
268 for(int i = 0; i < MAX_KEYBINDS; ++i)
270 strfree(KeyBinds_Functions[i]);
271 strfree(KeyBinds_Descriptions[i]);
275 void XonoticKeyBinder_editUserbind(entity me, string theName, string theCommandPress, string theCommandRelease)
277 if(!me.userbindEditDialog)
280 string func = KeyBinds_Functions[me.selectedItem];
281 if(func == "" || KEYBIND_IS_SPECIAL(func))
284 string descr = KeyBinds_Descriptions[me.selectedItem];
285 if(substring(descr, 0, 1) != "$")
287 descr = substring(descr, 1, strlen(descr) - 1);
289 // Hooray! It IS a user bind!
290 cvar_set(strcat(descr, "_description"), theName);
291 cvar_set(strcat(descr, "_press"), theCommandPress);
292 cvar_set(strcat(descr, "_release"), theCommandRelease);
294 void KeyBinder_Bind_Edit(entity btn, entity me)
296 if(!me.userbindEditDialog)
299 string func = KeyBinds_Functions[me.selectedItem];
300 if(func == "" || KEYBIND_IS_SPECIAL(func))
303 string descr = KeyBinds_Descriptions[me.selectedItem];
304 if(substring(descr, 0, 1) != "$")
306 descr = substring(descr, 1, strlen(descr) - 1);
308 // Hooray! It IS a user bind!
309 me.userbindEditDialog.loadUserBind(me.userbindEditDialog, cvar_string(strcat(descr, "_description")), cvar_string(strcat(descr, "_press")), cvar_string(strcat(descr, "_release")));
311 DialogOpenButton_Click(btn, me.userbindEditDialog);
313 void KeyBinder_Bind_Clear(entity btn, entity me)
317 string func = KeyBinds_Functions[me.selectedItem];
318 if(func == "" || KEYBIND_IS_SPECIAL(func))
321 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
322 for(j = 0; j < n; ++j)
327 // bind to empty cmd instead of using unbind so it gets saved in config and overrides any default binds
328 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
331 m_play_click_sound(MENU_SOUND_CLEAR);
332 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
333 cvar_set("_hud_showbinds_reload", "1");
335 void KeyBinder_Bind_Reset_All(entity btn, entity me)
337 localcmd("unbindall\n");
338 localcmd("exec binds-xonotic.cfg\n");
339 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
340 cvar_set("_hud_showbinds_reload", "1");
343 void XonoticKeyBinder_doubleClickListBoxItem(entity me, float i, vector where)
345 KeyBinder_Bind_Change(NULL, me);
347 void XonoticKeyBinder_setSelected(entity me, int i)
349 // handling of "unselectable" items
350 i = floor(0.5 + bound(0, i, me.nItems - 1));
351 if (KEYBIND_IS_SPECIAL(KeyBinds_Functions[i]))
353 SUPER(XonoticKeyBinder).setSelected(me, i - 1);
356 if(me.pressed == 0 || me.pressed == 1) // keyboard or scrolling - skip unselectable items
358 if(i > me.previouslySelected)
360 while((i < me.nItems - 1) && (KeyBinds_Functions[i] == ""))
363 while((i > 0) && (KeyBinds_Functions[i] == ""))
365 while((i < me.nItems - 1) && (KeyBinds_Functions[i] == ""))
368 if(me.pressed == 3) // released the mouse - fall back to last valid item
370 if(KeyBinds_Functions[i] == "")
371 i = me.previouslySelected;
373 if(KeyBinds_Functions[i] != "")
374 me.previouslySelected = i;
375 if(me.userbindEditButton)
376 me.userbindEditButton.disabled = (substring(KeyBinds_Descriptions[i], 0, 1) != "$");
377 SUPER(XonoticKeyBinder).setSelected(me, i);
379 float XonoticKeyBinder_keyDown(entity me, int key, bool ascii, float shift)
387 KeyBinder_Bind_Change(me, me);
392 KeyBinder_Bind_Clear(me, me);
395 r = SUPER(XonoticKeyBinder).keyDown(me, key, ascii, shift);
401 void XonoticKeyBinder_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
407 string descr = KeyBinds_Descriptions[i];
408 string func = KeyBinds_Functions[i];
412 theColor = SKINCOLOR_KEYGRABBER_TITLES;
413 theAlpha = SKINALPHA_KEYGRABBER_TITLES;
421 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_WAITING, SKINALPHA_LISTBOX_WAITING);
423 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
427 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
428 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
431 theAlpha = SKINALPHA_KEYGRABBER_KEYS;
432 theColor = SKINCOLOR_KEYGRABBER_KEYS;
433 extraMargin = me.realFontSize.x * 0.5;
436 if(substring(descr, 0, 1) == "$")
438 string s = substring(descr, 1, strlen(descr) - 1);
439 descr = cvar_string(strcat(s, "_description"));
442 if(cvar_string(strcat(s, "_press")) == "")
443 if(cvar_string(strcat(s, "_release")) == "")
444 theAlpha *= SKINALPHA_DISABLED;
447 string s = draw_TextShortenToWidth(descr, me.columnFunctionSize, 0, me.realFontSize);
448 draw_Text(me.realUpperMargin * eY + extraMargin * eX, s, me.realFontSize, theColor, theAlpha, 0);
454 if (KEYBIND_IS_SPECIAL(func))
455 s = substring(func, 1, -1);
458 bool joy_active = cvar("joy_active");
459 int n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
460 for(int j = 0; j < n; ++j)
462 float k = stof(argv(j));
465 string key = keynumtostring(k);
466 if (!joy_active && startsWith(key, "JOY"))
470 s = strcat(s, translate_key(key));
474 s = draw_TextShortenToWidth(s, me.columnKeysSize, 0, me.realFontSize);
475 draw_CenterText(me.realUpperMargin * eY + (me.columnKeysOrigin + 0.5 * me.columnKeysSize) * eX, s, me.realFontSize, theColor, theAlpha, 0);