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

403 lines
9.0 KiB
C++

// 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.
#pragma once
#include <kiwano/2d/Actor.h>
#include <kiwano/renderer/Brush.h>
#include <kiwano/renderer/Geometry.h>
#include <kiwano/renderer/GeometrySink.h>
#include <kiwano/renderer/StrokeStyle.h>
namespace kiwano
{
KGE_DECLARE_SMART_PTR(ShapeActor);
KGE_DECLARE_SMART_PTR(LineActor);
KGE_DECLARE_SMART_PTR(RectActor);
KGE_DECLARE_SMART_PTR(RoundRectActor);
KGE_DECLARE_SMART_PTR(CircleActor);
KGE_DECLARE_SMART_PTR(EllipseActor);
KGE_DECLARE_SMART_PTR(PolygonActor);
KGE_DECLARE_SMART_PTR(PathShapeActor);
/**
* \addtogroup Actors
* @{
*/
/**
* \~chinese
* @brief ¶þάÐÎ×´½ÇÉ«
*/
class KGE_API ShapeActor
: public Actor
{
public:
/// \~chinese
/// @brief ¹¹Ôì¶þάÐÎ×´½ÇÉ«
ShapeActor();
virtual ~ShapeActor();
/// \~chinese
/// @brief »ñÈ¡Ìî³ä»­Ë¢
BrushPtr GetFillBrush() const;
/// \~chinese
/// @brief »ñÈ¡ÂÖÀª»­Ë¢
BrushPtr GetStrokeBrush() const;
/// \~chinese
/// @brief »ñÈ¡ÏßÌõ¿í¶È
float GetStrokeWidth() const;
/// \~chinese
/// @brief »ñÈ¡ÏßÌõÑùʽ
const StrokeStyle& GetStrokeStyle() const;
/// \~chinese
/// @brief »ñÈ¡ÐÎ×´
Geometry GetGeometry() const;
/// \~chinese
/// @brief »ñÈ¡±ß½ç
Rect GetBounds() const override;
/// \~chinese
/// @brief »ñÈ¡ÍâÇаüΧºÐ
Rect GetBoundingBox() const override;
/// \~chinese
/// @brief ÅжϵãÊÇ·ñÔÚÐÎ×´ÄÚ
bool ContainsPoint(const Point& point) const override;
/// \~chinese
/// @brief ÉèÖÃÌî³äÑÕÉ«
/// @param color Ìî³äÑÕÉ«
void SetFillColor(Color const& color);
/// \~chinese
/// @brief ÉèÖÃÌî³ä»­Ë¢
/// @param[in] brush Ìî³ä»­Ë¢
void SetFillBrush(BrushPtr brush);
/// \~chinese
/// @brief ÉèÖÃÂÖÀªÑÕÉ«
/// @param color ÂÖÀªÑÕÉ«
void SetStrokeColor(Color const& color);
/// \~chinese
/// @brief ÉèÖÃÂÖÀª»­Ë¢
/// @param[in] brush ÂÖÀª»­Ë¢
void SetStrokeBrush(BrushPtr brush);
/// \~chinese
/// @brief ÉèÖÃÏßÌõ¿í¶È£¬Ä¬ÈÏΪ 1.0
void SetStrokeWidth(float width);
/// \~chinese
/// @brief ÉèÖÃÏßÌõÑùʽ
void SetStrokeStyle(const StrokeStyle& stroke_style);
/// \~chinese
/// @brief ÉèÖü¸ºÎÐÎ×´
void SetGeometry(Geometry const& geometry);
void OnRender(RenderContext& ctx) override;
protected:
bool CheckVisibility(RenderContext& ctx) const override;
private:
BrushPtr fill_brush_;
BrushPtr stroke_brush_;
float stroke_width_;
StrokeStyle stroke_style_;
Rect bounds_;
Geometry geo_;
};
/// \~chinese
/// @brief Ïß¶ÎͼÐνÇÉ«
class KGE_API LineActor
: public ShapeActor
{
public:
LineActor();
virtual ~LineActor();
/// \~chinese
/// @brief »ñÈ¡Ïß¶ÎÆðµã
Point const& GetBeginPoint() const;
/// \~chinese
/// @brief »ñÈ¡Ïß¶ÎÖÕµã
Point const& GetEndPoint() const;
/// \~chinese
/// @brief ÉèÖÃÏß¶ÎÆðµã
/// @param begin Ïß¶ÎÆðµã
void SetBeginPoint(Point const& begin);
/// \~chinese
/// @brief ÉèÖÃÏß¶ÎÖÕµã
/// @param end Ïß¶ÎÖÕµã
void SetEndPoint(Point const& end);
/// \~chinese
/// @brief ÉèÖþØÐδóС
/// @param begin Ïß¶ÎÆðµã
/// @param end Ïß¶ÎÖÕµã
void SetLine(Point const& begin, Point const& end);
private:
Point begin_;
Point end_;
};
/// \~chinese
/// @brief ¾ØÐνÇÉ«
class KGE_API RectActor
: public ShapeActor
{
public:
RectActor();
virtual ~RectActor();
/// \~chinese
/// @brief »ñÈ¡¾ØÐδóС
Size const& GetRectSize() const;
/// \~chinese
/// @brief ÉèÖþØÐδóС
/// @param size ¾ØÐδóС
void SetRectSize(Size const& size);
private:
Size rect_size_;
};
/// \~chinese
/// @brief Ô²½Ç¾ØÐνÇÉ«
class KGE_API RoundRectActor
: public ShapeActor
{
public:
RoundRectActor();
virtual ~RoundRectActor();
/// \~chinese
/// @brief »ñȡԲ½Ç°ë¾¶
Vec2 GetRadius() const;
/// \~chinese
/// @brief »ñȡԲ½Ç¾ØÐδóС
Size GetRectSize() const;
/// \~chinese
/// @brief ÉèÖÃÔ²½Ç°ë¾¶
/// @param radius Ô²½Ç°ë¾¶
void SetRadius(Vec2 const& radius);
/// \~chinese
/// @brief ÉèÖÃÔ²½Ç¾ØÐδóС
/// @param size Ô²½Ç¾ØÐδóС
void SetRectSize(Size const& size);
/// \~chinese
/// @brief ÉèÖÃÔ²½Ç¾ØÐÎ
/// @param size Ô²½Ç¾ØÐδóС
/// @param radius Ô²½Ç°ë¾¶
void SetRoundedRect(Size const& size, Vec2 const& radius);
private:
Size rect_size_;
Vec2 radius_;
};
/// \~chinese
/// @brief Ô²ÐνÇÉ«
class KGE_API CircleActor
: public ShapeActor
{
public:
CircleActor();
virtual ~CircleActor();
/// \~chinese
/// @brief »ñȡԲÐΰ뾶
float GetRadius() const;
/// \~chinese
/// @brief ÉèÖÃÔ²Ðΰ뾶
/// @param radius Ô²Ðΰ뾶
void SetRadius(float radius);
private:
float radius_;
};
/// \~chinese
/// @brief ÍÖÔ²½ÇÉ«
class KGE_API EllipseActor
: public ShapeActor
{
public:
EllipseActor();
virtual ~EllipseActor();
/// \~chinese
/// @brief »ñÈ¡ÍÖÔ²°ë¾¶
Vec2 GetRadius() const;
/// \~chinese
/// @brief ÉèÖÃÍÖÔ²°ë¾¶
/// @param radius ÍÖÔ²°ë¾¶
void SetRadius(Vec2 const& radius);
private:
Vec2 radius_;
};
/// \~chinese
/// @brief ¶à±ßÐνÇÉ«
class KGE_API PolygonActor
: public ShapeActor
{
public:
PolygonActor();
virtual ~PolygonActor();
/// \~chinese
/// @brief ÉèÖöà±ßÐζ˵ã
/// @param points ¶à±ßÐζ˵㼯ºÏ
void SetVertices(Vector<Point> const& points);
};
/// \~chinese
/// @brief ·¾¶Í¼ÐνÇÉ«
class KGE_API PathShapeActor
: public ShapeActor
{
public:
PathShapeActor();
virtual ~PathShapeActor();
/// \~chinese
/// @brief ¿ªÊ¼Ìí¼Ó·¾¶
/// @param begin_pos Æðʼµã
void BeginPath(Point const& begin_pos = Point());
/// \~chinese
/// @brief ½áÊøÌí¼Ó·¾¶
/// @param closed ·¾¶ÊÇ·ñ±ÕºÏ
void EndPath(bool closed = true);
/// \~chinese
/// @brief Ìí¼ÓÒ»ÌõÏß¶Î
/// @param point Ï߶ζ˵ã
void AddLine(Point const& point);
/// \~chinese
/// @brief Ìí¼Ó¶àÌõÏß¶Î
/// @param points Ï߶ζ˵㼯ºÏ
void AddLines(Vector<Point> const& points);
/// \~chinese
/// @brief Ìí¼ÓÒ»ÌõÈý´Î·½±´Èû¶ûÇúÏß
/// @param point1 ±´Èû¶ûÇúÏߵĵÚÒ»¸ö¿ØÖƵã
/// @param point2 ±´Èû¶ûÇúÏߵĵڶþ¸ö¿ØÖƵã
/// @param point3 ±´Èû¶ûÇúÏßµÄÖÕµã
void AddBezier(Point const& point1, Point const& point2, Point const& point3);
/// \~chinese
/// @brief Ìí¼Ó»¡Ïß
/// @param point ÍÖÔ²Ô²ÐÄ
/// @param radius ÍÖÔ²°ë¾¶
/// @param rotation ÍÖÔ²Ðýת½Ç¶È
/// @param clockwise ˳ʱÕë or ÄæÊ±Õë
/// @param is_small ÊÇ·ñȡСÓÚ 180¡ã µÄ»¡
void AddArc(Point const& point, Size const& radius, float rotation, bool clockwise = true, bool is_small = true);
/// \~chinese
/// @brief Çå³ý·¾¶
void ClearPath();
private:
GeometrySink sink_;
};
/** @} */
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; }
inline void ShapeActor::SetStrokeBrush(BrushPtr brush) { stroke_brush_ = brush; }
inline BrushPtr ShapeActor::GetFillBrush() const { return fill_brush_; }
inline BrushPtr ShapeActor::GetStrokeBrush() const { return stroke_brush_; }
inline float ShapeActor::GetStrokeWidth() const { return stroke_width_; }
inline const StrokeStyle& ShapeActor::GetStrokeStyle() const { return stroke_style_; }
inline Geometry ShapeActor::GetGeometry() const { return geo_; }
inline Point const& LineActor::GetBeginPoint() const { return begin_; }
inline Point const& LineActor::GetEndPoint() const { return end_; }
inline void LineActor::SetBeginPoint(Point const& begin) { SetLine(begin, end_); }
inline void LineActor::SetEndPoint(Point const& end) { SetLine(begin_, end); }
inline Size const& RectActor::GetRectSize() const { return rect_size_; }
inline Vec2 RoundRectActor::GetRadius() const { return radius_; }
inline Size RoundRectActor::GetRectSize() const { return GetSize(); }
inline float CircleActor::GetRadius() const { return radius_; }
inline Vec2 EllipseActor::GetRadius() const { return radius_; }
}