Support drawing rotating images in ui::painter

This commit is contained in:
Nikita Lisitsa 2022-02-17 10:17:55 +03:00
parent 59a0b4ef13
commit 9e24c875a1
3 changed files with 26 additions and 11 deletions

View file

@ -16,8 +16,8 @@ namespace psemek::ui
virtual void draw_triangle(geom::triangle<geom::point<float, 2>> const & tri, gfx::color_rgba const & color) = 0;
virtual void draw_glyph(font const & f, char32_t c, geom::box<float, 2> const & rect, gfx::color_rgba const & color) = 0;
virtual void draw_image(geom::box<float, 2> const & rect, gfx::texture_2d const & tex, gfx::color_rgba const & color = {0, 0, 0, 0}) = 0;
virtual void draw_image(geom::box<float, 2> const & rect, gfx::texture_view_2d const & tex, gfx::color_rgba const & color = {0, 0, 0, 0}) = 0;
virtual void draw_subimage(geom::box<float, 2> const & rect, gfx::texture_2d const & tex, geom::box<float, 2> const & part, gfx::color_rgba const & color = {0, 0, 0, 0}) = 0;
virtual void draw_image(geom::box<float, 2> 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<float, 2> const & rect, gfx::texture_2d const & tex, geom::box<float, 2> 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;

View file

@ -19,8 +19,8 @@ namespace psemek::ui
void draw_triangle(geom::triangle<geom::point<float, 2>> const & tri, gfx::color_rgba const & color) override;
void draw_glyph(font const & f, char32_t c, geom::box<float, 2> const & rect, gfx::color_rgba const & color) override;
void draw_image(geom::box<float, 2> const & rect, gfx::texture_2d const & tex, gfx::color_rgba const & color) override;
void draw_image(geom::box<float, 2> const & rect, gfx::texture_view_2d const & tex, gfx::color_rgba const & color = {0, 0, 0, 0}) override;
void draw_subimage(geom::box<float, 2> const & rect, gfx::texture_2d const & tex, geom::box<float, 2> const & part, gfx::color_rgba const & color = {0, 0, 0, 0}) override;
void draw_image(geom::box<float, 2> 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<float, 2> const & rect, gfx::texture_2d const & tex, geom::box<float, 2> const & part, gfx::color_rgba const & color = {0, 0, 0, 0}, float rotation = 0.f) override;
void begin_stencil() override;
void commit_stencil() override;

View file

@ -342,22 +342,37 @@ void main()
draw_subimage(rect, tex, part, color);
}
void painter_impl::draw_image(geom::box<float, 2> const & rect, gfx::texture_view_2d const & tex, gfx::color_rgba const & color)
void painter_impl::draw_image(geom::box<float, 2> const & rect, gfx::texture_view_2d const & tex, gfx::color_rgba const & color, float rotation)
{
draw_subimage(rect, *tex.texture, geom::cast<float>(tex.part), color);
draw_subimage(rect, *tex.texture, geom::cast<float>(tex.part), color, rotation);
}
void painter_impl::draw_subimage(geom::box<float, 2> const & rect, gfx::texture_2d const & tex, geom::box<float, 2> const & part, gfx::color_rgba const & color)
void painter_impl::draw_subimage(geom::box<float, 2> const & rect, gfx::texture_2d const & tex, geom::box<float, 2> const & part, gfx::color_rgba const & color, float rotation)
{
auto & batch = impl().batch<textured_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});