]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/item/inputbox.c
Merge remote-tracking branch 'origin/mrbougo/killspree_bugfix'
[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) // if negative, it counts bytes, not chars
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(me.maxLength > 0)
83         {
84                 if(strlen(ch) + strlen(me.text) > me.maxLength)
85                         return;
86         }
87         else if(me.maxLength < 0)
88         {
89                 if(u8_strsize(ch) + u8_strsize(me.text) > -me.maxLength)
90                         return;
91         }
92         me.setText(me, strcat(substring(me.text, 0, me.cursorPos), ch, substring(me.text, me.cursorPos, strlen(me.text) - me.cursorPos)));
93         me.cursorPos += strlen(ch);
94 }
95
96 float InputBox_keyDown(entity me, float key, float ascii, float shift)
97 {
98         me.lastChangeTime = time;
99         me.dragScrollTimer = time;
100         if(ascii >= 32 && ascii != 127)
101         {
102                 me.enterText(me, chr(ascii));
103                 return 1;
104         }
105         switch(key)
106         {
107                 case K_KP_LEFTARROW:
108                 case K_LEFTARROW:
109                         me.cursorPos -= 1;
110                         return 1;
111                 case K_KP_RIGHTARROW:
112                 case K_RIGHTARROW:
113                         me.cursorPos += 1;
114                         return 1;
115                 case K_KP_HOME:
116                 case K_HOME:
117                         me.cursorPos = 0;
118                         return 1;
119                 case K_KP_END:
120                 case K_END:
121                         me.cursorPos = strlen(me.text);
122                         return 1;
123                 case K_BACKSPACE:
124                         if(me.cursorPos > 0)
125                         {
126                                 me.cursorPos -= 1;
127                                 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
128                         }
129                         return 1;
130                 case K_KP_DEL:
131                 case K_DEL:
132                         if(shift & S_CTRL)
133                                 me.setText(me, "");
134                         else
135                                 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
136                         return 1;
137         }
138         return 0;
139 }
140
141 void InputBox_draw(entity me)
142 {
143 #define CURSOR "_"
144         float cursorPosInWidths, totalSizeInWidths;
145
146         if(me.pressed)
147                 me.mouseDrag(me, me.dragScrollPos); // simulate mouseDrag event
148
149         if(me.recalcPos)
150                 me.recalcPositionWithText(me, me.text);
151
152         me.focusable = !me.disabled;
153         if(me.disabled)
154                 draw_alpha *= me.disabledAlpha;
155
156         if(me.src)
157         {
158                 if(me.focused && !me.disabled)
159                         draw_ButtonPicture('0 0 0', strcat(me.src, "_f"), '1 1 0', me.colorF, 1);
160                 else
161                         draw_ButtonPicture('0 0 0', strcat(me.src, "_n"), '1 1 0', me.color, 1);
162         }
163
164         me.cursorPos = bound(0, me.cursorPos, strlen(me.text));
165         cursorPosInWidths = draw_TextWidth(substring(me.text, 0, me.cursorPos), 0, me.realFontSize);
166         totalSizeInWidths = draw_TextWidth(strcat(me.text, CURSOR), 0, me.realFontSize);
167
168         if(me.dragScrollTimer < time)
169         {
170                 float save;
171                 save = me.scrollPos;
172                 me.scrollPos = bound(cursorPosInWidths - (0.875 - me.keepspaceLeft - me.keepspaceRight), me.scrollPos, cursorPosInWidths - 0.125);
173                 if(me.scrollPos != save)
174                         me.dragScrollTimer = time + 0.2;
175         }
176         me.scrollPos = min(me.scrollPos, totalSizeInWidths - (1 - me.keepspaceRight - me.keepspaceLeft));
177         me.scrollPos = max(0, me.scrollPos);
178
179         draw_SetClipRect(eX * me.keepspaceLeft, eX * (1 - me.keepspaceLeft - me.keepspaceRight) + eY);
180         if(me.editColorCodes)
181         {
182                 string ch, ch2;
183                 float i, n;
184                 vector theColor;
185                 float theAlpha;    //float theVariableAlpha;
186                 vector p;
187                 vector theTempColor;
188                 float component;
189                 
190                 p = me.realOrigin - eX * me.scrollPos;
191                 theColor = '1 1 1';
192                 theAlpha = 1;    //theVariableAlpha = 1; // changes when ^ax found
193                 
194                 n = strlen(me.text);
195                 for(i = 0; i < n; ++i)
196                 {
197                         ch = substring(me.text, i, 1);
198                         if(ch == "^")
199                         {
200                                 float w;
201                                 ch2 = substring(me.text, i+1, 1);
202                                 w = draw_TextWidth(strcat(ch, ch2), 0, me.realFontSize);
203                                 if(ch2 == "^")
204                                 {
205                                         draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
206                                         draw_Text(p + eX * 0.25 * w, "^", me.realFontSize, theColor, theAlpha, 0);
207                                 }
208                                 else if(ch2 == "0" || stof(ch2)) // digit?
209                                 {
210                                         switch(stof(ch2))
211                                         {
212                                                 case 0: theColor = '0 0 0'; theAlpha = 1; break;
213                                                 case 1: theColor = '1 0 0'; theAlpha = 1; break;
214                                                 case 2: theColor = '0 1 0'; theAlpha = 1; break;
215                                                 case 3: theColor = '1 1 0'; theAlpha = 1; break;
216                                                 case 4: theColor = '0 0 1'; theAlpha = 1; break;
217                                                 case 5: theColor = '0 1 1'; theAlpha = 1; break;
218                                                 case 6: theColor = '1 0 1'; theAlpha = 1; break;
219                                                 case 7: theColor = '1 1 1'; theAlpha = 1; break;
220                                                 case 8: theColor = '1 1 1'; theAlpha = 0.5; break;
221                                                 case 9: theColor = '0.5 0.5 0.5'; theAlpha = 1; break;
222                                         }
223                                         draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
224                                         draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
225                                 }
226                                 else if(ch2 == "x") // ^x found
227                                 {
228                                         theColor = '1 1 1';
229                                         
230                                         component = HEXDIGIT_TO_DEC(substring(me.text, i+2, 1));
231                                         if (component >= 0) // ^xr found
232                                         {
233                                                 theTempColor_x = component/15;
234                                                 
235                                                 component = HEXDIGIT_TO_DEC(substring(me.text, i+3, 1));
236                                                 if (component >= 0) // ^xrg found
237                                                 {
238                                                         theTempColor_y = component/15;
239                                                         
240                                                         component = HEXDIGIT_TO_DEC(substring(me.text, i+4, 1));
241                                                         if (component >= 0) // ^xrgb found
242                                                         {
243                                                                 theTempColor_z = component/15;
244                                                                 theColor = theTempColor;
245                                                                 w = draw_TextWidth(substring(me.text, i, 5), 0, me.realFontSize);
246                                                                 
247                                                                 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
248                                                                 draw_Text(p, substring(me.text, i, 5), me.realFontSize, theColor, 1, 0);    // theVariableAlpha instead of 1 using alpha tags ^ax
249                                                                 i += 3;
250                                                         }
251                                                         else
252                                                         {
253                                                                 // blue missing
254                                                                 w = draw_TextWidth(substring(me.text, i, 4), 0, me.realFontSize);
255                                                                 draw_Fill(p, eX * w + eY * me.realFontSize_y, eZ, 0.5);
256                                                                 draw_Text(p, substring(me.text, i, 4), me.realFontSize, '1 1 1', theAlpha, 0);
257                                                                 i += 2;
258                                                         }
259                                                 }
260                                                 else
261                                                 {
262                                                         // green missing
263                                                         w = draw_TextWidth(substring(me.text, i, 3), 0, me.realFontSize);
264                                                         draw_Fill(p, eX * w + eY * me.realFontSize_y, eY, 0.5);
265                                                         draw_Text(p, substring(me.text, i, 3), me.realFontSize, '1 1 1', theAlpha, 0);
266                                                         i += 1;
267                                                 }
268                                         }
269                                         else
270                                         {
271                                                 // red missing
272                                                 //w = draw_TextWidth(substring(me.text, i, 2), 0) * me.realFontSize_x;
273                                                 draw_Fill(p, eX * w + eY * me.realFontSize_y, eX, 0.5);
274                                                 draw_Text(p, substring(me.text, i, 2), me.realFontSize, '1 1 1', theAlpha, 0);
275                                         }
276                                 }
277                                 else
278                                 {
279                                         draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
280                                         draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
281                                 }
282                                 p += w * eX;
283                                 ++i;
284                                 continue;
285                         }
286                         draw_Text(p, ch, me.realFontSize, theColor, theAlpha, 0); // TODO theVariableAlpha
287                         p += eX * draw_TextWidth(ch, 0, me.realFontSize);
288                 }
289         }
290         else
291                 draw_Text(me.realOrigin - eX * me.scrollPos, me.text, me.realFontSize, '1 1 1', 1, 0);
292
293         if(!me.focused || (time - me.lastChangeTime) < floor(time - me.lastChangeTime) + 0.5)
294                 draw_Text(me.realOrigin + eX * (cursorPosInWidths - me.scrollPos), CURSOR, me.realFontSize, '1 1 1', 1, 0);
295
296         draw_ClearClip();
297
298         // skipping SUPER(InputBox).draw(me);
299         Item_draw(me);
300 }
301
302 void InputBox_showNotify(entity me)
303 {
304         me.focusable = !me.disabled;
305 }
306 #endif