]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/item/inputbox.c
Merge branch 'master' into mirceakitsune/multijump
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / item / inputbox.c
1 #ifdef INTERFACE
2 CLASS(InputBox) EXTENDS(Label)
3         METHOD(InputBox, configureInputBox, void(entity, string, float, float, string))
4         METHOD(InputBox, draw, void(entity))
5         METHOD(InputBox, setText, void(entity, string))
6         METHOD(InputBox, enterText, void(entity, string))
7         METHOD(InputBox, keyDown, float(entity, float, float, float))
8         METHOD(InputBox, mouseRelease, float(entity, vector))
9         METHOD(InputBox, mousePress, float(entity, vector))
10         METHOD(InputBox, mouseDrag, float(entity, vector))
11         METHOD(InputBox, showNotify, void(entity))
12
13         ATTRIB(InputBox, src, string, string_null)
14
15         ATTRIB(InputBox, cursorPos, float, 0) // characters
16         ATTRIB(InputBox, scrollPos, float, 0) // widths
17
18         ATTRIB(InputBox, focusable, float, 1)
19         ATTRIB(InputBox, disabled, float, 0)
20         ATTRIB(InputBox, lastChangeTime, float, 0)
21         ATTRIB(InputBox, dragScrollTimer, float, 0)
22         ATTRIB(InputBox, dragScrollPos, vector, '0 0 0')
23         ATTRIB(InputBox, pressed, float, 0)
24         ATTRIB(InputBox, editColorCodes, float, 1)
25         ATTRIB(InputBox, forbiddenCharacters, string, "")
26         ATTRIB(InputBox, color, vector, '1 1 1')
27         ATTRIB(InputBox, colorF, vector, '1 1 1')
28         ATTRIB(InputBox, maxLength, float, 255)
29 ENDCLASS(InputBox)
30 void InputBox_Clear_Click(entity btn, entity me);
31 #endif
32
33 #ifdef IMPLEMENTATION
34 void InputBox_configureInputBox(entity me, string theText, float theCursorPos, float theFontSize, string gfx)
35 {
36         SUPER(InputBox).configureLabel(me, theText, theFontSize, 0.0);
37         me.src = gfx;
38         me.cursorPos = theCursorPos;
39 }
40
41 void InputBox_setText(entity me, string txt)
42 {
43         if(me.text)
44                 strunzone(me.text);
45         SUPER(InputBox).setText(me, strzone(txt));
46 }
47
48 void InputBox_Clear_Click(entity btn, entity me)
49 {
50         me.setText(me, "");
51 }
52
53 float InputBox_mouseDrag(entity me, vector pos)
54 {
55         float p;
56         me.dragScrollPos = pos;
57         p = me.scrollPos + pos_x - me.keepspaceLeft;
58         me.cursorPos = draw_TextLengthUpToWidth(me.text, p, 0, me.realFontSize);
59         me.lastChangeTime = time;
60         return 1;
61 }
62
63 float InputBox_mousePress(entity me, vector pos)
64 {
65         me.dragScrollTimer = time;
66         me.pressed = 1;
67         return InputBox_mouseDrag(me, pos);
68 }
69
70 float InputBox_mouseRelease(entity me, vector pos)
71 {
72         me.pressed = 0;
73         return InputBox_mouseDrag(me, pos);
74 }
75
76 void InputBox_enterText(entity me, string ch)
77 {
78         float i;
79         for(i = 0; i < strlen(ch); ++i)
80                 if(strstrofs(me.forbiddenCharacters, substring(ch, i, 1), 0) > -1)
81                         return;
82         if(strlen(ch) + strlen(me.text) > me.maxLength)
83                 return;
84         me.setText(me, strcat(substring(me.text, 0, me.cursorPos), ch, substring(me.text, me.cursorPos, strlen(me.text) - me.cursorPos)));
85         me.cursorPos += strlen(ch);
86 }
87
88 float InputBox_keyDown(entity me, float key, float ascii, float shift)
89 {
90         me.lastChangeTime = time;
91         me.dragScrollTimer = time;
92         if(ascii >= 32 && ascii != 127)
93         {
94                 me.enterText(me, chr(ascii));
95                 return 1;
96         }
97         switch(key)
98         {
99                 case K_KP_LEFTARROW:
100                 case K_LEFTARROW:
101                         me.cursorPos -= 1;
102                         return 1;
103                 case K_KP_RIGHTARROW:
104                 case K_RIGHTARROW:
105                         me.cursorPos += 1;
106                         return 1;
107                 case K_KP_HOME:
108                 case K_HOME:
109                         me.cursorPos = 0;
110                         return 1;
111                 case K_KP_END:
112                 case K_END:
113                         me.cursorPos = strlen(me.text);
114                         return 1;
115                 case K_BACKSPACE:
116                         if(me.cursorPos > 0)
117                         {
118                                 me.cursorPos -= 1;
119                                 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
120                         }
121                         return 1;
122                 case K_KP_DEL:
123                 case K_DEL:
124                         if(shift & S_CTRL)
125                                 me.setText(me, "");
126                         else
127                                 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
128                         return 1;
129         }
130         return 0;
131 }
132
133 void InputBox_draw(entity me)
134 {
135 #define CURSOR "_"
136         float cursorPosInWidths, totalSizeInWidths;
137
138         if(me.pressed)
139                 me.mouseDrag(me, me.dragScrollPos); // simulate mouseDrag event
140
141         me.focusable = !me.disabled;
142         if(me.disabled)
143                 draw_alpha *= me.disabledAlpha;
144
145         if(me.src)
146         {
147                 if(me.focused && !me.disabled)
148                         draw_ButtonPicture('0 0 0', strcat(me.src, "_f"), '1 1 0', me.colorF, 1);
149                 else
150                         draw_ButtonPicture('0 0 0', strcat(me.src, "_n"), '1 1 0', me.color, 1);
151         }
152
153         me.cursorPos = bound(0, me.cursorPos, strlen(me.text));
154         cursorPosInWidths = draw_TextWidth(substring(me.text, 0, me.cursorPos), 0, me.realFontSize);
155         totalSizeInWidths = draw_TextWidth(strcat(me.text, CURSOR), 0, me.realFontSize);
156
157         if(me.dragScrollTimer < time)
158         {
159                 float save;
160                 save = me.scrollPos;
161                 me.scrollPos = bound(cursorPosInWidths - (0.875 - me.keepspaceLeft - me.keepspaceRight), me.scrollPos, cursorPosInWidths - 0.125);
162                 if(me.scrollPos != save)
163                         me.dragScrollTimer = time + 0.2;
164         }
165         me.scrollPos = min(me.scrollPos, totalSizeInWidths - (1 - me.keepspaceRight - me.keepspaceLeft));
166         me.scrollPos = max(0, me.scrollPos);
167
168         draw_SetClipRect(eX * me.keepspaceLeft, eX * (1 - me.keepspaceLeft - me.keepspaceRight) + eY);
169         if(me.editColorCodes)
170         {
171                 string ch, ch2;
172                 float i, n;
173                 vector theColor;
174                 float theAlpha;    //float theVariableAlpha;
175                 vector p;
176                 vector theTempColor;
177                 float component;
178                 
179                 p = me.realOrigin - eX * me.scrollPos;
180                 theColor = '1 1 1';
181                 theAlpha = 1;    //theVariableAlpha = 1; // changes when ^ax found
182                 
183                 n = strlen(me.text);
184                 for(i = 0; i < n; ++i)
185                 {
186                         ch = substring(me.text, i, 1);
187                         if(ch == "^")
188                         {
189                                 float w;
190                                 ch2 = substring(me.text, i+1, 1);
191                                 w = draw_TextWidth(strcat(ch, ch2), 0, me.realFontSize);
192                                 if(ch2 == "^")
193                                 {
194                                         draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
195                                         draw_Text(p + eX * 0.25 * w, "^", me.realFontSize, theColor, theAlpha, 0);
196                                 }
197                                 else if(ch2 == "0" || stof(ch2)) // digit?
198                                 {
199                                         switch(stof(ch2))
200                                         {
201                                                 case 0: theColor = '0 0 0'; theAlpha = 1; break;
202                                                 case 1: theColor = '1 0 0'; theAlpha = 1; break;
203                                                 case 2: theColor = '0 1 0'; theAlpha = 1; break;
204                                                 case 3: theColor = '1 1 0'; theAlpha = 1; break;
205                                                 case 4: theColor = '0 0 1'; theAlpha = 1; break;
206                                                 case 5: theColor = '0 1 1'; theAlpha = 1; break;
207                                                 case 6: theColor = '1 0 1'; theAlpha = 1; break;
208                                                 case 7: theColor = '1 1 1'; theAlpha = 1; break;
209                                                 case 8: theColor = '1 1 1'; theAlpha = 0.5; break;
210                                                 case 9: theColor = '0.5 0.5 0.5'; theAlpha = 1; break;
211                                         }
212                                         draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
213                                         draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
214                                 }
215                                 else if(ch2 == "x") // ^x found
216                                 {
217                                         theColor = '1 1 1';
218                                         theTempColor = '0 0 0';
219                                         
220                                         component = HEXDIGIT_TO_DEC(substring(me.text, i+2, 1));
221                                         if (component >= 0) // ^xr found
222                                         {
223                                                 theTempColor_x = component/15;
224                                                 
225                                                 component = HEXDIGIT_TO_DEC(substring(me.text, i+3, 1));
226                                                 if (component >= 0) // ^xrg found
227                                                 {
228                                                         theTempColor_y = component/15;
229                                                         
230                                                         component = HEXDIGIT_TO_DEC(substring(me.text, i+4, 1));
231                                                         if (component >= 0) // ^xrgb found
232                                                         {
233                                                                 theTempColor_z = component/15;
234                                                                 theColor = theTempColor;
235                                                                 w = draw_TextWidth(substring(me.text, i, 5), 0, me.realFontSize);
236                                                                 
237                                                                 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
238                                                                 draw_Text(p, substring(me.text, i, 5), me.realFontSize, theColor, 1, 0);    // theVariableAlpha instead of 1 using alpha tags ^ax
239                                                                 i += 3;
240                                                         }
241                                                         else
242                                                         {
243                                                                 // blue missing
244                                                                 w = draw_TextWidth(substring(me.text, i, 4), 0, me.realFontSize);
245                                                                 draw_Fill(p, eX * w + eY * me.realFontSize_y, eZ, 0.5);
246                                                                 draw_Text(p, substring(me.text, i, 4), me.realFontSize, '1 1 1', theAlpha, 0);
247                                                                 i += 2;
248                                                         }
249                                                 }
250                                                 else
251                                                 {
252                                                         // green missing
253                                                         w = draw_TextWidth(substring(me.text, i, 3), 0, me.realFontSize);
254                                                         draw_Fill(p, eX * w + eY * me.realFontSize_y, eY, 0.5);
255                                                         draw_Text(p, substring(me.text, i, 3), me.realFontSize, '1 1 1', theAlpha, 0);
256                                                         i += 1;
257                                                 }
258                                         }
259                                         else
260                                         {
261                                                 // red missing
262                                                 //w = draw_TextWidth(substring(me.text, i, 2), 0) * me.realFontSize_x;
263                                                 draw_Fill(p, eX * w + eY * me.realFontSize_y, eX, 0.5);
264                                                 draw_Text(p, substring(me.text, i, 2), me.realFontSize, '1 1 1', theAlpha, 0);
265                                         }
266                                 }
267                                 /*else if(ch2 == "a") // ^a found
268                                 {
269                                         draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
270                                         draw_Text(p, substring(me.text, i, 2), me.realFontSize, theColor, 0.8, 0);
271                                         
272                                         component = str2chr(me.text, i+2);
273                                         if (component >= '0' && component <= '9')
274                                                 component = component - '0';
275                                         else if (component >= 'a' && component <= 'f')
276                                                 component = component - 87;
277                                         else if (component >= 'A' && component <= 'F')
278                                                 component = component - 55;
279                                         else
280                                                 component = -1;
281                                         
282                                         if (component >= 0) // ^ah found
283                                         {
284                                                 // FIX ME: overflow here
285                                                 if (component == 20 && theVariableAlpha <= 0.97)
286                                                         theVariableAlpha = theVariableAlpha + 0.0625;
287                                                 else if (component == 30 && theVariableAlpha >= 0.03)
288                                                         theVariableAlpha = theVariableAlpha - 0.0625;
289                                                 else
290                                                         theVariableAlpha = component*0.0625;
291                                                 
292                                                 draw_Fill(p, eX * draw_TextWidth(substring(me.text, i, 3), 0, me.realFontSize) + eY * me.realFontSize_y, '0.8 0.8 0.8', 0.5);
293                                                 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, 0.8, 0);
294                                         }
295                                 }*/
296                                 else
297                                 {
298                                         draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
299                                         draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
300                                 }
301                                 p += w * eX;
302                                 ++i;
303                                 continue;
304                         }
305                         draw_Text(p, ch, me.realFontSize, theColor, theAlpha, 0); // TODO theVariableAlpha
306                         p += eX * draw_TextWidth(ch, 0, me.realFontSize);
307                 }
308         }
309         else
310                 draw_Text(me.realOrigin - eX * me.scrollPos, me.text, me.realFontSize, '1 1 1', 1, 0);
311                 // skipping SUPER(InputBox).draw(me);
312         if(!me.focused || (time - me.lastChangeTime) < floor(time - me.lastChangeTime) + 0.5)
313                 draw_Text(me.realOrigin + eX * (cursorPosInWidths - me.scrollPos), CURSOR, me.realFontSize, '1 1 1', 1, 0);
314
315         draw_ClearClip();
316 }
317
318 void InputBox_showNotify(entity me)
319 {
320         me.focusable = !me.disabled;
321 }
322 #endif