]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/keybinder.c
New cvar _hud_showbinds_reload 0 "set it to 1 to reload binds if you changed any...
[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         float n, j, k;
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         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         if(key == K_ESCAPE)
140                 return;
141
142         func = Xonotic_KeyBinds_Functions[me.selectedItem];
143         if(func == "")
144                 return;
145
146         n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
147         nvalid = 0;
148         for(j = 0; j < n; ++j)
149         {
150                 k = stof(argv(j));
151                 if(k != -1)
152                         ++nvalid;
153         }
154         if(nvalid >= MAX_KEYS_PER_FUNCTION)
155         {
156                 for(j = 0; j < n; ++j)
157                 {
158                         k = stof(argv(j));
159                         if(k != -1)
160                                 //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
161                                 localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
162                 }
163         }
164         localcmd("\nbind \"", keynumtostring(key), "\" \"", func, "\"\n");
165         localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
166         cvar_set("_hud_showbinds_reload", "1");
167 }
168 void XonoticKeyBinder_editUserbind(entity me, string theName, string theCommandPress, string theCommandRelease)
169 {
170         string func, descr;
171
172         if(!me.userbindEditDialog)
173                 return;
174
175         func = Xonotic_KeyBinds_Functions[me.selectedItem];
176         if(func == "")
177                 return;
178
179         descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
180         if(substring(descr, 0, 1) != "$")
181                 return;
182         descr = substring(descr, 1, strlen(descr) - 1);
183
184         // Hooray! It IS a user bind!
185         cvar_set(strcat(descr, "_description"), theName);
186         cvar_set(strcat(descr, "_press"), theCommandPress);
187         cvar_set(strcat(descr, "_release"), theCommandRelease);
188 }
189 void KeyBinder_Bind_Edit(entity btn, entity me)
190 {
191         string func, descr;
192
193         if(!me.userbindEditDialog)
194                 return;
195
196         func = Xonotic_KeyBinds_Functions[me.selectedItem];
197         if(func == "")
198                 return;
199
200         descr = Xonotic_KeyBinds_Descriptions[me.selectedItem];
201         if(substring(descr, 0, 1) != "$")
202                 return;
203         descr = substring(descr, 1, strlen(descr) - 1);
204
205         // Hooray! It IS a user bind!
206         me.userbindEditDialog.loadUserBind(me.userbindEditDialog, cvar_string(strcat(descr, "_description")), cvar_string(strcat(descr, "_press")), cvar_string(strcat(descr, "_release")));
207
208         DialogOpenButton_Click(btn, me.userbindEditDialog);
209 }
210 void KeyBinder_Bind_Clear(entity btn, entity me)
211 {
212         float n, j, k;
213         string func;
214
215         func = Xonotic_KeyBinds_Functions[me.selectedItem];
216         if(func == "")
217                 return;
218
219         n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
220         for(j = 0; j < n; ++j)
221         {
222                 k = stof(argv(j));
223                 if(k != -1)
224                         //localcmd("\nunbind \"", keynumtostring(k), "\"\n");
225                         localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n");
226         }
227         localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state
228         cvar_set("_hud_showbinds_reload", "1");
229 }
230 void XonoticKeyBinder_clickListBoxItem(entity me, float i, vector where)
231 {
232         if(i == me.lastClickedServer)
233                 if(time < me.lastClickedTime + 0.3)
234                 {
235                         // DOUBLE CLICK!
236                         KeyBinder_Bind_Change(NULL, me);
237                 }
238         me.lastClickedServer = i;
239         me.lastClickedTime = time;
240 }
241 void XonoticKeyBinder_setSelected(entity me, float i)
242 {
243         // handling of "unselectable" items
244         i = floor(0.5 + bound(0, i, me.nItems - 1));
245         if(me.pressed == 0 || me.pressed == 1) // keyboard or scrolling - skip unselectable items
246         {
247                 if(i > me.previouslySelected)
248                 {
249                         while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
250                                 ++i;
251                 }
252                 while((i > 0) && (Xonotic_KeyBinds_Functions[i] == ""))
253                         --i;
254                 while((i < me.nItems - 1) && (Xonotic_KeyBinds_Functions[i] == ""))
255                         ++i;
256         }
257         if(me.pressed == 3) // released the mouse - fall back to last valid item
258         {
259                 if(Xonotic_KeyBinds_Functions[i] == "")
260                         i = me.previouslySelected;
261         }
262         if(Xonotic_KeyBinds_Functions[i] != "")
263                 me.previouslySelected = i;
264         if(me.userbindEditButton)
265                 me.userbindEditButton.disabled = (substring(Xonotic_KeyBinds_Descriptions[i], 0, 1) != "$");
266         SUPER(XonoticKeyBinder).setSelected(me, i);
267 }
268 float XonoticKeyBinder_keyDown(entity me, float key, float ascii, float shift)
269 {
270         float r;
271         r = 1;
272         switch(key)
273         {
274                 case K_ENTER:
275                 case K_KP_ENTER:
276                 case K_SPACE:
277                         KeyBinder_Bind_Change(me, me);
278                         break;
279                 case K_DEL:
280                 case K_KP_DEL:
281                 case K_BACKSPACE:
282                         KeyBinder_Bind_Clear(me, me);
283                         break;
284                 default:
285                         r = SUPER(XonoticKeyBinder).keyDown(me, key, ascii, shift);
286                         break;
287         }
288         return r;
289 }
290 void XonoticKeyBinder_drawListBoxItem(entity me, float i, vector absSize, float isSelected)
291 {
292         string s;
293         float j, k, n;
294         vector theColor;
295         float theAlpha;
296         string func, descr;
297         float extraMargin;
298
299         descr = Xonotic_KeyBinds_Descriptions[i];
300         func = Xonotic_KeyBinds_Functions[i];
301
302         if(func == "")
303         {
304                 theAlpha = 1;
305                 theColor = SKINCOLOR_KEYGRABBER_TITLES;
306                 theAlpha = SKINALPHA_KEYGRABBER_TITLES;
307                 extraMargin = 0;
308         }
309         else
310         {
311                 if(isSelected)
312                 {
313                         if(keyGrabber == me)
314                                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_WAITING, SKINALPHA_LISTBOX_WAITING);
315                         else
316                                 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_LISTBOX_SELECTED, SKINALPHA_LISTBOX_SELECTED);
317                 }
318                 theAlpha = SKINALPHA_KEYGRABBER_KEYS;
319                 theColor = SKINCOLOR_KEYGRABBER_KEYS;
320                 extraMargin = me.realFontSize_x * 0.5;
321         }
322
323         if(substring(descr, 0, 1) == "$")
324         {
325                 s = substring(descr, 1, strlen(descr) - 1);
326                 descr = cvar_string(strcat(s, "_description"));
327                 if(descr == "")
328                         descr = s;
329                 if(cvar_string(strcat(s, "_press")) == "")
330                         if(cvar_string(strcat(s, "_release")) == "")
331                                 theAlpha *= SKINALPHA_DISABLED;
332         }
333
334         s = draw_TextShortenToWidth(descr, me.columnFunctionSize, 0, me.realFontSize);
335         draw_Text(me.realUpperMargin * eY + extraMargin * eX, s, me.realFontSize, theColor, theAlpha, 0);
336         if(func != "")
337         {
338                 n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings
339                 s = "";
340                 for(j = 0; j < n; ++j)
341                 {
342                         k = stof(argv(j));
343                         if(k != -1)
344                         {
345                                 if(s != "")
346                                         s = strcat(s, ", ");
347                                 s = strcat(s, keynumtostring(k));
348                         }
349                 }
350                 s = draw_TextShortenToWidth(s, me.columnKeysSize, 0, me.realFontSize);
351                 draw_CenterText(me.realUpperMargin * eY + (me.columnKeysOrigin + 0.5 * me.columnKeysSize) * eX, s, me.realFontSize, theColor, theAlpha, 0);
352         }
353 }
354 #endif