diff options
author | dec05eba <dec05eba@protonmail.com> | 2022-09-25 17:29:17 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2022-09-25 17:29:17 +0200 |
commit | 497217a3e09b577f650aaf503356588515067ca8 (patch) | |
tree | 2fd43b747957510c55d66caf81df30732c39c398 /src/gui/WidgetContainer.cpp | |
parent | 663a36df4aaea583e7cd56ad35b26d05f6151746 (diff) |
widgets
Diffstat (limited to 'src/gui/WidgetContainer.cpp')
-rw-r--r-- | src/gui/WidgetContainer.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/gui/WidgetContainer.cpp b/src/gui/WidgetContainer.cpp new file mode 100644 index 0000000..ab97b5b --- /dev/null +++ b/src/gui/WidgetContainer.cpp @@ -0,0 +1,56 @@ +#include "../../include/gui/WidgetContainer.hpp" +#include "../../include/gui/Widget.hpp" + +namespace gsr { + // static + WidgetContainer& WidgetContainer::get_instance() { + static WidgetContainer instance; + return instance; + } + + void WidgetContainer::add_widget(Widget *widget) { + // TODO: to_be_added, and remove in the draw loop + #ifdef DEBUG + for(Widget *existing_widget : widgets) { + if(existing_widget == widget) + return; + } + #endif + widgets.push_back(widget); + } + + void WidgetContainer::remove_widget(Widget *widget) { + // TODO: to_be_removed, and remove in draw loop + for(auto it = widgets.begin(), end = widgets.end(); it != end; ++it) { + if(*it == widget) { + widgets.erase(it); + return; + } + } + } + + void WidgetContainer::on_event(mgl::Event &event, mgl::Window &window) { + // Process widgets by visibility (backwards) + for(auto it = widgets.rbegin(), end = widgets.rend(); it != end; ++it) { + if(!(*it)->on_event(event, window)) + return; + } + } + + void WidgetContainer::draw(mgl::Window &window) { + for(auto it = widgets.begin(); it != widgets.end(); ++it) { + Widget *widget = *it; + if(widget->move_to_top) { + widget->move_to_top = false; + if(widgets.back() != widget) { + widgets.erase(it); + widgets.push_back(widget); + } + } + } + + for(Widget *widget : widgets) { + widget->draw(window); + } + } +}
\ No newline at end of file |