1 #include "keybinder.qh"
5 CLASS(XonoticKeyBinder, XonoticListBox)
6 METHOD(XonoticKeyBinder, configureXonoticKeyBinder, void(entity));
7 ATTRIB(XonoticKeyBinder, rowsPerItem, int, 1)
8 METHOD(XonoticKeyBinder, drawListBoxItem, void(entity, int, vector, bool, bool));
9 METHOD(XonoticKeyBinder, doubleClickListBoxItem, void(entity, float, vector));
10 METHOD(XonoticKeyBinder, resizeNotify, void(entity, vector, vector, vector, vector));
11 METHOD(XonoticKeyBinder, showNotify, void(entity));
12 METHOD(XonoticKeyBinder, setSelected, void(entity, float));
13 METHOD(XonoticKeyBinder, keyDown, float(entity, float, float, float));
14 METHOD(XonoticKeyBinder, keyGrabbed, void(entity, float, float));
15 METHOD(XonoticKeyBinder, destroy, void(entity));
17 ATTRIB(XonoticKeyBinder, realFontSize, vector, '0 0 0')
18 ATTRIB(XonoticKeyBinder, realUpperMargin, float, 0)
19 ATTRIB(XonoticKeyBinder, columnFunctionOrigin, float, 0)
20 ATTRIB(XonoticKeyBinder, columnFunctionSize, float, 0)
21 ATTRIB(XonoticKeyBinder, columnKeysOrigin, float, 0)
22 ATTRIB(XonoticKeyBinder, columnKeysSize, float, 0)
24 METHOD(XonoticKeyBinder, loadKeyBinds, void(entity));
25 ATTRIB(XonoticKeyBinder, previouslySelected, int, -1)
26 ATTRIB(XonoticKeyBinder, inMouseHandler, float, 0)
27 ATTRIB(XonoticKeyBinder, userbindEditButton, entity, NULL)
28 ATTRIB(XonoticKeyBinder, keyGrabButton, entity, NULL)
29 ATTRIB(XonoticKeyBinder, clearButton, entity, NULL)
30 ATTRIB(XonoticKeyBinder, userbindEditDialog, entity, NULL)
31 METHOD(XonoticKeyBinder, editUserbind, void(entity, string, string, string));
32 ENDCLASS(XonoticKeyBinder)
33 entity makeXonoticKeyBinder();
34 void KeyBinder_Bind_Change(entity btn, entity me);
35 void KeyBinder_Bind_Clear(entity btn, entity me);
36 void KeyBinder_Bind_Edit(entity btn, entity me);
37 void KeyBinder_Bind_Reset_All(entity btn, entity me);
42 const string KEY_NOT_BOUND_CMD = "// not bound";
44 const int MAX_KEYS_PER_FUNCTION = 2;
45 const int MAX_KEYBINDS = 256;
46 string Xonotic_KeyBinds_Functions[MAX_KEYBINDS];
47 string Xonotic_KeyBinds_Descriptions[MAX_KEYBINDS];
48 int Xonotic_KeyBinds_Count = -1;
50 void Xonotic_KeyBinds_Read()
52 Xonotic_KeyBinds_Count = 0;
54 #define KEYBIND_DEF(func, desc) MACRO_BEGIN { \
55 if((Xonotic_KeyBinds_Count < MAX_KEYBINDS)) { \
56 Xonotic_KeyBinds_Functions[Xonotic_KeyBinds_Count] = strzone(func); \
57 Xonotic_KeyBinds_Descriptions[Xonotic_KeyBinds_Count] = strzone(desc); \
58 ++Xonotic_KeyBinds_Count; \
62 KEYBIND_DEF("" , _("Moving"));
63 KEYBIND_DEF("+forward" , _("forward"));
64 KEYBIND_DEF("+back" , _("backpedal"));
65 KEYBIND_DEF("+moveleft" , _("strafe left"));
66 KEYBIND_DEF("+moveright" , _("strafe right"));
67 KEYBIND_DEF("+jump" , _("jump / swim"));
68 KEYBIND_DEF("+crouch" , _("crouch / sink"));
69 KEYBIND_DEF("+hook" , _("off-hand hook"));
70 KEYBIND_DEF("+jetpack" , _("jet pack"));
72 KEYBIND_DEF("" , _("Attacking"));
73 KEYBIND_DEF("+fire" , _("primary fire"));
74 KEYBIND_DEF("+fire2" , _("secondary fire"));
76 KEYBIND_DEF("" , _("Weapon switching"));
77 KEYBIND_DEF("weapprev" , _("previous"));
78 KEYBIND_DEF("weapnext" , _("next"));
79 KEYBIND_DEF("weaplast" , _("previously used"));
80 KEYBIND_DEF("weapbest" , _("best"));
81 KEYBIND_DEF("reload" , _("reload"));
85 #define ADD_TO_W_LIST(pred) \
86 FOREACH(Weapons, it != WEP_Null, LAMBDA( \
87 if (it.impulse != imp) continue; \
88 if (!(pred)) continue; \
89 w_list = strcat(w_list, it.m_name, " / "); \
92 for(int imp = 1; imp <= 9; ++imp)
95 ADD_TO_W_LIST(!(it.flags & WEP_FLAG_MUTATORBLOCKED) && !(it.flags & WEP_FLAG_SUPERWEAPON));
96 ADD_TO_W_LIST(it.flags & WEP_FLAG_SUPERWEAPON);
97 ADD_TO_W_LIST(it.flags & WEP_FLAG_MUTATORBLOCKED);
99 KEYBIND_DEF(strcat("weapon_group_", itos(imp)), substring(w_list, 0, -4));
107 KEYBIND_DEF("" , "");
108 KEYBIND_DEF("" , _("View"));
109 KEYBIND_DEF("+zoom" , _("hold zoom"));
110 KEYBIND_DEF("togglezoom" , _("toggle zoom"));
111 KEYBIND_DEF("+showscores" , _("show scores"));
112 KEYBIND_DEF("screenshot" , _("screen shot"));
113 KEYBIND_DEF("+hud_panel_radar_maximized" , _("maximize radar"));
114 KEYBIND_DEF("" , "");
115 KEYBIND_DEF("" , _("Communicate"));
116 KEYBIND_DEF("messagemode" , _("public chat"));
117 KEYBIND_DEF("messagemode2" , _("team chat"));
118 KEYBIND_DEF("+con_chat_maximize" , _("show chat history"));
119 KEYBIND_DEF("vyes" , _("vote YES"));
120 KEYBIND_DEF("vno" , _("vote NO"));
121 KEYBIND_DEF("ready" , _("ready"));
122 KEYBIND_DEF("" , "");
123 KEYBIND_DEF("" , _("Client"));
124 KEYBIND_DEF("+show_info" , _("server info"));
125 KEYBIND_DEF("toggleconsole" , _("enter console"));
126 KEYBIND_DEF("disconnect" , _("disconnect"));
127 KEYBIND_DEF("menu_showquitdialog" , _("quit"));
128 KEYBIND_DEF("" , "");
129 KEYBIND_DEF("" , _("Teamplay"));
130 KEYBIND_DEF("messagemode2" , _("team chat"));
131 KEYBIND_DEF("team_auto" , _("auto-join team"));
132 KEYBIND_DEF("menu_showteamselect" , _("team menu"));
133 KEYBIND_DEF("menu_showsandboxtools" , _("sandbox menu"));
134 KEYBIND_DEF("spec" , _("enter spectator mode"));
135 KEYBIND_DEF("dropweapon" , _("drop weapon"));
136 KEYBIND_DEF("+use" , _("drop key / drop flag"));
137 KEYBIND_DEF("+button8" , _("drag object"));
138 KEYBIND_DEF("toggle chase_active" , _("3rd person view"));
139 KEYBIND_DEF("" , "");
140 KEYBIND_DEF("" , _("User defined"));
142 for(i = 1; i <= 32; ++i)
143 KEYBIND_DEF(strcat("+userbind ", itos(i)), strcat("$userbind", itos(i)));
148 entity makeXonoticKeyBinder()
151 me = NEW(XonoticKeyBinder);
152 me.configureXonoticKeyBinder(me);
155 void replace_bind(string from, string to)
158 float k; // not sure if float or int
159 n = tokenize(findkeysforcommand(from, 0)); // uses '...' strings
160 for(j = 0; j < n; ++j)
164 localcmd("\nbind \"", keynumtostring(k), "\" \"", to, "\"\n");
167 cvar_set("_hud_showbinds_reload", "1");
169 void XonoticKeyBinder_configureXonoticKeyBinder(entity me)
171 me.configureXonoticListBox(me);
174 // TEMP: Xonotic 0.1 to later
175 replace_bind("impulse 1", "weapon_group_1");
176 replace_bind("impulse 2", "weapon_group_2");
177 replace_bind("impulse 3", "weapon_group_3");
178 replace_bind("impulse 4", "weapon_group_4");
179 replace_bind("impulse 5", "weapon_group_5");
180 replace_bind("impulse 6", "weapon_group_6");
181 replace_bind("impulse 7", "weapon_group_7");
182 replace_bind("impulse 8", "weapon_group_8");
183 replace_bind("impulse 9", "weapon_group_9");
184 replace_bind("impulse 14", "weapon_group_0");
186 void XonoticKeyBinder_loadKeyBinds(entity me)
188 bool force_initial_selection = false;
189 if(Xonotic_KeyBinds_Count < 0) // me.handle not loaded yet?
190 force_initial_selection = true;
191 Xonotic_KeyBinds_Read();
192 me.nItems = Xonotic_KeyBinds_Count;
193 if(force_initial_selection)
194 me.setSelected(me, 0);
196 void XonoticKeyBinder_showNotify(entity me)
201 void XonoticKeyBinder_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
203 SUPER(XonoticKeyBinder).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
205 me.realFontSize_y = me.fontSize / (absSize.y * me.itemHeight);
206 me.realFontSize_x = me.fontSize / (absSize.x * (1 - me.controlWidth));
207 me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
209 me.columnFunctionOrigin = 0;
210 me.columnKeysSize = me.realFontSize.x * 12;
211 me.columnFunctionSize = 1 - me.columnKeysSize - 2 * me.realFontSize.x;
212 me.columnKeysOrigin = me.columnFunctionOrigin + me.columnFunctionSize + me.realFontSize.x;
214 void KeyBinder_Bind_Change(entity btn, entity me)
218 func = Xonotic_KeyBinds_Functions[me.selectedItem];
222 me.keyGrabButton.forcePressed = 1;
223 me.clearButton.disabled = 1;
226 void XonoticKeyBinder_keyGrabbed(entity me, int key, bool ascii)
232 me.keyGrabButton.forcePressed = 0;
233 me.clearButton.disabled = 0;
238 // forbid these keys from being bound in the menu
239 if(key == K_CAPSLOCK || key == K_NUMLOCK)
241 KeyBinder_Bind_Change(me, me);
245 func = Xonotic_KeyBinds_Functions[me.selectedItem];
249 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
251 for(j = 0; j < n; ++j)
257 if(nvalid >= MAX_KEYS_PER_FUNCTION)
259 for(j = 0; j < n; ++j)
263 //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
264 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
267 m_play_click_sound(MENU_SOUND_SELECT);
268 localcmd("\nbind \"", keynumtostring(key), "\" \"", func, "\"\n");
269 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
270 cvar_set("_hud_showbinds_reload", "1");
272 void XonoticKeyBinder_destroy(entity me)
274 if(Xonotic_KeyBinds_Count < 0)
277 for(int i = 0; i < MAX_KEYBINDS; ++i)
279 if(Xonotic_KeyBinds_Functions[i])
280 strunzone(Xonotic_KeyBinds_Functions[i]);
281 Xonotic_KeyBinds_Functions[i] = string_null;
282 if(Xonotic_KeyBinds_Descriptions[i])
283 strunzone(Xonotic_KeyBinds_Descriptions[i]);
284 Xonotic_KeyBinds_Descriptions[i] = string_null;
286 Xonotic_KeyBinds_Count = 0;
288 void XonoticKeyBinder_editUserbind(entity me, string theName, string theCommandPress, string theCommandRelease)
292 if(!me.userbindEditDialog)
295 func = Xonotic_KeyBinds_Functions[me.selectedItem];
299 descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
300 if(substring(descr, 0, 1) != "$")
302 descr = substring(descr, 1, strlen(descr) - 1);
304 // Hooray! It IS a user bind!
305 cvar_set(strcat(descr, "_description"), theName);
306 cvar_set(strcat(descr, "_press"), theCommandPress);
307 cvar_set(strcat(descr, "_release"), theCommandRelease);
309 void KeyBinder_Bind_Edit(entity btn, entity me)
313 if(!me.userbindEditDialog)
316 func = Xonotic_KeyBinds_Functions[me.selectedItem];
320 descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
321 if(substring(descr, 0, 1) != "$")
323 descr = substring(descr, 1, strlen(descr) - 1);
325 // Hooray! It IS a user bind!
326 me.userbindEditDialog.loadUserBind(me.userbindEditDialog, cvar_string(strcat(descr, "_description")), cvar_string(strcat(descr, "_press")), cvar_string(strcat(descr, "_release")));
328 DialogOpenButton_Click(btn, me.userbindEditDialog);
330 void KeyBinder_Bind_Clear(entity btn, entity me)
335 func = Xonotic_KeyBinds_Functions[me.selectedItem];
339 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
340 for(j = 0; j < n; ++j)
344 //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
345 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
347 m_play_click_sound(MENU_SOUND_CLEAR);
348 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
349 cvar_set("_hud_showbinds_reload", "1");
351 void KeyBinder_Bind_Reset_All(entity btn, entity me)
353 localcmd("unbindall\n");
354 localcmd("exec binds-xonotic.cfg\n");
355 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
356 cvar_set("_hud_showbinds_reload", "1");
358 void XonoticKeyBinder_doubleClickListBoxItem(entity me, float i, vector where)
360 KeyBinder_Bind_Change(NULL, me);
362 void XonoticKeyBinder_setSelected(entity me, int i)
364 // handling of "unselectable" items
365 i = floor(0.5 + bound(0, i, me.nItems - 1));
366 if(me.pressed == 0 || me.pressed == 1) // keyboard or scrolling - skip unselectable items
368 if(i > me.previouslySelected)
370 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
373 while((i > 0) && (Xonotic_KeyBinds_Functions[i] == ""))
375 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
378 if(me.pressed == 3) // released the mouse - fall back to last valid item
380 if(Xonotic_KeyBinds_Functions[i] == "")
381 i = me.previouslySelected;
383 if(Xonotic_KeyBinds_Functions[i] != "")
384 me.previouslySelected = i;
385 if(me.userbindEditButton)
386 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[i], 0, 1) != "$");
387 SUPER(XonoticKeyBinder).setSelected(me, i);
389 float XonoticKeyBinder_keyDown(entity me, int key, bool ascii, float shift)
397 KeyBinder_Bind_Change(me, me);
402 KeyBinder_Bind_Clear(me, me);
405 r = SUPER(XonoticKeyBinder).keyDown(me, key, ascii, shift);
410 void XonoticKeyBinder_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
420 descr = Xonotic_KeyBinds_Descriptions[i];
421 func = Xonotic_KeyBinds_Functions[i];
426 theColor = SKINCOLOR_KEYGRABBER_TITLES;
427 theAlpha = SKINALPHA_KEYGRABBER_TITLES;
435 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_WAITING, SKINALPHA_LISTBOX_WAITING);
437 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
441 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
442 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
445 theAlpha = SKINALPHA_KEYGRABBER_KEYS;
446 theColor = SKINCOLOR_KEYGRABBER_KEYS;
447 extraMargin = me.realFontSize.x * 0.5;
450 if(substring(descr, 0, 1) == "$")
452 s = substring(descr, 1, strlen(descr) - 1);
453 descr = cvar_string(strcat(s, "_description"));
456 if(cvar_string(strcat(s, "_press")) == "")
457 if(cvar_string(strcat(s, "_release")) == "")
458 theAlpha *= SKINALPHA_DISABLED;
461 s = draw_TextShortenToWidth(descr, me.columnFunctionSize, 0, me.realFontSize);
462 draw_Text(me.realUpperMargin * eY + extraMargin * eX, s, me.realFontSize, theColor, theAlpha, 0);
465 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
467 for(j = 0; j < n; ++j)
474 s = strcat(s, keynumtostring(k));
477 s = draw_TextShortenToWidth(s, me.columnKeysSize, 0, me.realFontSize);
478 draw_CenterText(me.realUpperMargin * eY + (me.columnKeysOrigin + 0.5 * me.columnKeysSize) * eX, s, me.realFontSize, theColor, theAlpha, 0);