1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program 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 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GUI |
---|
17 | |
---|
18 | #include "glgui_slider.h" |
---|
19 | #include "event_def.h" |
---|
20 | |
---|
21 | #include "glgui_handler.h" |
---|
22 | |
---|
23 | namespace OrxGui |
---|
24 | { |
---|
25 | |
---|
26 | /** |
---|
27 | * @brief standard constructor |
---|
28 | */ |
---|
29 | GLGuiSlider::GLGuiSlider () |
---|
30 | { |
---|
31 | this->init(); |
---|
32 | |
---|
33 | } |
---|
34 | |
---|
35 | |
---|
36 | /** |
---|
37 | * @brief standard deconstructor |
---|
38 | */ |
---|
39 | GLGuiSlider::~GLGuiSlider() |
---|
40 | {} |
---|
41 | |
---|
42 | /** |
---|
43 | * @brief initializes the GUI-element |
---|
44 | */ |
---|
45 | void GLGuiSlider::init() |
---|
46 | { |
---|
47 | |
---|
48 | this->setClassID(CL_GLGUI_SLIDER, "GLGuiSlider"); |
---|
49 | |
---|
50 | this->setClickable( ); |
---|
51 | this->setFocusable( ); |
---|
52 | |
---|
53 | this->_value = 0.0; |
---|
54 | this->_minValue = 0.0; |
---|
55 | this->_maxValue = 1.0; |
---|
56 | this->_step = 0.1; |
---|
57 | this->_sliderWidth = 5.0; |
---|
58 | this->grabbed = false; |
---|
59 | |
---|
60 | this->setSize2D(100, 30); |
---|
61 | this->resize(); |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * @param value the new Value. |
---|
66 | * @note will automatically be set between max() and min() |
---|
67 | */ |
---|
68 | void GLGuiSlider::setValue(float value) |
---|
69 | { |
---|
70 | if (value < this->min()) |
---|
71 | this->_value = min(); |
---|
72 | else if (value > max()) |
---|
73 | this->_value = max(); |
---|
74 | else |
---|
75 | this->_value = value; |
---|
76 | this->_handle.setCenter(this->sliderPosition(), borderTop() + (this->getSizeY2D() - borderTop() - borderBottom()) / 2.0); |
---|
77 | |
---|
78 | emit(valueChanged(this->_value)); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * @param minimum the minumum of the range. |
---|
83 | * |
---|
84 | * @note will rearange value if necessary and will not be made bigger than max() |
---|
85 | */ |
---|
86 | void GLGuiSlider::setMin(float minimum) |
---|
87 | { |
---|
88 | if (minimum <= max()) |
---|
89 | { |
---|
90 | this->_minValue = minimum; |
---|
91 | emit(rangeChanged(this->_minValue, this->_maxValue)); |
---|
92 | } |
---|
93 | if (this->value() < this->min()) |
---|
94 | this->setValue(this->min()); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | /** |
---|
99 | * @param maximum the maximum of the range. |
---|
100 | * |
---|
101 | * @note will rearange value if necessary and will not be made smaller than min() |
---|
102 | */ |
---|
103 | void GLGuiSlider::setMax(float maximum) |
---|
104 | { |
---|
105 | if (maximum >= min()) |
---|
106 | { |
---|
107 | this->_maxValue = maximum; |
---|
108 | emit(rangeChanged(this->_minValue, this->_maxValue)); |
---|
109 | } |
---|
110 | if (this->value() > this->max()) |
---|
111 | this->setValue(this->max()); |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * @param minimum the minimum |
---|
116 | * @param maximum the maximum |
---|
117 | * |
---|
118 | * @see setMax |
---|
119 | * @see setMin |
---|
120 | */ |
---|
121 | void GLGuiSlider::setRange(float minimum, float maximum) |
---|
122 | { |
---|
123 | if (minimum < maximum) |
---|
124 | { |
---|
125 | this->_minValue = minimum; |
---|
126 | this->_maxValue = maximum; |
---|
127 | emit(rangeChanged(this->_minValue, this->_maxValue)); |
---|
128 | } |
---|
129 | if (this->value() < this->min()) |
---|
130 | this->setValue(this->min()); |
---|
131 | else if (this->value() > this->max()) |
---|
132 | this->setValue(this->max()); |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * @brief sets the stepSize |
---|
137 | */ |
---|
138 | void GLGuiSlider::setStep(float step) |
---|
139 | { |
---|
140 | this->_step = step; |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * @brief makes one step into the minus direction |
---|
145 | */ |
---|
146 | void GLGuiSlider::stepMinus() |
---|
147 | { |
---|
148 | this->setValue(value() - step()); |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * @brief makes one step into the minus direction |
---|
153 | */ |
---|
154 | void GLGuiSlider::stepPlus() |
---|
155 | { |
---|
156 | this->setValue(value() + step()); |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * @brief resizes the Slider, and through this Synchronizes the GUI-size. |
---|
161 | */ |
---|
162 | void GLGuiSlider::resize() |
---|
163 | { |
---|
164 | GLGuiWidget::resize(); |
---|
165 | // this->frontRect().setTopLeft(this->borderLeft(), this->getSizeY2D()/2.0 - 2.0); |
---|
166 | // this->frontRect().setSize(this->getSizeX2D() - borderLeft() - borderRight(), 4.0); |
---|
167 | this->_slider.setTopLeft(borderLeft(), this->getSizeY2D() / 2.0 -2.0); |
---|
168 | this->_slider.setSize(this->getSizeX2D() - borderLeft() - borderRight(), 4.0); |
---|
169 | this->_handle.setSize(this->_sliderWidth, this->getSizeY2D() - borderTop() - borderBottom()); |
---|
170 | } |
---|
171 | |
---|
172 | void GLGuiSlider::updateFrontColor() |
---|
173 | { |
---|
174 | |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * @brief handle the clicked event. |
---|
179 | * @param pos the position the Click occured (from the topleft corner out) |
---|
180 | */ |
---|
181 | void GLGuiSlider::clicking(const Vector2D& pos) |
---|
182 | { |
---|
183 | GLGuiWidget::clicking(pos); |
---|
184 | |
---|
185 | float sliderPosition = this->sliderPosition(); |
---|
186 | if (sliderPosition > pos.x + this->_sliderWidth) |
---|
187 | this->setValue(this->value() - this->step()); |
---|
188 | |
---|
189 | else if (sliderPosition < pos.x - this->_sliderWidth) |
---|
190 | this->setValue(this->value() + this->step()); |
---|
191 | else |
---|
192 | this->grabbed = true; |
---|
193 | } |
---|
194 | |
---|
195 | void GLGuiSlider::releasing(const Vector2D& pos) |
---|
196 | { |
---|
197 | GLGuiWidget::releasing(pos); |
---|
198 | this->grabbed = false; |
---|
199 | } |
---|
200 | |
---|
201 | void GLGuiSlider::removedFocus() |
---|
202 | { |
---|
203 | GLGuiWidget::removedFocus(); |
---|
204 | this->grabbed = false; |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * @returns the current SliderPosition calculated from the current value and the Silders' size. |
---|
209 | */ |
---|
210 | float GLGuiSlider::sliderPosition() const |
---|
211 | { |
---|
212 | return (this->_value - this->_minValue)/( this->_maxValue - this->_minValue) * |
---|
213 | (this->getSizeX2D() - (borderLeft() + borderRight() + 2.0*_sliderWidth)) + |
---|
214 | (borderLeft() +_sliderWidth); |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | * @param position the position relative from the left border. |
---|
219 | * @returns the Value at the given position. |
---|
220 | */ |
---|
221 | float GLGuiSlider::sliderValue(float position) const |
---|
222 | { |
---|
223 | return (position - (borderLeft()+_sliderWidth)) / (this->getSizeX2D() - (borderLeft() + borderRight() + 2.0*_sliderWidth)) |
---|
224 | *( this->_maxValue - this->_minValue) + |
---|
225 | this->_minValue ; |
---|
226 | } |
---|
227 | |
---|
228 | void GLGuiSlider::tick(float dt) |
---|
229 | { |
---|
230 | GLGuiWidget::tick(dt); |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * @brief draws the GLGuiSlider |
---|
235 | */ |
---|
236 | void GLGuiSlider::draw() const |
---|
237 | { |
---|
238 | this->beginDraw(); |
---|
239 | GLGuiWidget::draw(); |
---|
240 | |
---|
241 | glColor4fv(&this->foregroundColor()[0]); |
---|
242 | this->drawRect(this->_slider); |
---|
243 | this->drawRect(this->_handle); |
---|
244 | //this->drawRect(Rect2D(this->sliderPosition()-_sliderWidth/2.0, 0*this->borderTop(), _sliderWidth, this->getSizeY2D() - (borderTop() + borderBottom()) )); |
---|
245 | |
---|
246 | this->endDraw(); |
---|
247 | } |
---|
248 | |
---|
249 | |
---|
250 | bool GLGuiSlider::processEvent( const Event& event ) |
---|
251 | { |
---|
252 | if (this->grabbed && event.type == EV_MOUSE_MOTION) |
---|
253 | { |
---|
254 | this->setValue(sliderValue(GLGuiHandler::getInstance()->cursorPositionRel(this).x)); |
---|
255 | return true; |
---|
256 | } |
---|
257 | else if (event.bPressed) |
---|
258 | { |
---|
259 | if (event.type == SDLK_LEFT) |
---|
260 | { |
---|
261 | this->stepMinus(); |
---|
262 | return true; |
---|
263 | } |
---|
264 | else if (event.type == SDLK_RIGHT) |
---|
265 | { |
---|
266 | this->stepPlus(); |
---|
267 | return true; |
---|
268 | } |
---|
269 | } |
---|
270 | return false; |
---|
271 | } |
---|
272 | } |
---|