]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu-div0test/item/inputbox.c
fix bug in filter; add Morphed's inputbox
[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 ENDCLASS(InputBox)
23 #endif
24
25 #ifdef IMPLEMENTATION
26 void configureInputBoxInputBox(entity me, string theText, float theCursorPos, float theFontSize, string gfx)
27 {
28         configureLabelLabel(me, theText, theFontSize, 0.0);
29         me.src = gfx;
30         me.cursorPos = theCursorPos;
31 }
32
33 void setTextInputBox(entity me, string txt)
34 {
35         if(me.text)
36                 strunzone(me.text);
37         setTextLabel(me, strzone(txt));
38 }
39
40 float mouseDragInputBox(entity me, vector pos)
41 {
42         float p;
43         me.dragScrollPos = pos;
44         p = me.scrollPos + pos_x - me.keepspaceLeft;
45         me.cursorPos = draw_TextLengthUpToWidth(me.text, p / me.realFontSize_x, 0);
46         me.lastChangeTime = time;
47         return 1;
48 }
49
50 float mousePressInputBox(entity me, vector pos)
51 {
52         me.dragScrollTimer = 0;
53         me.pressed = 1;
54         return mouseDragInputBox(me, pos);
55 }
56
57 float mouseReleaseInputBox(entity me, vector pos)
58 {
59         me.pressed = 0;
60         return mouseDragInputBox(me, pos);
61 }
62
63 float keyDownInputBox(entity me, float key, float ascii, float shift)
64 {
65         me.lastChangeTime = time;
66         me.dragScrollTimer = 0;
67         if(ascii >= 32 && ascii != 127)
68         {
69                 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), chr(ascii), substring(me.text, me.cursorPos, strlen(me.text) - me.cursorPos)));
70                 me.cursorPos += 1;
71                 return 1;
72         }
73         switch(key)
74         {
75                 case K_LEFTARROW:
76                         me.cursorPos -= 1;
77                         return 1;
78                 case K_RIGHTARROW:
79                         me.cursorPos += 1;
80                         return 1;
81                 case K_HOME:
82                         me.cursorPos = 0;
83                         return 1;
84                 case K_END:
85                         me.cursorPos = strlen(me.text);
86                         return 1;
87                 case K_BACKSPACE:
88                         if(me.cursorPos > 0)
89                         {
90                                 me.cursorPos -= 1;
91                                 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
92                         }
93                         return 1;
94                 case K_DEL:
95                         if(shift)
96                                 me.setText(me, "");
97                         else
98                                 me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
99                         return 1;
100         }
101         return 0;
102 }
103
104 void drawInputBox(entity me)
105 {
106 #define CURSOR "_"
107         float cursorPosInWidths, totalSizeInWidths;
108
109         if(me.pressed)
110                 me.mouseDrag(me, me.dragScrollPos); // simulate mouseDrag event
111
112         if(me.src)
113         {
114                 if(me.focused)
115                         draw_ButtonPicture('0 0 0', strcat(me.src, "_f"), '1 1 0', '1 1 1', 1);
116                 else
117                         draw_ButtonPicture('0 0 0', strcat(me.src, "_n"), '1 1 0', '1 1 1', 1);
118         }
119
120         me.cursorPos = bound(0, me.cursorPos, strlen(me.text));
121         cursorPosInWidths = draw_TextWidth(substring(me.text, 0, me.cursorPos), 0) * me.realFontSize_x;
122         totalSizeInWidths = draw_TextWidth(strcat(me.text, CURSOR), 0) * me.realFontSize_x;
123
124         me.dragScrollTimer -= frametime;
125         if(me.dragScrollTimer < 0)
126         {
127                 float save;
128                 save = me.scrollPos;
129                 me.scrollPos = bound(cursorPosInWidths - (0.875 - me.keepspaceLeft - me.keepspaceRight), me.scrollPos, cursorPosInWidths - 0.125);
130                 if(me.scrollPos != save)
131                         me.dragScrollTimer = 0.2;
132         }
133         me.scrollPos = min(me.scrollPos, totalSizeInWidths - (1 - me.keepspaceRight - me.keepspaceLeft));
134         me.scrollPos = max(0, me.scrollPos);
135
136         draw_SetClipRect(eX * me.keepspaceLeft, eX * (1 - me.keepspaceLeft - me.keepspaceRight) + eY);
137         if(me.editColorCodes)
138         {
139                 string ch, ch2;
140                 float i;
141                 vector theColor;
142                 float theAlpha;
143                 vector p;
144                 float brightness;
145                 brightness = cvar("r_textbrightness");
146                 p = me.realOrigin - eX * me.scrollPos;
147                 theColor = '1 1 1';
148                 theAlpha = 1;
149                 for(i = 0; i < strlen(me.text); ++i)
150                 {
151                         ch = substring(me.text, i, 1);
152                         if(ch == "^")
153                         {
154                                 float w;
155                                 ch2 = substring(me.text, i+1, 1);
156                                 w = draw_TextWidth(strcat(ch, ch2), 0) * me.realFontSize_x;
157                                 if(ch2 == "^")
158                                 {
159                                         draw_Fill(p, eX * w + eY * me.realFontSize_y, '0 0 1', 0.5);
160                                         draw_Text(p + eX * 0.25 * w, "^", me.realFontSize, theColor, theAlpha, 0);
161                                 }
162                                 else if(ch2 == "0" || stof(ch2)) // digit?
163                                 {
164                                         switch(stof(ch2))
165                                         {
166                                                 case 0: theColor = '0 0 0'; theAlpha = 1; break;
167                                                 case 1: theColor = '1 0 0'; theAlpha = 1; break;
168                                                 case 2: theColor = '0 1 0'; theAlpha = 1; break;
169                                                 case 3: theColor = '1 1 0'; theAlpha = 1; break;
170                                                 case 4: theColor = '0 0 1'; theAlpha = 1; break;
171                                                 case 5: theColor = '0 1 1'; theAlpha = 1; break;
172                                                 case 6: theColor = '1 0 1'; theAlpha = 1; break;
173                                                 case 7: theColor = '1 1 1'; theAlpha = 1; break;
174                                                 case 8: theColor = '1 1 1'; theAlpha = 0.5; break;
175                                                 case 9: theColor = '0.5 0.5 0.5'; theAlpha = 1; break;
176                                         }
177                                         theColor = theColor * (1 - brightness) + brightness * '1 1 1';
178                                         draw_Fill(p, eX * w + eY * me.realFontSize_y, '0 0 0', 0.5);
179                                         draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
180                                         draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
181                                 }
182                                 else
183                                 {
184                                         draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 0 0', 0.5);
185                                         draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
186                                         draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, theAlpha, 0);
187                                 }
188                                 p += w * eX;
189                                 ++i;
190                                 continue;
191                         }
192                         draw_Text(p, ch, me.realFontSize, theColor, theAlpha, 0); p += eX * draw_TextWidth(ch, 0) * me.realFontSize_x;
193                 }
194         }
195         else
196                 draw_Text(me.realOrigin - eX * me.scrollPos, me.text, me.realFontSize, '1 1 1', 1, 0);
197                 // skipping drawLabel(me);
198         draw_ClearClip();
199
200         if(!me.focused || (time - me.lastChangeTime) < floor(time - me.lastChangeTime) + 0.5)
201                 draw_Text(me.realOrigin + eX * (cursorPosInWidths - me.scrollPos), CURSOR, me.realFontSize, '1 1 1', 1, 0);
202 }
203 #endif