1 /*
2  * Copyright 2010-2014 OpenXcom Developers.
3  *
4  * This file is part of OpenXcom.
5  *
6  * OpenXcom is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * OpenXcom is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with OpenXcom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "Bar.h"
20 #include <cmath>
21 #include <SDL.h>
22 
23 namespace OpenXcom
24 {
25 
26 /**
27  * Sets up a blank bar with the specified size and position.
28  * @param width Width in pixels.
29  * @param height Height in pixels.
30  * @param x X position in pixels.
31  * @param y Y position in pixels.
32  */
Bar(int width,int height,int x,int y)33 Bar::Bar(int width, int height, int x, int y) : Surface(width, height, x, y), _color(0), _color2(0), _scale(0), _max(0), _value(0), _value2(0), _invert(false), _secondOnTop(true)
34 {
35 
36 }
37 
38 /**
39  *
40  */
~Bar()41 Bar::~Bar()
42 {
43 }
44 
45 /**
46  * Changes the color used to draw the border and contents.
47  * @param color Color value.
48  */
setColor(Uint8 color)49 void Bar::setColor(Uint8 color)
50 {
51 	_color = color;
52 	_redraw = true;
53 }
54 
55 /**
56  * Returns the color used to draw the bar.
57  * @return Color value.
58  */
getColor() const59 Uint8 Bar::getColor() const
60 {
61 	return _color;
62 }
63 
64 /**
65  * Changes the color used to draw the second contents.
66  * @param color Color value.
67  */
setColor2(Uint8 color)68 void Bar::setColor2(Uint8 color)
69 {
70 	_color2 = color;
71 	_redraw = true;
72 }
73 
74 /**
75  * Returns the second color used to draw the bar.
76  * @return Color value.
77  */
getColor2() const78 Uint8 Bar::getColor2() const
79 {
80 	return _color2;
81 }
82 
83 /**
84  * Changes the scale factor used to draw the bar values.
85  * @param scale Scale in pixels/unit.
86  */
setScale(double scale)87 void Bar::setScale(double scale)
88 {
89 	_scale = scale;
90 	_redraw = true;
91 }
92 
93 /**
94  * Returns the scale factor used to draw the bar values.
95  * @return Scale in pixels/unit.
96  */
getScale() const97 double Bar::getScale() const
98 {
99 	return _scale;
100 }
101 
102 /**
103  * Changes the maximum value used to draw the outer border.
104  * @param max Maximum value.
105  */
setMax(double max)106 void Bar::setMax(double max)
107 {
108 	_max = max;
109 	_redraw = true;
110 }
111 
112 /**
113  * Returns the maximum value used to draw the outer border.
114  * @return Maximum value.
115  */
getMax() const116 double Bar::getMax() const
117 {
118 	return _max;
119 }
120 
121 /**
122  * Changes the value used to draw the inner contents.
123  * @param value Current value.
124  */
setValue(double value)125 void Bar::setValue(double value)
126 {
127 	_value = value;
128 	_redraw = true;
129 }
130 
131 /**
132  * Returns the value used to draw the inner contents.
133  * @return Current value.
134  */
getValue() const135 double Bar::getValue() const
136 {
137 	return _value;
138 }
139 
140 /**
141  * Changes the value used to draw the second inner contents.
142  * @param value Current value.
143  */
setValue2(double value)144 void Bar::setValue2(double value)
145 {
146 	_value2 = value;
147 	_redraw = true;
148 }
149 
150 /**
151  * Returns the value used to draw the second inner contents.
152  * @return Current value.
153  */
getValue2() const154 double Bar::getValue2() const
155 {
156 	return _value2;
157 }
158 
159 /**
160  * Defines whether the second value should be drawn on top. Default this is true.
161  * @param onTop Second value on top?
162  */
setSecondValueOnTop(bool onTop)163 void Bar::setSecondValueOnTop(bool onTop)
164 {
165 	_secondOnTop = onTop;
166 }
167 
168 /**
169  * Enables/disables color inverting. Some bars have
170  * darker borders and others have lighter borders.
171  * @param invert Invert setting.
172  */
setInvert(bool invert)173 void Bar::setInvert(bool invert)
174 {
175 	_invert = invert;
176 	_redraw = true;
177 }
178 
179 /**
180  * Draws the bordered bar filled according
181  * to its values.
182  */
draw()183 void Bar::draw()
184 {
185 	Surface::draw();
186 	SDL_Rect square;
187 
188 	square.x = 0;
189 	square.y = 0;
190 	square.w = (Uint16)(_scale * _max) + 1;
191 	square.h = getHeight();
192 
193 	if (_invert)
194 	{
195 		drawRect(&square, _color);
196 	}
197 	else
198 	{
199 		drawRect(&square, _color + 4);
200 	}
201 
202 	square.y++;
203 	square.w--;
204 	square.h -= 2;
205 
206 	drawRect(&square, 0);
207 
208 	if (_invert)
209 	{
210 		if (_secondOnTop)
211 		{
212 			square.w = (Uint16)(_scale * _value);
213 			drawRect(&square, _color + 4);
214 			square.w = (Uint16)(_scale * _value2);
215 			drawRect(&square, _color2 + 4);
216 		}
217 		else
218 		{
219 			square.w = (Uint16)(_scale * _value2);
220 			drawRect(&square, _color2 + 4);
221 			square.w = (Uint16)(_scale * _value);
222 			drawRect(&square, _color + 4);
223 		}
224 	}
225 	else
226 	{
227 		if (_secondOnTop)
228 		{
229 			square.w = (Uint16)(_scale * _value);
230 			drawRect(&square, _color);
231 			square.w = (Uint16)(_scale * _value2);
232 			drawRect(&square, _color2);
233 		}
234 		else
235 		{
236 			square.w = (Uint16)(_scale * _value2);
237 			drawRect(&square, _color2);
238 			square.w = (Uint16)(_scale * _value);
239 			drawRect(&square, _color);
240 		}
241 	}
242 }
243 
244 }
245