2 CLASS(XonoticKeyBinder) EXTENDS(XonoticListBox)
3 METHOD(XonoticKeyBinder, configureXonoticKeyBinder, void(entity))
4 ATTRIB(XonoticKeyBinder, rowsPerItem, float, 1)
5 METHOD(XonoticKeyBinder, drawListBoxItem, void(entity, float, vector, 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, float, -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 float MAX_KEYS_PER_FUNCTION = 2;
38 const float MAX_KEYBINDS = 256;
39 string Xonotic_KeyBinds_Functions[MAX_KEYBINDS];
40 string Xonotic_KeyBinds_Descriptions[MAX_KEYBINDS];
41 var float 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 n = tokenize(findkeysforcommand(from, 0)); // uses '...' strings
76 for(j = 0; j < n; ++j)
80 localcmd("\nbind \"", keynumtostring(k), "\" \"", to, "\"\n");
83 cvar_set("_hud_showbinds_reload", "1");
85 void XonoticKeyBinder_configureXonoticKeyBinder(entity me)
87 me.configureXonoticListBox(me);
88 if(Xonotic_KeyBinds_Count < 0)
89 Xonotic_KeyBinds_Read();
90 me.nItems = Xonotic_KeyBinds_Count;
91 me.setSelected(me, 0);
93 // TEMP: Xonotic 0.1 to later
94 replace_bind("impulse 1", "weapon_group_1");
95 replace_bind("impulse 2", "weapon_group_2");
96 replace_bind("impulse 3", "weapon_group_3");
97 replace_bind("impulse 4", "weapon_group_4");
98 replace_bind("impulse 5", "weapon_group_5");
99 replace_bind("impulse 6", "weapon_group_6");
100 replace_bind("impulse 7", "weapon_group_7");
101 replace_bind("impulse 8", "weapon_group_8");
102 replace_bind("impulse 9", "weapon_group_9");
103 replace_bind("impulse 14", "weapon_group_0");
105 void XonoticKeyBinder_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
107 SUPER(XonoticKeyBinder).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
109 me.realFontSize_y = me.fontSize / (absSize_y * me.itemHeight);
110 me.realFontSize_x = me.fontSize / (absSize_x * (1 - me.controlWidth));
111 me.realUpperMargin = 0.5 * (1 - me.realFontSize_y);
113 me.columnFunctionOrigin = 0;
114 me.columnKeysSize = me.realFontSize_x * 12;
115 me.columnFunctionSize = 1 - me.columnKeysSize - 2 * me.realFontSize_x;
116 me.columnKeysOrigin = me.columnFunctionOrigin + me.columnFunctionSize + me.realFontSize_x;
118 if(me.userbindEditButton)
119 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[me.selectedItem], 0, 1) != "$");
121 void KeyBinder_Bind_Change(entity btn, entity me)
125 func = Xonotic_KeyBinds_Functions[me.selectedItem];
129 me.keyGrabButton.forcePressed = 1;
130 me.clearButton.disabled = 1;
133 void XonoticKeyBinder_keyGrabbed(entity me, float key, float ascii)
135 float n, j, k, nvalid;
138 me.keyGrabButton.forcePressed = 0;
139 me.clearButton.disabled = 0;
144 // forbid these keys from being bound in the menu
145 if(key == K_CAPSLOCK || key == K_NUMLOCK)
147 KeyBinder_Bind_Change(me, me);
151 func = Xonotic_KeyBinds_Functions[me.selectedItem];
155 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
157 for(j = 0; j < n; ++j)
163 if(nvalid >= MAX_KEYS_PER_FUNCTION)
165 for(j = 0; j < n; ++j)
169 //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
170 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
173 localcmd("\nbind \"", keynumtostring(key), "\" \"", func, "\"\n");
174 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
175 cvar_set("_hud_showbinds_reload", "1");
177 void XonoticKeyBinder_editUserbind(entity me, string theName, string theCommandPress, string theCommandRelease)
181 if(!me.userbindEditDialog)
184 func = Xonotic_KeyBinds_Functions[me.selectedItem];
188 descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
189 if(substring(descr, 0, 1) != "$")
191 descr = substring(descr, 1, strlen(descr) - 1);
193 // Hooray! It IS a user bind!
194 cvar_set(strcat(descr, "_description"), theName);
195 cvar_set(strcat(descr, "_press"), theCommandPress);
196 cvar_set(strcat(descr, "_release"), theCommandRelease);
198 void KeyBinder_Bind_Edit(entity btn, entity me)
202 if(!me.userbindEditDialog)
205 func = Xonotic_KeyBinds_Functions[me.selectedItem];
209 descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
210 if(substring(descr, 0, 1) != "$")
212 descr = substring(descr, 1, strlen(descr) - 1);
214 // Hooray! It IS a user bind!
215 me.userbindEditDialog.loadUserBind(me.userbindEditDialog, cvar_string(strcat(descr, "_description")), cvar_string(strcat(descr, "_press")), cvar_string(strcat(descr, "_release")));
217 DialogOpenButton_Click(btn, me.userbindEditDialog);
219 void KeyBinder_Bind_Clear(entity btn, entity me)
224 func = Xonotic_KeyBinds_Functions[me.selectedItem];
228 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
229 for(j = 0; j < n; ++j)
233 //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
234 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
236 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
237 cvar_set("_hud_showbinds_reload", "1");
239 void KeyBinder_Bind_Reset_All(entity btn, entity me)
241 localcmd("unbindall\n");
242 localcmd("exec binds-default.cfg\n");
243 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
244 cvar_set("_hud_showbinds_reload", "1");
246 void XonoticKeyBinder_doubleClickListBoxItem(entity me, float i, vector where)
248 KeyBinder_Bind_Change(NULL, me);
250 void XonoticKeyBinder_setSelected(entity me, float i)
252 // handling of "unselectable" items
253 i = floor(0.5 + bound(0, i, me.nItems - 1));
254 if(me.pressed == 0 || me.pressed == 1) // keyboard or scrolling - skip unselectable items
256 if(i > me.previouslySelected)
258 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
261 while((i > 0) && (Xonotic_KeyBinds_Functions[i] == ""))
263 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
266 if(me.pressed == 3) // released the mouse - fall back to last valid item
268 if(Xonotic_KeyBinds_Functions[i] == "")
269 i = me.previouslySelected;
271 if(Xonotic_KeyBinds_Functions[i] != "")
272 me.previouslySelected = i;
273 if(me.userbindEditButton)
274 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[i], 0, 1) != "$");
275 SUPER(XonoticKeyBinder).setSelected(me, i);
277 float XonoticKeyBinder_keyDown(entity me, float key, float ascii, float shift)
286 KeyBinder_Bind_Change(me, me);
291 KeyBinder_Bind_Clear(me, me);
294 r = SUPER(XonoticKeyBinder).keyDown(me, key, ascii, shift);
299 void XonoticKeyBinder_drawListBoxItem(entity me, float i, vector absSize, float isSelected)
308 descr = Xonotic_KeyBinds_Descriptions[i];
309 func = Xonotic_KeyBinds_Functions[i];
314 theColor = SKINCOLOR_KEYGRABBER_TITLES;
315 theAlpha = SKINALPHA_KEYGRABBER_TITLES;
323 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_WAITING, SKINALPHA_LISTBOX_WAITING);
325 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
327 theAlpha = SKINALPHA_KEYGRABBER_KEYS;
328 theColor = SKINCOLOR_KEYGRABBER_KEYS;
329 extraMargin = me.realFontSize_x * 0.5;
332 if(substring(descr, 0, 1) == "$")
334 s = substring(descr, 1, strlen(descr) - 1);
335 descr = cvar_string(strcat(s, "_description"));
338 if(cvar_string(strcat(s, "_press")) == "")
339 if(cvar_string(strcat(s, "_release")) == "")
340 theAlpha *= SKINALPHA_DISABLED;
343 s = draw_TextShortenToWidth(descr, me.columnFunctionSize, 0, me.realFontSize);
344 draw_Text(me.realUpperMargin * eY + extraMargin * eX, s, me.realFontSize, theColor, theAlpha, 0);
347 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
349 for(j = 0; j < n; ++j)
356 s = strcat(s, keynumtostring(k));
359 s = draw_TextShortenToWidth(s, me.columnKeysSize, 0, me.realFontSize);
360 draw_CenterText(me.realUpperMargin * eY + (me.columnKeysOrigin + 0.5 * me.columnKeysSize) * eX, s, me.realFontSize, theColor, theAlpha, 0);