-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.hpp
44 lines (41 loc) · 1.45 KB
/
button.hpp
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
#pragma once
#include <SFML/Graphics.hpp>
#include <string>
class button {
public:
int positionX = 50;
int positionY = 50;
sf::Font font;
// void (*resultingaction)();
int buttonwidth;
int buttonheight;
bool buttonPressed = false;
button(std::string label, sf::RenderWindow& window, int positionX,
int positionY) {
font.loadFromFile("arial.ttf");
sf::Text text(label, font);
text.setCharacterSize(20);
text.setStyle(sf::Text::Regular);
text.setFillColor(sf::Color::Black);
text.setPosition(positionX, positionY);
sf::FloatRect textRect = text.getLocalBounds();
buttonwidth = textRect.width * 1.5;
buttonheight = textRect.height * 1.5;
text.setOrigin(textRect.left + textRect.width / 2.0f,
textRect.top + textRect.height / 2.0f);
sf::RectangleShape buttonbox(sf::Vector2f(buttonwidth, buttonheight));
buttonbox.setPosition(positionX - buttonwidth / 2.0f,
positionY - buttonheight / 2.0f);
buttonbox.setFillColor(sf::Color(100, 100, 100));
auto MousePosition = sf::Mouse::getPosition(window);
sf::Vector2f MousePositionF(static_cast<float>(MousePosition.x),
static_cast<float>(MousePosition.y));
window.draw(buttonbox);
window.draw(text);
if (buttonbox.getGlobalBounds().contains(MousePositionF) and
sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
buttonPressed = true;
}
}
private:
};