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))
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)
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);
38 const string KEY_NOT_BOUND_CMD = "// not bound";
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;
46 void Xonotic_KeyBinds_Read()
51 Xonotic_KeyBinds_Count = 0;
52 fh = fopen(language_filename("keybinds.txt"), FILE_READ);
55 while((s = fgets(fh)))
57 if(tokenize_console(s) != 2)
59 Xonotic_KeyBinds_Functions[Xonotic_KeyBinds_Count] = strzone(argv(0));
60 Xonotic_KeyBinds_Descriptions[Xonotic_KeyBinds_Count] = strzone(argv(1));
61 ++Xonotic_KeyBinds_Count;
62 if(Xonotic_KeyBinds_Count >= MAX_KEYBINDS)
68 entity makeXonoticKeyBinder()
71 me = NEW(XonoticKeyBinder);
72 me.configureXonoticKeyBinder(me);
75 void replace_bind(string from, string to)
78 float k; // not sure if float or int
79 n = tokenize(findkeysforcommand(from, 0)); // uses '...' strings
80 for(j = 0; j < n; ++j)
84 localcmd("\nbind \"", keynumtostring(k), "\" \"", to, "\"\n");
87 cvar_set("_hud_showbinds_reload", "1");
89 void XonoticKeyBinder_configureXonoticKeyBinder(entity me)
91 me.configureXonoticListBox(me);
92 if(Xonotic_KeyBinds_Count < 0)
93 Xonotic_KeyBinds_Read();
94 me.nItems = Xonotic_KeyBinds_Count;
95 me.setSelected(me, 0);
97 // TEMP: Xonotic 0.1 to later
98 replace_bind("impulse 1", "weapon_group_1");
99 replace_bind("impulse 2", "weapon_group_2");
100 replace_bind("impulse 3", "weapon_group_3");
101 replace_bind("impulse 4", "weapon_group_4");
102 replace_bind("impulse 5", "weapon_group_5");
103 replace_bind("impulse 6", "weapon_group_6");
104 replace_bind("impulse 7", "weapon_group_7");
105 replace_bind("impulse 8", "weapon_group_8");
106 replace_bind("impulse 9", "weapon_group_9");
107 replace_bind("impulse 14", "weapon_group_0");
109 void XonoticKeyBinder_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
111 SUPER(XonoticKeyBinder).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
113 me.realFontSize_y = me.fontSize / (absSize.y * me.itemHeight);
114 me.realFontSize_x = me.fontSize / (absSize.x * (1 - me.controlWidth));
115 me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
117 me.columnFunctionOrigin = 0;
118 me.columnKeysSize = me.realFontSize.x * 12;
119 me.columnFunctionSize = 1 - me.columnKeysSize - 2 * me.realFontSize.x;
120 me.columnKeysOrigin = me.columnFunctionOrigin + me.columnFunctionSize + me.realFontSize.x;
122 if(me.userbindEditButton)
123 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[me.selectedItem], 0, 1) != "$");
125 void KeyBinder_Bind_Change(entity btn, entity me)
129 func = Xonotic_KeyBinds_Functions[me.selectedItem];
133 me.keyGrabButton.forcePressed = 1;
134 me.clearButton.disabled = 1;
137 void XonoticKeyBinder_keyGrabbed(entity me, int key, bool ascii)
143 me.keyGrabButton.forcePressed = 0;
144 me.clearButton.disabled = 0;
149 // forbid these keys from being bound in the menu
150 if(key == K_CAPSLOCK || key == K_NUMLOCK)
152 KeyBinder_Bind_Change(me, me);
156 func = Xonotic_KeyBinds_Functions[me.selectedItem];
160 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
162 for(j = 0; j < n; ++j)
168 if(nvalid >= MAX_KEYS_PER_FUNCTION)
170 for(j = 0; j < n; ++j)
174 //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
175 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
178 m_play_click_sound(MENU_SOUND_SELECT);
179 localcmd("\nbind \"", keynumtostring(key), "\" \"", func, "\"\n");
180 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
181 cvar_set("_hud_showbinds_reload", "1");
183 void XonoticKeyBinder_editUserbind(entity me, string theName, string theCommandPress, string theCommandRelease)
187 if(!me.userbindEditDialog)
190 func = Xonotic_KeyBinds_Functions[me.selectedItem];
194 descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
195 if(substring(descr, 0, 1) != "$")
197 descr = substring(descr, 1, strlen(descr) - 1);
199 // Hooray! It IS a user bind!
200 cvar_set(strcat(descr, "_description"), theName);
201 cvar_set(strcat(descr, "_press"), theCommandPress);
202 cvar_set(strcat(descr, "_release"), theCommandRelease);
204 void KeyBinder_Bind_Edit(entity btn, entity me)
208 if(!me.userbindEditDialog)
211 func = Xonotic_KeyBinds_Functions[me.selectedItem];
215 descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
216 if(substring(descr, 0, 1) != "$")
218 descr = substring(descr, 1, strlen(descr) - 1);
220 // Hooray! It IS a user bind!
221 me.userbindEditDialog.loadUserBind(me.userbindEditDialog, cvar_string(strcat(descr, "_description")), cvar_string(strcat(descr, "_press")), cvar_string(strcat(descr, "_release")));
223 DialogOpenButton_Click(btn, me.userbindEditDialog);
225 void KeyBinder_Bind_Clear(entity btn, entity me)
230 func = Xonotic_KeyBinds_Functions[me.selectedItem];
234 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
235 for(j = 0; j < n; ++j)
239 //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
240 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
242 m_play_click_sound(MENU_SOUND_CLEAR);
243 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
244 cvar_set("_hud_showbinds_reload", "1");
246 void KeyBinder_Bind_Reset_All(entity btn, entity me)
248 localcmd("unbindall\n");
249 localcmd("exec binds-xonotic.cfg\n");
250 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
251 cvar_set("_hud_showbinds_reload", "1");
253 void XonoticKeyBinder_doubleClickListBoxItem(entity me, float i, vector where)
255 KeyBinder_Bind_Change(NULL, me);
257 void XonoticKeyBinder_setSelected(entity me, int i)
259 // handling of "unselectable" items
260 i = floor(0.5 + bound(0, i, me.nItems - 1));
261 if(me.pressed == 0 || me.pressed == 1) // keyboard or scrolling - skip unselectable items
263 if(i > me.previouslySelected)
265 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
268 while((i > 0) && (Xonotic_KeyBinds_Functions[i] == ""))
270 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
273 if(me.pressed == 3) // released the mouse - fall back to last valid item
275 if(Xonotic_KeyBinds_Functions[i] == "")
276 i = me.previouslySelected;
278 if(Xonotic_KeyBinds_Functions[i] != "")
279 me.previouslySelected = i;
280 if(me.userbindEditButton)
281 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[i], 0, 1) != "$");
282 SUPER(XonoticKeyBinder).setSelected(me, i);
284 float XonoticKeyBinder_keyDown(entity me, int key, bool ascii, float shift)
292 KeyBinder_Bind_Change(me, me);
297 KeyBinder_Bind_Clear(me, me);
300 r = SUPER(XonoticKeyBinder).keyDown(me, key, ascii, shift);
305 void XonoticKeyBinder_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
315 descr = Xonotic_KeyBinds_Descriptions[i];
316 func = Xonotic_KeyBinds_Functions[i];
321 theColor = SKINCOLOR_KEYGRABBER_TITLES;
322 theAlpha = SKINALPHA_KEYGRABBER_TITLES;
330 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_WAITING, SKINALPHA_LISTBOX_WAITING);
332 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
336 me.focusedItemAlpha = getFadedAlpha(me.focusedItemAlpha, SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED);
337 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, me.focusedItemAlpha);
340 theAlpha = SKINALPHA_KEYGRABBER_KEYS;
341 theColor = SKINCOLOR_KEYGRABBER_KEYS;
342 extraMargin = me.realFontSize.x * 0.5;
345 if(substring(descr, 0, 1) == "$")
347 s = substring(descr, 1, strlen(descr) - 1);
348 descr = cvar_string(strcat(s, "_description"));
351 if(cvar_string(strcat(s, "_press")) == "")
352 if(cvar_string(strcat(s, "_release")) == "")
353 theAlpha *= SKINALPHA_DISABLED;
356 s = draw_TextShortenToWidth(descr, me.columnFunctionSize, 0, me.realFontSize);
357 draw_Text(me.realUpperMargin * eY + extraMargin * eX, s, me.realFontSize, theColor, theAlpha, 0);
360 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
362 for(j = 0; j < n; ++j)
369 s = strcat(s, keynumtostring(k));
372 s = draw_TextShortenToWidth(s, me.columnKeysSize, 0, me.realFontSize);
373 draw_CenterText(me.realUpperMargin * eY + (me.columnKeysOrigin + 0.5 * me.columnKeysSize) * eX, s, me.realFontSize, theColor, theAlpha, 0);