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