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/ScrollablePage.cpp | |
parent | 67216467d4dc43134a07f7f843a80a1adf688acd (diff) |
Draw and event position relative to parent (for pages)
Diffstat (limited to 'src/gui/ScrollablePage.cpp')
-rw-r--r-- | src/gui/ScrollablePage.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/gui/ScrollablePage.cpp b/src/gui/ScrollablePage.cpp new file mode 100644 index 0000000..4a4f88b --- /dev/null +++ b/src/gui/ScrollablePage.cpp @@ -0,0 +1,53 @@ +#include "../../include/gui/ScrollablePage.hpp" +#include "../../include/Theme.hpp" + +#include <mglpp/graphics/Rectangle.hpp> +#include <mglpp/window/Window.hpp> + +namespace gsr { + ScrollablePage::ScrollablePage(mgl::vec2f size) : size(size) {} + + bool ScrollablePage::on_event(mgl::Event &event, mgl::Window &window, mgl::vec2f offset) { + const mgl::vec2f draw_pos = position + offset; + offset = draw_pos + mgl::vec2f(0.0f, get_border_size(window)).floor(); + + // Process widgets by visibility (backwards) + for(auto it = widgets.rbegin(), end = widgets.rend(); it != end; ++it) { + if(!(*it)->on_event(event, window, offset)) + return false; + } + + return true; + } + + void ScrollablePage::draw(mgl::Window &window, mgl::vec2f offset) { + const mgl::vec2f draw_pos = position + offset; + offset = draw_pos + mgl::vec2f(0.0f, get_border_size(window)).floor(); + + mgl::Rectangle background(size); + background.set_position(draw_pos); + background.set_color(get_theme().scrollable_page_bg_color); + window.draw(background); + + mgl::Rectangle border(mgl::vec2f(size.x, get_border_size(window)).floor()); + border.set_position(draw_pos); + border.set_color(get_theme().tint_color); + window.draw(border); + + for(auto &widget : widgets) { + if(widget->move_to_top) { + widget->move_to_top = false; + std::swap(widget, widgets.back()); + } + widget->draw(window, offset); + } + + for(auto &widget : widgets) { + widget->draw(window, offset); + } + } + + float ScrollablePage::get_border_size(mgl::Window &window) const { + return window.get_size().y * 0.004f; + } +}
\ No newline at end of file |