diff options
author | dec05eba <dec05eba@protonmail.com> | 2024-09-08 19:41:47 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2024-09-08 19:41:47 +0200 |
commit | 747344b858c4750e7b0b19ca10c3727148c2a161 (patch) | |
tree | 1406a38de09c4bfceb8bd09264eb32670636585e /src | |
parent | b145d957e3809fd6c2d814c34c58234ade983bb0 (diff) |
Add scrollbar
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/ScrollablePage.cpp | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/src/gui/ScrollablePage.cpp b/src/gui/ScrollablePage.cpp index 7e15edf..dc0e16f 100644 --- a/src/gui/ScrollablePage.cpp +++ b/src/gui/ScrollablePage.cpp @@ -1,13 +1,15 @@ #include "../../include/gui/ScrollablePage.hpp" #include "../../include/gui/Utils.hpp" +#include "../../include/Theme.hpp" #include <mglpp/window/Window.hpp> #include <mglpp/window/Event.hpp> -//#include <mglpp/graphics/Rectangle.hpp> +#include <mglpp/graphics/Rectangle.hpp> namespace gsr { static const int scroll_speed = 80; static const double scroll_update_speed = 10.0; + static const float scrollbar_width_scale = 0.004f; ScrollablePage::ScrollablePage(mgl::vec2f size) : size(size) {} @@ -50,6 +52,9 @@ namespace gsr { return; } + const double scrollbar_width = std::max(5.0f, scrollbar_width_scale * get_theme().window_height); + const mgl::vec2f scrollbar_pos = position + offset + mgl::vec2f(size.x - scrollbar_width, 0.0f); + offset = position + offset; const mgl::vec2f page_scroll_start = offset; @@ -97,6 +102,8 @@ namespace gsr { const double child_height = child_bottom - child_top; + //fprintf(stderr, "scrollbar height: %f\n", scrollbar_height); + // Debug output // mgl::Rectangle bottom(mgl::vec2f(size.x, 5.0f)); // bottom.set_color(mgl::Color(255, 0, 0, 255)); @@ -126,6 +133,29 @@ namespace gsr { } mgl_window_set_scissor(window.internal_window(), &prev_scissor); + + double scrollbar_height = 1.0; + if(child_height > 0.001) + scrollbar_height = size.y / child_height; + if(scrollbar_height > 1.0) + scrollbar_height = 1.0; + + if(scrollbar_height < 0.999) { + double scroll_amount = -scroll_y / (child_height - size.y); + if(scroll_amount < 0.0) + scroll_amount = 0.0; + else if(scroll_amount > 1.0) + scroll_amount = 1.0; + + const double scrollbar_height_absolute = size.y * scrollbar_height; + const double scrollbar_empty_space = size.y - scrollbar_height_absolute; + + mgl::Rectangle scrollbar( + (scrollbar_pos + mgl::vec2f(0.0f, scroll_amount * scrollbar_empty_space)).floor(), + mgl::vec2f(scrollbar_width, scrollbar_height_absolute).floor()); + scrollbar.set_color(mgl::Color(200, 200, 200)); + window.draw(scrollbar); + } } mgl::vec2f ScrollablePage::get_size() { |