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) // if negative, it counts bytes, not chars
30 void InputBox_Clear_Click(entity btn, entity me);
34 void InputBox_configureInputBox(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 InputBox_setText(entity me, string txt)
45 SUPER(InputBox).setText(me, strzone(txt));
48 void InputBox_Clear_Click(entity btn, entity me)
53 float InputBox_mouseDrag(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 InputBox_mousePress(entity me, vector pos)
65 me.dragScrollTimer = time;
67 return InputBox_mouseDrag(me, pos);
70 float InputBox_mouseRelease(entity me, vector pos)
73 return InputBox_mouseDrag(me, pos);
76 void InputBox_enterText(entity me, string ch)
79 for(i = 0; i < strlen(ch); ++i)
80 if(strstrofs(me.forbiddenCharacters, substring(ch, i, 1), 0) > -1)
84 if(strlen(ch) + strlen(me.text) > me.maxLength)
87 else if(me.maxLength < 0)
89 if(u8_strsize(ch) + u8_strsize(me.text) > -me.maxLength)
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);
96 float InputBox_keyDown(entity me, float key, float ascii, float shift)
98 me.lastChangeTime = time;
99 me.dragScrollTimer = time;
100 if(ascii >= 32 && ascii != 127)
102 me.enterText(me, chr(ascii));
111 case K_KP_RIGHTARROW:
121 me.cursorPos = strlen(me.text);
127 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
135 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
141 void InputBox_draw(entity me)
144 float cursorPosInWidths, totalSizeInWidths;
147 me.mouseDrag(me, me.dragScrollPos); // simulate mouseDrag event
150 me.recalcPositionWithText(me, me.text);
152 me.focusable = !me.disabled;
154 draw_alpha *= me.disabledAlpha;
158 if(me.focused && !me.disabled)
159 draw_ButtonPicture('0 0 0', strcat(me.src, "_f"), '1 1 0', me.colorF, 1);
161 draw_ButtonPicture('0 0 0', strcat(me.src, "_n"), '1 1 0', me.color, 1);
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);
168 if(me.dragScrollTimer < time)
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;
176 me.scrollPos = min(me.scrollPos, totalSizeInWidths - (1 - me.keepspaceRight - me.keepspaceLeft));
177 me.scrollPos = max(0, me.scrollPos);
179 draw_SetClipRect(eX * me.keepspaceLeft, eX * (1 - me.keepspaceLeft - me.keepspaceRight) + eY);
180 if(me.editColorCodes)
185 float theAlpha; //float theVariableAlpha;
190 p = me.realOrigin - eX * me.scrollPos;
192 theAlpha = 1; //theVariableAlpha = 1; // changes when ^ax found
195 for(i = 0; i < n; ++i)
197 ch = substring(me.text, i, 1);
201 ch2 = substring(me.text, i+1, 1);
202 w = draw_TextWidth(strcat(ch, ch2), 0, me.realFontSize);
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);
208 else if(ch2 == "0" || stof(ch2)) // digit?
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;
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);
226 else if(ch2 == "x") // ^x found
230 component = HEXDIGIT_TO_DEC(substring(me.text, i+2, 1));
231 if (component >= 0) // ^xr found
233 theTempColor_x = component/15;
235 component = HEXDIGIT_TO_DEC(substring(me.text, i+3, 1));
236 if (component >= 0) // ^xrg found
238 theTempColor_y = component/15;
240 component = HEXDIGIT_TO_DEC(substring(me.text, i+4, 1));
241 if (component >= 0) // ^xrgb found
243 theTempColor_z = component/15;
244 theColor = theTempColor;
245 w = draw_TextWidth(substring(me.text, i, 5), 0, me.realFontSize);
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
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);
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);
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);
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);
286 draw_Text(p, ch, me.realFontSize, theColor, theAlpha, 0); // TODO theVariableAlpha
287 p += eX * draw_TextWidth(ch, 0, me.realFontSize);
291 draw_Text(me.realOrigin - eX * me.scrollPos, me.text, me.realFontSize, '1 1 1', 1, 0);
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);
298 // skipping SUPER(InputBox).draw(me);
302 void InputBox_showNotify(entity me)
304 me.focusable = !me.disabled;