]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu-div0test/item/inputbox.c
add "clear" button in join dialog
[divverent/nexuiz.git] / data / qcsrc / menu-div0test / 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, 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))
10
11         ATTRIB(InputBox, src, string, string_null)
12
13         ATTRIB(InputBox, cursorPos, float, 0) // characters
14         ATTRIB(InputBox, scrollPos, float, 0) // widths
15
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')
24 ENDCLASS(InputBox)
25 void InputBox_Clear_Click(entity btn, entity me);
26 #endif
27
28 #ifdef IMPLEMENTATION
29 void configureInputBoxInputBox(entity me, string theText, float theCursorPos, float theFontSize, string gfx)
30 {
31         configureLabelLabel(me, theText, theFontSize, 0.0);
32         me.src = gfx;
33         me.cursorPos = theCursorPos;
34 }
35
36 void setTextInputBox(entity me, string txt)
37 {
38         if(me.text)
39                 strunzone(me.text);
40         setTextLabel(me, strzone(txt));
41 }
42
43 void InputBox_Clear_Click(entity btn, entity me)
44 {
45         me.setText(me, "");
46 }
47
48 float mouseDragInputBox(entity me, vector pos)
49 {
50         float p;
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;
55         return 1;
56 }
57
58 float mousePressInputBox(entity me, vector pos)
59 {
60         me.dragScrollTimer = 0;
61         me.pressed = 1;
62         return mouseDragInputBox(me, pos);
63 }
64
65 float mouseReleaseInputBox(entity me, vector pos)
66 {
67         me.pressed = 0;
68         return mouseDragInputBox(me, pos);
69 }
70
71 float keyDownInputBox(entity me, float key, float ascii, float shift)
72 {
73         me.lastChangeTime = time;
74         me.dragScrollTimer = 0;
75         if(ascii >= 32 && ascii != 127)
76         {
77                 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), chr(ascii), substring(me.text, me.cursorPos, strlen(me.text) - me.cursorPos)));
78                 me.cursorPos += 1;
79                 return 1;
80         }
81         switch(key)
82         {
83                 case K_LEFTARROW:
84                         me.cursorPos -= 1;
85                         return 1;
86                 case K_RIGHTARROW:
87                         me.cursorPos += 1;
88                         return 1;
89                 case K_HOME:
90                         me.cursorPos = 0;
91                         return 1;
92                 case K_END:
93                         me.cursorPos = strlen(me.text);
94                         return 1;
95                 case K_BACKSPACE:
96                         if(shift)
97                                 me.setText(me, "");
98                         else
99                                 if(me.cursorPos > 0)
100                                 {
101                                         me.cursorPos -= 1;
102                                         me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
103                                 }
104                         return 1;
105                 case K_DEL:
106                         if(shift)
107                                 me.setText(me, "");
108                         else
109                                 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
110                         return 1;
111         }
112         return 0;
113 }
114
115 void drawInputBox(entity me)
116 {
117 #define CURSOR "_"
118         float cursorPosInWidths, totalSizeInWidths;
119
120         if(me.pressed)
121                 me.mouseDrag(me, me.dragScrollPos); // simulate mouseDrag event
122
123         if(me.src)
124         {
125                 if(me.focused)
126                         draw_ButtonPicture('0 0 0', strcat(me.src, "_f"), '1 1 0', me.colorF, 1);
127                 else
128                         draw_ButtonPicture('0 0 0', strcat(me.src, "_n"), '1 1 0', me.color, 1);
129         }
130
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;
134
135         me.dragScrollTimer -= frametime;
136         if(me.dragScrollTimer < 0)
137         {
138                 float save;
139                 save = me.scrollPos;
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;
143         }
144         me.scrollPos = min(me.scrollPos, totalSizeInWidths - (1 - me.keepspaceRight - me.keepspaceLeft));
145         me.scrollPos = max(0, me.scrollPos);
146
147         draw_SetClipRect(eX * me.keepspaceLeft, eX * (1 - me.keepspaceLeft - me.keepspaceRight) + eY);
148         if(me.editColorCodes)
149         {
150                 string ch, ch2;
151                 float i;
152                 vector theColor;
153                 float theAlpha;
154                 vector p;
155                 float brightness;
156                 brightness = cvar("r_textbrightness");
157                 p = me.realOrigin - eX * me.scrollPos;
158                 theColor = '1 1 1';
159                 theAlpha = 1;
160                 for(i = 0; i < strlen(me.text); ++i)
161                 {
162                         ch = substring(me.text, i, 1);
163                         if(ch == "^")
164                         {
165                                 float w;
166                                 ch2 = substring(me.text, i+1, 1);
167                                 w = draw_TextWidth(strcat(ch, ch2), 0) * me.realFontSize_x;
168                                 if(ch2 == "^")
169                                 {
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);
172                                 }
173                                 else if(ch2 == "0" || stof(ch2)) // digit?
174                                 {
175                                         switch(stof(ch2))
176                                         {
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;
187                                         }
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);
192                                 }
193                                 else
194                                 {
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);
198                                 }
199                                 p += w * eX;
200                                 ++i;
201                                 continue;
202                         }
203                         draw_Text(p, ch, me.realFontSize, theColor, theAlpha, 0); p += eX * draw_TextWidth(ch, 0) * me.realFontSize_x;
204                 }
205         }
206         else
207                 draw_Text(me.realOrigin - eX * me.scrollPos, me.text, me.realFontSize, '1 1 1', 1, 0);
208                 // skipping drawLabel(me);
209         draw_ClearClip();
210
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);
213 }
214 #endif