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/ShapeActor.h>
|
|
|
|
|
#include <kiwano/base/Logger.h>
|
|
|
|
|
#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
|
|
|
{
|
2019-08-13 23:21:20 +08:00
|
|
|
ShapeActor::ShapeActor()
|
2019-08-08 14:15:06 +08:00
|
|
|
: fill_color_(Color::White)
|
|
|
|
|
, stroke_color_(Color(Color::Black, 0))
|
|
|
|
|
, stroke_width_(1.f)
|
2019-08-16 00:50:54 +08:00
|
|
|
, stroke_style_(StrokeStyle::Miter)
|
2019-08-08 14:15:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-08-26 10:22:58 +08:00
|
|
|
ShapeActor::ShapeActor(Geometry const& geometry)
|
2019-08-13 23:21:20 +08:00
|
|
|
: ShapeActor()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-08 14:15:06 +08:00
|
|
|
SetGeometry(geometry);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
ShapeActor::~ShapeActor()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 00:28:25 +08:00
|
|
|
Rect ShapeActor::GetBounds() const
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-20 21:15:15 +08:00
|
|
|
return bounds_;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-14 00:28:25 +08:00
|
|
|
Rect ShapeActor::GetBoundingBox() const
|
|
|
|
|
{
|
|
|
|
|
if (!geo_)
|
|
|
|
|
return Rect{};
|
|
|
|
|
|
2019-08-14 21:38:37 +08:00
|
|
|
return geo_.GetBoundingBox(GetTransformMatrix());
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-20 21:15:15 +08:00
|
|
|
bool ShapeActor::ContainsPoint(const Point& point) const
|
|
|
|
|
{
|
|
|
|
|
return geo_.ContainsPoint(point, GetTransformMatrix());
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
void ShapeActor::SetFillColor(const Color & color)
|
2019-08-08 14:15:06 +08:00
|
|
|
{
|
|
|
|
|
fill_color_ = color;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
void ShapeActor::SetStrokeColor(const Color & color)
|
2019-08-08 14:15:06 +08:00
|
|
|
{
|
|
|
|
|
stroke_color_ = color;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
void ShapeActor::SetStrokeWidth(float width)
|
2019-08-08 14:15:06 +08:00
|
|
|
{
|
|
|
|
|
stroke_width_ = std::max(width, 0.f);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-16 00:50:54 +08:00
|
|
|
void ShapeActor::SetStrokeStyle(StrokeStyle stroke_style)
|
2019-08-08 14:15:06 +08:00
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
stroke_style_ = stroke_style;
|
2019-08-08 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-26 10:22:58 +08:00
|
|
|
void ShapeActor::SetGeometry(Geometry const& geometry)
|
2019-08-20 21:15:15 +08:00
|
|
|
{
|
|
|
|
|
geo_ = geometry;
|
|
|
|
|
if (geo_)
|
|
|
|
|
{
|
2019-08-27 15:29:32 +08:00
|
|
|
bounds_ = geo_.GetBoundingBox();
|
2019-08-20 21:15:15 +08:00
|
|
|
SetSize(bounds_.GetSize());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bounds_ = Rect{};
|
|
|
|
|
SetSize(0.f, 0.f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 19:32:36 +08:00
|
|
|
void ShapeActor::OnRender(RenderTarget* rt)
|
2019-08-08 14:15:06 +08:00
|
|
|
{
|
2019-08-27 15:29:32 +08:00
|
|
|
if (geo_ && CheckVisibilty(rt))
|
2019-08-08 14:15:06 +08:00
|
|
|
{
|
2019-08-20 19:32:36 +08:00
|
|
|
PrepareRender(rt);
|
2019-08-14 00:28:25 +08:00
|
|
|
|
2019-09-09 22:02:53 +08:00
|
|
|
rt->SetDefaultBrushColor(stroke_color_);
|
2019-08-20 19:32:36 +08:00
|
|
|
rt->DrawGeometry(
|
2019-08-08 14:15:06 +08:00
|
|
|
geo_,
|
2019-08-27 15:29:32 +08:00
|
|
|
stroke_width_ * 2, // twice width for widening
|
2019-08-16 00:50:54 +08:00
|
|
|
stroke_style_
|
2019-08-08 14:15:06 +08:00
|
|
|
);
|
2019-08-27 15:29:32 +08:00
|
|
|
|
2019-09-09 22:02:53 +08:00
|
|
|
rt->SetDefaultBrushColor(fill_color_);
|
2019-08-27 15:29:32 +08:00
|
|
|
rt->FillGeometry(
|
2019-09-09 22:02:53 +08:00
|
|
|
geo_
|
2019-08-27 15:29:32 +08:00
|
|
|
);
|
2019-08-08 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
2019-08-13 23:21:20 +08:00
|
|
|
// LineActor
|
2019-03-31 01:37:06 +08:00
|
|
|
//-------------------------------------------------------
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
LineActor::LineActor()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 08:28:14 +08:00
|
|
|
LineActor::LineActor(Point const& begin, Point const& end)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-27 08:28:14 +08:00
|
|
|
SetLine(begin, end);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
LineActor::~LineActor()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 08:28:14 +08:00
|
|
|
void LineActor::SetLine(Point const& begin, Point const& end)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-27 08:28:14 +08:00
|
|
|
if (begin_ != begin || end_ != end)
|
|
|
|
|
{
|
|
|
|
|
begin_ = begin;
|
|
|
|
|
end_ = end;
|
|
|
|
|
SetGeometry(Geometry::CreateLine(begin, end));
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
2019-08-13 23:21:20 +08:00
|
|
|
// RectActor
|
2019-03-31 01:37:06 +08:00
|
|
|
//-------------------------------------------------------
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
RectActor::RectActor()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 21:38:37 +08:00
|
|
|
RectActor::RectActor(Size const& size)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-14 21:38:37 +08:00
|
|
|
SetRectSize(size);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
RectActor::~RectActor()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 21:38:37 +08:00
|
|
|
void RectActor::SetRectSize(Size const& size)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-27 08:28:14 +08:00
|
|
|
if (size != rect_size_)
|
|
|
|
|
{
|
|
|
|
|
rect_size_ = size;
|
|
|
|
|
SetGeometry(Geometry::CreateRect(Rect{ Point{}, size }));
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
2019-08-13 23:21:20 +08:00
|
|
|
// RoundRectActor
|
2019-08-08 14:15:06 +08:00
|
|
|
//-------------------------------------------------------
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
RoundRectActor::RoundRectActor()
|
2019-08-08 14:15:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 21:38:37 +08:00
|
|
|
RoundRectActor::RoundRectActor(Size const& size, Vec2 const& radius)
|
2019-08-08 14:15:06 +08:00
|
|
|
{
|
2019-08-14 21:38:37 +08:00
|
|
|
SetRoundedRect(size, radius);
|
2019-08-08 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
RoundRectActor::~RoundRectActor()
|
2019-08-08 14:15:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 21:38:37 +08:00
|
|
|
void RoundRectActor::SetRadius(Vec2 const& radius)
|
2019-08-08 14:15:06 +08:00
|
|
|
{
|
2019-10-31 20:34:38 +08:00
|
|
|
SetRoundedRect(GetSize(), radius);
|
2019-08-08 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-14 21:38:37 +08:00
|
|
|
void RoundRectActor::SetRectSize(Size const& size)
|
2019-08-08 14:15:06 +08:00
|
|
|
{
|
2019-08-14 21:38:37 +08:00
|
|
|
SetRoundedRect(size, radius_);
|
2019-08-08 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-14 21:38:37 +08:00
|
|
|
void RoundRectActor::SetRoundedRect(Size const& size, Vec2 const& radius)
|
2019-08-08 14:15:06 +08:00
|
|
|
{
|
2019-08-27 08:28:14 +08:00
|
|
|
if (rect_size_ != size || radius_ != radius)
|
|
|
|
|
{
|
|
|
|
|
rect_size_ = size;
|
|
|
|
|
radius_ = radius;
|
|
|
|
|
SetGeometry(Geometry::CreateRoundedRect(Rect{ Point{}, size }, radius));
|
|
|
|
|
}
|
2019-08-08 14:15:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
2019-08-13 23:21:20 +08:00
|
|
|
// CircleActor
|
2019-03-31 01:37:06 +08:00
|
|
|
//-------------------------------------------------------
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
CircleActor::CircleActor()
|
2019-03-31 01:37:06 +08:00
|
|
|
: radius_(0.f)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
CircleActor::CircleActor(float radius)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-14 21:38:37 +08:00
|
|
|
SetRadius(radius);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
CircleActor::~CircleActor()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
void CircleActor::SetRadius(float radius)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-27 08:28:14 +08:00
|
|
|
if (radius_ != radius)
|
|
|
|
|
{
|
|
|
|
|
radius_ = radius;
|
|
|
|
|
SetGeometry(Geometry::CreateCircle(Point{ radius, radius }, radius));
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
2019-08-13 23:21:20 +08:00
|
|
|
// EllipseActor
|
2019-03-31 01:37:06 +08:00
|
|
|
//-------------------------------------------------------
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
EllipseActor::EllipseActor()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 21:38:37 +08:00
|
|
|
EllipseActor::EllipseActor(Vec2 const& radius)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-14 21:38:37 +08:00
|
|
|
SetRadius(radius);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
EllipseActor::~EllipseActor()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 21:38:37 +08:00
|
|
|
void EllipseActor::SetRadius(Vec2 const& radius)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-27 08:28:14 +08:00
|
|
|
if (radius_ != radius)
|
|
|
|
|
{
|
|
|
|
|
radius_ = radius;
|
|
|
|
|
SetGeometry(Geometry::CreateEllipse(radius, radius));
|
|
|
|
|
}
|
2019-08-26 10:22:58 +08:00
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-08-26 10:22:58 +08:00
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
|
|
|
|
// PolygonActor
|
|
|
|
|
//-------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
PolygonActor::PolygonActor()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PolygonActor::PolygonActor(Vector<Point> const& points)
|
|
|
|
|
{
|
|
|
|
|
SetVertices(points);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PolygonActor::~PolygonActor()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PolygonActor::SetVertices(Vector<Point> const& points)
|
|
|
|
|
{
|
|
|
|
|
if (points.size() > 1)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-26 10:22:58 +08:00
|
|
|
SetGeometry(
|
|
|
|
|
GeometrySink()
|
|
|
|
|
.BeginPath(points[0])
|
|
|
|
|
.AddLines(&points[1], points.size() - 1)
|
|
|
|
|
.EndPath(true)
|
|
|
|
|
.GetGeometry()
|
|
|
|
|
);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
2019-08-13 23:21:20 +08:00
|
|
|
// PathActor
|
2019-03-31 01:37:06 +08:00
|
|
|
//-------------------------------------------------------
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
PathActor::PathActor()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
PathActor::~PathActor()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
void PathActor::BeginPath(Point const& begin_pos)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-14 21:38:37 +08:00
|
|
|
sink_.BeginPath(begin_pos);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
void PathActor::EndPath(bool closed)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-14 21:38:37 +08:00
|
|
|
sink_.EndPath(closed);
|
2019-08-20 21:15:15 +08:00
|
|
|
Geometry geo = sink_.GetGeometry();
|
2019-08-20 19:32:36 +08:00
|
|
|
|
2019-08-20 21:15:15 +08:00
|
|
|
if (geo)
|
2019-08-20 19:32:36 +08:00
|
|
|
{
|
2019-08-20 21:15:15 +08:00
|
|
|
SetGeometry(geo);
|
2019-08-20 19:32:36 +08:00
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
void PathActor::AddLine(Point const& point)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-14 21:38:37 +08:00
|
|
|
sink_.AddLine(point);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
void PathActor::AddLines(Vector<Point> const& points)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-14 21:38:37 +08:00
|
|
|
sink_.AddLines(points);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
void PathActor::AddBezier(Point const& point1, Point const& point2, Point const& point3)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-14 21:38:37 +08:00
|
|
|
sink_.AddBezier(point1, point2, point3);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
void PathActor::AddArc(Point const& point, Size const& radius, float rotation, bool clockwise, bool is_small)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-14 21:38:37 +08:00
|
|
|
sink_.AddArc(point, radius, rotation, clockwise, is_small);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-13 23:21:20 +08:00
|
|
|
void PathActor::ClearPath()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-14 21:38:37 +08:00
|
|
|
geo_.SetGeometry(nullptr);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-08 14:15:06 +08:00
|
|
|
}
|