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))
13 ATTRIB(InputBox, src, string, string_null)
15 ATTRIB(InputBox, cursorPos, float, 0) // characters
16 ATTRIB(InputBox, scrollPos, float, 0) // widths
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)
30 void InputBox_Clear_Click(entity btn, entity me);
34 void configureInputBoxInputBox(entity me, string theText, float theCursorPos, float theFontSize, string gfx)
36 SUPER(InputBox).configureLabel(me, theText, theFontSize, 0.0);
38 me.cursorPos = theCursorPos;
41 void setTextInputBox(entity me, string txt)
45 SUPER(InputBox).setText(me, strzone(txt));
48 void InputBox_Clear_Click(entity btn, entity me)
53 float mouseDragInputBox(entity me, vector pos)
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;
63 float mousePressInputBox(entity me, vector pos)
65 me.dragScrollTimer = time;
67 return mouseDragInputBox(me, pos);
70 float mouseReleaseInputBox(entity me, vector pos)
73 return mouseDragInputBox(me, pos);
76 void enterTextInputBox(entity me, string ch)
79 for(i = 0; i < strlen(ch); ++i)
80 if(strstrofs(me.forbiddenCharacters, substring(ch, i, 1), 0) > -1)
82 if(strlen(ch) + strlen(me.text) > me.maxLength)
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);
88 float keyDownInputBox(entity me, float key, float ascii, float shift)
90 me.lastChangeTime = time;
91 me.dragScrollTimer = time;
92 if(ascii >= 32 && ascii != 127)
94 me.enterText(me, chr(ascii));
109 me.cursorPos = strlen(me.text);
115 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
122 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
128 void drawInputBox(entity me)
131 float cursorPosInWidths, totalSizeInWidths;
134 me.mouseDrag(me, me.dragScrollPos); // simulate mouseDrag event
136 me.focusable = !me.disabled;
138 draw_alpha *= me.disabledAlpha;
142 if(me.focused && !me.disabled)
143 draw_ButtonPicture('0 0 0', strcat(me.src, "_f"), '1 1 0', me.colorF, 1);
145 draw_ButtonPicture('0 0 0', strcat(me.src, "_n"), '1 1 0', me.color, 1);
148 me.cursorPos = bound(0, me.cursorPos, strlen(me.text));
149 cursorPosInWidths = draw_TextWidth(substring(me.text, 0, me.cursorPos), 0, me.realFontSize);
150 totalSizeInWidths = draw_TextWidth(strcat(me.text, CURSOR), 0, me.realFontSize);
152 if(me.dragScrollTimer < time)
156 me.scrollPos = bound(cursorPosInWidths - (0.875 - me.keepspaceLeft - me.keepspaceRight), me.scrollPos, cursorPosInWidths - 0.125);
157 if(me.scrollPos != save)
158 me.dragScrollTimer = time + 0.2;
160 me.scrollPos = min(me.scrollPos, totalSizeInWidths - (1 - me.keepspaceRight - me.keepspaceLeft));
161 me.scrollPos = max(0, me.scrollPos);
163 draw_SetClipRect(eX * me.keepspaceLeft, eX * (1 - me.keepspaceLeft - me.keepspaceRight) + eY);
164 if(me.editColorCodes)
169 float theAlpha; //float theVariableAlpha;
174 p = me.realOrigin - eX * me.scrollPos;
176 theAlpha = 1; //theVariableAlpha = 1; // changes when ^ax found
179 for(i = 0; i < n; ++i)
181 ch = substring(me.text, i, 1);
185 ch2 = substring(me.text, i+1, 1);
186 w = draw_TextWidth(strcat(ch, ch2), 0, me.realFontSize);
189 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
190 draw_Text(p + eX * 0.25 * w, "^", me.realFontSize, theColor, theAlpha, 0);
192 else if(ch2 == "0" || stof(ch2)) // digit?
196 case 0: theColor = '0 0 0'; theAlpha = 1; break;
197 case 1: theColor = '1 0 0'; theAlpha = 1; break;
198 case 2: theColor = '0 1 0'; theAlpha = 1; break;
199 case 3: theColor = '1 1 0'; theAlpha = 1; break;
200 case 4: theColor = '0 0 1'; theAlpha = 1; break;
201 case 5: theColor = '0 1 1'; theAlpha = 1; break;
202 case 6: theColor = '1 0 1'; theAlpha = 1; break;
203 case 7: theColor = '1 1 1'; theAlpha = 1; break;
204 case 8: theColor = '1 1 1'; theAlpha = 0.5; break;
205 case 9: theColor = '0.5 0.5 0.5'; theAlpha = 1; break;
207 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
208 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
210 else if(ch2 == "x") // ^x found
213 theTempColor = '0 0 0';
215 component = HEXDIGIT_TO_DEC(substring(me.text, i+2, 1));
216 if (component >= 0) // ^xr found
218 theTempColor_x = component/15;
220 component = HEXDIGIT_TO_DEC(substring(me.text, i+3, 1));
221 if (component >= 0) // ^xrg found
223 theTempColor_y = component/15;
225 component = HEXDIGIT_TO_DEC(substring(me.text, i+4, 1));
226 if (component >= 0) // ^xrgb found
228 theTempColor_z = component/15;
229 theColor = theTempColor;
230 w = draw_TextWidth(substring(me.text, i, 5), 0, me.realFontSize);
232 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
233 draw_Text(p, substring(me.text, i, 5), me.realFontSize, theColor, 1, 0); // theVariableAlpha instead of 1 using alpha tags ^ax
239 w = draw_TextWidth(substring(me.text, i, 4), 0, me.realFontSize);
240 draw_Fill(p, eX * w + eY * me.realFontSize_y, eZ, 0.5);
241 draw_Text(p, substring(me.text, i, 4), me.realFontSize, '1 1 1', theAlpha, 0);
248 w = draw_TextWidth(substring(me.text, i, 3), 0, me.realFontSize);
249 draw_Fill(p, eX * w + eY * me.realFontSize_y, eY, 0.5);
250 draw_Text(p, substring(me.text, i, 3), me.realFontSize, '1 1 1', theAlpha, 0);
257 //w = draw_TextWidth(substring(me.text, i, 2), 0) * me.realFontSize_x;
258 draw_Fill(p, eX * w + eY * me.realFontSize_y, eX, 0.5);
259 draw_Text(p, substring(me.text, i, 2), me.realFontSize, '1 1 1', theAlpha, 0);
262 /*else if(ch2 == "a") // ^a found
264 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
265 draw_Text(p, substring(me.text, i, 2), me.realFontSize, theColor, 0.8, 0);
267 component = str2chr(me.text, i+2);
268 if (component >= '0' && component <= '9')
269 component = component - '0';
270 else if (component >= 'a' && component <= 'f')
271 component = component - 87;
272 else if (component >= 'A' && component <= 'F')
273 component = component - 55;
277 if (component >= 0) // ^ah found
279 // FIX ME: overflow here
280 if (component == 20 && theVariableAlpha <= 0.97)
281 theVariableAlpha = theVariableAlpha + 0.0625;
282 else if (component == 30 && theVariableAlpha >= 0.03)
283 theVariableAlpha = theVariableAlpha - 0.0625;
285 theVariableAlpha = component*0.0625;
287 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);
288 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, 0.8, 0);
293 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
294 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
300 draw_Text(p, ch, me.realFontSize, theColor, theAlpha, 0); // TODO theVariableAlpha
301 p += eX * draw_TextWidth(ch, 0, me.realFontSize);
305 draw_Text(me.realOrigin - eX * me.scrollPos, me.text, me.realFontSize, '1 1 1', 1, 0);
306 // skipping SUPER(InputBox).draw(me);
307 if(!me.focused || (time - me.lastChangeTime) < floor(time - me.lastChangeTime) + 0.5)
308 draw_Text(me.realOrigin + eX * (cursorPosInWidths - me.scrollPos), CURSOR, me.realFontSize, '1 1 1', 1, 0);
313 void showNotifyInputBox(entity me)
315 me.focusable = !me.disabled;