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