2 CLASS(NexuizKeyBinder) EXTENDS(NexuizListBox)
3 METHOD(NexuizKeyBinder, configureNexuizKeyBinder, void(entity))
4 ATTRIB(NexuizKeyBinder, rowsPerItem, float, 1)
5 METHOD(NexuizKeyBinder, drawListBoxItem, void(entity, float, vector, float))
6 METHOD(NexuizKeyBinder, clickListBoxItem, void(entity, float, vector))
7 METHOD(NexuizKeyBinder, resizeNotify, void(entity, vector, vector, vector, vector))
8 METHOD(NexuizKeyBinder, setSelected, void(entity, float))
9 METHOD(NexuizKeyBinder, keyDown, float(entity, float, float, float))
10 METHOD(NexuizKeyBinder, keyGrabbed, void(entity, float, float))
12 ATTRIB(NexuizKeyBinder, realFontSize, vector, '0 0 0')
13 ATTRIB(NexuizKeyBinder, realUpperMargin, float, 0)
14 ATTRIB(NexuizKeyBinder, columnFunctionOrigin, float, 0)
15 ATTRIB(NexuizKeyBinder, columnFunctionSize, float, 0)
16 ATTRIB(NexuizKeyBinder, columnKeysOrigin, float, 0)
17 ATTRIB(NexuizKeyBinder, columnKeysSize, float, 0)
19 ATTRIB(NexuizKeyBinder, lastClickedKey, float, -1)
20 ATTRIB(NexuizKeyBinder, lastClickedTime, float, 0)
21 ATTRIB(NexuizKeyBinder, previouslySelected, float, -1)
22 ATTRIB(NexuizKeyBinder, inMouseHandler, float, 0)
23 ATTRIB(NexuizKeyBinder, userbindEditButton, entity, NULL)
24 ATTRIB(NexuizKeyBinder, keyGrabButton, entity, NULL)
25 ATTRIB(NexuizKeyBinder, userbindEditDialog, entity, NULL)
26 METHOD(NexuizKeyBinder, editUserbind, void(entity, string, string, string))
27 ENDCLASS(NexuizKeyBinder)
28 entity makeNexuizKeyBinder();
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 Nexuiz_KeyBinds_Functions[MAX_KEYBINDS];
39 string Nexuiz_KeyBinds_Descriptions[MAX_KEYBINDS];
40 var float Nexuiz_KeyBinds_Count = -1;
42 void Nexuiz_KeyBinds_Read()
47 Nexuiz_KeyBinds_Count = 0;
48 fh = fopen("keybinds.txt", FILE_READ);
51 while((s = fgets(fh)))
53 if(tokenize_console(s) != 2)
55 Nexuiz_KeyBinds_Functions[Nexuiz_KeyBinds_Count] = strzone(argv(0));
56 Nexuiz_KeyBinds_Descriptions[Nexuiz_KeyBinds_Count] = strzone(argv(1));
57 ++Nexuiz_KeyBinds_Count;
58 if(Nexuiz_KeyBinds_Count >= MAX_KEYBINDS)
64 entity makeNexuizKeyBinder()
67 me = spawnNexuizKeyBinder();
68 me.configureNexuizKeyBinder(me);
71 void configureNexuizKeyBinderNexuizKeyBinder(entity me)
73 me.configureNexuizListBox(me);
74 if(Nexuiz_KeyBinds_Count < 0)
75 Nexuiz_KeyBinds_Read();
76 me.nItems = Nexuiz_KeyBinds_Count;
77 me.setSelected(me, 0);
79 void resizeNotifyNexuizKeyBinder(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
81 resizeNotifyNexuizListBox(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(Nexuiz_KeyBinds_Descriptions[me.selectedItem], 0, 1) != "$");
95 void KeyBinder_Bind_Change(entity btn, entity me)
99 func = Nexuiz_KeyBinds_Functions[me.selectedItem];
103 me.keyGrabButton.forcePressed = 1;
106 void keyGrabbedNexuizKeyBinder(entity me, float key, float ascii)
108 float n, j, k, nvalid;
111 me.keyGrabButton.forcePressed = 0;
115 func = Nexuiz_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 editUserbindNexuizKeyBinder(entity me, string theName, string theCommandPress, string theCommandRelease)
142 if(!me.userbindEditDialog)
145 func = Nexuiz_KeyBinds_Functions[me.selectedItem];
149 descr = Nexuiz_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 = Nexuiz_KeyBinds_Functions[me.selectedItem];
170 descr = Nexuiz_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 = Nexuiz_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 clickListBoxItemNexuizKeyBinder(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 setSelectedNexuizKeyBinder(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) && (Nexuiz_KeyBinds_Functions[i] == ""))
220 while((i > 0) && (Nexuiz_KeyBinds_Functions[i] == ""))
222 while((i < me.nItems - 1) && (Nexuiz_KeyBinds_Functions[i] == ""))
225 if(me.pressed == 3) // released the mouse - fall back to last valid item
227 if(Nexuiz_KeyBinds_Functions[i] == "")
228 i = me.previouslySelected;
230 if(Nexuiz_KeyBinds_Functions[i] != "")
231 me.previouslySelected = i;
232 if(me.userbindEditButton)
233 me.userbindEditButton.disabled = (substring(Nexuiz_KeyBinds_Descriptions[i], 0, 1) != "$");
234 setSelectedListBox(me, i);
236 float keyDownNexuizKeyBinder(entity me, float key, float ascii, float shift)
244 KeyBinder_Bind_Change(me, me);
248 KeyBinder_Bind_Clear(me, me);
251 r = keyDownListBox(me, key, ascii, shift);
256 void drawListBoxItemNexuizKeyBinder(entity me, float i, vector absSize, float isSelected)
265 descr = Nexuiz_KeyBinds_Descriptions[i];
266 func = Nexuiz_KeyBinds_Functions[i];
271 theColor = SKINCOLOR_KEYGRABBER_TITLES;
272 theAlpha = SKINALPHA_KEYGRABBER_TITLES;
280 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_WAITING, SKINALPHA_LISTBOX_WAITING);
282 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
284 theAlpha = SKINALPHA_KEYGRABBER_KEYS;
285 theColor = SKINCOLOR_KEYGRABBER_KEYS;
286 extraMargin = me.realFontSize_x * 0.5;
289 if(substring(descr, 0, 1) == "$")
291 s = substring(descr, 1, strlen(descr) - 1);
292 descr = cvar_string(strcat(s, "_description"));
295 if(cvar_string(strcat(s, "_press")) == "")
296 if(cvar_string(strcat(s, "_release")) == "")
297 theAlpha *= SKINALPHA_DISABLED;
300 draw_Text(me.realUpperMargin * eY + extraMargin * eX, descr, me.realFontSize, theColor, theAlpha, 0);
303 n = tokenize(findkeysforcommand(func)); // uses '...' strings
305 for(j = 0; j < n; ++j)
312 s = strcat(s, keynumtostring(k));
315 s = draw_TextShortenToWidth(s, me.columnKeysSize / me.realFontSize_x, 0);
316 draw_CenterText(me.realUpperMargin * eY + (me.columnKeysOrigin + 0.5 * me.columnKeysSize) * eX, s, me.realFontSize, theColor, theAlpha, 0);