-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
80 lines (69 loc) · 2.32 KB
/
Copy pathButton.cpp
File metadata and controls
80 lines (69 loc) · 2.32 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
#include <iostream>
#include "Button.h"
using namespace sf;
Button::Button(float x, float y, float width, float height, float shift, Font *font, std::string text) {
this->buttonState = BTN_IDLE;
this->textIdleColor = Color(100, 100, 100, 200);
this->textHoverColor = Color(0, 0, 0, 250);
this->textActiveColor = Color(20, 20, 20, 50);
this->idleColor = Color(70, 70, 70, 0);
this->hoverColor = Color(150, 150, 150, 0);
this->activeColor = Color(20, 20, 20, 0);
this->shape.setSize(Vector2f(width, height));
this->shape.setPosition(x / 2 - 100, y / 4 + shift);
this->shape.setFillColor(this->idleColor);
this->font = font;
this->text.setFont(*this->font);
this->text.setString(text);
this->text.setFillColor(this->textIdleColor);
this->text.setCharacterSize(50);
this->text.setPosition(
this->shape.getPosition().x + (this->shape.getGlobalBounds().width / 2.f) - this->text.getGlobalBounds().width / 2.f,
this->shape.getPosition().y + (this->shape.getGlobalBounds().height / 2.f) - this->text.getGlobalBounds().height / 2.f
);
}
Button::~Button() {
}
const bool Button::isPressed() const {
if (this->buttonState == BTN_ACTIVE)
{
return true;
} else
{
return false;
}
}
void Button::update(const sf::Vector2f mousePos) {
this->buttonState = BTN_IDLE;
if (this->shape.getGlobalBounds().contains(mousePos))
{
this->buttonState = BTN_HOVER;
if (Mouse::isButtonPressed(Mouse::Left))
{
this->buttonState = BTN_ACTIVE;
}
}
switch (this->buttonState)
{
case BTN_IDLE:
this->shape.setFillColor(this->idleColor);
this->text.setFillColor(this->textIdleColor);
break;
case BTN_HOVER:
this->shape.setFillColor(this->hoverColor);
this->text.setFillColor(this->textHoverColor);
break;
case BTN_ACTIVE:
this->shape.setFillColor(this->activeColor);
this->text.setFillColor(this->textActiveColor);
break;
default:
this->shape.setFillColor(Color::Red);
this->text.setFillColor(Color::Blue);
break;
}
}
void Button::render(sf::RenderTarget *target) {
target->draw(this->shape);
target->draw(this->text);
}