Magic_Game/src/kiwano/2d/ShapeActor.cpp

352 lines
6.8 KiB
C++
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// 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>
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-04-11 14:40:54 +08:00
namespace kiwano
{
ShapeActor::ShapeActor()
: 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-26 10:22:58 +08:00
ShapeActor::ShapeActor(Geometry const& geometry)
: ShapeActor()
{
SetGeometry(geometry);
}
ShapeActor::~ShapeActor()
{
}
2019-08-14 00:28:25 +08:00
Rect ShapeActor::GetBounds() const
{
2019-08-20 21:15:15 +08:00
return bounds_;
}
2019-08-14 00:28:25 +08:00
Rect ShapeActor::GetBoundingBox() const
{
if (!geo_)
return Rect{};
return geo_.GetBoundingBox(GetTransformMatrix());
}
2019-08-20 21:15:15 +08:00
bool ShapeActor::ContainsPoint(const Point& point) const
{
return geo_.ContainsPoint(point, GetTransformMatrix());
}
void ShapeActor::SetFillColor(const Color & color)
{
fill_color_ = color;
}
void ShapeActor::SetStrokeColor(const Color & color)
{
stroke_color_ = color;
}
2019-09-29 22:23:13 +08:00
void ShapeActor::SetStrokeWidth(float width)
{
stroke_width_ = std::max(width, 0.f);
}
2019-08-16 00:50:54 +08:00
void ShapeActor::SetStrokeStyle(StrokeStyle stroke_style)
{
2019-08-16 00:50:54 +08:00
stroke_style_ = stroke_style;
}
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-27 15:29:32 +08:00
if (geo_ && CheckVisibilty(rt))
{
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(
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-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
);
}
}
//-------------------------------------------------------
// LineActor
//-------------------------------------------------------
LineActor::LineActor()
{
}
2019-08-27 08:28:14 +08:00
LineActor::LineActor(Point const& begin, Point const& end)
{
2019-08-27 08:28:14 +08:00
SetLine(begin, end);
}
LineActor::~LineActor()
{
}
2019-08-27 08:28:14 +08:00
void LineActor::SetLine(Point const& begin, Point const& end)
{
2019-08-27 08:28:14 +08:00
if (begin_ != begin || end_ != end)
{
begin_ = begin;
end_ = end;
SetGeometry(Geometry::CreateLine(begin, end));
}
}
//-------------------------------------------------------
// RectActor
//-------------------------------------------------------
RectActor::RectActor()
{
}
RectActor::RectActor(Size const& size)
{
SetRectSize(size);
}
RectActor::~RectActor()
{
}
void RectActor::SetRectSize(Size const& size)
{
2019-08-27 08:28:14 +08:00
if (size != rect_size_)
{
rect_size_ = size;
SetGeometry(Geometry::CreateRect(Rect{ Point{}, size }));
}
}
//-------------------------------------------------------
// RoundRectActor
//-------------------------------------------------------
RoundRectActor::RoundRectActor()
{
}
RoundRectActor::RoundRectActor(Size const& size, Vec2 const& radius)
{
SetRoundedRect(size, radius);
}
RoundRectActor::~RoundRectActor()
{
}
void RoundRectActor::SetRadius(Vec2 const& radius)
{
2019-10-31 20:34:38 +08:00
SetRoundedRect(GetSize(), radius);
}
void RoundRectActor::SetRectSize(Size const& size)
{
SetRoundedRect(size, radius_);
}
void RoundRectActor::SetRoundedRect(Size const& size, Vec2 const& radius)
{
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));
}
}
//-------------------------------------------------------
// CircleActor
//-------------------------------------------------------
CircleActor::CircleActor()
: radius_(0.f)
{
}
2019-09-29 22:23:13 +08:00
CircleActor::CircleActor(float radius)
{
SetRadius(radius);
}
CircleActor::~CircleActor()
{
}
2019-09-29 22:23:13 +08:00
void CircleActor::SetRadius(float radius)
{
2019-08-27 08:28:14 +08:00
if (radius_ != radius)
{
radius_ = radius;
SetGeometry(Geometry::CreateCircle(Point{ radius, radius }, radius));
}
}
//-------------------------------------------------------
// EllipseActor
//-------------------------------------------------------
EllipseActor::EllipseActor()
{
}
EllipseActor::EllipseActor(Vec2 const& radius)
{
SetRadius(radius);
}
EllipseActor::~EllipseActor()
{
}
void EllipseActor::SetRadius(Vec2 const& radius)
{
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-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-08-26 10:22:58 +08:00
SetGeometry(
GeometrySink()
.BeginPath(points[0])
.AddLines(&points[1], points.size() - 1)
.EndPath(true)
.GetGeometry()
);
}
}
//-------------------------------------------------------
// PathActor
//-------------------------------------------------------
PathActor::PathActor()
{
}
PathActor::~PathActor()
{
}
void PathActor::BeginPath(Point const& begin_pos)
{
sink_.BeginPath(begin_pos);
}
void PathActor::EndPath(bool closed)
{
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
}
}
void PathActor::AddLine(Point const& point)
{
sink_.AddLine(point);
}
void PathActor::AddLines(Vector<Point> const& points)
{
sink_.AddLines(points);
}
void PathActor::AddBezier(Point const& point1, Point const& point2, Point const& point3)
{
sink_.AddBezier(point1, point2, point3);
}
2019-09-29 22:23:13 +08:00
void PathActor::AddArc(Point const& point, Size const& radius, float rotation, bool clockwise, bool is_small)
{
sink_.AddArc(point, radius, rotation, clockwise, is_small);
}
void PathActor::ClearPath()
{
geo_.SetGeometry(nullptr);
}
}