/
Slidebar.cpp
181 lines (151 loc) · 5.22 KB
/
Slidebar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "Slidebar.h"
#include "TextBox.h"
#include "InputBox.h"
#include "WindowHandler.h"
Slidebar::Slidebar(WindowHandler* handler, WindowID id, string display, int x, int y, int width, int height, float value, float minValue, float maxValue)
: Window(handler, id, x, y, width, height)
{
mMinValue = minValue;
mMaxValue = maxValue;
mMovingSlider = false;
mSliderWidth = 40;
// no invalid values
if(value > mMaxValue)
mValue = mMaxValue;
else if(value < mMinValue)
mValue = mMinValue;
else
mValue = value;
// coordinates of the slider background
mSlideBackground.left = getX() - 80;
mSlideBackground.right = getX() + 80;
mSlideBackground.top = getY() - 10;
mSlideBackground.bottom = getY() + 10;
// window controls
mTextName = new TextBox(handler, SLIDER_TEXT, display, mSlideBackground.left + 60 - handler->getRect().left,
getY()- handler->getRect().top-25, 120, 20);
mInputBox = new InputBox(handler, SLIDER_INPUT, TYPE_TEXT, mSlideBackground.right + 35 - handler->getRect().left,
getY()- handler->getRect().top, 50, 20, 4);
// sliders position, X% of the span * slidewidth + offset
mSliderPos = float((mValue / (maxValue - minValue)) * (mSlideBackground.right - mSlideBackground.left - mSliderWidth) + mSliderWidth/2);
// substract the minValue, 100-200 range is 0-100 on the slidebar
mSliderPos -= (minValue / (maxValue - minValue)) * (mSlideBackground.right - mSlideBackground.left - mSliderWidth);
// sliderects coordinates
mSlider.left = mSlideBackground.left + mSliderPos - mSliderWidth/2;
mSlider.right = mSlideBackground.left + mSliderPos + mSliderWidth/2;
mSlider.top = getY() - 10;
mSlider.bottom = getY() + 10;
// the inputbox value, ie the sliders real value
mInputBox->setValue(mValue);
}
Slidebar::~Slidebar()
{
// dtor
}
void Slidebar::draw(void)
{
// background of the slider
gGraphics->BlitRect(mSlideBackground, D3DCOLOR_ARGB(255, 255, 0, 0));
// the slider
gGraphics->BlitRect(mSlider, D3DCOLOR_ARGB(255, 0, 255, 0));
}
void Slidebar::update(double dt)
{
static char temp[10]; // ugly hack
if(gDInput->mouseButtonDown(LEFTBUTTON))
{
if(gDInput->cursorInsideRect(mSlider) || mMovingSlider)
{
if(gDInput->mouseDX() < 0)
{
// slider has a valid position
if(mSliderPos > 0 + mSliderWidth/2) {
moveSlider(gDInput->mouseDX());
// set the value, dx in 0-1 range * value span
mValue += (mMaxValue - mMinValue)*((gDInput->mouseDX()) / (mSlideBackground.right - mSlideBackground.left - mSliderWidth)); // - mSliderWidth på vänster sidan?:O
callback(getID(), WindowMessage(mValue));
// stop invalid movement
if(mSliderPos <= 0 + mSliderWidth/2) {
stopSlider(LEFT);
}
sprintf(temp, "%.2f", mValue); // hack >_>
string temp2 = string(temp);
mInputBox->setValue(temp2);
}
else {
stopSlider(LEFT);
}
}
else if(gDInput->mouseDX() > 0)
{
if(mSliderPos < (mSlideBackground.right - mSlideBackground.left - mSliderWidth/2)) {
moveSlider(gDInput->mouseDX());
// set the value, dx in 0-1 range * value span
mValue += (mMaxValue - mMinValue)*((gDInput->mouseDX()) / (mSlideBackground.right - mSlideBackground.left - mSliderWidth));
callback(getID(), WindowMessage(mValue));
// stop invalid movement
if(mSliderPos >= (mSlideBackground.right - mSlideBackground.left - mSliderWidth/2)) {
stopSlider(RIGHT);
}
sprintf(temp, "%.2f", mValue); // hack >_>
string temp2 = string(temp);
mInputBox->setValue(temp2);
}
else {
stopSlider(RIGHT);
}
}
// dont allow Y movement while sliding
gDInput->setCursorY(gDInput->getCursorY() - gDInput->mouseDY());
}
}
if(gDInput->mouseButtonReleased(LEFTBUTTON))
mMovingSlider = false;
}
// hack - needed?
bool Slidebar::pressed(int mx, int my)
{
if(gDInput->cursorInsideRect(mSlider))
mMovingSlider = true;
return true;
}
void Slidebar::moveSlider(float dx)
{
mSlider.left += dx;
mSlider.right += dx;
mSliderPos += dx;
}
void Slidebar::stopSlider(direction dir)
{
if(dir == LEFT)
{
mSlider.left = mSlideBackground.left;
mSlider.right = mSlideBackground.left + mSliderWidth;
gDInput->setCursorX(gDInput->getCursorX() - gDInput->mouseDX());
mSliderPos = mSliderWidth/2;
mValue = mMinValue;
}
else if(dir == RIGHT)
{
mSlider.left = mSlideBackground.right - mSliderWidth;
mSlider.right = mSlideBackground.right;
gDInput->setCursorX(gDInput->getCursorX() - gDInput->mouseDX());
mSliderPos = mSlideBackground.right - mSlideBackground.left - mSliderWidth/2;
mValue = mMaxValue;
}
}
void Slidebar::setValue(float value)
{
char temp[10];
mValue = value;
sprintf(temp, "%.2f", mValue); // hack >_>
string temp2 = string(temp);
mInputBox->setValue(temp2);
// sliders position, X% of the span * slidewidth + offset
mSliderPos = float((mValue / (mMaxValue - mMinValue)) * (mSlideBackground.right - mSlideBackground.left - mSliderWidth) + mSliderWidth/2);
// substract the minValue, 100-200 range is 0-100 on the slidebar
mSliderPos -= (mMinValue / (mMaxValue - mMinValue)) * (mSlideBackground.right - mSlideBackground.left - mSliderWidth);
// sliderects coordinates
mSlider.left = mSlideBackground.left + mSliderPos - mSliderWidth/2;
mSlider.right = mSlideBackground.left + mSliderPos + mSliderWidth/2;
}