Debug system v2 -- now, it supports debug levels, there's a global level, but overrid...
[dbw.git] / src / widgets.cpp
blob05e64ef16bb0560ef64f0c08cd434ca4d59b075d
1 /*
2 Copyright © 2004 Parallel Realities
3 Copyright © 2007-2008 Kővágó Zoltán <DirtY.iCE.hu@gmail.com>
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.
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.
14 See the GNU General Public License for more details.
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.
22 #include "CGraphics.h"
23 #include "CWidget.h"
24 #include "console/console.hpp"
25 #include "gettext.hpp"
27 /**
28 * Draws the options for a radio button widget with the currently selected
29 * option highlighted
30 * @param widget The radio button widget
31 * @param maxWidth The width of the largest widget (used for spacing)
33 void drawOptions(Widget *widget, int maxWidth)
35 int x = widget->x + maxWidth + 55;
37 int count = 0;
39 std::string options = _(widget->options);
40 std::string::iterator it = options.begin();
41 std::string item;
43 SDL_Surface *text;
45 while (*it != '\0')
47 if (*it == '|')
49 if (widget->enabled)
51 graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
52 if (count == *widget->value)
53 graphics.setFontColor(0x00, 0x00, 0x00, 0x00, 0xff, 0x00);
55 else
57 graphics.setFontColor(0x77, 0x77, 0x77, 0x00, 0x00, 0x00);
60 text = graphics.getString(item, false);
62 if ((widget->enabled) && (count == *widget->value))
63 graphics.drawWidgetRect(bx + x, by + widget->y, text->w, text->h);
65 graphics.blit(text, bx + x, by + widget->y, graphics.screen, false);
67 x += text->w + 25;
68 count++;
69 item = "";
71 else
73 item += *it;
76 it++;
79 if (widget->enabled)
81 graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
82 if (count == *widget->value)
83 graphics.setFontColor(0x00, 0x00, 0x00, 0x00, 0xff, 0x00);
85 else
87 graphics.setFontColor(0x77, 0x77, 0x77, 0x00, 0x00, 0x00);
90 text = graphics.getString(item, false);
92 if ((widget->enabled) && (count == *widget->value))
94 graphics.drawWidgetRect(bx + x, by + widget->y, text->w, text->h);
97 graphics.blit(text, bx + x, by + widget->y, graphics.screen, false);
101 * Draws a horizontal slider widget with its current value
102 * used to fill the bar
103 * @param widget The slider widget
104 * @param maxWidth The width of the largest widget (used for spacing)
106 void drawSlider(Widget *widget, int maxWidth)
108 int x = widget->x + maxWidth + 50;
110 if (widget->enabled)
112 graphics.drawRect(bx + x, by + widget->y, 300, 18, graphics.white, graphics.screen);
114 else
116 graphics.drawRect(bx + x, by + widget->y, 300, 18, graphics.grey, graphics.screen);
119 graphics.drawRect(bx + x + 1, by + widget->y + 1, 298, 16, graphics.black, graphics.screen);
121 float width = 296.00 / widget->max;
123 width *= *widget->value;
125 if (widget->enabled)
127 graphics.drawRect(bx + x + 2, by + widget->y + 2, (int)width, 14, graphics.green, graphics.screen);
129 else
131 graphics.drawRect(bx + x + 2, by + widget->y + 2, (int)width, 14, graphics.darkGreen, graphics.screen);
137 * Draws a widget used to represent joypad buttons. A widget with a value of
138 * -1000 or less appears as ...
139 * @param widget The Joypad Button widget
141 void drawJoypadButtonOption(Widget *widget)
143 graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
145 int x = 300;
147 char text[25];
148 strcpy(text, "");
150 // joysticks have a button 0 so we can't
151 // do the same thing as the keyboard(!)
152 if (*widget->value <= -1000)
154 sprintf(text, "...");
156 else
158 sprintf(text, _("Button #%d").c_str(), *widget->value);
161 graphics.drawString(text, bx + x, by + widget->y, TXT_LEFT, graphics.screen);
165 * Draws a widget used to represent a key based on the widget's current value
166 * @param widget The Widget
168 void drawKeyOption(Widget *widget)
170 graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
172 int x = 300;
174 char text[25];
175 strcpy(text, "");
177 sprintf(text, "%s", Keyboard::translateKey(*widget->value));
179 graphics.drawString(text, bx + x, by + widget->y, TXT_LEFT, graphics.screen);
183 * Creates an image for the specified image. The text colour and background
184 * colour as set depending on the status of the widget
185 * @param widget The Widget to create the image for
187 void generateWidgetImage(Widget *widget)
189 if (widget == engine.highlightedWidget)
191 graphics.setFontColor(0x00, 0x00, 0x00, 0x00, 0xff, 0x00);
193 else
195 if (widget->enabled)
197 graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
199 else
201 graphics.setFontColor(0x77, 0x77, 0x77, 0x00, 0x00, 0x00);
205 widget->image = graphics.getString(_(widget->label), false);
209 * Draws all the widgets in their specified positions. Widgets that
210 * have no image have an image created for them. Invisible widgets
211 * are not drawn.
213 void drawWidgets()
215 graphics.setFontSize(0);
217 Widget *widget = (Widget*)engine.widgetList.getHead();
219 int maxWidth = 0;
221 while (widget->next != NULL)
223 widget = (Widget*)widget->next;
225 if (widget->image == NULL)
227 generateWidgetImage(widget);
230 maxWidth = std::max(maxWidth, widget->image->w);
233 widget = (Widget*)engine.widgetList.getHead();
235 while (widget->next != NULL)
237 widget = (Widget*)widget->next;
239 if (!widget->visible)
241 continue;
244 if ((!widget->value) && (widget->type != WG_LABEL))
246 warning << "Widget variable for '" << widget->name << "' not set!" << std::endl;
247 continue;
250 if (widget->x == -1)
252 widget->x = (640 - widget->image->w) / 2;
255 if (widget == engine.highlightedWidget)
257 graphics.drawWidgetRect(bx + widget->x, by + widget->y, widget->image->w, widget->image->h);
259 else
261 graphics.drawRect(bx + widget->x - 3, by + widget->y - 2, widget->image->w + 6, widget->image->h + 4, graphics.black, graphics.screen);
264 graphics.blit(widget->image, bx + widget->x, by + widget->y, graphics.screen, false);
266 switch (widget->type)
268 case WG_RADIO:
269 drawOptions(widget, maxWidth);
270 break;
271 case WG_SLIDER:
272 case WG_SMOOTH_SLIDER:
273 drawSlider(widget, maxWidth);
274 break;
275 case WG_KEYBOARD:
276 drawKeyOption(widget);
277 break;
278 case WG_JOYPAD:
279 drawJoypadButtonOption(widget);
280 break;
283 widget->changed = false;