1 /*
2 Solar Conquest
3 Copyright (C) 2006 Greg Beaman
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 
20 class CTextBox : public CMenuItem
21 {
22 public:
23 	CMenuItem* link;
24 
25 	int maxText;
26 
27 	CTextBox(int iid, int px, int py, int w, int h);
28 	virtual ~CTextBox();
29 
30 	void DrawItem(bool selected);
31 	void DrawItemText(bool selected);
32 	void OnChar(char input);
33 };
34 
CTextBox(int iid,int px,int py,int w,int h)35 CTextBox::CTextBox(int iid, int px, int py, int w, int h)
36 {
37 	id = iid;
38 
39 	x = px;
40 	y = py;
41 	height = h;
42 	itemType = MENU_ITEM_TEXTBOX;
43 	strcpy(caption,"");
44 
45 	maxText = w+1;
46 	if (maxText > MAX_STRING_SIZE)
47 		maxText = MAX_STRING_SIZE;
48 
49 	width = w * (FONT_SIZE_X+FONT_SPACING) + 12;
50 
51 	nextItem = NULL;
52 	link = NULL;
53 };
54 
~CTextBox()55 CTextBox::~CTextBox()
56 {
57 }
58 
DrawItem(bool selected)59 void CTextBox::DrawItem(bool selected)
60 {
61 	//Draw the box
62 	glBegin(GL_LINES);
63 		glColor4f(0,0,0.2,1);
64 		glVertex2f(x,y);
65 		glVertex2f(x+width,y);
66 		glVertex2f(x+width,y);
67 		glVertex2f(x+width,y+height);
68 		glVertex2f(x+width,y+height);
69 		glVertex2f(x,y+height);
70 		glVertex2f(x,y+height);
71 		glVertex2f(x,y);
72 
73 		glVertex2f(x+6,y+6);
74 		glVertex2f(x+width-6,y+6);
75 		glVertex2f(x+width-6,y+6);
76 		glVertex2f(x+width-6,y+height-6);
77 		glVertex2f(x+width-6,y+height-6);
78 		glVertex2f(x+6,y+height-6);
79 		glVertex2f(x+6,y+height-6);
80 		glVertex2f(x+6,y+6);
81 
82 		glColor4f(0,0,0.5,1);
83 		glVertex2f(x+1,y+1);
84 		glVertex2f(x+width-1,y+1);
85 		glVertex2f(x+width-1,y+1);
86 		glVertex2f(x+width-1,y+height-1);
87 		glVertex2f(x+width-1,y+height-1);
88 		glVertex2f(x+1,y+height-1);
89 		glVertex2f(x+1,y+height-1);
90 		glVertex2f(x+1,y+1);
91 
92 		glVertex2f(x+5,y+5);
93 		glVertex2f(x+width-5,y+5);
94 		glVertex2f(x+width-5,y+5);
95 		glVertex2f(x+width-5,y+height-5);
96 		glVertex2f(x+width-5,y+height-5);
97 		glVertex2f(x+5,y+height-5);
98 		glVertex2f(x+5,y+height-5);
99 		glVertex2f(x+5,y+5);
100 
101 		glColor4f(0,0,1,1);
102 		glVertex2f(x+2,y+2);
103 		glVertex2f(x+width-2,y+2);
104 		glVertex2f(x+width-2,y+2);
105 		glVertex2f(x+width-2,y+height-2);
106 		glVertex2f(x+width-2,y+height-2);
107 		glVertex2f(x+2,y+height-2);
108 		glVertex2f(x+2,y+height-2);
109 		glVertex2f(x+2,y+2);
110 
111 		glVertex2f(x+4,y+4);
112 		glVertex2f(x+width-4,y+4);
113 		glVertex2f(x+width-4,y+4);
114 		glVertex2f(x+width-4,y+height-4);
115 		glVertex2f(x+width-4,y+height-4);
116 		glVertex2f(x+4,y+height-4);
117 		glVertex2f(x+4,y+height-4);
118 		glVertex2f(x+4,y+4);
119 
120 		glColor4f(1,1,1,1);
121 		glVertex2f(x+3,y+3);
122 		glVertex2f(x+width-3,y+3);
123 		glVertex2f(x+width-3,y+3);
124 		glVertex2f(x+width-3,y+height-3);
125 		glVertex2f(x+width-3,y+height-3);
126 		glVertex2f(x+3,y+height-3);
127 		glVertex2f(x+3,y+height-3);
128 		glVertex2f(x+3,y+3);
129 	glEnd();
130 
131 	glBegin(GL_TRIANGLES);
132 		//Top
133 		glColor4f(0,0,1,0);
134 		glVertex2f(x,y);
135 		glVertex2f(x+width,y);
136 		glColor4f(1,1,1,1);
137 		glVertex2f(x,y+3);
138 
139 		glVertex2f(x,y+3);
140 		glVertex2f(x+width,y+3);
141 		glColor4f(0,0,1,0);
142 		glVertex2f(x+width,y);
143 
144 		glColor4f(0,0,1,0);
145 		glVertex2f(x,y+6);
146 		glVertex2f(x+width,y+6);
147 		glColor4f(1,1,1,1);
148 		glVertex2f(x,y+3);
149 
150 		glVertex2f(x,y+3);
151 		glVertex2f(x+width,y+3);
152 		glColor4f(0,0,1,0);
153 		glVertex2f(x+width,y+6);
154 
155 		//Left side
156 		glColor4f(0,0,1,0);
157 		glVertex2f(x,y);
158 		glVertex2f(x,y+height);
159 		glColor4f(1,1,1,1);
160 		glVertex2f(x+3,y);
161 
162 		glVertex2f(x+3,y);
163 		glVertex2f(x+3,y+height);
164 		glColor4f(0,0,1,0);
165 		glVertex2f(x,y+height);
166 
167 		glVertex2f(x+6,y);
168 		glVertex2f(x+6,y+height);
169 		glColor4f(1,1,1,1);
170 		glVertex2f(x+3,y+height);
171 
172 		glVertex2f(x+3,y);
173 		glVertex2f(x+3,y+height);
174 		glColor4f(0,0,1,0);
175 		glVertex2f(x+6,y);
176 
177 		//Right side
178 		glColor4f(0,0,1,0);
179 		glVertex2f(x+width,y);
180 		glVertex2f(x+width,y+height);
181 		glColor4f(1,1,1,1);
182 		glVertex2f(x+width-3,y);
183 
184 		glVertex2f(x+width-3,y);
185 		glVertex2f(x+width-3,y+height);
186 		glColor4f(0,0,1,0);
187 		glVertex2f(x+width,y+height);
188 
189 		glVertex2f(x+width-6,y);
190 		glVertex2f(x+width-6,y+height);
191 		glColor4f(1,1,1,1);
192 		glVertex2f(x+width-3,y+height);
193 
194 		glVertex2f(x+width-3,y);
195 		glVertex2f(x+width-3,y+height);
196 		glColor4f(0,0,1,0);
197 		glVertex2f(x+width-6,y);
198 
199 		//Bottom
200 		glColor4f(0,0,1,0);
201 		glVertex2f(x,y+height);
202 		glVertex2f(x+width,y+height);
203 		glColor4f(1,1,1,1);
204 		glVertex2f(x,y+height-3);
205 
206 		glVertex2f(x,y+height-3);
207 		glVertex2f(x+width,y+height-3);
208 		glColor4f(0,0,1,0);
209 		glVertex2f(x+width,y+height);
210 
211 		glColor4f(0,0,1,0);
212 		glVertex2f(x,y+height-6);
213 		glVertex2f(x+width,y+height-6);
214 		glColor4f(1,1,1,1);
215 		glVertex2f(x,y+height-3);
216 
217 		glVertex2f(x,y+height-3);
218 		glVertex2f(x+width,y+height-3);
219 		glColor4f(0,0,1,0);
220 		glVertex2f(x+width,y+height-6);
221 	glEnd();
222 
223 }
224 
DrawItemText(bool selected)225 void CTextBox::DrawItemText(bool selected)
226 {
227 	g_MainFont->RenderText(x+6,y+6,1,1,caption);
228 	if (selected)
229 		g_MainFont->RenderText(x+6 + (FONT_SIZE_X+FONT_SPACING)*strlen(caption),y+6,1,1,"|");
230 }
231 
OnChar(char input)232 void CTextBox::OnChar(char input)
233 {
234 	//Tab
235 	if (input == '\t')
236 	{
237 		if (strlen(caption)+5 < maxText)
238 			strcat(caption, "     ");
239 		return;
240 	}
241 
242 	//Return/newline
243 	if ((input == '\r') || (input == '\n'))
244 	{
245 		//Not ready for events yet
246 		return;
247 	}
248 
249 	//Backspace
250 	if (input == '\b')
251 	{
252 		if (strlen(caption) <= 0)
253 			return;
254 
255 		char temp[MAX_STRING_SIZE] = "";
256 		strncpy(temp, caption, strlen(caption)-1);
257 		strcpy(caption, temp);
258 		return;
259 	}
260 
261 	if (strlen(caption) < maxText-1)
262 		strncat(caption,&input,1);
263 }