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, clickListBoxItem, 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, lastClickedKey, float, -1)
20 ATTRIB(XonoticKeyBinder, lastClickedTime, float, 0)
21 ATTRIB(XonoticKeyBinder, previouslySelected, float, -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);
37 const string KEY_NOT_BOUND_CMD = "// not bound";
39 #define MAX_KEYS_PER_FUNCTION 2
40 #define MAX_KEYBINDS 256
41 string Xonotic_KeyBinds_Functions[MAX_KEYBINDS];
42 string Xonotic_KeyBinds_Descriptions[MAX_KEYBINDS];
43 var float Xonotic_KeyBinds_Count = -1;
45 void Xonotic_KeyBinds_Read()
50 Xonotic_KeyBinds_Count = 0;
51 fh = fopen(language_filename("keybinds.txt"), FILE_READ);
54 while((s = fgets(fh)))
56 if(tokenize_console(s) != 2)
58 Xonotic_KeyBinds_Functions[Xonotic_KeyBinds_Count] = strzone(argv(0));
59 Xonotic_KeyBinds_Descriptions[Xonotic_KeyBinds_Count] = strzone(argv(1));
60 ++Xonotic_KeyBinds_Count;
61 if(Xonotic_KeyBinds_Count >= MAX_KEYBINDS)
67 entity makeXonoticKeyBinder()
70 me = spawnXonoticKeyBinder();
71 me.configureXonoticKeyBinder(me);
74 void replace_bind(string from, string to)
77 n = tokenize(findkeysforcommand(from, 0)); // uses '...' strings
78 for(j = 0; j < n; ++j)
82 localcmd("\nbind \"", keynumtostring(k), "\" \"", to, "\"\n");
85 cvar_set("_hud_showbinds_reload", "1");
87 void XonoticKeyBinder_configureXonoticKeyBinder(entity me)
89 me.configureXonoticListBox(me);
90 if(Xonotic_KeyBinds_Count < 0)
91 Xonotic_KeyBinds_Read();
92 me.nItems = Xonotic_KeyBinds_Count;
93 me.setSelected(me, 0);
95 // TEMP: Xonotic 0.1 to later
96 replace_bind("impulse 1", "weapon_group_1");
97 replace_bind("impulse 2", "weapon_group_2");
98 replace_bind("impulse 3", "weapon_group_3");
99 replace_bind("impulse 4", "weapon_group_4");
100 replace_bind("impulse 5", "weapon_group_5");
101 replace_bind("impulse 6", "weapon_group_6");
102 replace_bind("impulse 7", "weapon_group_7");
103 replace_bind("impulse 8", "weapon_group_8");
104 replace_bind("impulse 9", "weapon_group_9");
105 replace_bind("impulse 14", "weapon_group_0");
107 void XonoticKeyBinder_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
109 SUPER(XonoticKeyBinder).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
111 me.realFontSize_y = me.fontSize / (absSize_y * me.itemHeight);
112 me.realFontSize_x = me.fontSize / (absSize_x * (1 - me.controlWidth));
113 me.realUpperMargin = 0.5 * (1 - me.realFontSize_y);
115 me.columnFunctionOrigin = 0;
116 me.columnKeysSize = me.realFontSize_x * 12;
117 me.columnFunctionSize = 1 - me.columnKeysSize - 2 * me.realFontSize_x;
118 me.columnKeysOrigin = me.columnFunctionOrigin + me.columnFunctionSize + me.realFontSize_x;
120 if(me.userbindEditButton)
121 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[me.selectedItem], 0, 1) != "$");
123 void KeyBinder_Bind_Change(entity btn, entity me)
127 func = Xonotic_KeyBinds_Functions[me.selectedItem];
131 me.keyGrabButton.forcePressed = 1;
132 me.clearButton.disabled = 1;
135 void XonoticKeyBinder_keyGrabbed(entity me, float key, float ascii)
137 float n, j, k, nvalid;
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 localcmd("\nbind \"", keynumtostring(key), "\" \"", func, "\"\n");
176 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
177 cvar_set("_hud_showbinds_reload", "1");
179 void XonoticKeyBinder_editUserbind(entity me, string theName, string theCommandPress, string theCommandRelease)
183 if(!me.userbindEditDialog)
186 func = Xonotic_KeyBinds_Functions[me.selectedItem];
190 descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
191 if(substring(descr, 0, 1) != "$")
193 descr = substring(descr, 1, strlen(descr) - 1);
195 // Hooray! It IS a user bind!
196 cvar_set(strcat(descr, "_description"), theName);
197 cvar_set(strcat(descr, "_press"), theCommandPress);
198 cvar_set(strcat(descr, "_release"), theCommandRelease);
200 void KeyBinder_Bind_Edit(entity btn, entity me)
204 if(!me.userbindEditDialog)
207 func = Xonotic_KeyBinds_Functions[me.selectedItem];
211 descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
212 if(substring(descr, 0, 1) != "$")
214 descr = substring(descr, 1, strlen(descr) - 1);
216 // Hooray! It IS a user bind!
217 me.userbindEditDialog.loadUserBind(me.userbindEditDialog, cvar_string(strcat(descr, "_description")), cvar_string(strcat(descr, "_press")), cvar_string(strcat(descr, "_release")));
219 DialogOpenButton_Click(btn, me.userbindEditDialog);
221 void KeyBinder_Bind_Clear(entity btn, entity me)
226 func = Xonotic_KeyBinds_Functions[me.selectedItem];
230 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
231 for(j = 0; j < n; ++j)
235 //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
236 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
238 localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
239 cvar_set("_hud_showbinds_reload", "1");
241 void XonoticKeyBinder_clickListBoxItem(entity me, float i, vector where)
243 if(i == me.lastClickedServer)
244 if(time < me.lastClickedTime + 0.3)
247 KeyBinder_Bind_Change(NULL, me);
249 me.lastClickedServer = i;
250 me.lastClickedTime = time;
252 void XonoticKeyBinder_setSelected(entity me, float i)
254 // handling of "unselectable" items
255 i = floor(0.5 + bound(0, i, me.nItems - 1));
256 if(me.pressed == 0 || me.pressed == 1) // keyboard or scrolling - skip unselectable items
258 if(i > me.previouslySelected)
260 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
263 while((i > 0) && (Xonotic_KeyBinds_Functions[i] == ""))
265 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
268 if(me.pressed == 3) // released the mouse - fall back to last valid item
270 if(Xonotic_KeyBinds_Functions[i] == "")
271 i = me.previouslySelected;
273 if(Xonotic_KeyBinds_Functions[i] != "")
274 me.previouslySelected = i;
275 if(me.userbindEditButton)
276 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[i], 0, 1) != "$");
277 SUPER(XonoticKeyBinder).setSelected(me, i);
279 float XonoticKeyBinder_keyDown(entity me, float key, float ascii, float shift)
288 KeyBinder_Bind_Change(me, me);
293 KeyBinder_Bind_Clear(me, me);
296 r = SUPER(XonoticKeyBinder).keyDown(me, key, ascii, shift);
301 void XonoticKeyBinder_drawListBoxItem(entity me, float i, vector absSize, float isSelected)
310 descr = Xonotic_KeyBinds_Descriptions[i];
311 func = Xonotic_KeyBinds_Functions[i];
316 theColor = SKINCOLOR_KEYGRABBER_TITLES;
317 theAlpha = SKINALPHA_KEYGRABBER_TITLES;
325 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_WAITING, SKINALPHA_LISTBOX_WAITING);
327 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
329 theAlpha = SKINALPHA_KEYGRABBER_KEYS;
330 theColor = SKINCOLOR_KEYGRABBER_KEYS;
331 extraMargin = me.realFontSize_x * 0.5;
334 if(substring(descr, 0, 1) == "$")
336 s = substring(descr, 1, strlen(descr) - 1);
337 descr = cvar_string(strcat(s, "_description"));
340 if(cvar_string(strcat(s, "_press")) == "")
341 if(cvar_string(strcat(s, "_release")) == "")
342 theAlpha *= SKINALPHA_DISABLED;
345 s = draw_TextShortenToWidth(descr, me.columnFunctionSize, 0, me.realFontSize);
346 draw_Text(me.realUpperMargin * eY + extraMargin * eX, s, me.realFontSize, theColor, theAlpha, 0);
349 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
351 for(j = 0; j < n; ++j)
358 s = strcat(s, keynumtostring(k));
361 s = draw_TextShortenToWidth(s, me.columnKeysSize, 0, me.realFontSize);
362 draw_CenterText(me.realUpperMargin * eY + (me.columnKeysOrigin + 0.5 * me.columnKeysSize) * eX, s, me.realFontSize, theColor, theAlpha, 0);