-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.cpp
More file actions
56 lines (49 loc) · 1.87 KB
/
button.cpp
File metadata and controls
56 lines (49 loc) · 1.87 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
#include "button.h"
void Button::reloadLabel()
{
float size;
if ((shape->getGlobalBounds().size.y / shape->getGlobalBounds().size.x) >= (label.getBounds().size.y / label.getBounds().size.x)) {
size = (gaps * label.getSize() * shape->getGlobalBounds().size.x) / label.getBounds().size.x;
} else {
size = (gaps * label.getSize() * shape->getGlobalBounds().size.y) / label.getBounds().size.y;
}
label.setSize(size);
label.setPositionCenter(shape->getGlobalBounds().getCenter());
}
Button::Button(Label label, sf::Shape *shape, float newGaps): label{label}, shape{shape}
{
if (newGaps < 0.f || newGaps > 1.f) throw std::invalid_argument("Gaps must be in the range from 0 to 1. Переменная gaps может принимать значения только от 0 до 1.)");
gaps = newGaps;
reloadLabel();
}
Button::Button(Label label, sf::Vector2f pos, sf::Vector2f size, sf::Color color, float newGaps): label{label}, shape{new sf::RectangleShape{size}}
{
if (newGaps < 0.f || newGaps > 1.f) throw std::invalid_argument("Gaps must be in the range from 0 to 1. Переменная gaps может принимать значения только от 0 до 1.)");
gaps = newGaps;
shape->setPosition(pos);
shape->setFillColor(color);
reloadLabel();
}
void Button::render(sf::RenderWindow *window)
{
window->draw(*shape);
window->draw(label);
}
bool Button::isPosIn(sf::Vector2f pos)
{
if (shape->getGlobalBounds().position.x <= pos.x &&
shape->getGlobalBounds().position.x + shape->getGlobalBounds().size.x >= pos.x &&
shape->getGlobalBounds().position.y <= pos.y &&
shape->getGlobalBounds().position.y + shape->getGlobalBounds().size.y >= pos.y) {
return true;
}
return false;
}
sf::Shape *Button::getShape()
{
return shape;
}
Label *Button::getLabel()
{
return &label;
}