2 CLASS(VoretKeyBinder) EXTENDS(VoretListBox)
\r
3 METHOD(VoretKeyBinder, configureVoretKeyBinder, void(entity))
\r
4 ATTRIB(VoretKeyBinder, rowsPerItem, float, 1)
\r
5 METHOD(VoretKeyBinder, drawListBoxItem, void(entity, float, vector, float))
\r
6 METHOD(VoretKeyBinder, clickListBoxItem, void(entity, float, vector))
\r
7 METHOD(VoretKeyBinder, resizeNotify, void(entity, vector, vector, vector, vector))
\r
8 METHOD(VoretKeyBinder, setSelected, void(entity, float))
\r
9 METHOD(VoretKeyBinder, keyDown, float(entity, float, float, float))
\r
10 METHOD(VoretKeyBinder, keyGrabbed, void(entity, float, float))
\r
12 ATTRIB(VoretKeyBinder, realFontSize, vector, '0 0 0')
\r
13 ATTRIB(VoretKeyBinder, realUpperMargin, float, 0)
\r
14 ATTRIB(VoretKeyBinder, columnFunctionOrigin, float, 0)
\r
15 ATTRIB(VoretKeyBinder, columnFunctionSize, float, 0)
\r
16 ATTRIB(VoretKeyBinder, columnKeysOrigin, float, 0)
\r
17 ATTRIB(VoretKeyBinder, columnKeysSize, float, 0)
\r
19 ATTRIB(VoretKeyBinder, lastClickedKey, float, -1)
\r
20 ATTRIB(VoretKeyBinder, lastClickedTime, float, 0)
\r
21 ATTRIB(VoretKeyBinder, previouslySelected, float, -1)
\r
22 ATTRIB(VoretKeyBinder, inMouseHandler, float, 0)
\r
23 ATTRIB(VoretKeyBinder, userbindEditButton, entity, NULL)
\r
24 ATTRIB(VoretKeyBinder, keyGrabButton, entity, NULL)
\r
25 ATTRIB(VoretKeyBinder, userbindEditDialog, entity, NULL)
\r
26 METHOD(VoretKeyBinder, editUserbind, void(entity, string, string, string))
\r
27 ENDCLASS(VoretKeyBinder)
\r
28 entity makeVoretKeyBinder();
\r
29 void KeyBinder_Bind_Change(entity btn, entity me);
\r
30 void KeyBinder_Bind_Clear(entity btn, entity me);
\r
31 void KeyBinder_Bind_Edit(entity btn, entity me);
\r
34 #ifdef IMPLEMENTATION
\r
36 #define MAX_KEYS_PER_FUNCTION 2
\r
37 #define MAX_KEYBINDS 256
\r
38 string Voret_KeyBinds_Functions[MAX_KEYBINDS];
\r
39 string Voret_KeyBinds_Descriptions[MAX_KEYBINDS];
\r
40 var float Voret_KeyBinds_Count = -1;
\r
42 void Voret_KeyBinds_Read()
\r
47 Voret_KeyBinds_Count = 0;
\r
48 fh = fopen("keybinds.txt", FILE_READ);
\r
51 while((s = fgets(fh)))
\r
53 if(tokenize_console(s) != 2)
\r
55 Voret_KeyBinds_Functions[Voret_KeyBinds_Count] = strzone(argv(0));
\r
56 Voret_KeyBinds_Descriptions[Voret_KeyBinds_Count] = strzone(argv(1));
\r
57 ++Voret_KeyBinds_Count;
\r
58 if(Voret_KeyBinds_Count >= MAX_KEYBINDS)
\r
64 entity makeVoretKeyBinder()
\r
67 me = spawnVoretKeyBinder();
\r
68 me.configureVoretKeyBinder(me);
\r
71 void configureVoretKeyBinderVoretKeyBinder(entity me)
\r
73 me.configureVoretListBox(me);
\r
74 if(Voret_KeyBinds_Count < 0)
\r
75 Voret_KeyBinds_Read();
\r
76 me.nItems = Voret_KeyBinds_Count;
\r
77 me.setSelected(me, 0);
\r
79 void resizeNotifyVoretKeyBinder(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
\r
81 resizeNotifyVoretListBox(me, relOrigin, relSize, absOrigin, absSize);
\r
83 me.realFontSize_y = me.fontSize / (absSize_y * me.itemHeight);
\r
84 me.realFontSize_x = me.fontSize / (absSize_x * (1 - me.controlWidth));
\r
85 me.realUpperMargin = 0.5 * (1 - me.realFontSize_y);
\r
87 me.columnFunctionOrigin = 0;
\r
88 me.columnKeysSize = me.realFontSize_x * 12;
\r
89 me.columnFunctionSize = 1 - me.columnKeysSize - 2 * me.realFontSize_x;
\r
90 me.columnKeysOrigin = me.columnFunctionOrigin + me.columnFunctionSize + me.realFontSize_x;
\r
92 if(me.userbindEditButton)
\r
93 me.userbindEditButton.disabled = (substring(Voret_KeyBinds_Descriptions[me.selectedItem], 0, 1) != "$");
\r
95 void KeyBinder_Bind_Change(entity btn, entity me)
\r
99 func = Voret_KeyBinds_Functions[me.selectedItem];
\r
103 me.keyGrabButton.forcePressed = 1;
\r
106 void keyGrabbedVoretKeyBinder(entity me, float key, float ascii)
\r
108 float n, j, k, nvalid;
\r
111 me.keyGrabButton.forcePressed = 0;
\r
112 if(key == K_ESCAPE)
\r
115 func = Voret_KeyBinds_Functions[me.selectedItem];
\r
119 n = tokenize(findkeysforcommand(func)); // uses '...' strings
\r
121 for(j = 0; j < n; ++j)
\r
127 if(nvalid >= MAX_KEYS_PER_FUNCTION)
\r
129 for(j = 0; j < n; ++j)
\r
133 localcmd("\nunbind \"", keynumtostring(k), "\"\n");
\r
136 localcmd("\nbind \"", keynumtostring(key), "\" \"", func, "\"\n");
\r
138 void editUserbindVoretKeyBinder(entity me, string theName, string theCommandPress, string theCommandRelease)
\r
140 string func, descr;
\r
142 if(!me.userbindEditDialog)
\r
145 func = Voret_KeyBinds_Functions[me.selectedItem];
\r
149 descr = Voret_KeyBinds_Descriptions[me.selectedItem];
\r
150 if(substring(descr, 0, 1) != "$")
\r
152 descr = substring(descr, 1, strlen(descr) - 1);
\r
154 // Hooray! It IS a user bind!
\r
155 cvar_set(strcat(descr, "_description"), theName);
\r
156 cvar_set(strcat(descr, "_press"), theCommandPress);
\r
157 cvar_set(strcat(descr, "_release"), theCommandRelease);
\r
159 void KeyBinder_Bind_Edit(entity btn, entity me)
\r
161 string func, descr;
\r
163 if(!me.userbindEditDialog)
\r
166 func = Voret_KeyBinds_Functions[me.selectedItem];
\r
170 descr = Voret_KeyBinds_Descriptions[me.selectedItem];
\r
171 if(substring(descr, 0, 1) != "$")
\r
173 descr = substring(descr, 1, strlen(descr) - 1);
\r
175 // Hooray! It IS a user bind!
\r
176 me.userbindEditDialog.loadUserBind(me.userbindEditDialog, cvar_string(strcat(descr, "_description")), cvar_string(strcat(descr, "_press")), cvar_string(strcat(descr, "_release")));
\r
178 DialogOpenButton_Click(btn, me.userbindEditDialog);
\r
180 void KeyBinder_Bind_Clear(entity btn, entity me)
\r
185 func = Voret_KeyBinds_Functions[me.selectedItem];
\r
189 n = tokenize(findkeysforcommand(func)); // uses '...' strings
\r
190 for(j = 0; j < n; ++j)
\r
194 localcmd("\nunbind \"", keynumtostring(k), "\"\n");
\r
198 void clickListBoxItemVoretKeyBinder(entity me, float i, vector where)
\r
200 if(i == me.lastClickedServer)
\r
201 if(time < me.lastClickedTime + 0.3)
\r
204 KeyBinder_Bind_Change(NULL, me);
\r
206 me.lastClickedServer = i;
\r
207 me.lastClickedTime = time;
\r
209 void setSelectedVoretKeyBinder(entity me, float i)
\r
211 // handling of "unselectable" items
\r
212 i = floor(0.5 + bound(0, i, me.nItems - 1));
\r
213 if(me.pressed == 0 || me.pressed == 1) // keyboard or scrolling - skip unselectable items
\r
215 if(i > me.previouslySelected)
\r
217 while((i < me.nItems - 1) && (Voret_KeyBinds_Functions[i] == ""))
\r
220 while((i > 0) && (Voret_KeyBinds_Functions[i] == ""))
\r
222 while((i < me.nItems - 1) && (Voret_KeyBinds_Functions[i] == ""))
\r
225 if(me.pressed == 3) // released the mouse - fall back to last valid item
\r
227 if(Voret_KeyBinds_Functions[i] == "")
\r
228 i = me.previouslySelected;
\r
230 if(Voret_KeyBinds_Functions[i] != "")
\r
231 me.previouslySelected = i;
\r
232 if(me.userbindEditButton)
\r
233 me.userbindEditButton.disabled = (substring(Voret_KeyBinds_Descriptions[i], 0, 1) != "$");
\r
234 setSelectedListBox(me, i);
\r
236 float keyDownVoretKeyBinder(entity me, float key, float ascii, float shift)
\r
244 KeyBinder_Bind_Change(me, me);
\r
248 KeyBinder_Bind_Clear(me, me);
\r
251 r = keyDownListBox(me, key, ascii, shift);
\r
256 void drawListBoxItemVoretKeyBinder(entity me, float i, vector absSize, float isSelected)
\r
262 string func, descr;
\r
265 descr = Voret_KeyBinds_Descriptions[i];
\r
266 func = Voret_KeyBinds_Functions[i];
\r
271 theColor = SKINCOLOR_KEYGRABBER_TITLES;
\r
272 theAlpha = SKINALPHA_KEYGRABBER_TITLES;
\r
279 if(keyGrabber == me)
\r
280 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_WAITING, SKINALPHA_LISTBOX_WAITING);
\r
282 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
\r
284 theAlpha = SKINALPHA_KEYGRABBER_KEYS;
\r
285 theColor = SKINCOLOR_KEYGRABBER_KEYS;
\r
286 extraMargin = me.realFontSize_x * 0.5;
\r
289 if(substring(descr, 0, 1) == "$")
\r
291 s = substring(descr, 1, strlen(descr) - 1);
\r
292 descr = cvar_string(strcat(s, "_description"));
\r
295 if(cvar_string(strcat(s, "_press")) == "")
\r
296 if(cvar_string(strcat(s, "_release")) == "")
\r
297 theAlpha *= SKINALPHA_DISABLED;
\r
300 draw_Text(me.realUpperMargin * eY + extraMargin * eX, descr, me.realFontSize, theColor, theAlpha, 0);
\r
303 n = tokenize(findkeysforcommand(func)); // uses '...' strings
\r
305 for(j = 0; j < n; ++j)
\r
311 s = strcat(s, ", ");
\r
312 s = strcat(s, keynumtostring(k));
\r
315 s = draw_TextShortenToWidth(s, me.columnKeysSize, 0, me.realFontSize);
\r
316 draw_CenterText(me.realUpperMargin * eY + (me.columnKeysOrigin + 0.5 * me.columnKeysSize) * eX, s, me.realFontSize, theColor, theAlpha, 0);
\r