xoreos  0.0.5
slider.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cassert>
26 
27 #include "src/common/util.h"
28 #include "src/common/ustring.h"
29 
32 
34 
35 namespace Engines {
36 
37 namespace NWN {
38 
40  const Common::UString &model) :
41  ModelWidget(gui, tag, model), _position(0.0f), _steps(0), _state(0) {
42 
43  _model->setClickable(true);
44 
45  _width = getWidth();
46 
47  _thumb = _model->getNode("thumb");
48 
49  assert(_thumb);
50 
51  changePosition(0.0f);
52 }
53 
55 }
56 
57 void WidgetSlider::setPosition(float x, float y, float z) {
58  ModelWidget::setPosition(x, y, z);
59 }
60 
61 void WidgetSlider::setSteps(int steps) {
62  _steps = steps;
63 }
64 
66  return _state;
67 }
68 
69 void WidgetSlider::setState(int state) {
70  _state = state;
71 
72  changePosition(CLIP(((float) _state) / _steps, 0.0f, 1.0f));
73 }
74 
75 void WidgetSlider::mouseMove(uint8 state, float x, float y) {
76  if (isDisabled())
77  return;
78 
79  if (state != SDL_BUTTON_LMASK)
80  // We only care about moves with the left mouse button pressed
81  return;
82 
83  changedValue(x, y);
84 }
85 
86 void WidgetSlider::mouseDown(uint8 state, float x, float y) {
87  if (isDisabled())
88  return;
89 
90  if (state != SDL_BUTTON_LMASK)
91  return;
92 
93  changedValue(x, y);
94 }
95 
96 void WidgetSlider::changedValue(float x, float UNUSED(y)) {
97  float curX, curY, curZ;
98  getPosition(curX, curY, curZ);
99 
100  float pX = CLIP(x - curX, 0.0f, _width) / _width;
101  int state = roundf(pX * _steps);
102 
103  if (state == _state)
104  // No change
105  return;
106 
107  _state = state;
108 
109  if (_steps == 0) {
110  changePosition(0.0f);
111  return;
112  }
113 
114  changePosition(((float) _state) / _steps);
115 
116  setActive(true);
117 }
118 
119 void WidgetSlider::changePosition(float value) {
120  value = (value * _width) - (_thumb->getWidth() / 2.0f);
121 
122  _thumb->move(-_position + value, 0.0f, 0.0f);
123 
124  _position = value;
125 }
126 
127 } // End of namespace NWN
128 
129 } // End of namespace Engines
A class holding an UTF-8 string.
Definition: ustring.h:48
A NWN slider widget.
uint8_t uint8
Definition: types.h:200
void setActive(bool active)
The widget&#39;s active state.
Definition: widget.cpp:268
virtual void getPosition(float &x, float &y, float &z) const
Get the widget&#39;s position.
Definition: widget.cpp:140
void changePosition(float value)
Definition: slider.cpp:119
A GUI.
Definition: gui.h:43
void setClickable(bool clickable)
Set the object&#39;s clickable state.
Definition: renderable.cpp:94
#define UNUSED(x)
Definition: system.h:170
Utility templates and functions.
float getWidth() const
Get the width of the node&#39;s bounding box.
Definition: modelnode.cpp:160
A node within a 3D model.
A 3D model of an object.
void setSteps(int steps)
Definition: slider.cpp:61
void mouseDown(uint8 state, float x, float y)
A mouse button was pressed on the widget.
Definition: slider.cpp:86
WidgetSlider(::Engines::GUI &gui, const Common::UString &tag, const Common::UString &model)
Definition: slider.cpp:39
Graphics::Aurora::Model * _model
Definition: modelwidget.h:64
Graphics::Aurora::ModelNode * _thumb
Definition: slider.h:65
void mouseMove(uint8 state, float x, float y)
The mouse was moved over the widget.
Definition: slider.cpp:75
Unicode string handling.
void changedValue(float x, float y)
Definition: slider.cpp:96
void setPosition(float x, float y, float z)
Set the widget&#39;s position.
Definition: modelwidget.cpp:71
A NWN model widget.
Definition: modelwidget.h:45
bool isDisabled() const
Is the widget disabled?
Definition: widget.cpp:63
void move(float x, float y, float z)
Move the node, relative to its current position.
Definition: modelnode.cpp:251
void setState(int state)
Definition: slider.cpp:69
float getWidth() const
Get the widget&#39;s width.
Definition: modelwidget.cpp:78
T CLIP(T v, T amin, T amax)
Definition: util.h:72
ModelNode * getNode(const Common::UString &node)
Get the specified node, from the current state.
Definition: model.cpp:379
void setPosition(float x, float y, float z)
Set the widget&#39;s position.
Definition: slider.cpp:57