seminar11: slider circle void methods

master
nihonium 2 years ago
parent f1e450cc9e
commit 8fc5c26a79
No known key found for this signature in database
GPG Key ID: 0251623741027CFC

@ -6,10 +6,7 @@
class Slider class Slider
{ {
private: private:
inline static const sf::Color defaultColor {sf::Color(190, 210, 190)}; const int kCharacterSize = 14;
inline static const sf::Color pressedColor {sf::Color(150, 170, 150)};
inline static const sf::Color textColor {sf::Color::Black};
inline static const int kCharacterSize = 14;
int mMinValue; int mMinValue;
int mMaxValue; int mMaxValue;
@ -20,10 +17,9 @@ private:
sf::RectangleShape mAxis; sf::RectangleShape mAxis;
sf::Text mText; sf::Text mText;
/* Is slider pressed at the moment */
bool mIsPressed = false; bool mIsPressed = false;
int onMousePressed(const sf::Event& event) void onMousePressed(const sf::Event& event)
{ {
if (event.mouseButton.button == sf::Mouse::Left) { if (event.mouseButton.button == sf::Mouse::Left) {
sf::Vector2f mousePosition = mRenderWindow.mapPixelToCoords({event.mouseButton.x, event.mouseButton.y}); sf::Vector2f mousePosition = mRenderWindow.mapPixelToCoords({event.mouseButton.x, event.mouseButton.y});
@ -42,13 +38,12 @@ private:
} }
} }
} }
return mMinValue + (mMaxValue - mMinValue) * mSliderPosition;
} }
int onMouseMove(const sf::Event& event) void onMouseMove(const sf::Event& event)
{ {
if (!mIsPressed) { if (!mIsPressed) {
return mMinValue + (mMaxValue - mMinValue) * mSliderPosition; return;
} }
sf::Vector2f mousePosition = mRenderWindow.mapPixelToCoords({event.mouseMove.x, event.mouseMove.y}); sf::Vector2f mousePosition = mRenderWindow.mapPixelToCoords({event.mouseMove.x, event.mouseMove.y});
if ((mousePosition.x >= mAxis.getPosition().x) && (mousePosition.x + mSlider.getSize().x <= mAxis.getPosition().x + mAxis.getSize().x)) { if ((mousePosition.x >= mAxis.getPosition().x) && (mousePosition.x + mSlider.getSize().x <= mAxis.getPosition().x + mAxis.getSize().x)) {
@ -63,7 +58,6 @@ private:
mSliderPosition = (mSlider.getPosition().x - mAxis.getPosition().x) / (mAxis.getSize().x - mSlider.getSize().x); mSliderPosition = (mSlider.getPosition().x - mAxis.getPosition().x) / (mAxis.getSize().x - mSlider.getSize().x);
return mMinValue + (mMaxValue - mMinValue) * mSliderPosition;
} }
public: public:
@ -76,7 +70,7 @@ public:
mSlider.setOutlineColor(sf::Color::Black); mSlider.setOutlineColor(sf::Color::Black);
mSlider.setOutlineThickness(2); mSlider.setOutlineThickness(2);
mAxis.setFillColor(defaultColor); mAxis.setFillColor(sf::Color::White);
mAxis.setSize({500,10}); mAxis.setSize({500,10});
mAxis.setPosition(position); mAxis.setPosition(position);
@ -106,10 +100,10 @@ public:
int handleEvent(const sf::Event& event) { int handleEvent(const sf::Event& event) {
if (event.type == sf::Event::MouseMoved) { if (event.type == sf::Event::MouseMoved) {
return onMouseMove(event); onMouseMove(event);
} }
else if (event.type == sf::Event::MouseButtonPressed) { else if (event.type == sf::Event::MouseButtonPressed) {
return onMousePressed(event); onMousePressed(event);
} }
else if (event.type == sf::Event::MouseButtonReleased) { else if (event.type == sf::Event::MouseButtonReleased) {
mIsPressed = false; mIsPressed = false;