-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlider.cpp
More file actions
118 lines (92 loc) · 2.64 KB
/
Slider.cpp
File metadata and controls
118 lines (92 loc) · 2.64 KB
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
#include <iostream>
#include <cmath>
#include "Slider.hpp"
using namespace gui;
Slider::Slider()
: mMin(0),
mMax(100),
mValue(0),
mFinalSprite(),
mRenderTexture(),
mOldMouseState(false),
mHasStartedDrag(false),
mDragValue(0),
mEnable(true),
mOldDrawingValue(-1)
{
}
void Slider::setLayout(const sf::IntRect &layout)
{
mFinalSprite.setPosition(layout.left, layout.top);
mRenderTexture.create(layout.width, layout.height);
mFinalSprite.setTexture(mRenderTexture.getTexture());
}
void Slider::setMin(float min)
{
mMin = std::min(min, mMax-.1f);
setValue(mValue);
}
void Slider::setMax(float max)
{
mMax = std::max(mMin+.1f, max);
setValue(mValue);
}
void Slider::setValue(float value)
{
mValue = std::max(mMin, std::min(value, mMax));
}
void Slider::setEnable(bool enable)
{
mEnable = enable;
}
bool Slider::update(sf::Vector2f mousePosition, bool mouseButtonPressed, float &userValue)
{
bool hasUserValue(false);
const sf::Vector2f finalPosition(mFinalSprite.getGlobalBounds().left,
mFinalSprite.getGlobalBounds().top);
const sf::Vector2f finalSize(mFinalSprite.getGlobalBounds().width,
mFinalSprite.getGlobalBounds().height);
bool hasFocus = mFinalSprite.getGlobalBounds().contains(mousePosition);
// test if user has enter new value
if (mEnable) {
if (mouseButtonPressed && !mOldMouseState) {// user pressed
if (hasFocus)
mHasStartedDrag = true;
} else if (!mouseButtonPressed && mOldMouseState) { // user released
if (mHasStartedDrag) {
mHasStartedDrag = false;
hasUserValue = true;
}
}
mOldMouseState = mouseButtonPressed;
// compute user value
if (mHasStartedDrag || hasUserValue) {
float relativeMousePos = mousePosition.x - finalPosition.x;
relativeMousePos = std::max(0.f, std::min(finalSize.x, relativeMousePos));
mDragValue = relativeMousePos / finalSize.x
* (mMax-mMin)
+ mMin;
// return user value
//if (hasUserValue)
userValue = mDragValue;
//std::cout << userValue << std::endl;
}
}
float drawingValue = (mHasStartedDrag) ? mDragValue : mValue;
if (drawingValue != mOldDrawingValue)
{
mOldDrawingValue = drawingValue;
float horizontalPos = (drawingValue - mMin) / (mMax-mMin) *
mFinalSprite.getGlobalBounds().width ;
mRenderTexture.clear(sf::Color(240,240,240));
sf::RectangleShape rect(sf::Vector2f(horizontalPos, mFinalSprite.getGlobalBounds().height));
rect.setFillColor(sf::Color::Black);
mRenderTexture.draw(rect);
mRenderTexture.display();
}
return hasUserValue;
}
void Slider::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
target.draw(mFinalSprite);
}