aboutsummaryrefslogtreecommitdiff
path: root/src/gui/WidgetContainer.cpp
blob: 8824d1a86c5840e9bcbb66d1d3ea7cc34111e51e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#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;
                std::swap(*it, widgets.back());
                /*if(widgets.back() != widget) {
                    widgets.erase(it);
                    widgets.push_back(widget);
                }*/
            }
        }

        for(Widget *widget : widgets) {
            widget->draw(window);
        }
    }
}