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')
29 void InputBox_Clear_Click(entity btn, entity me);
33 void configureInputBoxInputBox(entity me, string theText, float theCursorPos, float theFontSize, string gfx)
35 configureLabelLabel(me, theText, theFontSize, 0.0);
37 me.cursorPos = theCursorPos;
40 void setTextInputBox(entity me, string txt)
44 setTextLabel(me, strzone(txt));
47 void InputBox_Clear_Click(entity btn, entity me)
52 float mouseDragInputBox(entity me, vector pos)
55 me.dragScrollPos = pos;
56 p = me.scrollPos + pos_x - me.keepspaceLeft;
57 me.cursorPos = draw_TextLengthUpToWidth(me.text, p / me.realFontSize_x, 0);
58 me.lastChangeTime = time;
62 float mousePressInputBox(entity me, vector pos)
64 me.dragScrollTimer = time;
66 return mouseDragInputBox(me, pos);
69 float mouseReleaseInputBox(entity me, vector pos)
72 return mouseDragInputBox(me, pos);
75 void enterTextInputBox(entity me, string ch)
78 for(i = 0; i < strlen(ch); ++i)
79 if(strstrofs(me.forbiddenCharacters, substring(ch, i, 1), 0) > -1)
81 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), ch, substring(me.text, me.cursorPos, strlen(me.text) - me.cursorPos)));
82 me.cursorPos += strlen(ch);
85 float keyDownInputBox(entity me, float key, float ascii, float shift)
87 me.lastChangeTime = time;
88 me.dragScrollTimer = time;
89 if(ascii >= 32 && ascii != 127)
91 me.enterText(me, chr(ascii));
106 me.cursorPos = strlen(me.text);
112 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
119 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
125 void drawInputBox(entity me)
128 float cursorPosInWidths, totalSizeInWidths;
131 me.mouseDrag(me, me.dragScrollPos); // simulate mouseDrag event
133 me.focusable = !me.disabled;
135 draw_alpha *= me.disabledAlpha;
139 if(me.focused && !me.disabled)
140 draw_ButtonPicture('0 0 0', strcat(me.src, "_f"), '1 1 0', me.colorF, 1);
142 draw_ButtonPicture('0 0 0', strcat(me.src, "_n"), '1 1 0', me.color, 1);
145 me.cursorPos = bound(0, me.cursorPos, strlen(me.text));
146 cursorPosInWidths = draw_TextWidth(substring(me.text, 0, me.cursorPos), 0) * me.realFontSize_x;
147 totalSizeInWidths = draw_TextWidth(strcat(me.text, CURSOR), 0) * me.realFontSize_x;
149 if(me.dragScrollTimer < time)
153 me.scrollPos = bound(cursorPosInWidths - (0.875 - me.keepspaceLeft - me.keepspaceRight), me.scrollPos, cursorPosInWidths - 0.125);
154 if(me.scrollPos != save)
155 me.dragScrollTimer = time + 0.2;
157 me.scrollPos = min(me.scrollPos, totalSizeInWidths - (1 - me.keepspaceRight - me.keepspaceLeft));
158 me.scrollPos = max(0, me.scrollPos);
160 draw_SetClipRect(eX * me.keepspaceLeft, eX * (1 - me.keepspaceLeft - me.keepspaceRight) + eY);
161 if(me.editColorCodes)
169 brightness = cvar("r_textbrightness");
170 p = me.realOrigin - eX * me.scrollPos;
173 for(i = 0; i < strlen(me.text); ++i)
175 ch = substring(me.text, i, 1);
179 ch2 = substring(me.text, i+1, 1);
180 w = draw_TextWidth(strcat(ch, ch2), 0) * me.realFontSize_x;
183 draw_Fill(p, eX * w + eY * me.realFontSize_y, '0 0 1', 0.5);
184 draw_Text(p + eX * 0.25 * w, "^", me.realFontSize, theColor, theAlpha, 0);
186 else if(ch2 == "0" || stof(ch2)) // digit?
190 case 0: theColor = '0 0 0'; theAlpha = 1; break;
191 case 1: theColor = '1 0 0'; theAlpha = 1; break;
192 case 2: theColor = '0 1 0'; theAlpha = 1; break;
193 case 3: theColor = '1 1 0'; theAlpha = 1; break;
194 case 4: theColor = '0 0 1'; theAlpha = 1; break;
195 case 5: theColor = '0 1 1'; theAlpha = 1; break;
196 case 6: theColor = '1 0 1'; theAlpha = 1; break;
197 case 7: theColor = '1 1 1'; theAlpha = 1; break;
198 case 8: theColor = '1 1 1'; theAlpha = 0.5; break;
199 case 9: theColor = '0.5 0.5 0.5'; theAlpha = 1; break;
201 theColor = theColor * (1 - brightness) + brightness * '1 1 1';
202 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
203 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
204 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
208 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 0 0', 0.5);
209 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
210 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
216 draw_Text(p, ch, me.realFontSize, theColor, theAlpha, 0); p += eX * draw_TextWidth(ch, 0) * me.realFontSize_x;
220 draw_Text(me.realOrigin - eX * me.scrollPos, me.text, me.realFontSize, '1 1 1', 1, 0);
221 // skipping drawLabel(me);
222 if(!me.focused || (time - me.lastChangeTime) < floor(time - me.lastChangeTime) + 0.5)
223 draw_Text(me.realOrigin + eX * (cursorPosInWidths - me.scrollPos), CURSOR, me.realFontSize, '1 1 1', 1, 0);
228 void showNotifyInputBox(entity me)
230 me.focusable = !me.disabled;