tx.hpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#pragma once


#include <planet/ui/reflowable.hpp>
#include <planet/vk/colour.hpp>
#include <planet/vk/engine/textured.pipeline.hpp>
#include <planet/vk/texture.hpp>
#include <planet/vk/engine/forward.hpp>


namespace planet::vk::engine::ui {

Draws a texture in the screen space coordinates for layout

 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
110
111
112
113
114
115
116
117
    template<typename Pipeline, typename Texture>
    struct tx final : public planet::ui::reflowable {
        using constrained_type = planet::ui::reflowable::constrained_type;
        using pipeline_type = Pipeline;
        using texture_type = Texture;


        tx(pipeline_type &p)
        : reflowable{"planet::vk::engine::ui::on_screen"}, pl{p} {}
        tx(pipeline_type &p, std::string_view const n) : reflowable{n}, pl{p} {}
        tx(pipeline_type &p, texture_type tx)
        : reflowable{"planet::vk::engine::ui::on_screen"},
          pl{p},
          texture{std::move(tx)} {}
        tx(pipeline_type &p, std::string_view const n, texture_type tx)
        : reflowable{n}, pl{p}, texture{std::move(tx)} {}
        tx(pipeline_type &p, texture_type tx, vk::colour const &c)
        : reflowable{"planet::vk::engine::ui::on_screen"},
          pl{p},
          texture{std::move(tx)},
          colour{c} {}
        tx(pipeline_type &p,
           std::string_view const n,
           texture_type tx,
           vk::colour const &c)
        : reflowable{n}, pl{p}, texture{std::move(tx)}, colour{c} {}


        pipeline_type &pl;
        texture_type texture;
        vk::colour colour = colour::white;


        void draw();


      private:
        constrained_type do_reflow(
                reflow_parameters const &p, constrained_type const &c) override;
        affine::rectangle2d move_sub_elements(
                reflow_parameters const &,
                affine::rectangle2d const &r) override {
            return {r.top_left, constraints().extents()};
        }
    };


    template<>
    inline void tx<pipeline::textured, vk::texture>::draw() {
        if (texture) { pl.this_frame.draw(texture, position(), colour); }
    }
    template<>
    inline void tx<pipeline::textured, vk::texture *>::draw() {
        if (texture and *texture) {
            pl.this_frame.draw(*texture, position(), colour);
        }
    }
    template<>
    inline void tx<pipeline::textured, sub_texture>::draw() {
        if (texture.first) { pl.this_frame.draw(texture, position(), colour); }
    }


    template<>
    inline auto tx<pipeline::textured, vk::texture>::do_reflow(
            reflow_parameters const &, constrained_type const &c)
            -> constrained_type {
        if (texture) {
            auto const r = planet::ui::scaling(
                    texture.image.extents(), c, texture.fit);
            return r;
        } else {
            return {};
        }
    }
    template<>
    inline auto tx<pipeline::textured, vk::texture *>::do_reflow(
            reflow_parameters const &, constrained_type const &c)
            -> constrained_type {
        if (texture and *texture) {
            return planet::ui::scaling(
                    texture->image.extents(), c, texture->fit);
        } else {
            return {};
        }
    }
    template<>
    inline auto tx<pipeline::textured, sub_texture>::do_reflow(
            reflow_parameters const &, constrained_type const &c)
            -> constrained_type {
        if (texture.first) {
            auto const width =
                    texture.second.extents.width * texture.first.image.width;
            auto const height =
                    texture.second.extents.height * texture.first.image.height;
            return planet::ui::scaling({width, height}, c, texture.first.fit);
        } else {
            return {};
        }
    }


}