Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/gui/gl/glgui_slider.cc @ 10291

Last change on this file since 10291 was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File size: 6.4 KB
Line 
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
23namespace OrxGui
24{
25  ObjectListDefinition(GLGuiSlider);
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->registerObject(this, GLGuiSlider::_objectList);
49
50    this->setClickable(true);
51    this->setFocusable(true);
52    this->setSelectable(true);
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();
63  }
64
65  /**
66   * @param value the new Value.
67   * @note will automatically be set between max() and min()
68   */
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;
77    this->_handle.setCenter(this->sliderPosition(), borderTop() + (this->getSizeY2D() - borderTop() - borderBottom()) / 2.0);
78
79    valueChanged.emit(this->_value);
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;
92      rangeChanged.emit(this->_minValue, this->_maxValue);
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;
109      rangeChanged.emit(this->_minValue, this->_maxValue);
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;
128      rangeChanged.emit(this->_minValue, this->_maxValue);
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();
166//    this->frontRect().setTopLeft(this->borderLeft(), this->getSizeY2D()/2.0 - 2.0);
167//    this->frontRect().setSize(this->getSizeX2D() - borderLeft() - borderRight(), 4.0);
168    this->_slider.setTopLeft(borderLeft(), this->getSizeY2D() / 2.0 -2.0);
169    this->_slider.setSize(this->getSizeX2D() - borderLeft() - borderRight(), 4.0);
170    this->_handle.setSize(this->_sliderWidth, this->getSizeY2D() - borderTop() - borderBottom());
171  }
172
173  void GLGuiSlider::updateFrontColor()
174  {
175
176  }
177
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
196  void GLGuiSlider::releasing(const Vector2D& pos, bool focused)
197  {
198    GLGuiWidget::releasing(pos, focused);
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) *
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}
Note: See TracBrowser for help on using the repository browser.