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

256 lines
4.7 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 "Actor.h"
#include "../renderer/RenderTarget.h"
#ifdef DrawText
# undef DrawText
#endif
namespace kiwano
{
// »­²¼
class KGE_API Canvas
: public Actor
{
public:
Canvas();
virtual ~Canvas();
// ¿ªÊ¼»æÍ¼
void BeginDraw();
// ½áÊø»æÍ¼
void EndDraw();
// »­Ö±Ïß
void DrawLine(
Point const& begin,
Point const& end
);
// »­Ô²Ðα߿ò
void DrawCircle(
Point const& center,
Float32 radius
);
// »­ÍÖÔ²Ðα߿ò
void DrawEllipse(
Point const& center,
Vec2 const& radius
);
// »­¾ØÐα߿ò
void DrawRect(
Rect const& rect
);
// »­Ô²½Ç¾ØÐα߿ò
void DrawRoundedRect(
Rect const& rect,
Vec2 const& radius
);
// Ìî³äÔ²ÐÎ
void FillCircle(
Point const& center,
Float32 radius
);
// Ìî³äÍÖÔ²ÐÎ
void FillEllipse(
Point const& center,
Vec2 const& radius
);
// Ìî³ä¾ØÐÎ
void FillRect(
Rect const& rect
);
// Ìî³äÔ²½Ç¾ØÐÎ
void FillRoundedRect(
Rect const& rect,
Vec2 const& radius
);
// »­Í¼Æ¬
void DrawImage(
Image const& image,
const Rect* src_rect = nullptr,
const Rect* dest_rect = nullptr
);
// »­ÎÄ×Ö
void DrawText(
String const& text, /* ÎÄ×Ö */
Point const& point /* ÎÄ×ÖλÖà */
);
// ¿ªÊ¼»æÖÆÂ·¾¶
void BeginPath(
Point const& begin_pos /* ·¾¶Æðʼµã */
);
// ½áÊøÂ·¾¶
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, /* ÖÕµã */
Point const& radius, /* ÍÖÔ²°ë¾¶ */
Float32 rotation, /* ÍÖÔ²Ðýת½Ç¶È */
bool clockwise = true, /* ˳ʱÕë or ÄæÊ±Õë */
bool is_small = true /* ÊÇ·ñȡСÓÚ 180¡ã µÄ»¡ */
);
// ·¾¶Ãè±ß
void StrokePath();
// ·¾¶Ìî³ä
void FillPath();
// Çå¿Õ»­²¼
void Clear();
// Çå¿Õ»­²¼
void Clear(
Color const& clear_color
);
// ÉèÖÃÌî³äÑÕÉ«
void SetFillColor(
Color const& color
);
// ÉèÖÃÏßÌõÑÕÉ«
void SetStrokeColor(
Color const& color
);
// ÉèÖÃÏßÌõ¿í¶È
void SetStrokeWidth(
Float32 width
);
// ÉèÖÃÏßÌõÑùʽ
void SetStrokeStyle(
StrokeStyle stroke_style
);
// ÉèÖÃÎÄ×Ö×ÖÌå
void SetTextFont(
Font const& font
);
// ÉèÖÃÎÄ×Ö»­Ë¢Ñùʽ
void SetTextStyle(
TextStyle const& text_style
);
// ÉèÖû­±Ê͸Ã÷¶È
void SetBrushOpacity(
Float32 opacity
);
// »­±Ê¶þά±ä»»
void SetBrushTransform(
Transform const& transform
);
// »­±Ê¶þά±ä»»
void SetBrushTransform(
Matrix3x2 const& transform
);
// ÉèÖÃͼ²ã
void PushLayerArea(
LayerArea& area
);
// µ¯³öͼ²ã
void PopLayerArea();
// ÉèÖòüôÇøÓò
void PushClipRect(
Rect const& clip_rect
);
// µ¯³ö²Ã¼ôÇøÓò
void PopClipRect();
// »ñÈ¡Ìî³äÑÕÉ«
Color GetFillColor() const;
// »ñÈ¡ÏßÌõÑÕÉ«
Color GetStrokeColor() const;
// »ñÈ¡ÏßÌõ¿í¶È
Float32 GetStrokeWidth() const;
// »ñÈ¡»­±Ê͸Ã÷¶È
Float32 GetBrushOpacity() const;
// µ¼³öΪͼƬ
Image ExportToImage() const;
void OnRender(RenderTarget* rt) override;
protected:
void UpdateCache() const;
protected:
Float32 stroke_width_;
Color fill_color_;
Color stroke_color_;
Font text_font_;
TextStyle text_style_;
StrokeStyle stroke_style_;
GeometrySink geo_sink_;
ImageRenderTarget rt_;
mutable bool cache_expired_;
mutable Image image_cached_;
};
}