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, keyDown, float(entity, float, float, float))
7 METHOD(InputBox, mouseRelease, float(entity, vector))
8 METHOD(InputBox, mousePress, float(entity, vector))
9 METHOD(InputBox, mouseDrag, float(entity, vector))
11 ATTRIB(InputBox, src, string, string_null)
13 ATTRIB(InputBox, cursorPos, float, 0) // characters
14 ATTRIB(InputBox, scrollPos, float, 0) // widths
16 ATTRIB(InputBox, focusable, float, 1)
17 ATTRIB(InputBox, lastChangeTime, float, 0)
18 ATTRIB(InputBox, dragScrollTimer, float, 0)
19 ATTRIB(InputBox, dragScrollPos, vector, '0 0 0')
20 ATTRIB(InputBox, pressed, float, 0)
21 ATTRIB(InputBox, editColorCodes, float, 1)
22 ATTRIB(InputBox, color, vector, '1 1 1')
23 ATTRIB(InputBox, colorF, vector, '1 1 1')
25 void InputBox_Clear_Click(entity btn, entity me);
29 void configureInputBoxInputBox(entity me, string theText, float theCursorPos, float theFontSize, string gfx)
31 configureLabelLabel(me, theText, theFontSize, 0.0);
33 me.cursorPos = theCursorPos;
36 void setTextInputBox(entity me, string txt)
40 setTextLabel(me, strzone(txt));
43 void InputBox_Clear_Click(entity btn, entity me)
48 float mouseDragInputBox(entity me, vector pos)
51 me.dragScrollPos = pos;
52 p = me.scrollPos + pos_x - me.keepspaceLeft;
53 me.cursorPos = draw_TextLengthUpToWidth(me.text, p / me.realFontSize_x, 0);
54 me.lastChangeTime = time;
58 float mousePressInputBox(entity me, vector pos)
60 me.dragScrollTimer = 0;
62 return mouseDragInputBox(me, pos);
65 float mouseReleaseInputBox(entity me, vector pos)
68 return mouseDragInputBox(me, pos);
71 float keyDownInputBox(entity me, float key, float ascii, float shift)
73 me.lastChangeTime = time;
74 me.dragScrollTimer = 0;
75 if(ascii >= 32 && ascii != 127)
77 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), chr(ascii), substring(me.text, me.cursorPos, strlen(me.text) - me.cursorPos)));
93 me.cursorPos = strlen(me.text);
102 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
109 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
115 void drawInputBox(entity me)
118 float cursorPosInWidths, totalSizeInWidths;
121 me.mouseDrag(me, me.dragScrollPos); // simulate mouseDrag event
126 draw_ButtonPicture('0 0 0', strcat(me.src, "_f"), '1 1 0', me.colorF, 1);
128 draw_ButtonPicture('0 0 0', strcat(me.src, "_n"), '1 1 0', me.color, 1);
131 me.cursorPos = bound(0, me.cursorPos, strlen(me.text));
132 cursorPosInWidths = draw_TextWidth(substring(me.text, 0, me.cursorPos), 0) * me.realFontSize_x;
133 totalSizeInWidths = draw_TextWidth(strcat(me.text, CURSOR), 0) * me.realFontSize_x;
135 me.dragScrollTimer -= frametime;
136 if(me.dragScrollTimer < 0)
140 me.scrollPos = bound(cursorPosInWidths - (0.875 - me.keepspaceLeft - me.keepspaceRight), me.scrollPos, cursorPosInWidths - 0.125);
141 if(me.scrollPos != save)
142 me.dragScrollTimer = 0.2;
144 me.scrollPos = min(me.scrollPos, totalSizeInWidths - (1 - me.keepspaceRight - me.keepspaceLeft));
145 me.scrollPos = max(0, me.scrollPos);
147 draw_SetClipRect(eX * me.keepspaceLeft, eX * (1 - me.keepspaceLeft - me.keepspaceRight) + eY);
148 if(me.editColorCodes)
156 brightness = cvar("r_textbrightness");
157 p = me.realOrigin - eX * me.scrollPos;
160 for(i = 0; i < strlen(me.text); ++i)
162 ch = substring(me.text, i, 1);
166 ch2 = substring(me.text, i+1, 1);
167 w = draw_TextWidth(strcat(ch, ch2), 0) * me.realFontSize_x;
170 draw_Fill(p, eX * w + eY * me.realFontSize_y, '0 0 1', 0.5);
171 draw_Text(p + eX * 0.25 * w, "^", me.realFontSize, theColor, theAlpha, 0);
173 else if(ch2 == "0" || stof(ch2)) // digit?
177 case 0: theColor = '0 0 0'; theAlpha = 1; break;
178 case 1: theColor = '1 0 0'; theAlpha = 1; break;
179 case 2: theColor = '0 1 0'; theAlpha = 1; break;
180 case 3: theColor = '1 1 0'; theAlpha = 1; break;
181 case 4: theColor = '0 0 1'; theAlpha = 1; break;
182 case 5: theColor = '0 1 1'; theAlpha = 1; break;
183 case 6: theColor = '1 0 1'; theAlpha = 1; break;
184 case 7: theColor = '1 1 1'; theAlpha = 1; break;
185 case 8: theColor = '1 1 1'; theAlpha = 0.5; break;
186 case 9: theColor = '0.5 0.5 0.5'; theAlpha = 1; break;
188 theColor = theColor * (1 - brightness) + brightness * '1 1 1';
189 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
190 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
191 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
195 draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 0 0', 0.5);
196 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
197 draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
203 draw_Text(p, ch, me.realFontSize, theColor, theAlpha, 0); p += eX * draw_TextWidth(ch, 0) * me.realFontSize_x;
207 draw_Text(me.realOrigin - eX * me.scrollPos, me.text, me.realFontSize, '1 1 1', 1, 0);
208 // skipping drawLabel(me);
211 if(!me.focused || (time - me.lastChangeTime) < floor(time - me.lastChangeTime) + 0.5)
212 draw_Text(me.realOrigin + eX * (cursorPosInWidths - me.scrollPos), CURSOR, me.realFontSize, '1 1 1', 1, 0);