From 35eaf17a3bb34d4125f942920bbd94510022e280 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Thu, 19 May 2022 11:38:09 +0300 Subject: [PATCH] Refactor ui::painter interface: gather draw_image options into a struct --- libs/ui/include/psemek/ui/painter.hpp | 18 +++++++++++++++++- libs/ui/include/psemek/ui/painter_impl.hpp | 3 ++- libs/ui/source/image_view.cpp | 2 +- libs/ui/source/label.cpp | 4 ++-- libs/ui/source/painter_impl.cpp | 20 ++++++++++---------- libs/ui/source/rich_image_view.cpp | 2 +- 6 files changed, 33 insertions(+), 16 deletions(-) diff --git a/libs/ui/include/psemek/ui/painter.hpp b/libs/ui/include/psemek/ui/painter.hpp index f5656d86..a25aef07 100644 --- a/libs/ui/include/psemek/ui/painter.hpp +++ b/libs/ui/include/psemek/ui/painter.hpp @@ -10,12 +10,28 @@ namespace psemek::ui { + namespace detail + { + + // Have to define this struct outside of ui::painter to workaround + // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88165 + struct image_options + { + gfx::color_rgba color{0, 0, 0, 0}; + float rotation{0.f}; + }; + + } + struct painter { virtual void draw_rect(geom::box const & rect, gfx::color_rgba const & color) = 0; virtual void draw_triangle(geom::triangle> const & tri, gfx::color_rgba const & color) = 0; + + using image_options = detail::image_options; + virtual void draw_glyph(font const & f, char32_t c, geom::box const & rect, gfx::color_rgba const & color) = 0; - virtual void draw_image(geom::box const & rect, gfx::texture_view_2d const & tex, gfx::color_rgba const & color = {0, 0, 0, 0}, float rotation = 0.f) = 0; + virtual void draw_image(geom::box const & rect, gfx::texture_view_2d const & tex, image_options const & opts = image_options{}) = 0; virtual void begin_stencil() = 0; virtual void commit_stencil() = 0; diff --git a/libs/ui/include/psemek/ui/painter_impl.hpp b/libs/ui/include/psemek/ui/painter_impl.hpp index f8c134d2..1b15deae 100644 --- a/libs/ui/include/psemek/ui/painter_impl.hpp +++ b/libs/ui/include/psemek/ui/painter_impl.hpp @@ -17,8 +17,9 @@ namespace psemek::ui void draw_rect(geom::box const & rect, gfx::color_rgba const & color) override; void draw_triangle(geom::triangle> const & tri, gfx::color_rgba const & color) override; + void draw_glyph(font const & f, char32_t c, geom::box const & rect, gfx::color_rgba const & color) override; - void draw_image(geom::box const & rect, gfx::texture_view_2d const & tex, gfx::color_rgba const & color = {0, 0, 0, 0}, float rotation = 0.f) override; + void draw_image(geom::box const & rect, gfx::texture_view_2d const & tex, image_options const & opts) override; void begin_stencil() override; void commit_stencil() override; diff --git a/libs/ui/source/image_view.cpp b/libs/ui/source/image_view.cpp index cc50981b..0f60caf8 100644 --- a/libs/ui/source/image_view.cpp +++ b/libs/ui/source/image_view.cpp @@ -85,7 +85,7 @@ namespace psemek::ui } } - p.draw_image(box, image_, color_); + p.draw_image(box, image_, {color_}); } } diff --git a/libs/ui/source/label.cpp b/libs/ui/source/label.cpp index 98502a45..e2d89473 100644 --- a/libs/ui/source/label.cpp +++ b/libs/ui/source/label.cpp @@ -111,12 +111,12 @@ namespace psemek::ui auto const offset = geom::cast(*st->text_shadow_offset); for (auto const & batch : cached_state_->batches) for (auto const & image : batch.images) - p.draw_image(image.position + offset, gfx::texture_view_2d{batch.texture, image.texcoords}, *st->shadow_color); + p.draw_image(image.position + offset, gfx::texture_view_2d{batch.texture, image.texcoords}, {*st->shadow_color}); } for (auto const & batch : cached_state_->batches) for (auto const & image : batch.images) - p.draw_image(image.position, gfx::texture_view_2d{batch.texture, image.texcoords}, *st->text_color); + p.draw_image(image.position, gfx::texture_view_2d{batch.texture, image.texcoords}, {*st->text_color}); } void label::on_state_changed() diff --git a/libs/ui/source/painter_impl.cpp b/libs/ui/source/painter_impl.cpp index b28c36b4..18df7499 100644 --- a/libs/ui/source/painter_impl.cpp +++ b/libs/ui/source/painter_impl.cpp @@ -342,7 +342,7 @@ void main() impl().draw_bbox |= rect; } - void painter_impl::draw_image(geom::box const & rect, gfx::texture_view_2d const & tex, gfx::color_rgba const & color, float rotation) + void painter_impl::draw_image(geom::box const & rect, gfx::texture_view_2d const & tex, image_options const & options) { auto & batch = impl().batch(textured_batch{tex.texture}); @@ -354,20 +354,20 @@ void main() auto p01 = rect.corner(0.f, 1.f); auto p11 = rect.corner(1.f, 1.f); - if (rotation != 0.f) + if (options.rotation != 0.f) { auto const c = rect.center(); - p00 = c + geom::rotate(p00 - c, rotation); - p10 = c + geom::rotate(p10 - c, rotation); - p01 = c + geom::rotate(p01 - c, rotation); - p11 = c + geom::rotate(p11 - c, rotation); + p00 = c + geom::rotate(p00 - c, options.rotation); + p10 = c + geom::rotate(p10 - c, options.rotation); + p01 = c + geom::rotate(p01 - c, options.rotation); + p11 = c + geom::rotate(p11 - c, options.rotation); } - batch.vertices.push_back({p00, depth, color, tex.part.corner(0.f, 0.f)}); - batch.vertices.push_back({p10, depth, color, tex.part.corner(1.f, 0.f)}); - batch.vertices.push_back({p01, depth, color, tex.part.corner(0.f, 1.f)}); - batch.vertices.push_back({p11, depth, color, tex.part.corner(1.f, 1.f)}); + batch.vertices.push_back({p00, depth, options.color, tex.part.corner(0.f, 0.f)}); + batch.vertices.push_back({p10, depth, options.color, tex.part.corner(1.f, 0.f)}); + batch.vertices.push_back({p01, depth, options.color, tex.part.corner(0.f, 1.f)}); + batch.vertices.push_back({p11, depth, options.color, tex.part.corner(1.f, 1.f)}); batch.indices.insert(batch.indices.end(), {base + 0, base + 1, base + 2, base + 2, base + 1, base + 3}); diff --git a/libs/ui/source/rich_image_view.cpp b/libs/ui/source/rich_image_view.cpp index ba7548af..1c4d170f 100644 --- a/libs/ui/source/rich_image_view.cpp +++ b/libs/ui/source/rich_image_view.cpp @@ -177,7 +177,7 @@ namespace psemek::ui if (*st->shadow_offset != geom::vector{0, 0}) p.draw_rect(box + geom::cast(*st->shadow_offset), *st->shadow_color); - p.draw_image(box, {image_.get(), reg}, color_); + p.draw_image(box, {image_.get(), reg}, {color_}); } }