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 | * standard constructor |
---|
28 | */ |
---|
29 | GLGuiSlider::GLGuiSlider () |
---|
30 | { |
---|
31 | this->init(); |
---|
32 | |
---|
33 | } |
---|
34 | |
---|
35 | |
---|
36 | /** |
---|
37 | * standard deconstructor |
---|
38 | */ |
---|
39 | GLGuiSlider::~GLGuiSlider() |
---|
40 | {} |
---|
41 | |
---|
42 | /** |
---|
43 | * 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 | void GLGuiSlider::setValue(float value) |
---|
66 | { |
---|
67 | if (value < this->min()) |
---|
68 | this->_value = min(); |
---|
69 | else if (value > max()) |
---|
70 | this->_value = max(); |
---|
71 | else |
---|
72 | this->_value = value; |
---|
73 | printf("VALUE %f\n", _value); |
---|
74 | } |
---|
75 | |
---|
76 | void GLGuiSlider::setMin(float minimum) |
---|
77 | { |
---|
78 | if (minimum <= max()) |
---|
79 | this->_minValue = minimum; |
---|
80 | |
---|
81 | if (this->value() < minimum) |
---|
82 | this->_value = minimum; |
---|
83 | } |
---|
84 | |
---|
85 | void GLGuiSlider::setMax(float maximum) |
---|
86 | { |
---|
87 | if (maximum >= min()) |
---|
88 | this->_maxValue = maximum; |
---|
89 | |
---|
90 | if (this->value() > maximum) |
---|
91 | this->_value = maximum; |
---|
92 | } |
---|
93 | |
---|
94 | void GLGuiSlider::setRange(float minimum, float maximum) |
---|
95 | { |
---|
96 | if (minimum < maximum) |
---|
97 | { |
---|
98 | this->_minValue = minimum; |
---|
99 | this->_maxValue = maximum; |
---|
100 | } |
---|
101 | if (this->value() < minimum) |
---|
102 | this->_value = minimum; |
---|
103 | else if (this->value() > maximum) |
---|
104 | this->_value = maximum; |
---|
105 | } |
---|
106 | |
---|
107 | void GLGuiSlider::setStep(float step) |
---|
108 | { |
---|
109 | this->_step = step; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | void GLGuiSlider::resize() |
---|
114 | { |
---|
115 | GLGuiWidget::resize(); |
---|
116 | this->frontRect().setTopLeft(5, this->getSizeY2D()/2.0 - borderSize()); |
---|
117 | this->frontRect().setSize(this->getSizeX2D() - 10.0, borderSize()); |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | void GLGuiSlider::clicked(const Vector2D& pos) |
---|
122 | { |
---|
123 | GLGuiWidget::clicked(pos); |
---|
124 | |
---|
125 | float sliderPosition = this->sliderPosition(); |
---|
126 | if (sliderPosition > pos.x + this->_sliderWidth) |
---|
127 | this->setValue(this->value() - this->step()); |
---|
128 | |
---|
129 | else if (sliderPosition < pos.x - this->_sliderWidth) |
---|
130 | this->setValue(this->value() + this->step()); |
---|
131 | else |
---|
132 | this->grabbed = true; |
---|
133 | |
---|
134 | |
---|
135 | |
---|
136 | printf("clicked at position: "), pos.debug(); |
---|
137 | } |
---|
138 | |
---|
139 | void GLGuiSlider::released(const Vector2D& pos) |
---|
140 | { |
---|
141 | GLGuiWidget::released(pos); |
---|
142 | this->grabbed = false; |
---|
143 | } |
---|
144 | |
---|
145 | void GLGuiSlider::removedFocus() |
---|
146 | { |
---|
147 | GLGuiWidget::removedFocus(); |
---|
148 | this->grabbed = false; |
---|
149 | } |
---|
150 | |
---|
151 | float GLGuiSlider::sliderPosition() const |
---|
152 | { |
---|
153 | return (this->_value - this->_minValue)/( this->_maxValue - this->_minValue) * |
---|
154 | (this->getSizeX2D() - 2.0*(borderSize() + _sliderWidth)) + |
---|
155 | (borderSize() +_sliderWidth); |
---|
156 | } |
---|
157 | |
---|
158 | float GLGuiSlider::sliderValue(float position) const |
---|
159 | { |
---|
160 | return (position - (borderSize()+_sliderWidth)) / (this->getSizeX2D() - 2.0*(borderSize() + _sliderWidth)) |
---|
161 | *( this->_maxValue - this->_minValue) + |
---|
162 | this->_minValue ; |
---|
163 | } |
---|
164 | |
---|
165 | /* virtual void GLGuiSlider::tick(float dt) |
---|
166 | { |
---|
167 | if (this->grabbed) |
---|
168 | { |
---|
169 | this->setValue( 1); |
---|
170 | } |
---|
171 | }*/ |
---|
172 | |
---|
173 | void GLGuiSlider::tick(float dt) |
---|
174 | { |
---|
175 | /* this->setValue(this->value() + dt); |
---|
176 | if (this->value() >= this->max()) |
---|
177 | this->setValue(this->min()); |
---|
178 | */ |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * @brief draws the GLGuiSlider |
---|
183 | */ |
---|
184 | void GLGuiSlider::draw() const |
---|
185 | { |
---|
186 | this->beginDraw(); |
---|
187 | GLGuiWidget::draw(); |
---|
188 | |
---|
189 | this->frontMaterial().select(); |
---|
190 | this->drawRect(this->frontRect()); |
---|
191 | |
---|
192 | this->drawRect(Rect2D(this->sliderPosition()-_sliderWidth/2.0, borderSize(), _sliderWidth, this->getSizeY2D() - 2 * borderSize())); |
---|
193 | |
---|
194 | this->endDraw(); |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | bool GLGuiSlider::processEvent( const Event& event ) |
---|
199 | { |
---|
200 | if (this->grabbed && event.type == EV_MOUSE_MOTION) |
---|
201 | { |
---|
202 | this->setValue(sliderValue(GLGuiHandler::getInstance()->cursorPositionRel(this).x)); |
---|
203 | return true; |
---|
204 | } |
---|
205 | return false; |
---|
206 | } |
---|
207 | } |
---|