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, mouseMove, float(entity, vector))
9 METHOD(InputBox, mouseRelease, float(entity, vector))
10 METHOD(InputBox, mousePress, float(entity, vector))
11 METHOD(InputBox, mouseDrag, float(entity, vector))
12 METHOD(InputBox, showNotify, void(entity))
13 METHOD(InputBox, resizeNotify, void(entity, vector, vector, vector, vector))
15 ATTRIB(InputBox, src, string, string_null)
17 ATTRIB(InputBox, cursorPos, float, 0) // characters
18 ATTRIB(InputBox, scrollPos, float, 0) // widths
20 ATTRIB(InputBox, focusable, float, 1)
21 ATTRIB(InputBox, allowFocusSound, float, 1)
22 ATTRIB(InputBox, disabled, float, 0)
23 ATTRIB(InputBox, lastChangeTime, float, 0)
24 ATTRIB(InputBox, dragScrollTimer, float, 0)
25 ATTRIB(InputBox, dragScrollPos, vector, '0 0 0')
26 ATTRIB(InputBox, pressed, float, 0)
27 ATTRIB(InputBox, editColorCodes, float, 1)
28 ATTRIB(InputBox, forbiddenCharacters, string, "")
29 ATTRIB(InputBox, color, vector, '1 1 1')
30 ATTRIB(InputBox, colorF, vector, '1 1 1')
31 ATTRIB(InputBox, maxLength, float, 255) // if negative, it counts bytes, not chars
33 ATTRIB(InputBox, enableClearButton, float, 1)
34 ATTRIB(InputBox, clearButton, entity, NULL)
35 ATTRIB(InputBox, cb_width, float, 0)
36 ATTRIB(InputBox, cb_pressed, float, 0)
37 ATTRIB(InputBox, cb_focused, float, 0)
38 ATTRIB(InputBox, cb_color, vector, '1 1 1')
39 ATTRIB(InputBox, cb_colorF, vector, '1 1 1')
40 ATTRIB(InputBox, cb_colorC, vector, '1 1 1')
42 void InputBox_Clear_Click(entity btn, entity me);
46 void InputBox_configureInputBox(entity me, string theText, float theCursorPos, float theFontSize, string gfx)
48 SUPER(InputBox).configureLabel(me, theText, theFontSize, 0.0);
50 me.cursorPos = theCursorPos;
52 void InputBox_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
54 SUPER(InputBox).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
55 if (me.enableClearButton)
57 me.cb_width = absSize_y / absSize_x;
58 me.cb_offset = bound(-1, me.cb_offset, 0) * me.cb_width; // bound to range -1, 0
59 me.keepspaceRight = me.keepspaceRight - me.cb_offset + me.cb_width;
63 void InputBox_setText(entity me, string txt)
67 SUPER(InputBox).setText(me, strzone(txt));
70 void InputBox_Clear_Click(entity btn, entity me)
75 float over_ClearButton(entity me, vector pos)
77 if (pos_x >= 1 + me.cb_offset - me.cb_width)
78 if (pos_x < 1 + me.cb_offset)
85 float InputBox_mouseMove(entity me, vector pos)
87 if (me.enableClearButton)
89 if (over_ClearButton(me, pos))
99 float InputBox_mouseDrag(entity me, vector pos)
104 me.dragScrollPos = pos;
105 p = me.scrollPos + pos_x - me.keepspaceLeft;
106 me.cursorPos = draw_TextLengthUpToWidth(me.text, p, 0, me.realFontSize);
107 me.lastChangeTime = time;
109 else if (me.enableClearButton)
111 if (over_ClearButton(me, pos))
121 float InputBox_mousePress(entity me, vector pos)
123 if (me.enableClearButton)
124 if (over_ClearButton(me, pos))
129 me.dragScrollTimer = time;
131 return InputBox_mouseDrag(me, pos);
134 float InputBox_mouseRelease(entity me, vector pos)
137 if (over_ClearButton(me, pos))
140 InputBox_Clear_Click(world, me);
143 float r = InputBox_mouseDrag(me, pos);
144 //reset cb_pressed after mouseDrag, mouseDrag could set cb_pressed in this case:
145 //mouse press out of the clear button, drag and then mouse release over the clear button
151 void InputBox_enterText(entity me, string ch)
154 for(i = 0; i < strlen(ch); ++i)
155 if(strstrofs(me.forbiddenCharacters, substring(ch, i, 1), 0) > -1)
159 if(strlen(ch) + strlen(me.text) > me.maxLength)
162 else if(me.maxLength < 0)
164 if(u8_strsize(ch) + u8_strsize(me.text) > -me.maxLength)
167 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), ch, substring(me.text, me.cursorPos, strlen(me.text) - me.cursorPos)));
168 me.cursorPos += strlen(ch);
171 float InputBox_keyDown(entity me, float key, float ascii, float shift)
173 me.lastChangeTime = time;
174 me.dragScrollTimer = time;
175 if(ascii >= 32 && ascii != 127)
177 me.enterText(me, chr(ascii));
186 case K_KP_RIGHTARROW:
196 me.cursorPos = strlen(me.text);
202 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
210 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
216 void InputBox_draw(entity me)
219 float cursorPosInWidths, totalSizeInWidths;
222 me.mouseDrag(me, me.dragScrollPos); // simulate mouseDrag event
225 me.recalcPositionWithText(me, me.text);
227 me.focusable = !me.disabled;
229 draw_alpha *= me.disabledAlpha;
233 if(me.focused && !me.disabled)
234 draw_ButtonPicture('0 0 0', strcat(me.src, "_f"), '1 1 0', me.colorF, 1);
236 draw_ButtonPicture('0 0 0', strcat(me.src, "_n"), '1 1 0', me.color, 1);
239 me.cursorPos = bound(0, me.cursorPos, strlen(me.text));
240 cursorPosInWidths = draw_TextWidth(substring(me.text, 0, me.cursorPos), 0, me.realFontSize);
241 totalSizeInWidths = draw_TextWidth(strcat(me.text, CURSOR), 0, me.realFontSize);
243 if(me.dragScrollTimer < time)
247 me.scrollPos = bound(cursorPosInWidths - (0.875 - me.keepspaceLeft - me.keepspaceRight), me.scrollPos, cursorPosInWidths - 0.125);
248 if(me.scrollPos != save)
249 me.dragScrollTimer = time + 0.2;
251 me.scrollPos = min(me.scrollPos, totalSizeInWidths - (1 - me.keepspaceRight - me.keepspaceLeft));
252 me.scrollPos = max(0, me.scrollPos);
254 draw_SetClipRect(eX * me.keepspaceLeft, eX * (1 - me.keepspaceLeft - me.keepspaceRight) + eY);
255 if(me.editColorCodes)
260 float theAlpha; //float theVariableAlpha;
265 p = me.realOrigin - eX * me.scrollPos;
267 theAlpha = 1; //theVariableAlpha = 1; // changes when ^ax found
270 for(i = 0; i < n; ++i)
272 ch = substring(me.text, i, 1);
276 ch2 = substring(me.text, i+1, 1);
277 w = draw_TextWidth(strcat(ch, ch2), 0, me.realFontSize);
280 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
281 draw_Text(p + eX * 0.25 * w, "^", me.realFontSize, theColor, theAlpha, 0);
283 else if(ch2 == "0" || stof(ch2)) // digit?
287 case 0: theColor = '0 0 0'; theAlpha = 1; break;
288 case 1: theColor = '1 0 0'; theAlpha = 1; break;
289 case 2: theColor = '0 1 0'; theAlpha = 1; break;
290 case 3: theColor = '1 1 0'; theAlpha = 1; break;
291 case 4: theColor = '0 0 1'; theAlpha = 1; break;
292 case 5: theColor = '0 1 1'; theAlpha = 1; break;
293 case 6: theColor = '1 0 1'; theAlpha = 1; break;
294 case 7: theColor = '1 1 1'; theAlpha = 1; break;
295 case 8: theColor = '1 1 1'; theAlpha = 0.5; break;
296 case 9: theColor = '0.5 0.5 0.5'; theAlpha = 1; break;
298 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
299 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
301 else if(ch2 == "x") // ^x found
305 component = HEXDIGIT_TO_DEC(substring(me.text, i+2, 1));
306 if (component >= 0) // ^xr found
308 theTempColor_x = component/15;
310 component = HEXDIGIT_TO_DEC(substring(me.text, i+3, 1));
311 if (component >= 0) // ^xrg found
313 theTempColor_y = component/15;
315 component = HEXDIGIT_TO_DEC(substring(me.text, i+4, 1));
316 if (component >= 0) // ^xrgb found
318 theTempColor_z = component/15;
319 theColor = theTempColor;
320 w = draw_TextWidth(substring(me.text, i, 5), 0, me.realFontSize);
322 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
323 draw_Text(p, substring(me.text, i, 5), me.realFontSize, theColor, 1, 0); // theVariableAlpha instead of 1 using alpha tags ^ax
329 w = draw_TextWidth(substring(me.text, i, 4), 0, me.realFontSize);
330 draw_Fill(p, eX * w + eY * me.realFontSize_y, eZ, 0.5);
331 draw_Text(p, substring(me.text, i, 4), me.realFontSize, '1 1 1', theAlpha, 0);
338 w = draw_TextWidth(substring(me.text, i, 3), 0, me.realFontSize);
339 draw_Fill(p, eX * w + eY * me.realFontSize_y, eY, 0.5);
340 draw_Text(p, substring(me.text, i, 3), me.realFontSize, '1 1 1', theAlpha, 0);
347 //w = draw_TextWidth(substring(me.text, i, 2), 0) * me.realFontSize_x;
348 draw_Fill(p, eX * w + eY * me.realFontSize_y, eX, 0.5);
349 draw_Text(p, substring(me.text, i, 2), me.realFontSize, '1 1 1', theAlpha, 0);
354 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
355 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
361 draw_Text(p, ch, me.realFontSize, theColor, theAlpha, 0); // TODO theVariableAlpha
362 p += eX * draw_TextWidth(ch, 0, me.realFontSize);
366 draw_Text(me.realOrigin - eX * me.scrollPos, me.text, me.realFontSize, '1 1 1', 1, 0);
368 if(!me.focused || (time - me.lastChangeTime) < floor(time - me.lastChangeTime) + 0.5)
369 draw_Text(me.realOrigin + eX * (cursorPosInWidths - me.scrollPos), CURSOR, me.realFontSize, '1 1 1', 1, 0);
373 if (me.enableClearButton)
376 if(me.focused && me.cb_pressed)
377 draw_Picture(eX * (1 + me.cb_offset - me.cb_width), strcat(me.cb_src, "_c"), eX * me.cb_width + eY, me.cb_colorC, 1);
378 else if(me.focused && me.cb_focused)
379 draw_Picture(eX * (1 + me.cb_offset - me.cb_width), strcat(me.cb_src, "_f"), eX * me.cb_width + eY, me.cb_colorF, 1);
381 draw_Picture(eX * (1 + me.cb_offset - me.cb_width), strcat(me.cb_src, "_n"), eX * me.cb_width + eY, me.cb_color, 1);
384 // skipping SUPER(InputBox).draw(me);
388 void InputBox_showNotify(entity me)
390 me.focusable = !me.disabled;