aboutsummaryrefslogtreecommitdiff
path: root/src/gui
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-05-11 14:24:52 +0200
committerdec05eba <dec05eba@protonmail.com>2021-05-11 14:24:52 +0200
commite308d77b06405b91885cf6f97c0dc2a1b70679ef (patch)
tree7fb0ed73c20354f3e0ff49ebb199e33ebc3427f7 /src/gui
parent6b90f55f4dc14b9d39fa0dbf6b82b3c12ccc29d9 (diff)
Improve file saving gui
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/Button.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/gui/Button.cpp b/src/gui/Button.cpp
new file mode 100644
index 0000000..ebbb8bb
--- /dev/null
+++ b/src/gui/Button.cpp
@@ -0,0 +1,76 @@
+#include "../../include/gui/Button.hpp"
+#include <SFML/Graphics/RenderTarget.hpp>
+#include <SFML/Window/Event.hpp>
+#include <cmath>
+
+namespace QuickMedia {
+ static const float PADDING_Y = 10.0f;
+
+ Button::Button(const std::string &label, sf::Font *font, unsigned int character_size, float width, sf::Shader *rounded_rectangle_shader, float scale) :
+ label(label, *font, character_size * scale),
+ background(sf::Vector2f(1.0f, 1.0f), 10.0f, sf::Color(33, 37, 44), rounded_rectangle_shader),
+ scale(scale)
+ {
+ background.set_size(sf::Vector2f(std::floor(width * scale), get_height()));
+ set_position(sf::Vector2f(0.0f, 0.0f));
+ }
+
+ ButtonEvent Button::on_event(sf::Event &event) {
+ ButtonEvent performed_event = BUTTON_EVENT_NONE;
+ if(event.type == sf::Event::MouseMoved) {
+ if(sf::FloatRect(background.get_position(), background.get_size()).contains(event.mouseMove.x, event.mouseMove.y)) {
+ const int inc = 20;
+ background.set_color(sf::Color(
+ std::min(255, (int)background_color.r + inc),
+ std::min(255, (int)background_color.g + inc),
+ std::min(255, (int)background_color.b + inc)));
+ } else {
+ background.set_color(background_color);
+ }
+ } else if(event.type == sf::Event::MouseButtonPressed) {
+ if(event.mouseButton.button == sf::Mouse::Left && sf::FloatRect(background.get_position(), background.get_size()).contains(event.mouseButton.x, event.mouseButton.y)) {
+ clicked_inside = true;
+ } else {
+ clicked_inside = false;
+ }
+ } else if(event.type == sf::Event::MouseButtonReleased) {
+ if(clicked_inside && event.mouseButton.button == sf::Mouse::Left && sf::FloatRect(background.get_position(), background.get_size()).contains(event.mouseButton.x, event.mouseButton.y)) {
+ performed_event = BUTTON_EVENT_CLICKED;
+ }
+ clicked_inside = false;
+ }
+ return performed_event;
+ }
+
+ void Button::draw(sf::RenderTarget &target) {
+ background.draw(target);
+ target.draw(label);
+ }
+
+ void Button::set_background_color(sf::Color color) {
+ background_color = color;
+ background.set_color(background_color);
+ }
+
+ void Button::set_position(sf::Vector2f pos) {
+ background.set_position(pos);
+
+ const auto label_bounds = label.getLocalBounds();
+ sf::Vector2f label_pos(pos + background.get_size() * 0.5f - sf::Vector2f(label_bounds.width * 0.5f, label_bounds.height * 0.5f) - sf::Vector2f(0.0f, 5.0f * scale));
+ label_pos.x = std::floor(label_pos.x);
+ label_pos.y = std::floor(label_pos.y);
+ label.setPosition(label_pos);
+ }
+
+ sf::Vector2f Button::get_position() const {
+ return background.get_position();
+ }
+
+ float Button::get_width() const {
+ return background.get_size().x;
+ }
+
+ float Button::get_height() const {
+ return std::floor((PADDING_Y * 2.0f) * scale + label.getLocalBounds().height);
+ }
+} \ No newline at end of file