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

308 lines
6.9 KiB
C++
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +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:
2020-01-21 10:09:55 +08:00
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
// 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>
2020-01-17 16:55:47 +08:00
#include <kiwano/render/Renderer.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-02-12 10:04:16 +08:00
ShapeActorPtr ShapeActor::Create(ShapePtr shape)
2020-02-06 16:54:47 +08:00
{
2020-02-20 22:27:09 +08:00
ShapeActorPtr ptr = memory::New<ShapeActor>();
2020-02-12 10:04:16 +08:00
if (ptr)
{
ptr->SetShape(shape);
}
return ptr;
}
ShapeActorPtr ShapeActor::Create(ShapePtr shape, const Color& fill_color, const Color& stroke_color)
{
ShapeActorPtr ptr = ShapeActor::Create(shape);
if (ptr)
{
ptr->SetFillColor(fill_color);
ptr->SetStrokeColor(stroke_color);
}
return ptr;
}
ShapeActorPtr ShapeActor::Create(ShapePtr shape, BrushPtr fill_brush, BrushPtr stroke_brush)
{
ShapeActorPtr ptr = ShapeActor::Create(shape);
if (ptr)
{
ptr->SetFillBrush(fill_brush);
ptr->SetStrokeBrush(stroke_brush);
}
2020-02-06 16:54:47 +08:00
return ptr;
}
2020-02-05 19:56:22 +08:00
2020-01-21 10:09:55 +08:00
ShapeActor::ShapeActor()
{
}
ShapeActor::~ShapeActor() {}
Rect ShapeActor::GetBounds() const
{
return bounds_;
}
Rect ShapeActor::GetBoundingBox() const
{
2020-02-05 19:56:22 +08:00
if (!shape_)
2020-01-21 10:09:55 +08:00
return Rect{};
2020-02-05 19:56:22 +08:00
return shape_->GetBoundingBox(GetTransformMatrix());
2020-01-21 10:09:55 +08:00
}
bool ShapeActor::ContainsPoint(const Point& point) const
{
2020-02-05 19:56:22 +08:00
if (!shape_)
return false;
return shape_->ContainsPoint(point, &GetTransformMatrix());
2020-01-21 10:09:55 +08:00
}
2020-02-05 19:56:22 +08:00
void ShapeActor::SetShape(ShapePtr shape)
2020-01-21 10:09:55 +08:00
{
2020-02-05 19:56:22 +08:00
shape_ = shape;
if (shape_)
2020-01-21 10:09:55 +08:00
{
2020-02-05 19:56:22 +08:00
bounds_ = shape_->GetBoundingBox();
2020-01-21 10:09:55 +08:00
SetSize(bounds_.GetSize());
}
else
{
bounds_ = Rect{};
SetSize(0.f, 0.f);
}
}
void ShapeActor::OnRender(RenderContext& ctx)
{
2020-02-05 19:56:22 +08:00
if (shape_)
2020-01-21 10:09:55 +08:00
{
2020-02-05 19:56:22 +08:00
if (stroke_brush_)
{
ctx.SetCurrentBrush(stroke_brush_);
2020-02-16 20:14:01 +08:00
ctx.SetCurrentStrokeStyle(stroke_style_);
ctx.DrawShape(*shape_);
2020-02-05 19:56:22 +08:00
}
if (fill_brush_)
{
ctx.SetCurrentBrush(fill_brush_);
ctx.FillShape(*shape_);
}
2020-01-21 10:09:55 +08:00
}
}
bool ShapeActor::CheckVisibility(RenderContext& ctx) const
{
2020-02-05 19:56:22 +08:00
return shape_ && Actor::CheckVisibility(ctx);
2020-01-21 10:09:55 +08:00
}
//-------------------------------------------------------
// LineActor
//-------------------------------------------------------
2020-02-19 12:09:50 +08:00
LineActorPtr LineActor::Create(const Point& begin, const Point& end)
2020-02-06 16:54:47 +08:00
{
2020-02-20 22:27:09 +08:00
LineActorPtr ptr = memory::New<LineActor>();
2020-02-06 16:54:47 +08:00
if (ptr)
{
ptr->SetLine(begin, end);
}
return ptr;
}
2020-01-21 10:09:55 +08:00
LineActor::LineActor() {}
LineActor::~LineActor() {}
2020-02-19 12:09:50 +08:00
void LineActor::SetLine(const Point& begin, const Point& end)
2020-01-21 10:09:55 +08:00
{
if (begin_ != begin || end_ != end)
{
begin_ = begin;
end_ = end;
2020-02-05 19:56:22 +08:00
SetShape(Shape::CreateLine(begin, end));
2020-01-21 10:09:55 +08:00
}
}
//-------------------------------------------------------
// RectActor
//-------------------------------------------------------
2020-02-19 12:09:50 +08:00
RectActorPtr RectActor::Create(const Size& size)
2020-02-06 16:54:47 +08:00
{
2020-02-20 22:27:09 +08:00
RectActorPtr ptr = memory::New<RectActor>();
2020-02-06 16:54:47 +08:00
if (ptr)
{
ptr->SetRectSize(size);
}
return ptr;
}
2020-01-21 10:09:55 +08:00
RectActor::RectActor() {}
RectActor::~RectActor() {}
2020-02-19 12:09:50 +08:00
void RectActor::SetRectSize(const Size& size)
2020-01-21 10:09:55 +08:00
{
if (size != rect_size_)
{
rect_size_ = size;
2020-02-05 19:56:22 +08:00
SetShape(Shape::CreateRect(Rect{ Point{}, size }));
2020-01-21 10:09:55 +08:00
}
}
//-------------------------------------------------------
2020-02-06 16:54:47 +08:00
// RoundedRectActor
2020-01-21 10:09:55 +08:00
//-------------------------------------------------------
2020-02-19 12:09:50 +08:00
RoundedRectActorPtr RoundedRectActor::Create(const Size& size, const Vec2& radius)
2020-02-06 16:54:47 +08:00
{
2020-02-20 22:27:09 +08:00
RoundedRectActorPtr ptr = memory::New<RoundedRectActor>();
2020-02-06 16:54:47 +08:00
if (ptr)
{
ptr->SetRoundedRect(size, radius);
}
return ptr;
}
RoundedRectActor::RoundedRectActor() {}
2020-01-21 10:09:55 +08:00
2020-02-06 16:54:47 +08:00
RoundedRectActor::~RoundedRectActor() {}
2020-01-21 10:09:55 +08:00
2020-02-19 12:09:50 +08:00
void RoundedRectActor::SetRadius(const Vec2& radius)
2020-01-21 10:09:55 +08:00
{
SetRoundedRect(GetSize(), radius);
}
2020-02-19 12:09:50 +08:00
void RoundedRectActor::SetRectSize(const Size& size)
2020-01-21 10:09:55 +08:00
{
SetRoundedRect(size, radius_);
}
2020-02-19 12:09:50 +08:00
void RoundedRectActor::SetRoundedRect(const Size& size, const Vec2& radius)
2020-01-21 10:09:55 +08:00
{
if (rect_size_ != size || radius_ != radius)
{
rect_size_ = size;
radius_ = radius;
2020-02-05 19:56:22 +08:00
SetShape(Shape::CreateRoundedRect(Rect{ Point{}, size }, radius));
2020-01-21 10:09:55 +08:00
}
}
//-------------------------------------------------------
// CircleActor
//-------------------------------------------------------
2020-02-06 16:54:47 +08:00
CircleActorPtr CircleActor::Create(float radius)
{
2020-02-20 22:27:09 +08:00
CircleActorPtr ptr = memory::New<CircleActor>();
2020-02-06 16:54:47 +08:00
if (ptr)
{
ptr->SetRadius(radius);
}
return ptr;
}
2020-01-21 10:09:55 +08:00
CircleActor::CircleActor()
: radius_(0.f)
{
}
CircleActor::~CircleActor() {}
void CircleActor::SetRadius(float radius)
{
if (radius_ != radius)
{
radius_ = radius;
2020-02-05 19:56:22 +08:00
SetShape(Shape::CreateCircle(Point{ radius, radius }, radius));
2020-01-21 10:09:55 +08:00
}
}
//-------------------------------------------------------
// EllipseActor
//-------------------------------------------------------
2020-02-19 12:09:50 +08:00
EllipseActorPtr EllipseActor::Create(const Vec2& radius)
2020-02-06 16:54:47 +08:00
{
2020-02-20 22:27:09 +08:00
EllipseActorPtr ptr = memory::New<EllipseActor>();
2020-02-06 16:54:47 +08:00
if (ptr)
{
ptr->SetRadius(radius);
}
return ptr;
}
2020-01-21 10:09:55 +08:00
EllipseActor::EllipseActor() {}
EllipseActor::~EllipseActor() {}
2020-02-19 12:09:50 +08:00
void EllipseActor::SetRadius(const Vec2& radius)
2020-01-21 10:09:55 +08:00
{
if (radius_ != radius)
{
radius_ = radius;
2020-02-05 19:56:22 +08:00
SetShape(Shape::CreateEllipse(radius, radius));
2020-01-21 10:09:55 +08:00
}
}
//-------------------------------------------------------
// PolygonActor
//-------------------------------------------------------
2020-02-19 12:09:50 +08:00
PolygonActorPtr PolygonActor::Create(const Vector<Point>& points)
2020-02-06 16:54:47 +08:00
{
2020-02-20 22:27:09 +08:00
PolygonActorPtr ptr = memory::New<PolygonActor>();
2020-02-06 16:54:47 +08:00
if (ptr)
{
ptr->SetVertices(points);
}
return ptr;
}
2020-01-21 10:09:55 +08:00
PolygonActor::PolygonActor() {}
PolygonActor::~PolygonActor() {}
2020-02-19 12:09:50 +08:00
void PolygonActor::SetVertices(const Vector<Point>& points)
2020-01-21 10:09:55 +08:00
{
if (points.size() > 1)
{
2020-02-16 20:14:01 +08:00
ShapeMakerPtr maker = ShapeMaker::Create();
maker->BeginPath(points[0]);
maker->AddLines(&points[1], points.size() - 1);
maker->EndPath(true);
SetShape(maker->GetShape());
2020-01-21 10:09:55 +08:00
}
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano