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