]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/widgets.cpp
Use time_t to store time data.
[quix0rs-blobwars.git] / src / widgets.cpp
1 /*
2 Copyright (C) 2004-2011 Parallel Realities
3 Copyright (C) 2011-2015 Perpendicular Dimensions
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (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.
13
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 */
21
22 #include "widgets.h"
23
24 /**
25 * Draws the options for a radio button widget with the currently selected
26 * option highlighted
27 * @param widget The radio button widget
28 * @param maxWidth The width of the largest widget (used for spacing)
29 */
30 void drawOptions(Widget *widget, int maxWidth)
31 {
32         int x = widget->x + maxWidth + 55;
33
34         int count = 0;
35
36         char *c = widget->options;
37
38         SDL_Surface *text;
39
40         while (*c)
41         {
42                 char token[100];
43                 unsigned int token_len;
44
45                 char *eow = strchr (c, '|');
46                 if (!eow)
47                         eow = strchr (c, 0);
48
49                 token_len = eow - c;
50                 if (token_len > sizeof (token) - 1)
51                         token_len = sizeof (token) - 1;
52                 memcpy (token, c, token_len);
53                 token [token_len] = 0;
54
55                 c = eow;
56                 if (*c)
57                         c++;
58
59                 if (widget->enabled)
60                 {
61                         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
62                         if (count == *widget->value)
63                                 graphics.setFontColor(0x00, 0x00, 0x00, 0x00, 0xff, 0x00);
64                 }
65                 else
66                 {
67                         graphics.setFontColor(0x77, 0x77, 0x77, 0x00, 0x00, 0x00);
68                 }
69
70                 text = graphics.getString(token, false);
71
72                 if ((widget->enabled) && (count == *widget->value))
73                         graphics.drawWidgetRect(x, widget->y, text->w, text->h);
74
75                 graphics.blit(text, x, widget->y, graphics.screen, false);
76
77                 x += text->w + 25;
78                 count++;
79         }
80 }
81
82 /**
83 * Draws a horizontal slider widget with its current value
84 * used to fill the bar
85 * @param widget The slider widget
86 * @param maxWidth The width of the largest widget (used for spacing)
87 */
88 void drawSlider(Widget *widget, int maxWidth)
89 {
90         int x = widget->x + maxWidth + 50;
91
92         if (widget->enabled)
93         {
94                 graphics.drawRect(x, widget->y, 300, 18, graphics.white, graphics.screen);
95         }
96         else
97         {
98                 graphics.drawRect(x, widget->y, 300, 18, graphics.grey, graphics.screen);
99         }
100
101         graphics.drawRect(x + 1, widget->y + 1, 298, 16, graphics.black, graphics.screen);
102
103         float width = 296.00 / widget->max;
104
105         width *= *widget->value;
106
107         if (widget->enabled)
108         {
109                 graphics.drawRect(x + 2, widget->y + 2, (int)width, 14, graphics.green, graphics.screen);
110         }
111         else
112         {
113                 graphics.drawRect(x + 2, widget->y + 2, (int)width, 14, graphics.darkGreen, graphics.screen);
114         }
115
116 }
117
118 /**
119 * Draws a widget used to represent joypad buttons. A widget with a value of
120 * -1000 or less appears as ...
121 * @param widget The Joypad Button widget
122 */
123 void drawJoypadButtonOption(Widget *widget)
124 {
125         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
126         
127         int x = 300;
128         
129         char text[25];
130         text[0] = 0;
131         
132         // joysticks have a button 0 so we can't
133         // do the same thing as the keyboard(!)
134         if (*widget->value < -2)
135         {
136                 snprintf(text, sizeof text, "...");
137         }
138         else if (*widget->value == -2)
139         {
140                 snprintf(text, sizeof text, "N/A");
141         }
142         else
143         {
144                 snprintf(text, sizeof text, "Button #%d", *widget->value);
145         }
146         
147         graphics.drawString(text, x, widget->y, TXT_LEFT, graphics.screen);
148 }
149
150 /**
151 * Draws a widget used to represent a key based on the widget's current value
152 * @param widget The Widget
153 */
154 void drawKeyOption(Widget *widget)
155 {
156         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
157         
158         int x = 300;
159         
160         graphics.drawString(Keyboard::translateKey(*widget->value),
161                             x, widget->y, TXT_LEFT, graphics.screen);
162 }
163
164 /**
165 * Creates an image for the specified image. The text colour and background
166 * colour as set depending on the status of the widget
167 * @param widget The Widget to create the image for
168 */
169 void generateWidgetImage(Widget *widget)
170 {
171         if (widget == engine.highlightedWidget)
172         {
173                 graphics.setFontColor(0x00, 0x00, 0x00, 0x00, 0xff, 0x00);
174         }
175         else
176         {
177                 if (widget->enabled)
178                 {
179                         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
180                 }
181                 else
182                 {
183                         graphics.setFontColor(0x77, 0x77, 0x77, 0x00, 0x00, 0x00);
184                 }
185         }
186
187         widget->image = graphics.getString(widget->label, false);
188 }
189
190 /**
191 * Draws all the widgets in their specified positions. Widgets that
192 * have no image have an image created for them. Invisible widgets
193 * are not drawn.
194 */
195 void drawWidgets()
196 {
197         graphics.setFontSize(0);
198
199         Widget *widget = (Widget*)engine.widgetList.getHead();
200
201         int maxWidth = 0;
202
203         while (widget->next != NULL)
204         {
205                 widget = (Widget*)widget->next;
206
207                 if (widget->image == NULL)
208                 {
209                         generateWidgetImage(widget);
210                 }
211                         
212                 maxWidth = max(maxWidth, widget->image->w);
213         }
214
215         widget = (Widget*)engine.widgetList.getHead();
216
217         while (widget->next != NULL)
218         {
219                 widget = (Widget*)widget->next;
220
221                 if (!widget->visible)
222                 {
223                         continue;
224                 }
225
226                 if ((!widget->value) && (widget->type != WG_LABEL))
227                 {
228                         debug(("WARNING: Widget variable for '%s' not set!\n", widget->name));
229                         continue;
230                 }
231
232                 if (widget->x == -1)
233                 {
234                         widget->x = (640 - widget->image->w) / 2;
235                 }
236
237                 if (widget == engine.highlightedWidget)
238                 {
239                         graphics.drawWidgetRect(widget->x, widget->y, widget->image->w, widget->image->h);
240                 }
241                 else
242                 {
243                         graphics.drawRect(widget->x - 3, widget->y - 2, widget->image->w + 6, widget->image->h + 4, graphics.black, graphics.screen);
244                 }
245
246                 graphics.blit(widget->image, widget->x, widget->y, graphics.screen, false);
247
248                 switch (widget->type)
249                 {
250                         case WG_RADIO:
251                                 drawOptions(widget, maxWidth);
252                                 break;
253                         case WG_SLIDER:
254                         case WG_SMOOTH_SLIDER:
255                                 drawSlider(widget, maxWidth);
256                                 break;
257                         case WG_KEYBOARD:
258                                 drawKeyOption(widget);
259                                 break;
260                         case WG_JOYPAD:
261                                 drawJoypadButtonOption(widget);
262                                 break;
263                 }
264
265                 widget->changed = false;
266         }
267 }