Velvet 0.1a
A beginner-friendly C++ GUI framework built on SFML.
Loading...
Searching...
No Matches
Button.hpp
Go to the documentation of this file.
1#pragma once
2#include <SFML/Graphics/RectangleShape.hpp>
3#include <SFML/System/Vector2.hpp>
5#include <SFML/Graphics/Color.hpp>
6#include <functional>
7#include <unordered_map>
8#include <variant>
9
28class Button : public Widget {
29
30 sf::Sprite sprite;
31 sf::Text text;
32 sf::RectangleShape shape;
33 sf::Font font;
34
35 std::unordered_map<std::string, std::variant<unsigned int, float, std::string>> styles = {
36 {"fontPath", "src/assets/AdwaitaSans-Regular.ttf"},
37 {"fontSize", 20.f},
38 {"letterSpacing", 1.f},
39 {"fontColor", 0x000000FFu},
40 {"hoverFontColor", 0x000000FFu},
41 {"outlineColor", 0x000000FFu},
42 {"outlineThickness", 1.f},
43 {"backgroundColor", 0xFFFFFFFFu},
44 {"hoverBackgroundColor", 0xEEEEEEFFu},
45 {"width", 200.f},
46 {"height", 50.f}
47 };
48
49 bool hovered;
50 bool clicked;
51
52 float width, height;
53
54 void draw(sf::RenderWindow &window) override;
55 void update(sf::RenderWindow &window) override;
56
57public:
61 std::function<void()> onclick;
62
64 void render(sf::RenderWindow &window) override;
66 void handleEvent(const sf::Event &event, sf::RenderWindow &window) override;
67
79 Button(float width,
80 float height,
81 std::string label,
82 std::unordered_map<std::string, std::variant<unsigned int, float, std::string>> styling = {},
83 unsigned int borderColor = 0x000000FFu,
84 float borderThickness = 1.f);
85
87 Button(const Button& other);
88
90 void setPosition(float x, float y) override;
91
93 sf::Vector2<float> getDimensions() override;
94
100 void overrideStyling(std::unordered_map<std::string, std::variant<unsigned int, float, std::string>> styling);
101};
Clickable button widget with text label.
Definition Button.hpp:28
void setPosition(float x, float y) override
Set top-left position of the button.
void overrideStyling(std::unordered_map< std::string, std::variant< unsigned int, float, std::string > > styling)
Apply/override style properties.
void handleEvent(const sf::Event &event, sf::RenderWindow &window) override
Process hover/press input events.
Button(const Button &other)
Copy constructor.
std::function< void()> onclick
Called when the button is pressed with left mouse button while hovered.
Definition Button.hpp:61
void render(sf::RenderWindow &window) override
Render the button.
Button(float width, float height, std::string label, std::unordered_map< std::string, std::variant< unsigned int, float, std::string > > styling={}, unsigned int borderColor=0x000000FFu, float borderThickness=1.f)
Create a button.
sf::Vector2< float > getDimensions() override
Get button dimensions.
Base class for all Velvet UI elements.
Definition Widget.hpp:16
float y
Definition Widget.hpp:31
float x
Definition Widget.hpp:31