]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu/item/inputbox.c
change dragScrollTimer to be an absolute time instead of a counter that gets decrease...
[divverent/nexuiz.git] / data / qcsrc / menu / item / inputbox.c
1 #ifdef INTERFACE
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))
12
13         ATTRIB(InputBox, src, string, string_null)
14
15         ATTRIB(InputBox, cursorPos, float, 0) // characters
16         ATTRIB(InputBox, scrollPos, float, 0) // widths
17
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 ENDCLASS(InputBox)
29 void InputBox_Clear_Click(entity btn, entity me);
30 #endif
31
32 #ifdef IMPLEMENTATION
33 void configureInputBoxInputBox(entity me, string theText, float theCursorPos, float theFontSize, string gfx)
34 {
35         configureLabelLabel(me, theText, theFontSize, 0.0);
36         me.src = gfx;
37         me.cursorPos = theCursorPos;
38 }
39
40 void setTextInputBox(entity me, string txt)
41 {
42         if(me.text)
43                 strunzone(me.text);
44         setTextLabel(me, strzone(txt));
45 }
46
47 void InputBox_Clear_Click(entity btn, entity me)
48 {
49         me.setText(me, "");
50 }
51
52 float mouseDragInputBox(entity me, vector pos)
53 {
54         float p;
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;
59         return 1;
60 }
61
62 float mousePressInputBox(entity me, vector pos)
63 {
64         me.dragScrollTimer = time;
65         me.pressed = 1;
66         return mouseDragInputBox(me, pos);
67 }
68
69 float mouseReleaseInputBox(entity me, vector pos)
70 {
71         me.pressed = 0;
72         return mouseDragInputBox(me, pos);
73 }
74
75 void enterTextInputBox(entity me, string ch)
76 {
77         float i;
78         for(i = 0; i < strlen(ch); ++i)
79                 if(strstrofs(me.forbiddenCharacters, substring(ch, i, 1), 0) > -1)
80                         return;
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);
83 }
84
85 float keyDownInputBox(entity me, float key, float ascii, float shift)
86 {
87         me.lastChangeTime = time;
88         me.dragScrollTimer = time;
89         if(ascii >= 32 && ascii != 127)
90         {
91                 me.enterText(me, chr(ascii));
92                 return 1;
93         }
94         switch(key)
95         {
96                 case K_LEFTARROW:
97                         me.cursorPos -= 1;
98                         return 1;
99                 case K_RIGHTARROW:
100                         me.cursorPos += 1;
101                         return 1;
102                 case K_HOME:
103                         me.cursorPos = 0;
104                         return 1;
105                 case K_END:
106                         me.cursorPos = strlen(me.text);
107                         return 1;
108                 case K_BACKSPACE:
109                         if(shift)
110                                 me.setText(me, "");
111                         else
112                                 if(me.cursorPos > 0)
113                                 {
114                                         me.cursorPos -= 1;
115                                         me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
116                                 }
117                         return 1;
118                 case K_DEL:
119                         if(shift)
120                                 me.setText(me, "");
121                         else
122                                 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
123                         return 1;
124         }
125         return 0;
126 }
127
128 void drawInputBox(entity me)
129 {
130 #define CURSOR "_"
131         float cursorPosInWidths, totalSizeInWidths;
132
133         if(me.pressed)
134                 me.mouseDrag(me, me.dragScrollPos); // simulate mouseDrag event
135
136         me.focusable = !me.disabled;
137         if(me.disabled)
138                 draw_alpha *= me.disabledAlpha;
139
140         if(me.src)
141         {
142                 if(me.focused && !me.disabled)
143                         draw_ButtonPicture('0 0 0', strcat(me.src, "_f"), '1 1 0', me.colorF, 1);
144                 else
145                         draw_ButtonPicture('0 0 0', strcat(me.src, "_n"), '1 1 0', me.color, 1);
146         }
147
148         me.cursorPos = bound(0, me.cursorPos, strlen(me.text));
149         cursorPosInWidths = draw_TextWidth(substring(me.text, 0, me.cursorPos), 0) * me.realFontSize_x;
150         totalSizeInWidths = draw_TextWidth(strcat(me.text, CURSOR), 0) * me.realFontSize_x;
151
152         if(me.dragScrollTimer < time)
153         {
154                 float save;
155                 save = me.scrollPos;
156                 me.scrollPos = bound(cursorPosInWidths - (0.875 - me.keepspaceLeft - me.keepspaceRight), me.scrollPos, cursorPosInWidths - 0.125);
157                 if(me.scrollPos != save)
158                         me.dragScrollTimer = time + 0.2;
159         }
160         me.scrollPos = min(me.scrollPos, totalSizeInWidths - (1 - me.keepspaceRight - me.keepspaceLeft));
161         me.scrollPos = max(0, me.scrollPos);
162
163         draw_SetClipRect(eX * me.keepspaceLeft, eX * (1 - me.keepspaceLeft - me.keepspaceRight) + eY);
164         if(me.editColorCodes)
165         {
166                 string ch, ch2;
167                 float i;
168                 vector theColor;
169                 float theAlpha;
170                 vector p;
171                 float brightness;
172                 brightness = cvar("r_textbrightness");
173                 p = me.realOrigin - eX * me.scrollPos;
174                 theColor = '1 1 1';
175                 theAlpha = 1;
176                 for(i = 0; i < strlen(me.text); ++i)
177                 {
178                         ch = substring(me.text, i, 1);
179                         if(ch == "^")
180                         {
181                                 float w;
182                                 ch2 = substring(me.text, i+1, 1);
183                                 w = draw_TextWidth(strcat(ch, ch2), 0) * me.realFontSize_x;
184                                 if(ch2 == "^")
185                                 {
186                                         draw_Fill(p, eX * w + eY * me.realFontSize_y, '0 0 1', 0.5);
187                                         draw_Text(p + eX * 0.25 * w, "^", me.realFontSize, theColor, theAlpha, 0);
188                                 }
189                                 else if(ch2 == "0" || stof(ch2)) // digit?
190                                 {
191                                         switch(stof(ch2))
192                                         {
193                                                 case 0: theColor = '0 0 0'; theAlpha = 1; break;
194                                                 case 1: theColor = '1 0 0'; theAlpha = 1; break;
195                                                 case 2: theColor = '0 1 0'; theAlpha = 1; break;
196                                                 case 3: theColor = '1 1 0'; theAlpha = 1; break;
197                                                 case 4: theColor = '0 0 1'; theAlpha = 1; break;
198                                                 case 5: theColor = '0 1 1'; theAlpha = 1; break;
199                                                 case 6: theColor = '1 0 1'; theAlpha = 1; break;
200                                                 case 7: theColor = '1 1 1'; theAlpha = 1; break;
201                                                 case 8: theColor = '1 1 1'; theAlpha = 0.5; break;
202                                                 case 9: theColor = '0.5 0.5 0.5'; theAlpha = 1; break;
203                                         }
204                                         theColor = theColor * (1 - brightness) + brightness * '1 1 1';
205                                         draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
206                                         draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
207                                         draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
208                                 }
209                                 else
210                                 {
211                                         draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 0 0', 0.5);
212                                         draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
213                                         draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
214                                 }
215                                 p += w * eX;
216                                 ++i;
217                                 continue;
218                         }
219                         draw_Text(p, ch, me.realFontSize, theColor, theAlpha, 0); p += eX * draw_TextWidth(ch, 0) * me.realFontSize_x;
220                 }
221         }
222         else
223                 draw_Text(me.realOrigin - eX * me.scrollPos, me.text, me.realFontSize, '1 1 1', 1, 0);
224                 // skipping drawLabel(me);
225         if(!me.focused || (time - me.lastChangeTime) < floor(time - me.lastChangeTime) + 0.5)
226                 draw_Text(me.realOrigin + eX * (cursorPosInWidths - me.scrollPos), CURSOR, me.realFontSize, '1 1 1', 1, 0);
227
228         draw_ClearClip();
229 }
230
231 void showNotifyInputBox(entity me)
232 {
233         me.focusable = !me.disabled;
234 }
235 #endif