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 string KEY_NOT_BOUND_CMD = "// not bound";
38 #define MAX_KEYS_PER_FUNCTION 2
39 #define MAX_KEYBINDS 256
40 string Xonotic_KeyBinds_Functions[MAX_KEYBINDS];
41 string Xonotic_KeyBinds_Descriptions[MAX_KEYBINDS];
42 var float Xonotic_KeyBinds_Count = -1;
44 void Xonotic_KeyBinds_Read()
49 Xonotic_KeyBinds_Count = 0;
50 fh = fopen("keybinds.txt", FILE_READ);
53 while((s = fgets(fh)))
55 if(tokenize_console(s) != 2)
57 Xonotic_KeyBinds_Functions[Xonotic_KeyBinds_Count] = strzone(argv(0));
58 Xonotic_KeyBinds_Descriptions[Xonotic_KeyBinds_Count] = strzone(argv(1));
59 ++Xonotic_KeyBinds_Count;
60 if(Xonotic_KeyBinds_Count >= MAX_KEYBINDS)
66 entity makeXonoticKeyBinder()
69 me = spawnXonoticKeyBinder();
70 me.configureXonoticKeyBinder(me);
73 void XonoticKeyBinder_configureXonoticKeyBinder(entity me)
75 me.configureXonoticListBox(me);
76 if(Xonotic_KeyBinds_Count < 0)
77 Xonotic_KeyBinds_Read();
78 me.nItems = Xonotic_KeyBinds_Count;
79 me.setSelected(me, 0);
81 void XonoticKeyBinder_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
83 SUPER(XonoticKeyBinder).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
85 me.realFontSize_y = me.fontSize / (absSize_y * me.itemHeight);
86 me.realFontSize_x = me.fontSize / (absSize_x * (1 - me.controlWidth));
87 me.realUpperMargin = 0.5 * (1 - me.realFontSize_y);
89 me.columnFunctionOrigin = 0;
90 me.columnKeysSize = me.realFontSize_x * 12;
91 me.columnFunctionSize = 1 - me.columnKeysSize - 2 * me.realFontSize_x;
92 me.columnKeysOrigin = me.columnFunctionOrigin + me.columnFunctionSize + me.realFontSize_x;
94 if(me.userbindEditButton)
95 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[me.selectedItem], 0, 1) != "$");
97 void KeyBinder_Bind_Change(entity btn, entity me)
101 func = Xonotic_KeyBinds_Functions[me.selectedItem];
105 me.keyGrabButton.forcePressed = 1;
108 void XonoticKeyBinder_keyGrabbed(entity me, float key, float ascii)
110 float n, j, k, nvalid;
113 me.keyGrabButton.forcePressed = 0;
117 func = Xonotic_KeyBinds_Functions[me.selectedItem];
121 n = tokenize(findkeysforcommand(func)); // uses '...' strings
123 for(j = 0; j < n; ++j)
129 if(nvalid >= MAX_KEYS_PER_FUNCTION)
131 for(j = 0; j < n; ++j)
135 //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
136 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
139 localcmd("\nbind \"", keynumtostring(key), "\" \"", func, "\"\n");
141 void XonoticKeyBinder_editUserbind(entity me, string theName, string theCommandPress, string theCommandRelease)
145 if(!me.userbindEditDialog)
148 func = Xonotic_KeyBinds_Functions[me.selectedItem];
152 descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
153 if(substring(descr, 0, 1) != "$")
155 descr = substring(descr, 1, strlen(descr) - 1);
157 // Hooray! It IS a user bind!
158 cvar_set(strcat(descr, "_description"), theName);
159 cvar_set(strcat(descr, "_press"), theCommandPress);
160 cvar_set(strcat(descr, "_release"), theCommandRelease);
162 void KeyBinder_Bind_Edit(entity btn, entity me)
166 if(!me.userbindEditDialog)
169 func = Xonotic_KeyBinds_Functions[me.selectedItem];
173 descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
174 if(substring(descr, 0, 1) != "$")
176 descr = substring(descr, 1, strlen(descr) - 1);
178 // Hooray! It IS a user bind!
179 me.userbindEditDialog.loadUserBind(me.userbindEditDialog, cvar_string(strcat(descr, "_description")), cvar_string(strcat(descr, "_press")), cvar_string(strcat(descr, "_release")));
181 DialogOpenButton_Click(btn, me.userbindEditDialog);
183 void KeyBinder_Bind_Clear(entity btn, entity me)
188 func = Xonotic_KeyBinds_Functions[me.selectedItem];
192 n = tokenize(findkeysforcommand(func)); // uses '...' strings
193 for(j = 0; j < n; ++j)
197 //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
198 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
202 void XonoticKeyBinder_clickListBoxItem(entity me, float i, vector where)
204 if(i == me.lastClickedServer)
205 if(time < me.lastClickedTime + 0.3)
208 KeyBinder_Bind_Change(NULL, me);
210 me.lastClickedServer = i;
211 me.lastClickedTime = time;
213 void XonoticKeyBinder_setSelected(entity me, float i)
215 // handling of "unselectable" items
216 i = floor(0.5 + bound(0, i, me.nItems - 1));
217 if(me.pressed == 0 || me.pressed == 1) // keyboard or scrolling - skip unselectable items
219 if(i > me.previouslySelected)
221 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
224 while((i > 0) && (Xonotic_KeyBinds_Functions[i] == ""))
226 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
229 if(me.pressed == 3) // released the mouse - fall back to last valid item
231 if(Xonotic_KeyBinds_Functions[i] == "")
232 i = me.previouslySelected;
234 if(Xonotic_KeyBinds_Functions[i] != "")
235 me.previouslySelected = i;
236 if(me.userbindEditButton)
237 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[i], 0, 1) != "$");
238 SUPER(XonoticKeyBinder).setSelected(me, i);
240 float XonoticKeyBinder_keyDown(entity me, float key, float ascii, float shift)
249 KeyBinder_Bind_Change(me, me);
254 KeyBinder_Bind_Clear(me, me);
257 r = SUPER(XonoticKeyBinder).keyDown(me, key, ascii, shift);
262 void XonoticKeyBinder_drawListBoxItem(entity me, float i, vector absSize, float isSelected)
271 descr = Xonotic_KeyBinds_Descriptions[i];
272 func = Xonotic_KeyBinds_Functions[i];
277 theColor = SKINCOLOR_KEYGRABBER_TITLES;
278 theAlpha = SKINALPHA_KEYGRABBER_TITLES;
286 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_WAITING, SKINALPHA_LISTBOX_WAITING);
288 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
290 theAlpha = SKINALPHA_KEYGRABBER_KEYS;
291 theColor = SKINCOLOR_KEYGRABBER_KEYS;
292 extraMargin = me.realFontSize_x * 0.5;
295 if(substring(descr, 0, 1) == "$")
297 s = substring(descr, 1, strlen(descr) - 1);
298 descr = cvar_string(strcat(s, "_description"));
301 if(cvar_string(strcat(s, "_press")) == "")
302 if(cvar_string(strcat(s, "_release")) == "")
303 theAlpha *= SKINALPHA_DISABLED;
306 s = draw_TextShortenToWidth(descr, me.columnFunctionSize, 0, me.realFontSize);
307 draw_Text(me.realUpperMargin * eY + extraMargin * eX, s, me.realFontSize, theColor, theAlpha, 0);
310 n = tokenize(findkeysforcommand(func)); // uses '...' strings
312 for(j = 0; j < n; ++j)
319 s = strcat(s, keynumtostring(k));
322 s = draw_TextShortenToWidth(s, me.columnKeysSize, 0, me.realFontSize);
323 draw_CenterText(me.realUpperMargin * eY + (me.columnKeysOrigin + 0.5 * me.columnKeysSize) * eX, s, me.realFontSize, theColor, theAlpha, 0);