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
149 me.focusable = !me.disabled;
151 draw_alpha *= me.disabledAlpha;
155 if(me.focused && !me.disabled)
156 draw_ButtonPicture('0 0 0', strcat(me.src, "_f"), '1 1 0', me.colorF, 1);
158 draw_ButtonPicture('0 0 0', strcat(me.src, "_n"), '1 1 0', me.color, 1);
161 me.cursorPos = bound(0, me.cursorPos, strlen(me.text));
162 cursorPosInWidths = draw_TextWidth(substring(me.text, 0, me.cursorPos), 0, me.realFontSize);
163 totalSizeInWidths = draw_TextWidth(strcat(me.text, CURSOR), 0, me.realFontSize);
165 if(me.dragScrollTimer < time)
169 me.scrollPos = bound(cursorPosInWidths - (0.875 - me.keepspaceLeft - me.keepspaceRight), me.scrollPos, cursorPosInWidths - 0.125);
170 if(me.scrollPos != save)
171 me.dragScrollTimer = time + 0.2;
173 me.scrollPos = min(me.scrollPos, totalSizeInWidths - (1 - me.keepspaceRight - me.keepspaceLeft));
174 me.scrollPos = max(0, me.scrollPos);
176 draw_SetClipRect(eX * me.keepspaceLeft, eX * (1 - me.keepspaceLeft - me.keepspaceRight) + eY);
177 if(me.editColorCodes)
182 float theAlpha; //float theVariableAlpha;
187 p = me.realOrigin - eX * me.scrollPos;
189 theAlpha = 1; //theVariableAlpha = 1; // changes when ^ax found
192 for(i = 0; i < n; ++i)
194 ch = substring(me.text, i, 1);
198 ch2 = substring(me.text, i+1, 1);
199 w = draw_TextWidth(strcat(ch, ch2), 0, me.realFontSize);
202 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
203 draw_Text(p + eX * 0.25 * w, "^", me.realFontSize, theColor, theAlpha, 0);
205 else if(ch2 == "0" || stof(ch2)) // digit?
209 case 0: theColor = '0 0 0'; theAlpha = 1; break;
210 case 1: theColor = '1 0 0'; theAlpha = 1; break;
211 case 2: theColor = '0 1 0'; theAlpha = 1; break;
212 case 3: theColor = '1 1 0'; theAlpha = 1; break;
213 case 4: theColor = '0 0 1'; theAlpha = 1; break;
214 case 5: theColor = '0 1 1'; theAlpha = 1; break;
215 case 6: theColor = '1 0 1'; theAlpha = 1; break;
216 case 7: theColor = '1 1 1'; theAlpha = 1; break;
217 case 8: theColor = '1 1 1'; theAlpha = 0.5; break;
218 case 9: theColor = '0.5 0.5 0.5'; theAlpha = 1; break;
220 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
221 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
223 else if(ch2 == "x") // ^x found
226 theTempColor = '0 0 0';
228 component = HEXDIGIT_TO_DEC(substring(me.text, i+2, 1));
229 if (component >= 0) // ^xr found
231 theTempColor_x = component/15;
233 component = HEXDIGIT_TO_DEC(substring(me.text, i+3, 1));
234 if (component >= 0) // ^xrg found
236 theTempColor_y = component/15;
238 component = HEXDIGIT_TO_DEC(substring(me.text, i+4, 1));
239 if (component >= 0) // ^xrgb found
241 theTempColor_z = component/15;
242 theColor = theTempColor;
243 w = draw_TextWidth(substring(me.text, i, 5), 0, me.realFontSize);
245 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
246 draw_Text(p, substring(me.text, i, 5), me.realFontSize, theColor, 1, 0); // theVariableAlpha instead of 1 using alpha tags ^ax
252 w = draw_TextWidth(substring(me.text, i, 4), 0, me.realFontSize);
253 draw_Fill(p, eX * w + eY * me.realFontSize_y, eZ, 0.5);
254 draw_Text(p, substring(me.text, i, 4), me.realFontSize, '1 1 1', theAlpha, 0);
261 w = draw_TextWidth(substring(me.text, i, 3), 0, me.realFontSize);
262 draw_Fill(p, eX * w + eY * me.realFontSize_y, eY, 0.5);
263 draw_Text(p, substring(me.text, i, 3), me.realFontSize, '1 1 1', theAlpha, 0);
270 //w = draw_TextWidth(substring(me.text, i, 2), 0) * me.realFontSize_x;
271 draw_Fill(p, eX * w + eY * me.realFontSize_y, eX, 0.5);
272 draw_Text(p, substring(me.text, i, 2), me.realFontSize, '1 1 1', theAlpha, 0);
277 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
278 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
284 draw_Text(p, ch, me.realFontSize, theColor, theAlpha, 0); // TODO theVariableAlpha
285 p += eX * draw_TextWidth(ch, 0, me.realFontSize);
289 draw_Text(me.realOrigin - eX * me.scrollPos, me.text, me.realFontSize, '1 1 1', 1, 0);
290 // skipping SUPER(InputBox).draw(me);
291 if(!me.focused || (time - me.lastChangeTime) < floor(time - me.lastChangeTime) + 0.5)
292 draw_Text(me.realOrigin + eX * (cursorPosInWidths - me.scrollPos), CURSOR, me.realFontSize, '1 1 1', 1, 0);
297 void InputBox_showNotify(entity me)
299 me.focusable = !me.disabled;