[4744] | 1 | /* |
---|
[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[5360] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[5360] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GUI |
---|
[1853] | 17 | |
---|
[5365] | 18 | #include "glgui_slider.h" |
---|
[8035] | 19 | #include "event_def.h" |
---|
[1853] | 20 | |
---|
[8035] | 21 | #include "glgui_handler.h" |
---|
| 22 | |
---|
[7779] | 23 | namespace OrxGui |
---|
[3365] | 24 | { |
---|
[4320] | 25 | |
---|
[7779] | 26 | /** |
---|
[8035] | 27 | * @brief standard constructor |
---|
| 28 | */ |
---|
[7779] | 29 | GLGuiSlider::GLGuiSlider () |
---|
| 30 | { |
---|
| 31 | this->init(); |
---|
[1853] | 32 | |
---|
[7779] | 33 | } |
---|
[1853] | 34 | |
---|
[5360] | 35 | |
---|
[7779] | 36 | /** |
---|
[8035] | 37 | * @brief standard deconstructor |
---|
| 38 | */ |
---|
[7779] | 39 | GLGuiSlider::~GLGuiSlider() |
---|
[8035] | 40 | {} |
---|
[5360] | 41 | |
---|
[7779] | 42 | /** |
---|
[8035] | 43 | * @brief initializes the GUI-element |
---|
[7779] | 44 | */ |
---|
| 45 | void GLGuiSlider::init() |
---|
| 46 | { |
---|
[8035] | 47 | |
---|
[7779] | 48 | this->setClassID(CL_GLGUI_SLIDER, "GLGuiSlider"); |
---|
[5360] | 49 | |
---|
[8717] | 50 | this->setClickable(true); |
---|
| 51 | this->setFocusable(true); |
---|
| 52 | this->setSelectable(true); |
---|
[8035] | 53 | |
---|
| 54 | this->_value = 0.0; |
---|
| 55 | this->_minValue = 0.0; |
---|
| 56 | this->_maxValue = 1.0; |
---|
| 57 | this->_step = 0.1; |
---|
| 58 | this->_sliderWidth = 5.0; |
---|
| 59 | this->grabbed = false; |
---|
| 60 | |
---|
| 61 | this->setSize2D(100, 30); |
---|
| 62 | this->resize(); |
---|
[7779] | 63 | } |
---|
[5360] | 64 | |
---|
[7779] | 65 | /** |
---|
[8035] | 66 | * @param value the new Value. |
---|
| 67 | * @note will automatically be set between max() and min() |
---|
[7779] | 68 | */ |
---|
[8035] | 69 | void GLGuiSlider::setValue(float value) |
---|
| 70 | { |
---|
| 71 | if (value < this->min()) |
---|
| 72 | this->_value = min(); |
---|
| 73 | else if (value > max()) |
---|
| 74 | this->_value = max(); |
---|
| 75 | else |
---|
| 76 | this->_value = value; |
---|
[8619] | 77 | this->_handle.setCenter(this->sliderPosition(), borderTop() + (this->getSizeY2D() - borderTop() - borderBottom()) / 2.0); |
---|
[8448] | 78 | |
---|
[9406] | 79 | valueChanged.emit(this->_value); |
---|
[8035] | 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * @param minimum the minumum of the range. |
---|
| 84 | * |
---|
| 85 | * @note will rearange value if necessary and will not be made bigger than max() |
---|
| 86 | */ |
---|
| 87 | void GLGuiSlider::setMin(float minimum) |
---|
| 88 | { |
---|
| 89 | if (minimum <= max()) |
---|
| 90 | { |
---|
| 91 | this->_minValue = minimum; |
---|
[9406] | 92 | rangeChanged.emit(this->_minValue, this->_maxValue); |
---|
[8035] | 93 | } |
---|
| 94 | if (this->value() < this->min()) |
---|
| 95 | this->setValue(this->min()); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * @param maximum the maximum of the range. |
---|
| 101 | * |
---|
| 102 | * @note will rearange value if necessary and will not be made smaller than min() |
---|
| 103 | */ |
---|
| 104 | void GLGuiSlider::setMax(float maximum) |
---|
| 105 | { |
---|
| 106 | if (maximum >= min()) |
---|
| 107 | { |
---|
| 108 | this->_maxValue = maximum; |
---|
[9406] | 109 | rangeChanged.emit(this->_minValue, this->_maxValue); |
---|
[8035] | 110 | } |
---|
| 111 | if (this->value() > this->max()) |
---|
| 112 | this->setValue(this->max()); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /** |
---|
| 116 | * @param minimum the minimum |
---|
| 117 | * @param maximum the maximum |
---|
| 118 | * |
---|
| 119 | * @see setMax |
---|
| 120 | * @see setMin |
---|
| 121 | */ |
---|
| 122 | void GLGuiSlider::setRange(float minimum, float maximum) |
---|
| 123 | { |
---|
| 124 | if (minimum < maximum) |
---|
| 125 | { |
---|
| 126 | this->_minValue = minimum; |
---|
| 127 | this->_maxValue = maximum; |
---|
[9406] | 128 | rangeChanged.emit(this->_minValue, this->_maxValue); |
---|
[8035] | 129 | } |
---|
| 130 | if (this->value() < this->min()) |
---|
| 131 | this->setValue(this->min()); |
---|
| 132 | else if (this->value() > this->max()) |
---|
| 133 | this->setValue(this->max()); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
| 137 | * @brief sets the stepSize |
---|
| 138 | */ |
---|
| 139 | void GLGuiSlider::setStep(float step) |
---|
| 140 | { |
---|
| 141 | this->_step = step; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /** |
---|
| 145 | * @brief makes one step into the minus direction |
---|
| 146 | */ |
---|
| 147 | void GLGuiSlider::stepMinus() |
---|
| 148 | { |
---|
| 149 | this->setValue(value() - step()); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | /** |
---|
| 153 | * @brief makes one step into the minus direction |
---|
| 154 | */ |
---|
| 155 | void GLGuiSlider::stepPlus() |
---|
| 156 | { |
---|
| 157 | this->setValue(value() + step()); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | * @brief resizes the Slider, and through this Synchronizes the GUI-size. |
---|
| 162 | */ |
---|
| 163 | void GLGuiSlider::resize() |
---|
| 164 | { |
---|
| 165 | GLGuiWidget::resize(); |
---|
[8448] | 166 | // this->frontRect().setTopLeft(this->borderLeft(), this->getSizeY2D()/2.0 - 2.0); |
---|
| 167 | // this->frontRect().setSize(this->getSizeX2D() - borderLeft() - borderRight(), 4.0); |
---|
[8619] | 168 | this->_slider.setTopLeft(borderLeft(), this->getSizeY2D() / 2.0 -2.0); |
---|
[8448] | 169 | this->_slider.setSize(this->getSizeX2D() - borderLeft() - borderRight(), 4.0); |
---|
| 170 | this->_handle.setSize(this->_sliderWidth, this->getSizeY2D() - borderTop() - borderBottom()); |
---|
[8035] | 171 | } |
---|
| 172 | |
---|
[8448] | 173 | void GLGuiSlider::updateFrontColor() |
---|
| 174 | { |
---|
| 175 | |
---|
| 176 | } |
---|
| 177 | |
---|
[8035] | 178 | /** |
---|
| 179 | * @brief handle the clicked event. |
---|
| 180 | * @param pos the position the Click occured (from the topleft corner out) |
---|
| 181 | */ |
---|
| 182 | void GLGuiSlider::clicking(const Vector2D& pos) |
---|
| 183 | { |
---|
| 184 | GLGuiWidget::clicking(pos); |
---|
| 185 | |
---|
| 186 | float sliderPosition = this->sliderPosition(); |
---|
| 187 | if (sliderPosition > pos.x + this->_sliderWidth) |
---|
| 188 | this->setValue(this->value() - this->step()); |
---|
| 189 | |
---|
| 190 | else if (sliderPosition < pos.x - this->_sliderWidth) |
---|
| 191 | this->setValue(this->value() + this->step()); |
---|
| 192 | else |
---|
| 193 | this->grabbed = true; |
---|
| 194 | } |
---|
| 195 | |
---|
[8717] | 196 | void GLGuiSlider::releasing(const Vector2D& pos, bool focused) |
---|
[8035] | 197 | { |
---|
[8717] | 198 | GLGuiWidget::releasing(pos, focused); |
---|
[8035] | 199 | this->grabbed = false; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | void GLGuiSlider::removedFocus() |
---|
| 203 | { |
---|
| 204 | GLGuiWidget::removedFocus(); |
---|
| 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) * |
---|
[8619] | 213 | (this->getSizeX2D() - (borderLeft() + borderRight() + 2.0*_sliderWidth)) + |
---|
| 214 | (borderLeft() +_sliderWidth); |
---|
[8035] | 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 | { |
---|
[8115] | 223 | return (position - (borderLeft()+_sliderWidth)) / (this->getSizeX2D() - (borderLeft() + borderRight() + 2.0*_sliderWidth)) |
---|
[8035] | 224 | *( this->_maxValue - this->_minValue) + |
---|
| 225 | this->_minValue ; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | void GLGuiSlider::tick(float dt) |
---|
| 229 | { |
---|
[8448] | 230 | GLGuiWidget::tick(dt); |
---|
[8035] | 231 | } |
---|
| 232 | |
---|
| 233 | /** |
---|
| 234 | * @brief draws the GLGuiSlider |
---|
| 235 | */ |
---|
[7919] | 236 | void GLGuiSlider::draw() const |
---|
[7779] | 237 | { |
---|
[8035] | 238 | this->beginDraw(); |
---|
| 239 | GLGuiWidget::draw(); |
---|
| 240 | |
---|
[8619] | 241 | glColor4fv(&this->foregroundColor()[0]); |
---|
[8448] | 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()) )); |
---|
[8035] | 245 | |
---|
| 246 | this->endDraw(); |
---|
[7779] | 247 | } |
---|
[8035] | 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 | } |
---|
[5360] | 272 | } |
---|