| 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 | emit(valueChanged(this->_value)); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | /** |
|---|
| 80 | * @param minimum the minumum of the range. |
|---|
| 81 | * |
|---|
| 82 | * @note will rearange value if necessary and will not be made bigger than max() |
|---|
| 83 | */ |
|---|
| 84 | void GLGuiSlider::setMin(float minimum) |
|---|
| 85 | { |
|---|
| 86 | if (minimum <= max()) |
|---|
| 87 | { |
|---|
| 88 | this->_minValue = minimum; |
|---|
| 89 | emit(rangeChanged(this->_minValue, this->_maxValue)); |
|---|
| 90 | } |
|---|
| 91 | if (this->value() < this->min()) |
|---|
| 92 | this->setValue(this->min()); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * @param maximum the maximum of the range. |
|---|
| 98 | * |
|---|
| 99 | * @note will rearange value if necessary and will not be made smaller than min() |
|---|
| 100 | */ |
|---|
| 101 | void GLGuiSlider::setMax(float maximum) |
|---|
| 102 | { |
|---|
| 103 | if (maximum >= min()) |
|---|
| 104 | { |
|---|
| 105 | this->_maxValue = maximum; |
|---|
| 106 | emit(rangeChanged(this->_minValue, this->_maxValue)); |
|---|
| 107 | } |
|---|
| 108 | if (this->value() > this->max()) |
|---|
| 109 | this->setValue(this->max()); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /** |
|---|
| 113 | * @param minimum the minimum |
|---|
| 114 | * @param maximum the maximum |
|---|
| 115 | * |
|---|
| 116 | * @see setMax |
|---|
| 117 | * @see setMin |
|---|
| 118 | */ |
|---|
| 119 | void GLGuiSlider::setRange(float minimum, float maximum) |
|---|
| 120 | { |
|---|
| 121 | if (minimum < maximum) |
|---|
| 122 | { |
|---|
| 123 | this->_minValue = minimum; |
|---|
| 124 | this->_maxValue = maximum; |
|---|
| 125 | emit(rangeChanged(this->_minValue, this->_maxValue)); |
|---|
| 126 | } |
|---|
| 127 | if (this->value() < this->min()) |
|---|
| 128 | this->setValue(this->min()); |
|---|
| 129 | else if (this->value() > this->max()) |
|---|
| 130 | this->setValue(this->max()); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | /** |
|---|
| 134 | * @brief sets the stepSize |
|---|
| 135 | */ |
|---|
| 136 | void GLGuiSlider::setStep(float step) |
|---|
| 137 | { |
|---|
| 138 | this->_step = step; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | /** |
|---|
| 142 | * @brief makes one step into the minus direction |
|---|
| 143 | */ |
|---|
| 144 | void GLGuiSlider::stepMinus() |
|---|
| 145 | { |
|---|
| 146 | this->setValue(value() - step()); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /** |
|---|
| 150 | * @brief makes one step into the minus direction |
|---|
| 151 | */ |
|---|
| 152 | void GLGuiSlider::stepPlus() |
|---|
| 153 | { |
|---|
| 154 | this->setValue(value() + step()); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | /** |
|---|
| 158 | * @brief resizes the Slider, and through this Synchronizes the GUI-size. |
|---|
| 159 | */ |
|---|
| 160 | void GLGuiSlider::resize() |
|---|
| 161 | { |
|---|
| 162 | GLGuiWidget::resize(); |
|---|
| 163 | this->frontRect().setTopLeft(this->borderLeft(), this->getSizeY2D()/2.0 - 2.0); |
|---|
| 164 | this->frontRect().setSize(this->getSizeX2D() - borderLeft() - borderRight(), 4.0); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | /** |
|---|
| 168 | * @brief handle the clicked event. |
|---|
| 169 | * @param pos the position the Click occured (from the topleft corner out) |
|---|
| 170 | */ |
|---|
| 171 | void GLGuiSlider::clicking(const Vector2D& pos) |
|---|
| 172 | { |
|---|
| 173 | GLGuiWidget::clicking(pos); |
|---|
| 174 | |
|---|
| 175 | float sliderPosition = this->sliderPosition(); |
|---|
| 176 | if (sliderPosition > pos.x + this->_sliderWidth) |
|---|
| 177 | this->setValue(this->value() - this->step()); |
|---|
| 178 | |
|---|
| 179 | else if (sliderPosition < pos.x - this->_sliderWidth) |
|---|
| 180 | this->setValue(this->value() + this->step()); |
|---|
| 181 | else |
|---|
| 182 | this->grabbed = true; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | void GLGuiSlider::releasing(const Vector2D& pos) |
|---|
| 186 | { |
|---|
| 187 | GLGuiWidget::releasing(pos); |
|---|
| 188 | this->grabbed = false; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | void GLGuiSlider::removedFocus() |
|---|
| 192 | { |
|---|
| 193 | GLGuiWidget::removedFocus(); |
|---|
| 194 | this->grabbed = false; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | /** |
|---|
| 198 | * @returns the current SliderPosition calculated from the current value and the Silders' size. |
|---|
| 199 | */ |
|---|
| 200 | float GLGuiSlider::sliderPosition() const |
|---|
| 201 | { |
|---|
| 202 | return (this->_value - this->_minValue)/( this->_maxValue - this->_minValue) * |
|---|
| 203 | (this->getSizeX2D() - (borderLeft() + borderRight() + 2.0*_sliderWidth)) + |
|---|
| 204 | (borderLeft() +_sliderWidth); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | /** |
|---|
| 208 | * @param position the position relative from the left border. |
|---|
| 209 | * @returns the Value at the given position. |
|---|
| 210 | */ |
|---|
| 211 | float GLGuiSlider::sliderValue(float position) const |
|---|
| 212 | { |
|---|
| 213 | return (position - (borderLeft()+_sliderWidth)) / (this->getSizeX2D() - (borderLeft() + borderRight() + 2.0*_sliderWidth)) |
|---|
| 214 | *( this->_maxValue - this->_minValue) + |
|---|
| 215 | this->_minValue ; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | void GLGuiSlider::tick(float dt) |
|---|
| 219 | { |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | /** |
|---|
| 223 | * @brief draws the GLGuiSlider |
|---|
| 224 | */ |
|---|
| 225 | void GLGuiSlider::draw() const |
|---|
| 226 | { |
|---|
| 227 | this->beginDraw(); |
|---|
| 228 | GLGuiWidget::draw(); |
|---|
| 229 | |
|---|
| 230 | this->frontMaterial().select(); |
|---|
| 231 | this->drawRect(this->frontRect()); |
|---|
| 232 | |
|---|
| 233 | this->drawRect(Rect2D(this->sliderPosition()-_sliderWidth/2.0, 0*this->borderTop(), _sliderWidth, this->getSizeY2D() - (borderTop() + borderBottom()) +20)); |
|---|
| 234 | |
|---|
| 235 | this->endDraw(); |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | bool GLGuiSlider::processEvent( const Event& event ) |
|---|
| 240 | { |
|---|
| 241 | if (this->grabbed && event.type == EV_MOUSE_MOTION) |
|---|
| 242 | { |
|---|
| 243 | this->setValue(sliderValue(GLGuiHandler::getInstance()->cursorPositionRel(this).x)); |
|---|
| 244 | return true; |
|---|
| 245 | } |
|---|
| 246 | else if (event.bPressed) |
|---|
| 247 | { |
|---|
| 248 | if (event.type == SDLK_LEFT) |
|---|
| 249 | { |
|---|
| 250 | this->stepMinus(); |
|---|
| 251 | return true; |
|---|
| 252 | } |
|---|
| 253 | else if (event.type == SDLK_RIGHT) |
|---|
| 254 | { |
|---|
| 255 | this->stepPlus(); |
|---|
| 256 | return true; |
|---|
| 257 | } |
|---|
| 258 | } |
|---|
| 259 | return false; |
|---|
| 260 | } |
|---|
| 261 | } |
|---|