diff options
author | dec05eba <dec05eba@protonmail.com> | 2024-08-06 03:11:43 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2024-08-06 03:11:43 +0200 |
commit | b778fd7cc654f28a2bfe0ff74537f120241b289c (patch) | |
tree | de1830608492356d818cce314094e2359a3a478a /src/gui/Button.cpp | |
parent | ae1897cf2ce6a447b253ffa8489b5c016a23fb41 (diff) |
Change fonts, nicer combobox, add/remove audio track button
Diffstat (limited to 'src/gui/Button.cpp')
-rw-r--r-- | src/gui/Button.cpp | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/src/gui/Button.cpp b/src/gui/Button.cpp index eb5932b..e8137c9 100644 --- a/src/gui/Button.cpp +++ b/src/gui/Button.cpp @@ -7,15 +7,21 @@ #include <mglpp/system/FloatRect.hpp> namespace gsr { + static const float padding_top = 10.0f; + static const float padding_bottom = 10.0f; + static const float padding_left = 10.0f; + static const float padding_right = 10.0f; + 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&, mgl::vec2f offset) { + const mgl::vec2f item_size = get_size().floor(); if(event.type == mgl::Event::MouseMoved) { - mouse_inside = mgl::FloatRect(position + offset, size).contains({ (float)event.mouse_move.x, (float)event.mouse_move.y }); + mouse_inside = mgl::FloatRect(position + offset, item_size).contains({ (float)event.mouse_move.x, (float)event.mouse_move.y }); } else if(event.type == mgl::Event::MouseButtonPressed && event.mouse_button.button == mgl::Mouse::Left) { - const bool clicked_inside = mouse_inside; + const bool clicked_inside = mgl::FloatRect(position + offset, item_size).contains({ (float)event.mouse_button.x, (float)event.mouse_button.y });; if(clicked_inside && on_click) on_click(); } @@ -23,18 +29,31 @@ namespace gsr { } void Button::draw(mgl::Window &window, mgl::vec2f offset) { - mgl::Rectangle background(size.floor()); - background.set_position((position + offset).floor()); + const mgl::vec2f draw_pos = position + offset; + const mgl::vec2f item_size = get_size().floor(); + mgl::Rectangle background(item_size); + background.set_position(draw_pos.floor()); background.set_color(bg_color); window.draw(background); - text.set_position((position + offset + size * 0.5f - text.get_bounds().size * 0.5f).floor()); + text.set_position((draw_pos + item_size * 0.5f - text.get_bounds().size * 0.5f).floor()); window.draw(text); + const bool mouse_inside = mgl::FloatRect(draw_pos, item_size).contains(window.get_mouse_position().to_vec2f()); if(mouse_inside) { const int border_size = 5; const mgl::Color border_color = gsr::get_theme().tint_color; - draw_rectangle_outline(window, position, size, border_color, border_size); + draw_rectangle_outline(window, position, item_size, border_color, border_size); } } + + mgl::vec2f Button::get_size() { + const mgl::vec2f text_bounds = text.get_bounds().size; + mgl::vec2f s = size; + if(s.x < 0.0001f) + s.x = padding_left + text_bounds.x + padding_right; + if(s.y < 0.0001f) + s.y = padding_top + text_bounds.y + padding_bottom; + return s; + } }
\ No newline at end of file |