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