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))
12 ATTRIB(InputBox, src, string, string_null)
14 ATTRIB(InputBox, cursorPos, float, 0) // characters
15 ATTRIB(InputBox, scrollPos, float, 0) // widths
17 ATTRIB(InputBox, focusable, float, 1)
18 ATTRIB(InputBox, lastChangeTime, float, 0)
19 ATTRIB(InputBox, dragScrollTimer, float, 0)
20 ATTRIB(InputBox, dragScrollPos, vector, '0 0 0')
21 ATTRIB(InputBox, pressed, float, 0)
22 ATTRIB(InputBox, editColorCodes, float, 1)
23 ATTRIB(InputBox, forbiddenCharacters, string, "")
24 ATTRIB(InputBox, color, vector, '1 1 1')
25 ATTRIB(InputBox, colorF, vector, '1 1 1')
27 void InputBox_Clear_Click(entity btn, entity me);
31 void configureInputBoxInputBox(entity me, string theText, float theCursorPos, float theFontSize, string gfx)
33 configureLabelLabel(me, theText, theFontSize, 0.0);
35 me.cursorPos = theCursorPos;
38 void setTextInputBox(entity me, string txt)
42 setTextLabel(me, strzone(txt));
45 void InputBox_Clear_Click(entity btn, entity me)
50 float mouseDragInputBox(entity me, vector pos)
53 me.dragScrollPos = pos;
54 p = me.scrollPos + pos_x - me.keepspaceLeft;
55 me.cursorPos = draw_TextLengthUpToWidth(me.text, p / me.realFontSize_x, 0);
56 me.lastChangeTime = time;
60 float mousePressInputBox(entity me, vector pos)
62 me.dragScrollTimer = 0;
64 return mouseDragInputBox(me, pos);
67 float mouseReleaseInputBox(entity me, vector pos)
70 return mouseDragInputBox(me, pos);
73 void enterTextInputBox(entity me, string ch)
76 for(i = 0; i < strlen(ch); ++i)
77 if(strstrofs(me.forbiddenCharacters, substring(ch, i, 1), 0) > -1)
79 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), ch, substring(me.text, me.cursorPos, strlen(me.text) - me.cursorPos)));
80 me.cursorPos += strlen(ch);
83 float keyDownInputBox(entity me, float key, float ascii, float shift)
85 me.lastChangeTime = time;
86 me.dragScrollTimer = 0;
87 if(ascii >= 32 && ascii != 127)
89 me.enterText(me, chr(ascii));
104 me.cursorPos = strlen(me.text);
113 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
120 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
126 void drawInputBox(entity me)
129 float cursorPosInWidths, totalSizeInWidths;
132 me.mouseDrag(me, me.dragScrollPos); // simulate mouseDrag event
137 draw_ButtonPicture('0 0 0', strcat(me.src, "_f"), '1 1 0', me.colorF, 1);
139 draw_ButtonPicture('0 0 0', strcat(me.src, "_n"), '1 1 0', me.color, 1);
142 me.cursorPos = bound(0, me.cursorPos, strlen(me.text));
143 cursorPosInWidths = draw_TextWidth(substring(me.text, 0, me.cursorPos), 0) * me.realFontSize_x;
144 totalSizeInWidths = draw_TextWidth(strcat(me.text, CURSOR), 0) * me.realFontSize_x;
146 me.dragScrollTimer -= frametime;
147 if(me.dragScrollTimer < 0)
151 me.scrollPos = bound(cursorPosInWidths - (0.875 - me.keepspaceLeft - me.keepspaceRight), me.scrollPos, cursorPosInWidths - 0.125);
152 if(me.scrollPos != save)
153 me.dragScrollTimer = 0.2;
155 me.scrollPos = min(me.scrollPos, totalSizeInWidths - (1 - me.keepspaceRight - me.keepspaceLeft));
156 me.scrollPos = max(0, me.scrollPos);
158 draw_SetClipRect(eX * me.keepspaceLeft, eX * (1 - me.keepspaceLeft - me.keepspaceRight) + eY);
159 if(me.editColorCodes)
167 brightness = cvar("r_textbrightness");
168 p = me.realOrigin - eX * me.scrollPos;
171 for(i = 0; i < strlen(me.text); ++i)
173 ch = substring(me.text, i, 1);
177 ch2 = substring(me.text, i+1, 1);
178 w = draw_TextWidth(strcat(ch, ch2), 0) * me.realFontSize_x;
181 draw_Fill(p, eX * w + eY * me.realFontSize_y, '0 0 1', 0.5);
182 draw_Text(p + eX * 0.25 * w, "^", me.realFontSize, theColor, theAlpha, 0);
184 else if(ch2 == "0" || stof(ch2)) // digit?
188 case 0: theColor = '0 0 0'; theAlpha = 1; break;
189 case 1: theColor = '1 0 0'; theAlpha = 1; break;
190 case 2: theColor = '0 1 0'; theAlpha = 1; break;
191 case 3: theColor = '1 1 0'; theAlpha = 1; break;
192 case 4: theColor = '0 0 1'; theAlpha = 1; break;
193 case 5: theColor = '0 1 1'; theAlpha = 1; break;
194 case 6: theColor = '1 0 1'; theAlpha = 1; break;
195 case 7: theColor = '1 1 1'; theAlpha = 1; break;
196 case 8: theColor = '1 1 1'; theAlpha = 0.5; break;
197 case 9: theColor = '0.5 0.5 0.5'; theAlpha = 1; break;
199 theColor = theColor * (1 - brightness) + brightness * '1 1 1';
200 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
201 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
202 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
206 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 0 0', 0.5);
207 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
208 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
214 draw_Text(p, ch, me.realFontSize, theColor, theAlpha, 0); p += eX * draw_TextWidth(ch, 0) * me.realFontSize_x;
218 draw_Text(me.realOrigin - eX * me.scrollPos, me.text, me.realFontSize, '1 1 1', 1, 0);
219 // skipping drawLabel(me);
220 if(!me.focused || (time - me.lastChangeTime) < floor(time - me.lastChangeTime) + 0.5)
221 draw_Text(me.realOrigin + eX * (cursorPosInWidths - me.scrollPos), CURSOR, me.realFontSize, '1 1 1', 1, 0);