generated from mcmarius/oop-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathButton.cpp
More file actions
63 lines (47 loc) · 1.7 KB
/
Button.cpp
File metadata and controls
63 lines (47 loc) · 1.7 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
//
// Created by danit on 04.11.2022.
//
#include "Button.h"
/// Constructor/Destructor
Button::Button(float width, float height, float x, float y, const sf::Font &font, const std::string &text,
const sf::Color &idleColor, const sf::Color &hoverColor, const sf::Color &pressedColor)
: font(font), idleColor(idleColor), hoverColor(hoverColor), pressedColor(pressedColor) {
this->buttonState = IDLE;
this->shape.setSize(sf::Vector2f(width, height));
this->shape.setOrigin(width / 2, height / 2);
this->shape.setPosition(sf::Vector2f(x, y));
this->shape.setFillColor(idleColor);
this->text.setFont(this->font);
this->text.setString(text);
this->text.setFillColor(sf::Color::White);
this->text.setCharacterSize(12);
this->text.setPosition(
this->shape.getPosition().x - this->text.getGlobalBounds().width / 2,
this->shape.getPosition().y - this->text.getGlobalBounds().height / 2
);
this->shape.setFillColor(this->idleColor);
}
/// Getters
bool Button::isPressed() const {
return this->buttonState == PRESSED;
}
/// Functions
void Button::update(sf::Vector2f mousePos) {
/// Idle
this->buttonState = IDLE;
this->shape.setFillColor(this->idleColor);
/// Hover
if (this->shape.getGlobalBounds().contains(mousePos)) {
this->buttonState = HOVER;
this->shape.setFillColor(this->hoverColor);
/// Pressed
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
this->buttonState = PRESSED;
this->shape.setFillColor(this->pressedColor);
}
}
}
void Button::render(sf::RenderTarget &target) {
target.draw(this->shape);
target.draw(this->text);
}