aboutsummaryrefslogtreecommitdiff
path: root/src/gui/CustomRendererWidget.cpp
blob: 98b7cafe5f5cac43d09d9c2db4db7c29d33187e5 (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
#include "../../include/gui/CustomRendererWidget.hpp"

#include <mglpp/window/Window.hpp>

namespace gsr {
    CustomRendererWidget::CustomRendererWidget(mgl::vec2f size) : size(size) {}
    
    bool CustomRendererWidget::on_event(mgl::Event &event, mgl::Window &window, mgl::vec2f offset) {
        if(!visible || !event_handler)
            return true;
        return event_handler(event, window, position + offset, size);
    }

    void CustomRendererWidget::draw(mgl::Window &window, mgl::vec2f offset) {
        if(!visible)
            return;

        const mgl::vec2f draw_pos = position + offset;

        mgl_scissor prev_scissor;
        mgl_window_get_scissor(window.internal_window(), &prev_scissor);

        mgl_scissor new_scissor = {
            mgl_vec2i{(int)draw_pos.x, (int)draw_pos.y},
            mgl_vec2i{(int)size.x, (int)size.y}
        };
        mgl_window_set_scissor(window.internal_window(), &new_scissor);

        if(draw_handler)
            draw_handler(window, draw_pos, size);

        mgl_window_set_scissor(window.internal_window(), &prev_scissor);
    }

    mgl::vec2f CustomRendererWidget::get_size() {
        if(!visible)
            return {0.0f, 0.0f};

        return size;
    }
}