diff options
author | dec05eba <dec05eba@protonmail.com> | 2024-08-01 22:19:59 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2024-08-01 22:22:45 +0200 |
commit | 10a5669330ba978c37d984a312ba1de7a2542503 (patch) | |
tree | 0ff8cc9660841ce3979ad87da954fb7e14de7d16 /src/gui/Button.cpp | |
parent | 67216467d4dc43134a07f7f843a80a1adf688acd (diff) |
Draw and event position relative to parent (for pages)
Diffstat (limited to 'src/gui/Button.cpp')
-rw-r--r-- | src/gui/Button.cpp | 70 |
1 files changed, 15 insertions, 55 deletions
diff --git a/src/gui/Button.cpp b/src/gui/Button.cpp index 3cad31f..48ab085 100644 --- a/src/gui/Button.cpp +++ b/src/gui/Button.cpp @@ -1,4 +1,5 @@ #include "../../include/gui/Button.hpp" +#include "../../include/gui/Utils.hpp" #include "../../include/Theme.hpp" #include <mglpp/graphics/Rectangle.hpp> #include <mglpp/window/Window.hpp> @@ -6,80 +7,39 @@ #include <mglpp/system/FloatRect.hpp> namespace gsr { - Button::Button(mgl::vec2f size) : size(size) { + Button::Button(mgl::Font *font, const char *text, mgl::vec2f size, mgl::Color bg_color) : size(size), bg_color(bg_color), text(text, *font) { } - bool Button::on_event(mgl::Event &event, mgl::Window&) { + bool Button::on_event(mgl::Event &event, mgl::Window&, mgl::vec2f offset) { if(event.type == mgl::Event::MouseMoved) { - const mgl::vec2f collision_margin(1.0f, 1.0f); // Makes sure that multiple buttons that are next to each other wont activate at the same time when the cursor is right between them - const bool inside = mgl::FloatRect(position + collision_margin, size - collision_margin).contains({ (float)event.mouse_move.x, (float)event.mouse_move.y }); + const bool inside = mgl::FloatRect(position + offset, size).contains({ (float)event.mouse_move.x, (float)event.mouse_move.y }); if(mouse_inside && !inside) { mouse_inside = false; } else if(!mouse_inside && inside) { mouse_inside = true; } } else if(event.type == mgl::Event::MouseButtonPressed) { - pressed_inside = mouse_inside; - } else if(event.type == mgl::Event::MouseButtonReleased) { - const bool clicked_inside = pressed_inside && mouse_inside; - pressed_inside = false; + const bool clicked_inside = mouse_inside; if(clicked_inside && on_click) on_click(); } return true; } - void Button::draw(mgl::Window &window) { - if(mouse_inside) { - // Background - { - mgl::Rectangle rect(size); - rect.set_position(position); - rect.set_color(mgl::Color(0, 0, 0, 255)); - window.draw(rect); - } + void Button::draw(mgl::Window &window, mgl::vec2f offset) { + mgl::Rectangle background(size); + background.set_position(position + offset); + background.set_color(bg_color); + window.draw(background); + + text.set_position((position + offset + size * 0.5f - text.get_bounds().size * 0.5f).floor()); + window.draw(text); + if(mouse_inside) { const int border_size = 5; const mgl::Color border_color = gsr::get_theme().tint_color; - - // Green line at top - { - mgl::Rectangle rect({ size.x, border_size }); - rect.set_position(position); - rect.set_color(border_color); - window.draw(rect); - } - - // Green line at bottom - { - mgl::Rectangle rect({ size.x, border_size }); - rect.set_position(position + mgl::vec2f(0.0f, size.y - border_size)); - rect.set_color(border_color); - window.draw(rect); - } - - // Green line at left - { - mgl::Rectangle rect({ border_size, size.y - border_size * 2 }); - rect.set_position(position + mgl::vec2f(0, border_size)); - rect.set_color(border_color); - window.draw(rect); - } - - // Green line at right - { - mgl::Rectangle rect({ border_size, size.y - border_size * 2 }); - rect.set_position(position + mgl::vec2f(size.x - border_size, border_size)); - rect.set_color(border_color); - window.draw(rect); - } - } else { - // Background - mgl::Rectangle rect(size); - rect.set_position(position); - rect.set_color(mgl::Color(0, 0, 0, 220)); - window.draw(rect); + draw_rectangle_outline(window, position, size, border_color, border_size); } } }
\ No newline at end of file |