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, userbindEditDialog, entity, NULL)
26 METHOD(XonoticKeyBinder, editUserbind, void(entity, string, string, string))
27 ENDCLASS(XonoticKeyBinder)
28 entity makeXonoticKeyBinder();
29 void KeyBinder_Bind_Change(entity btn, entity me);
30 void KeyBinder_Bind_Clear(entity btn, entity me);
31 void KeyBinder_Bind_Edit(entity btn, entity me);
36 #define MAX_KEYS_PER_FUNCTION 2
37 #define MAX_KEYBINDS 256
38 string Xonotic_KeyBinds_Functions[MAX_KEYBINDS];
39 string Xonotic_KeyBinds_Descriptions[MAX_KEYBINDS];
40 var float Xonotic_KeyBinds_Count = -1;
42 void Xonotic_KeyBinds_Read()
47 Xonotic_KeyBinds_Count = 0;
48 fh = fopen("keybinds.txt", FILE_READ);
51 while((s = fgets(fh)))
53 if(tokenize_console(s) != 2)
55 Xonotic_KeyBinds_Functions[Xonotic_KeyBinds_Count] = strzone(argv(0));
56 Xonotic_KeyBinds_Descriptions[Xonotic_KeyBinds_Count] = strzone(argv(1));
57 ++Xonotic_KeyBinds_Count;
58 if(Xonotic_KeyBinds_Count >= MAX_KEYBINDS)
64 entity makeXonoticKeyBinder()
67 me = spawnXonoticKeyBinder();
68 me.configureXonoticKeyBinder(me);
71 void XonoticKeyBinder_configureXonoticKeyBinder(entity me)
73 me.configureXonoticListBox(me);
74 if(Xonotic_KeyBinds_Count < 0)
75 Xonotic_KeyBinds_Read();
76 me.nItems = Xonotic_KeyBinds_Count;
77 me.setSelected(me, 0);
79 void XonoticKeyBinder_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
81 SUPER(XonoticKeyBinder).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
83 me.realFontSize_y = me.fontSize / (absSize_y * me.itemHeight);
84 me.realFontSize_x = me.fontSize / (absSize_x * (1 - me.controlWidth));
85 me.realUpperMargin = 0.5 * (1 - me.realFontSize_y);
87 me.columnFunctionOrigin = 0;
88 me.columnKeysSize = me.realFontSize_x * 12;
89 me.columnFunctionSize = 1 - me.columnKeysSize - 2 * me.realFontSize_x;
90 me.columnKeysOrigin = me.columnFunctionOrigin + me.columnFunctionSize + me.realFontSize_x;
92 if(me.userbindEditButton)
93 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[me.selectedItem], 0, 1) != "$");
95 void KeyBinder_Bind_Change(entity btn, entity me)
99 func = Xonotic_KeyBinds_Functions[me.selectedItem];
103 me.keyGrabButton.forcePressed = 1;
106 void XonoticKeyBinder_keyGrabbed(entity me, float key, float ascii)
108 float n, j, k, nvalid;
111 me.keyGrabButton.forcePressed = 0;
115 func = Xonotic_KeyBinds_Functions[me.selectedItem];
119 n = tokenize(findkeysforcommand(func)); // uses '...' strings
121 for(j = 0; j < n; ++j)
127 if(nvalid >= MAX_KEYS_PER_FUNCTION)
129 for(j = 0; j < n; ++j)
133 localcmd("\nunbind \"", keynumtostring(k), "\"\n");
136 localcmd("\nbind \"", keynumtostring(key), "\" \"", func, "\"\n");
138 void XonoticKeyBinder_editUserbind(entity me, string theName, string theCommandPress, string theCommandRelease)
142 if(!me.userbindEditDialog)
145 func = Xonotic_KeyBinds_Functions[me.selectedItem];
149 descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
150 if(substring(descr, 0, 1) != "$")
152 descr = substring(descr, 1, strlen(descr) - 1);
154 // Hooray! It IS a user bind!
155 cvar_set(strcat(descr, "_description"), theName);
156 cvar_set(strcat(descr, "_press"), theCommandPress);
157 cvar_set(strcat(descr, "_release"), theCommandRelease);
159 void KeyBinder_Bind_Edit(entity btn, entity me)
163 if(!me.userbindEditDialog)
166 func = Xonotic_KeyBinds_Functions[me.selectedItem];
170 descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
171 if(substring(descr, 0, 1) != "$")
173 descr = substring(descr, 1, strlen(descr) - 1);
175 // Hooray! It IS a user bind!
176 me.userbindEditDialog.loadUserBind(me.userbindEditDialog, cvar_string(strcat(descr, "_description")), cvar_string(strcat(descr, "_press")), cvar_string(strcat(descr, "_release")));
178 DialogOpenButton_Click(btn, me.userbindEditDialog);
180 void KeyBinder_Bind_Clear(entity btn, entity me)
185 func = Xonotic_KeyBinds_Functions[me.selectedItem];
189 n = tokenize(findkeysforcommand(func)); // uses '...' strings
190 for(j = 0; j < n; ++j)
194 localcmd("\nunbind \"", keynumtostring(k), "\"\n");
198 void XonoticKeyBinder_clickListBoxItem(entity me, float i, vector where)
200 if(i == me.lastClickedServer)
201 if(time < me.lastClickedTime + 0.3)
204 KeyBinder_Bind_Change(NULL, me);
206 me.lastClickedServer = i;
207 me.lastClickedTime = time;
209 void XonoticKeyBinder_setSelected(entity me, float i)
211 // handling of "unselectable" items
212 i = floor(0.5 + bound(0, i, me.nItems - 1));
213 if(me.pressed == 0 || me.pressed == 1) // keyboard or scrolling - skip unselectable items
215 if(i > me.previouslySelected)
217 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
220 while((i > 0) && (Xonotic_KeyBinds_Functions[i] == ""))
222 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
225 if(me.pressed == 3) // released the mouse - fall back to last valid item
227 if(Xonotic_KeyBinds_Functions[i] == "")
228 i = me.previouslySelected;
230 if(Xonotic_KeyBinds_Functions[i] != "")
231 me.previouslySelected = i;
232 if(me.userbindEditButton)
233 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[i], 0, 1) != "$");
234 SUPER(XonoticKeyBinder).setSelected(me, i);
236 float XonoticKeyBinder_keyDown(entity me, float key, float ascii, float shift)
245 KeyBinder_Bind_Change(me, me);
250 KeyBinder_Bind_Clear(me, me);
253 r = SUPER(XonoticKeyBinder).keyDown(me, key, ascii, shift);
258 void XonoticKeyBinder_drawListBoxItem(entity me, float i, vector absSize, float isSelected)
267 descr = Xonotic_KeyBinds_Descriptions[i];
268 func = Xonotic_KeyBinds_Functions[i];
273 theColor = SKINCOLOR_KEYGRABBER_TITLES;
274 theAlpha = SKINALPHA_KEYGRABBER_TITLES;
282 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_WAITING, SKINALPHA_LISTBOX_WAITING);
284 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
286 theAlpha = SKINALPHA_KEYGRABBER_KEYS;
287 theColor = SKINCOLOR_KEYGRABBER_KEYS;
288 extraMargin = me.realFontSize_x * 0.5;
291 if(substring(descr, 0, 1) == "$")
293 s = substring(descr, 1, strlen(descr) - 1);
294 descr = cvar_string(strcat(s, "_description"));
297 if(cvar_string(strcat(s, "_press")) == "")
298 if(cvar_string(strcat(s, "_release")) == "")
299 theAlpha *= SKINALPHA_DISABLED;
302 s = draw_TextShortenToWidth(descr, me.columnFunctionSize, 0, me.realFontSize);
303 draw_Text(me.realUpperMargin * eY + extraMargin * eX, s, me.realFontSize, theColor, theAlpha, 0);
306 n = tokenize(findkeysforcommand(func)); // uses '...' strings
308 for(j = 0; j < n; ++j)
315 s = strcat(s, keynumtostring(k));
318 s = draw_TextShortenToWidth(s, me.columnKeysSize, 0, me.realFontSize);
319 draw_CenterText(me.realUpperMargin * eY + (me.columnKeysOrigin + 0.5 * me.columnKeysSize) * eX, s, me.realFontSize, theColor, theAlpha, 0);