2019-04-11 14:40:54 +08:00
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2019-03-31 01:37:06 +08:00
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
2019-10-11 21:55:29 +08:00
|
|
|
#include <kiwano/2d/Canvas.h>
|
2019-11-13 14:33:15 +08:00
|
|
|
#include <kiwano/core/Logger.h>
|
2019-10-11 21:55:29 +08:00
|
|
|
#include <kiwano/renderer/Renderer.h>
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
Canvas::Canvas()
|
|
|
|
|
: cache_expired_(false)
|
|
|
|
|
, stroke_width_(1.0f)
|
2020-01-10 11:43:07 +08:00
|
|
|
, stroke_style_()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Canvas::~Canvas()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Canvas::BeginDraw()
|
|
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->BeginDraw();
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Canvas::EndDraw()
|
|
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->EndDraw();
|
2019-03-31 01:37:06 +08:00
|
|
|
cache_expired_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 19:32:36 +08:00
|
|
|
void Canvas::OnRender(RenderTarget* rt)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
UpdateCache();
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-12-26 14:15:25 +08:00
|
|
|
if (texture_cached_ && texture_cached_->IsValid())
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
PrepareToRender(rt);
|
2019-08-14 00:28:25 +08:00
|
|
|
|
2019-12-26 14:15:25 +08:00
|
|
|
Rect bitmap_rect(0.f, 0.f, texture_cached_->GetWidth(), texture_cached_->GetHeight());
|
2019-12-26 19:25:43 +08:00
|
|
|
rt->DrawTexture(*texture_cached_, bitmap_rect, bitmap_rect);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 23:42:51 +08:00
|
|
|
void Canvas::SetBrush(BrushPtr brush)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
|
|
|
|
rt_->SetCurrentBrush(brush);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
float Canvas::GetStrokeWidth() const
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
return stroke_width_;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 00:50:54 +08:00
|
|
|
void Canvas::SetBrushTransform(Transform const& transform)
|
|
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->SetTransform(transform.ToMatrix());
|
2019-08-16 00:50:54 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-14 23:36:29 +08:00
|
|
|
void Canvas::SetBrushTransform(Matrix3x2 const & transform)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->SetTransform(transform);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-20 19:32:36 +08:00
|
|
|
void Canvas::PushLayerArea(LayerArea& area)
|
|
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->PushLayer(area);
|
2019-08-20 19:32:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Canvas::PopLayerArea()
|
|
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->PopLayer();
|
2019-08-20 19:32:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Canvas::PushClipRect(Rect const& clip_rect)
|
|
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->PushClipRect(clip_rect);
|
2019-08-20 19:32:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Canvas::PopClipRect()
|
|
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->PopClipRect();
|
2019-08-20 19:32:36 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-16 00:50:54 +08:00
|
|
|
void Canvas::DrawLine(Point const& begin, Point const& end)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
|
|
|
|
rt_->SetCurrentBrush(stroke_brush_);
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->DrawLine(
|
2019-08-16 00:50:54 +08:00
|
|
|
begin,
|
|
|
|
|
end,
|
2019-03-31 01:37:06 +08:00
|
|
|
stroke_width_,
|
2019-08-16 00:50:54 +08:00
|
|
|
stroke_style_
|
2019-03-31 01:37:06 +08:00
|
|
|
);
|
|
|
|
|
cache_expired_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
void Canvas::DrawCircle(Point const& center, float radius)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
|
|
|
|
rt_->SetCurrentBrush(stroke_brush_);
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->DrawEllipse(
|
2019-08-16 00:50:54 +08:00
|
|
|
center,
|
|
|
|
|
Vec2(radius, radius),
|
2019-03-31 01:37:06 +08:00
|
|
|
stroke_width_,
|
2019-08-16 00:50:54 +08:00
|
|
|
stroke_style_
|
2019-03-31 01:37:06 +08:00
|
|
|
);
|
|
|
|
|
cache_expired_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 00:50:54 +08:00
|
|
|
void Canvas::DrawEllipse(Point const& center, Vec2 const& radius)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
|
|
|
|
rt_->SetCurrentBrush(stroke_brush_);
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->DrawEllipse(
|
2019-08-16 00:50:54 +08:00
|
|
|
center,
|
|
|
|
|
radius,
|
2019-03-31 01:37:06 +08:00
|
|
|
stroke_width_,
|
2019-08-16 00:50:54 +08:00
|
|
|
stroke_style_
|
2019-03-31 01:37:06 +08:00
|
|
|
);
|
|
|
|
|
cache_expired_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 00:50:54 +08:00
|
|
|
void Canvas::DrawRect(Rect const& rect)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
|
|
|
|
rt_->SetCurrentBrush(stroke_brush_);
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->DrawRectangle(
|
2019-08-16 00:50:54 +08:00
|
|
|
rect,
|
2019-03-31 01:37:06 +08:00
|
|
|
stroke_width_,
|
2019-08-16 00:50:54 +08:00
|
|
|
stroke_style_
|
2019-03-31 01:37:06 +08:00
|
|
|
);
|
|
|
|
|
cache_expired_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 00:50:54 +08:00
|
|
|
void Canvas::DrawRoundedRect(Rect const& rect, Vec2 const& radius)
|
|
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
|
|
|
|
rt_->SetCurrentBrush(stroke_brush_);
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->DrawRoundedRectangle(
|
2019-08-16 00:50:54 +08:00
|
|
|
rect,
|
|
|
|
|
radius,
|
2019-03-31 01:37:06 +08:00
|
|
|
stroke_width_,
|
2019-08-16 00:50:54 +08:00
|
|
|
stroke_style_
|
2019-03-31 01:37:06 +08:00
|
|
|
);
|
|
|
|
|
cache_expired_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
void Canvas::FillCircle(Point const& center, float radius)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
|
|
|
|
rt_->SetCurrentBrush(fill_brush_);
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->FillEllipse(
|
2019-08-16 00:50:54 +08:00
|
|
|
center,
|
2019-09-09 22:02:53 +08:00
|
|
|
Vec2(radius, radius)
|
2019-03-31 01:37:06 +08:00
|
|
|
);
|
2019-08-16 00:50:54 +08:00
|
|
|
cache_expired_ = true;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-16 00:50:54 +08:00
|
|
|
void Canvas::FillEllipse(Point const& center, Vec2 const& radius)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
|
|
|
|
rt_->SetCurrentBrush(fill_brush_);
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->FillEllipse(
|
2019-08-16 00:50:54 +08:00
|
|
|
center,
|
2019-09-09 22:02:53 +08:00
|
|
|
radius
|
2019-03-31 01:37:06 +08:00
|
|
|
);
|
|
|
|
|
cache_expired_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 00:50:54 +08:00
|
|
|
void Canvas::FillRect(Rect const& rect)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
|
|
|
|
rt_->SetCurrentBrush(fill_brush_);
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->FillRectangle(
|
2019-09-09 22:02:53 +08:00
|
|
|
rect
|
2019-03-31 01:37:06 +08:00
|
|
|
);
|
|
|
|
|
cache_expired_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 00:50:54 +08:00
|
|
|
void Canvas::FillRoundedRect(Rect const& rect, Vec2 const& radius)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
|
|
|
|
rt_->SetCurrentBrush(fill_brush_);
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->FillRoundedRectangle(
|
2019-08-16 00:50:54 +08:00
|
|
|
rect,
|
2019-09-09 22:02:53 +08:00
|
|
|
radius
|
2019-03-31 01:37:06 +08:00
|
|
|
);
|
|
|
|
|
cache_expired_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-26 14:15:25 +08:00
|
|
|
void Canvas::DrawTexture(TexturePtr texture, const Rect* src_rect, const Rect* dest_rect)
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
2019-12-26 14:15:25 +08:00
|
|
|
if (texture)
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->DrawTexture(*texture, src_rect, dest_rect);
|
2019-08-16 00:50:54 +08:00
|
|
|
cache_expired_ = true;
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-12-23 18:05:08 +08:00
|
|
|
void Canvas::DrawTextLayout(String const& text, Point const& point)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
if (text.empty())
|
|
|
|
|
return;
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-12-26 19:25:43 +08:00
|
|
|
TextLayout layout;
|
|
|
|
|
layout.SetStyle(text_style_);
|
|
|
|
|
layout.SetText(text);
|
2019-12-23 18:05:08 +08:00
|
|
|
DrawTextLayout(layout, point);
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-12-23 18:05:08 +08:00
|
|
|
void Canvas::DrawTextLayout(TextLayout const& layout, Point const& point)
|
|
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->DrawTextLayout(layout, point);
|
2019-08-16 00:50:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Canvas::BeginPath(Point const& begin_pos)
|
|
|
|
|
{
|
|
|
|
|
geo_sink_.BeginPath(begin_pos);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Canvas::EndPath(bool closed)
|
|
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
geo_sink_.EndPath(closed);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Canvas::AddLine(Point const & point)
|
|
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
geo_sink_.AddLine(point);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 21:16:38 +08:00
|
|
|
void Canvas::AddLines(Vector<Point> const& points)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
geo_sink_.AddLines(points);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Canvas::AddBezier(Point const & point1, Point const & point2, Point const & point3)
|
|
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
geo_sink_.AddBezier(point1, point2, point3);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-12-23 18:05:08 +08:00
|
|
|
void Canvas::AddArc(Point const & point, Size const & radius, float rotation, bool clockwise, bool is_small)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
geo_sink_.AddArc(point, radius, rotation, clockwise, is_small);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Canvas::StrokePath()
|
|
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
|
|
|
|
rt_->SetCurrentBrush(stroke_brush_);
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->DrawGeometry(
|
2019-08-16 00:50:54 +08:00
|
|
|
geo_sink_.GetGeometry(),
|
2019-03-31 01:37:06 +08:00
|
|
|
stroke_width_,
|
2019-08-16 00:50:54 +08:00
|
|
|
stroke_style_
|
2019-03-31 01:37:06 +08:00
|
|
|
);
|
|
|
|
|
cache_expired_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Canvas::FillPath()
|
|
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
|
|
|
|
rt_->SetCurrentBrush(fill_brush_);
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->FillGeometry(
|
2019-09-09 22:02:53 +08:00
|
|
|
geo_sink_.GetGeometry()
|
2019-03-31 01:37:06 +08:00
|
|
|
);
|
|
|
|
|
cache_expired_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Canvas::Clear()
|
|
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->Clear();
|
2019-08-16 00:50:54 +08:00
|
|
|
cache_expired_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Canvas::Clear(Color const& clear_color)
|
|
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
InitRenderTargetAndBrushs();
|
2019-12-27 10:51:34 +08:00
|
|
|
rt_->Clear(clear_color);
|
2019-03-31 01:37:06 +08:00
|
|
|
cache_expired_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-26 14:15:25 +08:00
|
|
|
TexturePtr Canvas::ExportToTexture() const
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
UpdateCache();
|
2019-08-21 16:33:41 +08:00
|
|
|
return texture_cached_;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-12-27 23:42:51 +08:00
|
|
|
void Canvas::InitRenderTargetAndBrushs()
|
|
|
|
|
{
|
|
|
|
|
if (!rt_)
|
|
|
|
|
{
|
|
|
|
|
Renderer::instance().CreateTextureRenderTarget(rt_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!stroke_brush_)
|
|
|
|
|
{
|
|
|
|
|
stroke_brush_ = new Brush;
|
|
|
|
|
stroke_brush_->SetColor(Color::White);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!fill_brush_)
|
|
|
|
|
{
|
|
|
|
|
fill_brush_ = new Brush;
|
|
|
|
|
fill_brush_->SetColor(Color::White);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 00:50:54 +08:00
|
|
|
void Canvas::UpdateCache() const
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
if (cache_expired_ && rt_)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-12-27 23:42:51 +08:00
|
|
|
if (!texture_cached_)
|
|
|
|
|
{
|
|
|
|
|
texture_cached_ = new Texture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rt_->GetOutput(*texture_cached_))
|
|
|
|
|
{
|
|
|
|
|
cache_expired_ = false;
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-08 14:15:06 +08:00
|
|
|
}
|