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