autoupdater.hpp

1
2
3
4
5
6
7
#pragma once


#include <planet/vk/engine/autodelete.hpp>


namespace planet::vk::engine {

Automatically update a widget

 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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
    template<typename V, std::invocable<> G>
    struct autoupdater final {
        using generator_type = G;
        using watching_type = std::decay_t<V>;
        using graphic_type =
                std::decay_t<decltype(std::declval<generator_type>()())>;

        using constrained_type = typename graphic_type::constrained_type;
        using reflow_parameters = typename graphic_type::reflow_parameters;


        engine::renderer &renderer;
        watching_type const &watching;
        generator_type generate;
        watching_type last_value = watching;
        felspar::memory::holding_pen<graphic_type> graphic{};
        autodelete<felspar::memory::holding_pen<graphic_type>> autodeleter{
                renderer};


        bool needs_reflow() {
            if (watching != last_value) {
                last_value = watching;
                if (graphic) {
                    autodeleter.manage(std::move(*graphic));
                    graphic.reset();
                }
                return true;
            } else {
                return false;
            }
        }

        void draw() { graphic->draw(); }


        constrained_type
                reflow(reflow_parameters const &p, constrained_type const &c) {
            if (needs_reflow() or not graphic) { graphic.assign(generate()); }
            return graphic->reflow(p, c);
        }
        auto move_to(reflow_parameters const &p, affine::rectangle2d const &r) {
            return graphic->move_to(p, r);
        }
        auto constraints() const { return graphic->constraints(); }
    };

    template<std::invocable<> V, std::invocable<> G>
    struct autoupdater<V, G> final {
        using generator_type = G;
        using watching_lambda_type = V;
        using watching_type = decltype(std::declval<watching_lambda_type>()());
        using graphic_type =
                std::decay_t<decltype(std::declval<generator_type>()())>;

        using constrained_type = typename graphic_type::constrained_type;
        using reflow_parameters = typename graphic_type::reflow_parameters;


        engine::renderer &renderer;
        watching_lambda_type watcher;
        generator_type generate;
        watching_type last_value = watcher();
        felspar::memory::holding_pen<graphic_type> graphic{};
        autodelete<felspar::memory::holding_pen<graphic_type>> autodeleter{
                renderer};


        bool needs_reflow() {
            if (auto nv = watcher(); nv != last_value) {
                last_value = nv;
                if (graphic) {
                    autodeleter.manage(std::move(*graphic));
                    graphic.reset();
                }
                return true;
            } else {
                return false;
            }
        }

        void draw() { graphic->draw(); }


        constrained_type
                reflow(reflow_parameters const &p, constrained_type const &c) {
            if (needs_reflow() or not graphic) { graphic.assign(generate()); }
            return graphic->reflow(p, c);
        }
        auto move_to(reflow_parameters const &p, affine::rectangle2d const &r) {
            return graphic->move_to(p, r);
        }
    };

    template<typename V, typename G>
    autoupdater(renderer &, V &&, G) -> autoupdater<V, G>;


}