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