Magic_Game/core/base/Canvas.h

241 lines
4.6 KiB
C++

// Copyright (c) 2016-2018 Easy2D - 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 "Node.h"
#include "Font.hpp"
#include "TextStyle.hpp"
#include "TextRenderer.h"
#undef DrawText
namespace easy2d
{
// »­²¼
class Canvas
: public Node
{
public:
Canvas();
Canvas(
Size const& size
);
Canvas(
float width,
float height
);
virtual ~Canvas();
// ¿ªÊ¼»æÍ¼
void BeginDraw();
// ½áÊø»æÍ¼
void EndDraw();
// »­Ö±Ïß
void DrawLine(
const Point& begin,
const Point& end
);
// »­Ô²Ðα߿ò
void DrawCircle(
const Point& center,
float radius
);
// »­ÍÖÔ²Ðα߿ò
void DrawEllipse(
const Point& center,
float radius_x,
float radius_y
);
// »­¾ØÐα߿ò
void DrawRect(
const Rect& rect
);
// »­Ô²½Ç¾ØÐα߿ò
void DrawRoundedRect(
const Rect& rect,
float radius_x,
float radius_y
);
// »­Í¼Æ¬
void DrawImage(
SpImage const& image,
float opacity = 1.f
);
// »­ÎÄ×Ö
void DrawText(
std::wstring const& text, /* ÎÄ×Ö */
Point const& point /* ÎÄ×ÖλÖà */
);
// »­¼¸ºÎͼÐα߿ò
void DrawGeometry(
SpGeometry const& geo
);
// Ìî³äÔ²ÐÎ
void FillCircle(
const Point& center,
float radius
);
// Ìî³äÍÖÔ²ÐÎ
void FillEllipse(
const Point& center,
float radius_x,
float radius_y
);
// Ìî³ä¾ØÐÎ
void FillRect(
const Rect& rect
);
// Ìî³äÔ²½Ç¾ØÐÎ
void FillRoundedRect(
const Rect& rect,
float radius_x,
float radius_y
);
// Ìî³ä¼¸ºÎͼÐÎ
void FillGeometry(
SpGeometry const& geo
);
// ¿ªÊ¼»æÖÆÂ·¾¶
void BeginPath(
Point const& begin_pos /* ·¾¶Æðʼµã */
);
// ½áÊøÂ·¾¶
void EndPath(
bool closed = true /* ·¾¶ÊÇ·ñ±ÕºÏ */
);
// Ìí¼ÓÒ»ÌõÏß¶Î
void AddLine(
Point const& point /* ¶Ëµã */
);
// Ìí¼Ó¶àÌõÏß¶Î
void AddLines(
std::vector<Point> const& points
);
// Ìí¼ÓÒ»ÌõÈý´Î·½±´Èû¶ûÇúÏß
void AddBezier(
Point const& point1, /* ±´Èû¶ûÇúÏߵĵÚÒ»¸ö¿ØÖƵã */
Point const& point2, /* ±´Èû¶ûÇúÏߵĵڶþ¸ö¿ØÖƵã */
Point const& point3 /* ±´Èû¶ûÇúÏßµÄÖÕµã */
);
// Ìí¼Ó»¡Ïß
void AddArc(
Point const& point, /* ÖÕµã */
Point const& radius, /* ÍÖÔ²°ë¾¶ */
float rotation, /* ÍÖÔ²Ðýת½Ç¶È */
bool clockwise = true, /* ˳ʱÕë or ÄæÊ±Õë */
bool is_small = true /* ÊÇ·ñȡСÓÚ 180¡ã µÄ»¡ */
);
// ·¾¶Ãè±ß
void StrokePath();
// ·¾¶Ìî³ä
void FillPath();
// Çå¿Õ»­²¼
void Clear();
// ÉèÖÃÌî³äÑÕÉ«
void SetFillColor(
const Color& color
);
// ÉèÖÃÏßÌõÑÕÉ«
void SetStrokeColor(
const Color& color
);
// ÉèÖÃÏßÌõ¿í¶È
void SetStrokeWidth(
float width
);
// ÉèÖÃÏßÌõÏཻÑùʽ
void SetOutlineJoinStyle(
StrokeStyle outline_join
);
// ÉèÖÃÎÄ×Ö»­Ë¢Ñùʽ
void SetTextStyle(
Font const& font,
TextStyle const& text_style
);
// »ñÈ¡Ìî³äÑÕÉ«
Color GetFillColor() const;
// »ñÈ¡ÏßÌõÑÕÉ«
Color GetStrokeColor() const;
// »ñÈ¡ÏßÌõ¿í¶È
float GetStrokeWidth() const;
// ±ä»»»­±Ê
void SetBrushTransform(
Matrix const& transform
);
// µ¼³öΪͼƬ
SpImage ExportToImage() const;
virtual void OnRender() override;
protected:
CpBitmap const& GetBitmap() const;
protected:
mutable bool cache_expired_;
mutable CpBitmap bitmap_cached_;
float stroke_width_;
Font text_font_;
TextStyle text_style_;
CpPathGeometry current_geometry_;
CpGeometrySink current_sink_;
CpStrokeStyle outline_join_style_;
CpSolidColorBrush fill_brush_;
CpSolidColorBrush stroke_brush_;
CpSolidColorBrush text_brush_;
CpTextRenderer text_renderer_;
CpBitmapRenderTarget render_target_;
};
}