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

308 lines
5.6 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/Geometry.h>
#include <kiwano/renderer/StrokeStyle.h>
namespace kiwano
{
// ¶þάͼÐνÇÉ«
class KGE_API ShapeActor
: public Actor
{
public:
ShapeActor();
ShapeActor(
Geometry const& geometry
);
virtual ~ShapeActor();
// »ñÈ¡Ìî³äÑÕÉ«
inline Color GetFillColor() const { return fill_color_; }
// »ñÈ¡ÏßÌõÑÕÉ«
inline Color GetStrokeColor() const { return stroke_color_; }
// »ñÈ¡ÏßÌõ¿í¶È
inline float GetStrokeWidth() const { return stroke_width_; }
// »ñÈ¡ÏßÌõÑùʽ
inline StrokeStyle SetStrokeStyle() const { return stroke_style_; }
// »ñÈ¡ÐÎ×´
inline Geometry GetGeometry() const { return geo_; }
// »ñÈ¡±ß½ç
Rect GetBounds() const override;
// »ñÈ¡ÍâÇаüΧºÐ
Rect GetBoundingBox() const override;
// ÅжϵãÊÇ·ñÔÚÐÎ×´ÄÚ
bool ContainsPoint(const Point& point) const override;
// ÉèÖÃÌî³äÑÕÉ«
void SetFillColor(const Color& color);
// ÉèÖÃÏßÌõÑÕÉ«
void SetStrokeColor(const Color& color);
// ÉèÖÃÏßÌõ¿í¶È
void SetStrokeWidth(float width);
// ÉèÖÃÏßÌõÑùʽ
void SetStrokeStyle(StrokeStyle stroke_style);
// ÉèÖÃÐÎ×´
void SetGeometry(Geometry const& geometry);
void OnRender(RenderTarget* rt) override;
protected:
Color fill_color_;
Color stroke_color_;
float stroke_width_;
StrokeStyle stroke_style_;
Rect bounds_;
Geometry geo_;
};
// Ö±Ïß½ÇÉ«
class KGE_API LineActor
: public ShapeActor
{
public:
LineActor();
LineActor(
Point const& begin,
Point const& end
);
virtual ~LineActor();
inline Point const& GetBeginPoint() const { return begin_; }
inline Point const& GetEndPoint() const { return end_; }
inline void SetBeginPoint(Point const& begin)
{
SetLine(begin, end_);
}
inline void SetEndPoint(Point const& end)
{
SetLine(begin_, end);
}
void SetLine(
Point const& begin,
Point const& end
);
protected:
Point begin_;
Point end_;
};
// ¾ØÐνÇÉ«
class KGE_API RectActor
: public ShapeActor
{
public:
RectActor();
RectActor(
Size const& size
);
virtual ~RectActor();
inline Size const& GetRectSize() const { return rect_size_; }
void SetRectSize(Size const& size);
protected:
Size rect_size_;
};
// Ô²½Ç¾ØÐνÇÉ«
class KGE_API RoundRectActor
: public ShapeActor
{
public:
RoundRectActor();
RoundRectActor(
Size const& size,
Vec2 const& radius
);
virtual ~RoundRectActor();
inline Vec2 GetRadius() const { return radius_; }
inline Size GetRectSize() const { return GetSize(); }
void SetRadius(
Vec2 const& radius
);
void SetRectSize(
Size const& size
);
void SetRoundedRect(
Size const& size,
Vec2 const& radius
);
protected:
Size rect_size_;
Vec2 radius_;
};
// Ô²ÐνÇÉ«
class KGE_API CircleActor
: public ShapeActor
{
public:
CircleActor();
CircleActor(
float radius
);
virtual ~CircleActor();
inline float GetRadius() const { return radius_; }
void SetRadius(float radius);
protected:
float radius_;
};
// ÍÖÔ²½ÇÉ«
class KGE_API EllipseActor
: public ShapeActor
{
public:
EllipseActor();
EllipseActor(
Vec2 const& radius
);
virtual ~EllipseActor();
Vec2 GetRadius() const { return radius_; }
void SetRadius(
Vec2 const& radius
);
protected:
Vec2 radius_;
};
// ¶à±ßÐνÇÉ«
class KGE_API PolygonActor
: public ShapeActor
{
public:
PolygonActor();
PolygonActor(
Vector<Point> const& points
);
virtual ~PolygonActor();
void SetVertices(
Vector<Point> const& points
);
};
// ·¾¶½ÇÉ«
class KGE_API PathActor
: public ShapeActor
{
public:
PathActor();
virtual ~PathActor();
// ¿ªÊ¼Ìí¼Ó·¾¶
void BeginPath(
Point const& begin_pos = Point{} /* Æðʼµã */
);
// ½áÊøÂ·¾¶
void EndPath(
bool closed = true /* ·¾¶ÊÇ·ñ±ÕºÏ */
);
// Ìí¼ÓÒ»ÌõÏß¶Î
void AddLine(
Point const& point /* ¶Ëµã */
);
// Ìí¼Ó¶àÌõÏß¶Î
void AddLines(
Vector<Point> const& points
);
// Ìí¼ÓÒ»ÌõÈý´Î·½±´Èû¶ûÇúÏß
void AddBezier(
Point const& point1, /* ±´Èû¶ûÇúÏߵĵÚÒ»¸ö¿ØÖƵã */
Point const& point2, /* ±´Èû¶ûÇúÏߵĵڶþ¸ö¿ØÖƵã */
Point const& point3 /* ±´Èû¶ûÇúÏßµÄÖÕµã */
);
// Ìí¼Ó»¡Ïß
void AddArc(
Point const& point, /* ÖÕµã */
Size const& radius, /* ÍÖÔ²°ë¾¶ */
float rotation, /* ÍÖÔ²Ðýת½Ç¶È */
bool clockwise = true, /* ˳ʱÕë or ÄæÊ±Õë */
bool is_small = true /* ÊÇ·ñȡСÓÚ 180¡ã µÄ»¡ */
);
// Çå³ý·¾¶
void ClearPath();
protected:
GeometrySink sink_;
};
}