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

440 lines
9.5 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.
#pragma once
2019-10-11 21:55:29 +08:00
#include <kiwano/2d/Actor.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/render/Brush.h>
2020-02-05 19:56:22 +08:00
#include <kiwano/render/Shape.h>
#include <kiwano/render/ShapeSink.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/render/StrokeStyle.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-02-06 16:54:47 +08:00
2020-01-21 10:09:55 +08:00
KGE_DECLARE_SMART_PTR(ShapeActor);
KGE_DECLARE_SMART_PTR(LineActor);
KGE_DECLARE_SMART_PTR(RectActor);
2020-02-06 16:54:47 +08:00
KGE_DECLARE_SMART_PTR(RoundedRectActor);
2020-01-21 10:09:55 +08:00
KGE_DECLARE_SMART_PTR(CircleActor);
KGE_DECLARE_SMART_PTR(EllipseActor);
KGE_DECLARE_SMART_PTR(PolygonActor);
/**
* \addtogroup Actors
* @{
*/
/**
* \~chinese
2020-02-10 17:32:04 +08:00
* @brief
2020-01-21 10:09:55 +08:00
*/
class KGE_API ShapeActor : public Actor
{
public:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建形状角色
2020-02-06 16:54:47 +08:00
static ShapeActorPtr Create();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 构造形状角色
2020-01-21 10:09:55 +08:00
ShapeActor();
virtual ~ShapeActor();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取填充画刷
2020-01-21 10:09:55 +08:00
BrushPtr GetFillBrush() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取轮廓画刷
2020-01-21 10:09:55 +08:00
BrushPtr GetStrokeBrush() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取线条宽度
2020-01-21 10:09:55 +08:00
float GetStrokeWidth() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取线条样式
2020-02-06 16:54:47 +08:00
StrokeStylePtr GetStrokeStyle() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取形状
2020-02-05 19:56:22 +08:00
ShapePtr GetShape() const;
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取边界
2020-01-21 10:09:55 +08:00
Rect GetBounds() const override;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取外切包围盒
2020-01-21 10:09:55 +08:00
Rect GetBoundingBox() const override;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 判断点是否在形状内
2020-01-21 10:09:55 +08:00
bool ContainsPoint(const Point& point) const override;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置填充颜色
/// @param color 填充颜色
2020-01-21 10:09:55 +08:00
void SetFillColor(Color const& color);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置填充画刷
/// @param[in] brush 填充画刷
2020-01-21 10:09:55 +08:00
void SetFillBrush(BrushPtr brush);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置轮廓颜色
/// @param color 轮廓颜色
2020-01-21 10:09:55 +08:00
void SetStrokeColor(Color const& color);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置轮廓画刷
/// @param[in] brush 轮廓画刷
2020-01-21 10:09:55 +08:00
void SetStrokeBrush(BrushPtr brush);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置线条宽度,默认为 1.0
2020-01-21 10:09:55 +08:00
void SetStrokeWidth(float width);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置线条样式
2020-02-06 16:54:47 +08:00
void SetStrokeStyle(StrokeStylePtr stroke_style);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置形状
2020-02-05 19:56:22 +08:00
void SetShape(ShapePtr shape);
2020-01-21 10:09:55 +08:00
void OnRender(RenderContext& ctx) override;
protected:
bool CheckVisibility(RenderContext& ctx) const override;
private:
2020-02-06 16:54:47 +08:00
BrushPtr fill_brush_;
BrushPtr stroke_brush_;
float stroke_width_;
StrokeStylePtr stroke_style_;
Rect bounds_;
ShapePtr shape_;
2020-01-21 10:09:55 +08:00
};
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 线段角色
2020-01-21 10:09:55 +08:00
class KGE_API LineActor : public ShapeActor
{
public:
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建线段角色
/// @param begin 线段起点
/// @param end 线段终点
2020-02-06 16:54:47 +08:00
static LineActorPtr Create(Point const& begin, Point const& end);
2020-01-21 10:09:55 +08:00
LineActor();
virtual ~LineActor();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取线段起点
2020-01-21 10:09:55 +08:00
Point const& GetBeginPoint() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取线段终点
2020-01-21 10:09:55 +08:00
Point const& GetEndPoint() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置线段起点
/// @param begin 线段起点
2020-01-21 10:09:55 +08:00
void SetBeginPoint(Point const& begin);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置线段终点
/// @param end 线段终点
2020-01-21 10:09:55 +08:00
void SetEndPoint(Point const& end);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置线段起点和终点
/// @param begin 线段起点
/// @param end 线段终点
2020-01-21 10:09:55 +08:00
void SetLine(Point const& begin, Point const& end);
private:
Point begin_;
Point end_;
};
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 矩形角色
2020-01-21 10:09:55 +08:00
class KGE_API RectActor : public ShapeActor
{
public:
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建矩形角色
/// @param size 矩形大小
2020-02-06 16:54:47 +08:00
static RectActorPtr Create(Size const& size);
2020-01-21 10:09:55 +08:00
RectActor();
virtual ~RectActor();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取矩形大小
2020-01-21 10:09:55 +08:00
Size const& GetRectSize() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置矩形大小
/// @param size 矩形大小
2020-01-21 10:09:55 +08:00
void SetRectSize(Size const& size);
private:
Size rect_size_;
};
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 圆角矩形角色
2020-02-06 16:54:47 +08:00
class KGE_API RoundedRectActor : public ShapeActor
2020-01-21 10:09:55 +08:00
{
public:
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建圆角矩形角色
/// @param size 圆角矩形大小
/// @param radius 圆角半径
2020-02-06 16:54:47 +08:00
static RoundedRectActorPtr Create(Size const& size, Vec2 const& radius);
RoundedRectActor();
2020-01-21 10:09:55 +08:00
2020-02-06 16:54:47 +08:00
virtual ~RoundedRectActor();
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取圆角半径
2020-01-21 10:09:55 +08:00
Vec2 GetRadius() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取圆角矩形大小
2020-01-21 10:09:55 +08:00
Size GetRectSize() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置圆角半径
/// @param radius 圆角半径
2020-01-21 10:09:55 +08:00
void SetRadius(Vec2 const& radius);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置圆角矩形大小
/// @param size 圆角矩形大小
2020-01-21 10:09:55 +08:00
void SetRectSize(Size const& size);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置圆角矩形
/// @param size 圆角矩形大小
/// @param radius 圆角半径
2020-01-21 10:09:55 +08:00
void SetRoundedRect(Size const& size, Vec2 const& radius);
private:
Size rect_size_;
Vec2 radius_;
};
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 圆形角色
2020-01-21 10:09:55 +08:00
class KGE_API CircleActor : public ShapeActor
{
public:
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建圆形角色
/// @param radius 圆形半径
2020-02-06 16:54:47 +08:00
static CircleActorPtr Create(float radius);
2020-01-21 10:09:55 +08:00
CircleActor();
virtual ~CircleActor();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取圆形半径
2020-01-21 10:09:55 +08:00
float GetRadius() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置圆形半径
/// @param radius 圆形半径
2020-01-21 10:09:55 +08:00
void SetRadius(float radius);
private:
float radius_;
};
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 椭圆角色
2020-01-21 10:09:55 +08:00
class KGE_API EllipseActor : public ShapeActor
{
public:
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建椭圆角色
/// @param radius 椭圆半径
2020-02-06 16:54:47 +08:00
static EllipseActorPtr Create(Vec2 const& radius);
2020-01-21 10:09:55 +08:00
EllipseActor();
virtual ~EllipseActor();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取椭圆半径
2020-01-21 10:09:55 +08:00
Vec2 GetRadius() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置椭圆半径
/// @param radius 椭圆半径
2020-01-21 10:09:55 +08:00
void SetRadius(Vec2 const& radius);
private:
Vec2 radius_;
};
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 多边形角色
2020-01-21 10:09:55 +08:00
class KGE_API PolygonActor : public ShapeActor
{
public:
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建多边形角色
/// @param points 多边形端点集合
2020-02-06 16:54:47 +08:00
static PolygonActorPtr Create(Vector<Point> const& points);
2020-01-21 10:09:55 +08:00
PolygonActor();
virtual ~PolygonActor();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置多边形端点
/// @param points 多边形端点集合
2020-01-21 10:09:55 +08:00
void SetVertices(Vector<Point> const& points);
};
/** @} */
inline void ShapeActor::SetStrokeColor(Color const& color)
{
if (!stroke_brush_)
{
stroke_brush_ = new Brush;
}
stroke_brush_->SetColor(color);
}
inline void ShapeActor::SetFillColor(Color const& color)
{
if (!fill_brush_)
{
fill_brush_ = new Brush;
}
fill_brush_->SetColor(color);
}
inline void ShapeActor::SetFillBrush(BrushPtr brush)
{
fill_brush_ = brush;
}
2020-02-11 12:09:59 +08:00
2020-01-21 10:09:55 +08:00
inline void ShapeActor::SetStrokeBrush(BrushPtr brush)
{
stroke_brush_ = brush;
}
2020-02-11 12:09:59 +08:00
2020-01-21 10:09:55 +08:00
inline BrushPtr ShapeActor::GetFillBrush() const
{
return fill_brush_;
}
2020-02-11 12:09:59 +08:00
2020-01-21 10:09:55 +08:00
inline BrushPtr ShapeActor::GetStrokeBrush() const
{
return stroke_brush_;
}
2020-02-11 12:09:59 +08:00
2020-01-21 10:09:55 +08:00
inline float ShapeActor::GetStrokeWidth() const
{
return stroke_width_;
}
2020-02-11 12:09:59 +08:00
2020-02-06 16:54:47 +08:00
inline StrokeStylePtr ShapeActor::GetStrokeStyle() const
2020-01-21 10:09:55 +08:00
{
return stroke_style_;
}
2020-02-11 12:09:59 +08:00
2020-02-05 19:56:22 +08:00
inline ShapePtr ShapeActor::GetShape() const
2020-01-21 10:09:55 +08:00
{
2020-02-05 19:56:22 +08:00
return shape_;
2020-01-21 10:09:55 +08:00
}
2020-02-11 12:09:59 +08:00
inline void ShapeActor::SetStrokeWidth(float width)
{
stroke_width_ = std::max(width, 0.f);
}
inline void ShapeActor::SetStrokeStyle(StrokeStylePtr stroke_style)
{
stroke_style_ = stroke_style;
}
2020-01-21 10:09:55 +08:00
inline Point const& LineActor::GetBeginPoint() const
{
return begin_;
}
2020-02-11 12:09:59 +08:00
2020-01-21 10:09:55 +08:00
inline Point const& LineActor::GetEndPoint() const
{
return end_;
}
2020-02-11 12:09:59 +08:00
2020-01-21 10:09:55 +08:00
inline void LineActor::SetBeginPoint(Point const& begin)
{
SetLine(begin, end_);
}
2020-02-11 12:09:59 +08:00
2020-01-21 10:09:55 +08:00
inline void LineActor::SetEndPoint(Point const& end)
{
SetLine(begin_, end);
}
inline Size const& RectActor::GetRectSize() const
{
return rect_size_;
}
2020-02-06 16:54:47 +08:00
inline Vec2 RoundedRectActor::GetRadius() const
2020-01-21 10:09:55 +08:00
{
return radius_;
}
2020-02-11 12:09:59 +08:00
2020-02-06 16:54:47 +08:00
inline Size RoundedRectActor::GetRectSize() const
2020-01-21 10:09:55 +08:00
{
return GetSize();
}
inline float CircleActor::GetRadius() const
{
return radius_;
}
inline Vec2 EllipseActor::GetRadius() const
{
return radius_;
}
2020-02-11 12:09:59 +08:00
2020-01-21 10:09:55 +08:00
} // namespace kiwano