]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/keybinder.qc
09fac4f28ac84a6cbba657f888c5f2b959ba5d3e
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / keybinder.qc
1 #ifdef INTERFACE
2 CLASS(XonoticKeyBinder) EXTENDS(XonoticListBox)
3         METHOD(XonoticKeyBinder, configureXonoticKeyBinder, void(entity))
4         ATTRIB(XonoticKeyBinder, rowsPerItem, int, 1)
5         METHOD(XonoticKeyBinder, drawListBoxItem, void(entity, int, vector, bool, float))
6         METHOD(XonoticKeyBinder, doubleClickListBoxItem, 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))
11
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)
18
19         ATTRIB(XonoticKeyBinder, previouslySelected, int, -1)
20         ATTRIB(XonoticKeyBinder, inMouseHandler, float, 0)
21         ATTRIB(XonoticKeyBinder, userbindEditButton, entity, NULL)
22         ATTRIB(XonoticKeyBinder, keyGrabButton, entity, NULL)
23         ATTRIB(XonoticKeyBinder, clearButton, entity, NULL)
24         ATTRIB(XonoticKeyBinder, userbindEditDialog, entity, NULL)
25         METHOD(XonoticKeyBinder, editUserbind, void(entity, string, string, string))
26 ENDCLASS(XonoticKeyBinder)
27 entity makeXonoticKeyBinder();
28 void KeyBinder_Bind_Change(entity btn, entity me);
29 void KeyBinder_Bind_Clear(entity btn, entity me);
30 void KeyBinder_Bind_Edit(entity btn, entity me);
31 #endif
32
33 #ifdef IMPLEMENTATION
34
35 const string KEY_NOT_BOUND_CMD = "// not bound";
36
37 const int MAX_KEYS_PER_FUNCTION = 2;
38 const int MAX_KEYBINDS = 256;
39 string Xonotic_KeyBinds_Functions[MAX_KEYBINDS];
40 string Xonotic_KeyBinds_Descriptions[MAX_KEYBINDS];
41 int Xonotic_KeyBinds_Count = -1;
42
43 void Xonotic_KeyBinds_Read()
44 {
45         int fh;
46         string s;
47
48         Xonotic_KeyBinds_Count = 0;
49         fh = fopen(language_filename("keybinds.txt"), FILE_READ);
50         if(fh < 0)
51                 return;
52         while((s = fgets(fh)))
53         {
54                 if(tokenize_console(s) != 2)
55                         continue;
56                 Xonotic_KeyBinds_Functions[Xonotic_KeyBinds_Count] = strzone(argv(0));
57                 Xonotic_KeyBinds_Descriptions[Xonotic_KeyBinds_Count] = strzone(argv(1));
58                 ++Xonotic_KeyBinds_Count;
59                 if(Xonotic_KeyBinds_Count >= MAX_KEYBINDS)
60                         break;
61         }
62         fclose(fh);
63 }
64
65 entity makeXonoticKeyBinder()
66 {
67         entity me;
68         me = spawnXonoticKeyBinder();
69         me.configureXonoticKeyBinder(me);
70         return me;
71 }
72 void replace_bind(string from, string to)
73 {
74         int n, j;
75         float k; // not sure if float or int
76         n = tokenize(findkeysforcommand(from, 0)); // uses '...' strings
77         for(j = 0; j < n; ++j)
78         {
79                 k = stof(argv(j));
80                 if(k != -1)
81                         localcmd("\nbind \"", keynumtostring(k), "\" \"", to, "\"\n");
82         }
83         if(n)
84                 cvar_set("_hud_showbinds_reload", "1");
85 }
86 void XonoticKeyBinder_configureXonoticKeyBinder(entity me)
87 {
88         me.configureXonoticListBox(me);
89         if(Xonotic_KeyBinds_Count < 0)
90                 Xonotic_KeyBinds_Read();
91         me.nItems = Xonotic_KeyBinds_Count;
92         me.setSelected(me, 0);
93
94         // TEMP: Xonotic 0.1 to later
95         replace_bind("impulse 1", "weapon_group_1");
96         replace_bind("impulse 2", "weapon_group_2");
97         replace_bind("impulse 3", "weapon_group_3");
98         replace_bind("impulse 4", "weapon_group_4");
99         replace_bind("impulse 5", "weapon_group_5");
100         replace_bind("impulse 6", "weapon_group_6");
101         replace_bind("impulse 7", "weapon_group_7");
102         replace_bind("impulse 8", "weapon_group_8");
103         replace_bind("impulse 9", "weapon_group_9");
104         replace_bind("impulse 14", "weapon_group_0");
105 }
106 void XonoticKeyBinder_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
107 {
108         SUPER(XonoticKeyBinder).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
109
110         me.realFontSize_y = me.fontSize / (absSize.y * me.itemHeight);
111         me.realFontSize_x = me.fontSize / (absSize.x * (1 - me.controlWidth));
112         me.realUpperMargin = 0.5 * (1 - me.realFontSize.y);
113
114         me.columnFunctionOrigin = 0;
115         me.columnKeysSize = me.realFontSize.x * 12;
116         me.columnFunctionSize = 1 - me.columnKeysSize - 2 * me.realFontSize.x;
117         me.columnKeysOrigin = me.columnFunctionOrigin + me.columnFunctionSize + me.realFontSize.x;
118
119         if(me.userbindEditButton)
120                 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[me.selectedItem], 0, 1) != "$");
121 }
122 void KeyBinder_Bind_Change(entity btn, entity me)
123 {
124         string func;
125
126         func = Xonotic_KeyBinds_Functions[me.selectedItem];
127         if(func == "")
128                 return;
129
130         me.keyGrabButton.forcePressed = 1;
131         me.clearButton.disabled = 1;
132         keyGrabber = me;
133 }
134 void XonoticKeyBinder_keyGrabbed(entity me, int key, bool ascii)
135 {
136         int n, j, nvalid;
137         float k;
138         string func;
139
140         me.keyGrabButton.forcePressed = 0;
141         me.clearButton.disabled = 0;
142
143         if(key == K_ESCAPE)
144                 return;
145
146         // forbid these keys from being bound in the menu
147         if(key == K_CAPSLOCK || key == K_NUMLOCK)
148         {
149                 KeyBinder_Bind_Change(me, me);
150                 return;
151         }
152
153         func = Xonotic_KeyBinds_Functions[me.selectedItem];
154         if(func == "")
155                 return;
156
157         n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
158         nvalid = 0;
159         for(j = 0; j < n; ++j)
160         {
161                 k = stof(argv(j));
162                 if(k != -1)
163                         ++nvalid;
164         }
165         if(nvalid >= MAX_KEYS_PER_FUNCTION)
166         {
167                 for(j = 0; j < n; ++j)
168                 {
169                         k = stof(argv(j));
170                         if(k != -1)
171                                 //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
172                                 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
173                 }
174         }
175         m_play_click_sound(MENU_SOUND_SELECT);
176         localcmd("\nbind \"", keynumtostring(key), "\" \"", func, "\"\n");
177         localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
178         cvar_set("_hud_showbinds_reload", "1");
179 }
180 void XonoticKeyBinder_editUserbind(entity me, string theName, string theCommandPress, string theCommandRelease)
181 {
182         string func, descr;
183
184         if(!me.userbindEditDialog)
185                 return;
186
187         func = Xonotic_KeyBinds_Functions[me.selectedItem];
188         if(func == "")
189                 return;
190
191         descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
192         if(substring(descr, 0, 1) != "$")
193                 return;
194         descr = substring(descr, 1, strlen(descr) - 1);
195
196         // Hooray! It IS a user bind!
197         cvar_set(strcat(descr, "_description"), theName);
198         cvar_set(strcat(descr, "_press"), theCommandPress);
199         cvar_set(strcat(descr, "_release"), theCommandRelease);
200 }
201 void KeyBinder_Bind_Edit(entity btn, entity me)
202 {
203         string func, descr;
204
205         if(!me.userbindEditDialog)
206                 return;
207
208         func = Xonotic_KeyBinds_Functions[me.selectedItem];
209         if(func == "")
210                 return;
211
212         descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
213         if(substring(descr, 0, 1) != "$")
214                 return;
215         descr = substring(descr, 1, strlen(descr) - 1);
216
217         // Hooray! It IS a user bind!
218         me.userbindEditDialog.loadUserBind(me.userbindEditDialog, cvar_string(strcat(descr, "_description")), cvar_string(strcat(descr, "_press")), cvar_string(strcat(descr, "_release")));
219
220         DialogOpenButton_Click(btn, me.userbindEditDialog);
221 }
222 void KeyBinder_Bind_Clear(entity btn, entity me)
223 {
224         float n, j, k;
225         string func;
226
227         func = Xonotic_KeyBinds_Functions[me.selectedItem];
228         if(func == "")
229                 return;
230
231         n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
232         for(j = 0; j < n; ++j)
233         {
234                 k = stof(argv(j));
235                 if(k != -1)
236                         //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
237                         localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
238         }
239         m_play_click_sound(MENU_SOUND_CLEAR);
240         localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
241         cvar_set("_hud_showbinds_reload", "1");
242 }
243 void KeyBinder_Bind_Reset_All(entity btn, entity me)
244 {
245         localcmd("unbindall\n");
246         localcmd("exec binds-xonotic.cfg\n");
247         localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
248         cvar_set("_hud_showbinds_reload", "1");
249 }
250 void XonoticKeyBinder_doubleClickListBoxItem(entity me, float i, vector where)
251 {
252         KeyBinder_Bind_Change(NULL, me);
253 }
254 void XonoticKeyBinder_setSelected(entity me, int i)
255 {
256         // handling of "unselectable" items
257         i = floor(0.5 + bound(0, i, me.nItems - 1));
258         if(me.pressed == 0 || me.pressed == 1) // keyboard or scrolling - skip unselectable items
259         {
260                 if(i > me.previouslySelected)
261                 {
262                         while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
263                                 ++i;
264                 }
265                 while((i > 0) && (Xonotic_KeyBinds_Functions[i] == ""))
266                         --i;
267                 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
268                         ++i;
269         }
270         if(me.pressed == 3) // released the mouse - fall back to last valid item
271         {
272                 if(Xonotic_KeyBinds_Functions[i] == "")
273                         i = me.previouslySelected;
274         }
275         if(Xonotic_KeyBinds_Functions[i] != "")
276                 me.previouslySelected = i;
277         if(me.userbindEditButton)
278                 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[i], 0, 1) != "$");
279         SUPER(XonoticKeyBinder).setSelected(me, i);
280 }
281 float XonoticKeyBinder_keyDown(entity me, int key, bool ascii, float shift)
282 {
283         bool r = true;
284         switch(key)
285         {
286                 case K_ENTER:
287                 case K_KP_ENTER:
288                 case K_SPACE:
289                         KeyBinder_Bind_Change(me, me);
290                         break;
291                 case K_DEL:
292                 case K_KP_DEL:
293                 case K_BACKSPACE:
294                         KeyBinder_Bind_Clear(me, me);
295                         break;
296                 default:
297                         r = SUPER(XonoticKeyBinder).keyDown(me, key, ascii, shift);
298                         break;
299         }
300         return r;
301 }
302 void XonoticKeyBinder_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, float highlightedTime)
303 {
304         string s;
305         int j, n;
306         float k;
307         vector theColor;
308         float theAlpha;
309         string func, descr;
310         float extraMargin;
311
312         descr = Xonotic_KeyBinds_Descriptions[i];
313         func = Xonotic_KeyBinds_Functions[i];
314
315         if(func == "")
316         {
317                 theAlpha = 1;
318                 theColor = SKINCOLOR_KEYGRABBER_TITLES;
319                 theAlpha = SKINALPHA_KEYGRABBER_TITLES;
320                 extraMargin = 0;
321         }
322         else
323         {
324                 if(isSelected)
325                 {
326                         if(keyGrabber == me)
327                                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_WAITING, SKINALPHA_LISTBOX_WAITING);
328                         else
329                                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
330                 }
331                 else if(highlightedTime)
332                         draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_FOCUSED, getHighlightAlpha(SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED, highlightedTime));
333
334                 theAlpha = SKINALPHA_KEYGRABBER_KEYS;
335                 theColor = SKINCOLOR_KEYGRABBER_KEYS;
336                 extraMargin = me.realFontSize.x * 0.5;
337         }
338
339         if(substring(descr, 0, 1) == "$")
340         {
341                 s = substring(descr, 1, strlen(descr) - 1);
342                 descr = cvar_string(strcat(s, "_description"));
343                 if(descr == "")
344                         descr = s;
345                 if(cvar_string(strcat(s, "_press")) == "")
346                         if(cvar_string(strcat(s, "_release")) == "")
347                                 theAlpha *= SKINALPHA_DISABLED;
348         }
349
350         s = draw_TextShortenToWidth(descr, me.columnFunctionSize, 0, me.realFontSize);
351         draw_Text(me.realUpperMargin * eY + extraMargin * eX, s, me.realFontSize, theColor, theAlpha, 0);
352         if(func != "")
353         {
354                 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
355                 s = "";
356                 for(j = 0; j < n; ++j)
357                 {
358                         k = stof(argv(j));
359                         if(k != -1)
360                         {
361                                 if(s != "")
362                                         s = strcat(s, ", ");
363                                 s = strcat(s, keynumtostring(k));
364                         }
365                 }
366                 s = draw_TextShortenToWidth(s, me.columnKeysSize, 0, me.realFontSize);
367                 draw_CenterText(me.realUpperMargin * eY + (me.columnKeysOrigin + 0.5 * me.columnKeysSize) * eX, s, me.realFontSize, theColor, theAlpha, 0);
368         }
369 }
370 #endif