diff --git a/libs/ui/include/psemek/ui/painter.hpp b/libs/ui/include/psemek/ui/painter.hpp index de9d997b..04576318 100644 --- a/libs/ui/include/psemek/ui/painter.hpp +++ b/libs/ui/include/psemek/ui/painter.hpp @@ -16,8 +16,8 @@ namespace psemek::ui virtual void draw_triangle(geom::triangle> const & tri, gfx::color_rgba const & color) = 0; 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_2d const & tex, gfx::color_rgba const & color = {0, 0, 0, 0}) = 0; - virtual void draw_image(geom::box const & rect, gfx::texture_view_2d const & tex, gfx::color_rgba const & color = {0, 0, 0, 0}) = 0; - virtual void draw_subimage(geom::box const & rect, gfx::texture_2d const & tex, geom::box const & part, gfx::color_rgba const & color = {0, 0, 0, 0}) = 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_subimage(geom::box const & rect, gfx::texture_2d const & tex, geom::box const & part, gfx::color_rgba const & color = {0, 0, 0, 0}, float rotation = 0.f) = 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 663e3a0c..6b66f55e 100644 --- a/libs/ui/include/psemek/ui/painter_impl.hpp +++ b/libs/ui/include/psemek/ui/painter_impl.hpp @@ -19,8 +19,8 @@ namespace psemek::ui 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_2d const & tex, 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}) override; - void draw_subimage(geom::box const & rect, gfx::texture_2d const & tex, geom::box const & part, gfx::color_rgba const & color = {0, 0, 0, 0}) 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_subimage(geom::box const & rect, gfx::texture_2d const & tex, geom::box const & part, gfx::color_rgba const & color = {0, 0, 0, 0}, float rotation = 0.f) override; void begin_stencil() override; void commit_stencil() override; diff --git a/libs/ui/source/painter_impl.cpp b/libs/ui/source/painter_impl.cpp index 6fb774ea..b233d829 100644 --- a/libs/ui/source/painter_impl.cpp +++ b/libs/ui/source/painter_impl.cpp @@ -342,22 +342,37 @@ void main() draw_subimage(rect, tex, part, color); } - void painter_impl::draw_image(geom::box const & rect, gfx::texture_view_2d const & tex, gfx::color_rgba const & color) + void painter_impl::draw_image(geom::box const & rect, gfx::texture_view_2d const & tex, gfx::color_rgba const & color, float rotation) { - draw_subimage(rect, *tex.texture, geom::cast(tex.part), color); + draw_subimage(rect, *tex.texture, geom::cast(tex.part), color, rotation); } - void painter_impl::draw_subimage(geom::box const & rect, gfx::texture_2d const & tex, geom::box const & part, gfx::color_rgba const & color) + void painter_impl::draw_subimage(geom::box const & rect, gfx::texture_2d const & tex, geom::box const & part, gfx::color_rgba const & color, float rotation) { auto & batch = impl().batch(textured_batch{&tex}); std::uint32_t const depth = impl().depth++; std::uint32_t const base = batch.vertices.size(); - batch.vertices.push_back({rect.corner(0.f, 0.f), depth, color, part.corner(0.f, 0.f)}); - batch.vertices.push_back({rect.corner(1.f, 0.f), depth, color, part.corner(1.f, 0.f)}); - batch.vertices.push_back({rect.corner(0.f, 1.f), depth, color, part.corner(0.f, 1.f)}); - batch.vertices.push_back({rect.corner(1.f, 1.f), depth, color, part.corner(1.f, 1.f)}); + auto p00 = rect.corner(0.f, 0.f); + auto p10 = rect.corner(1.f, 0.f); + auto p01 = rect.corner(0.f, 1.f); + auto p11 = rect.corner(1.f, 1.f); + + if (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); + } + + batch.vertices.push_back({p00, depth, color, part.corner(0.f, 0.f)}); + batch.vertices.push_back({p10, depth, color, part.corner(1.f, 0.f)}); + batch.vertices.push_back({p01, depth, color, part.corner(0.f, 1.f)}); + batch.vertices.push_back({p11, depth, color, part.corner(1.f, 1.f)}); batch.indices.insert(batch.indices.end(), {base + 0, base + 1, base + 2, base + 2, base + 1, base + 3});