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