2 CLASS(XonoticKeyBinder) EXTENDS(XonoticListBox)
3 METHOD(XonoticKeyBinder, configureXonoticKeyBinder, void(entity))
4 ATTRIB(XonoticKeyBinder, rowsPerItem, int, 1)
5 METHOD(XonoticKeyBinder, drawListBoxItem, void(entity, int, vector, bool, float))
6 METHOD(XonoticKeyBinder, doubleClickListBoxItem, void(entity, float, vector))
7 METHOD(XonoticKeyBinder, resizeNotify, void(entity, vector, vector, vector, vector))
8 METHOD(XonoticKeyBinder, setSelected, void(entity, float))
9 METHOD(XonoticKeyBinder, keyDown, float(entity, float, float, float))
10 METHOD(XonoticKeyBinder, keyGrabbed, void(entity, float, float))
12 ATTRIB(XonoticKeyBinder, realFontSize, vector, '0 0 0')
13 ATTRIB(XonoticKeyBinder, realUpperMargin, float, 0)
14 ATTRIB(XonoticKeyBinder, columnFunctionOrigin, float, 0)
15 ATTRIB(XonoticKeyBinder, columnFunctionSize, float, 0)
16 ATTRIB(XonoticKeyBinder, columnKeysOrigin, float, 0)
17 ATTRIB(XonoticKeyBinder, columnKeysSize, float, 0)
19 ATTRIB(XonoticKeyBinder, previouslySelected, int, -1)
20 ATTRIB(XonoticKeyBinder, inMouseHandler, float, 0)
21 ATTRIB(XonoticKeyBinder, userbindEditButton, entity, NULL)
22 ATTRIB(XonoticKeyBinder, keyGrabButton, entity, NULL)
23 ATTRIB(XonoticKeyBinder, clearButton, entity, NULL)
24 ATTRIB(XonoticKeyBinder, userbindEditDialog, entity, NULL)
25 METHOD(XonoticKeyBinder, editUserbind, void(entity, string, string, string))
26 ENDCLASS(XonoticKeyBinder)
27 entity makeXonoticKeyBinder();
28 void KeyBinder_Bind_Change(entity btn, entity me);
29 void KeyBinder_Bind_Clear(entity btn, entity me);
30 void KeyBinder_Bind_Edit(entity btn, entity me);
35 const string KEY_NOT_BOUND_CMD = "// not bound";
37 const int MAX_KEYS_PER_FUNCTION = 2;
38 const int MAX_KEYBINDS = 256;
39 string Xonotic_KeyBinds_Functions[MAX_KEYBINDS];
40 string Xonotic_KeyBinds_Descriptions[MAX_KEYBINDS];
41 int Xonotic_KeyBinds_Count = -1;
43 void Xonotic_KeyBinds_Read()
48 Xonotic_KeyBinds_Count = 0;
49 fh = fopen(language_filename("keybinds.txt"), FILE_READ);
52 while((s = fgets(fh)))
54 if(tokenize_console(s) != 2)
56 Xonotic_KeyBinds_Functions[Xonotic_KeyBinds_Count] = strzone(argv(0));
57 Xonotic_KeyBinds_Descriptions[Xonotic_KeyBinds_Count] = strzone(argv(1));
58 ++Xonotic_KeyBinds_Count;
59 if(Xonotic_KeyBinds_Count >= MAX_KEYBINDS)
65 entity makeXonoticKeyBinder()
68 me = spawnXonoticKeyBinder();
69 me.configureXonoticKeyBinder(me);
72 void replace_bind(string from, string to)
75 float k; // not sure if float or int
76 n = tokenize(findkeysforcommand(from, 0)); // uses '...' strings
77 for(j = 0; j < n; ++j)
81 localcmd("\nbind \"", keynumtostring(k), "\" \"", to, "\"\n");
84 cvar_set("_hud_showbinds_reload", "1");
86 void XonoticKeyBinder_configureXonoticKeyBinder(entity me)
88 me.configureXonoticListBox(me);
89 if(Xonotic_KeyBinds_Count < 0)
90 Xonotic_KeyBinds_Read();
91 me.nItems = Xonotic_KeyBinds_Count;
92 me.setSelected(me, 0);
94 // TEMP: Xonotic 0.1 to later
95 replace_bind("impulse 1", "weapon_group_1");
96 replace_bind("impulse 2", "weapon_group_2");
97 replace_bind("impulse 3", "weapon_group_3");
98 replace_bind("impulse 4", "weapon_group_4");
99 replace_bind("impulse 5", "weapon_group_5");
100 replace_bind("impulse 6", "weapon_group_6");
101 replace_bind("impulse 7", "weapon_group_7");
102 replace_bind("impulse 8", "weapon_group_8");
103 replace_bind("impulse 9", "weapon_group_9");
104 replace_bind("impulse 14", "weapon_group_0");
106 void XonoticKeyBinder_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
108 SUPER(XonoticKeyBinder).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
110 me.realFontSize_y = me.fontSize / (absSize.y * me.itemHeight);
111 me.realFontSize_x = me.fontSize / (absSize.x * (1 - me.controlWidth));
112 me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
114 me.columnFunctionOrigin = 0;
115 me.columnKeysSize = me.realFontSize.x * 12;
116 me.columnFunctionSize = 1 - me.columnKeysSize - 2 * me.realFontSize.x;
117 me.columnKeysOrigin = me.columnFunctionOrigin + me.columnFunctionSize + me.realFontSize.x;
119 if(me.userbindEditButton)
120 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[me.selectedItem], 0, 1) != "$");
122 void KeyBinder_Bind_Change(entity btn, entity me)
126 func = Xonotic_KeyBinds_Functions[me.selectedItem];
130 me.keyGrabButton.forcePressed = 1;
131 me.clearButton.disabled = 1;
134 void XonoticKeyBinder_keyGrabbed(entity me, int key, bool ascii)
140 me.keyGrabButton.forcePressed = 0;
141 me.clearButton.disabled = 0;
146 // forbid these keys from being bound in the menu
147 if(key == K_CAPSLOCK || key == K_NUMLOCK)
149 KeyBinder_Bind_Change(me, me);
153 func = Xonotic_KeyBinds_Functions[me.selectedItem];
157 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
159 for(j = 0; j < n; ++j)
165 if(nvalid >= MAX_KEYS_PER_FUNCTION)
167 for(j = 0; j < n; ++j)
171 //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
172 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
175 m_play_click_sound(MENU_SOUND_SELECT);
176 localcmd("\nbind \"", keynumtostring(key), "\" \"", func, "\"\n");
177 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
178 cvar_set("_hud_showbinds_reload", "1");
180 void XonoticKeyBinder_editUserbind(entity me, string theName, string theCommandPress, string theCommandRelease)
184 if(!me.userbindEditDialog)
187 func = Xonotic_KeyBinds_Functions[me.selectedItem];
191 descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
192 if(substring(descr, 0, 1) != "$")
194 descr = substring(descr, 1, strlen(descr) - 1);
196 // Hooray! It IS a user bind!
197 cvar_set(strcat(descr, "_description"), theName);
198 cvar_set(strcat(descr, "_press"), theCommandPress);
199 cvar_set(strcat(descr, "_release"), theCommandRelease);
201 void KeyBinder_Bind_Edit(entity btn, entity me)
205 if(!me.userbindEditDialog)
208 func = Xonotic_KeyBinds_Functions[me.selectedItem];
212 descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
213 if(substring(descr, 0, 1) != "$")
215 descr = substring(descr, 1, strlen(descr) - 1);
217 // Hooray! It IS a user bind!
218 me.userbindEditDialog.loadUserBind(me.userbindEditDialog, cvar_string(strcat(descr, "_description")), cvar_string(strcat(descr, "_press")), cvar_string(strcat(descr, "_release")));
220 DialogOpenButton_Click(btn, me.userbindEditDialog);
222 void KeyBinder_Bind_Clear(entity btn, entity me)
227 func = Xonotic_KeyBinds_Functions[me.selectedItem];
231 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
232 for(j = 0; j < n; ++j)
236 //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
237 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
239 m_play_click_sound(MENU_SOUND_CLEAR);
240 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
241 cvar_set("_hud_showbinds_reload", "1");
243 void KeyBinder_Bind_Reset_All(entity btn, entity me)
245 localcmd("unbindall\n");
246 localcmd("exec binds-xonotic.cfg\n");
247 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
248 cvar_set("_hud_showbinds_reload", "1");
250 void XonoticKeyBinder_doubleClickListBoxItem(entity me, float i, vector where)
252 KeyBinder_Bind_Change(NULL, me);
254 void XonoticKeyBinder_setSelected(entity me, int i)
256 // handling of "unselectable" items
257 i = floor(0.5 + bound(0, i, me.nItems - 1));
258 if(me.pressed == 0 || me.pressed == 1) // keyboard or scrolling - skip unselectable items
260 if(i > me.previouslySelected)
262 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
265 while((i > 0) && (Xonotic_KeyBinds_Functions[i] == ""))
267 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
270 if(me.pressed == 3) // released the mouse - fall back to last valid item
272 if(Xonotic_KeyBinds_Functions[i] == "")
273 i = me.previouslySelected;
275 if(Xonotic_KeyBinds_Functions[i] != "")
276 me.previouslySelected = i;
277 if(me.userbindEditButton)
278 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[i], 0, 1) != "$");
279 SUPER(XonoticKeyBinder).setSelected(me, i);
281 float XonoticKeyBinder_keyDown(entity me, int key, bool ascii, float shift)
289 KeyBinder_Bind_Change(me, me);
294 KeyBinder_Bind_Clear(me, me);
297 r = SUPER(XonoticKeyBinder).keyDown(me, key, ascii, shift);
302 void XonoticKeyBinder_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, float highlightedTime)
312 descr = Xonotic_KeyBinds_Descriptions[i];
313 func = Xonotic_KeyBinds_Functions[i];
318 theColor = SKINCOLOR_KEYGRABBER_TITLES;
319 theAlpha = SKINALPHA_KEYGRABBER_TITLES;
327 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_WAITING, SKINALPHA_LISTBOX_WAITING);
329 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
331 else if(highlightedTime)
332 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, getHighlightAlpha(SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED, highlightedTime));
334 theAlpha = SKINALPHA_KEYGRABBER_KEYS;
335 theColor = SKINCOLOR_KEYGRABBER_KEYS;
336 extraMargin = me.realFontSize.x * 0.5;
339 if(substring(descr, 0, 1) == "$")
341 s = substring(descr, 1, strlen(descr) - 1);
342 descr = cvar_string(strcat(s, "_description"));
345 if(cvar_string(strcat(s, "_press")) == "")
346 if(cvar_string(strcat(s, "_release")) == "")
347 theAlpha *= SKINALPHA_DISABLED;
350 s = draw_TextShortenToWidth(descr, me.columnFunctionSize, 0, me.realFontSize);
351 draw_Text(me.realUpperMargin * eY + extraMargin * eX, s, me.realFontSize, theColor, theAlpha, 0);
354 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
356 for(j = 0; j < n; ++j)
363 s = strcat(s, keynumtostring(k));
366 s = draw_TextShortenToWidth(s, me.columnKeysSize, 0, me.realFontSize);
367 draw_CenterText(me.realUpperMargin * eY + (me.columnKeysOrigin + 0.5 * me.columnKeysSize) * eX, s, me.realFontSize, theColor, theAlpha, 0);