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