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

333 lines
8.7 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>
#include <kiwano/2d/Frame.h>
2020-02-16 20:14:01 +08:00
#include <kiwano/render/ShapeMaker.h>
#include <kiwano/render/RenderContext.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(Canvas);
/**
* \addtogroup Actors
* @{
*/
/**
* \~chinese
2020-02-10 17:32:04 +08:00
* @brief
* @details
2020-01-21 10:09:55 +08:00
*/
class KGE_API Canvas : public Actor
{
public:
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建画布
/// @param size 画布大小
2020-02-19 12:09:50 +08:00
static CanvasPtr Create(const Size& size);
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
void BeginDraw();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 结束绘图
2020-01-21 10:09:55 +08:00
void EndDraw();
2020-02-05 19:56:22 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 画形状轮廓
/// @param shape 形状
2020-02-05 19:56:22 +08:00
void DrawShape(ShapePtr shape);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 画线段
/// @param begin 线段起点
/// @param end 线段终点
2020-02-19 12:09:50 +08:00
void DrawLine(const Point& begin, const Point& end);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 画圆形边框
/// @param center 圆形原点
/// @param radius 圆形半径
2020-02-19 12:09:50 +08:00
void DrawCircle(const Point& center, float radius);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 画椭圆形边框
/// @param center 椭圆原点
/// @param radius 椭圆半径
2020-02-19 12:09:50 +08:00
void DrawEllipse(const Point& center, const Vec2& radius);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 画矩形边框
/// @param rect 矩形
2020-02-19 12:09:50 +08:00
void DrawRect(const Rect& rect);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 画圆角矩形边框
/// @param rect 矩形
/// @param radius 矩形圆角半径
2020-02-19 12:09:50 +08:00
void DrawRoundedRect(const Rect& rect, const Vec2& radius);
2020-01-21 10:09:55 +08:00
2020-02-05 19:56:22 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 填充形状
/// @param shape 形状
2020-02-05 19:56:22 +08:00
void FillShape(ShapePtr shape);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 填充圆形
/// @param center 圆形原点
/// @param radius 圆形半径
2020-02-19 12:09:50 +08:00
void FillCircle(const Point& center, float radius);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 填充椭圆形
/// @param center 椭圆原点
/// @param radius 椭圆半径
2020-02-19 12:09:50 +08:00
void FillEllipse(const Point& center, const Vec2& radius);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 填充矩形
/// @param rect 矩形
2020-02-19 12:09:50 +08:00
void FillRect(const Rect& rect);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 填充圆角矩形
/// @param rect 矩形
/// @param radius 矩形圆角半径
2020-02-19 12:09:50 +08:00
void FillRoundedRect(const Rect& rect, const Vec2& radius);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief 绘制图像帧
/// @param frame 图像帧
/// @param pos 绘制图像的位置
void DrawFrame(FramePtr frame, const Point& pos);
/// \~chinese
/// @brief 绘制图像帧
/// @param frame 图像帧
/// @param pos 绘制图像的位置
/// @param size 渲染的图像大小
void DrawFrame(FramePtr frame, const Point& pos, const Size& size);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 绘制文字布局
/// @param text 文字
2020-02-16 12:53:18 +08:00
/// @param style 文字样式
2020-02-10 17:32:04 +08:00
/// @param point 绘制文字的位置
2020-02-19 12:09:50 +08:00
void DrawTextLayout(const String& text, const TextStyle& style, const Point& point);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 绘制文字布局
/// @param layout 文字布局
/// @param point 绘制布局的位置
2020-02-19 12:09:50 +08:00
void DrawTextLayout(TextLayoutPtr layout, const Point& point);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 开始绘制路径
/// @param begin_pos 路径起始点
2020-02-19 12:09:50 +08:00
void BeginPath(const Point& begin_pos);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 结束路径
/// @param closed 路径是否闭合
2020-01-21 10:09:55 +08:00
void EndPath(bool closed = false);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加一条线段
/// @param point 端点
2020-02-19 12:09:50 +08:00
void AddLine(const Point& point);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加多条线段
/// @param points 端点集合
2020-02-19 12:09:50 +08:00
void AddLines(const Vector<Point>& points);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加一条三次方贝塞尔曲线
/// @param point1 贝塞尔曲线的第一个控制点
/// @param point2 贝塞尔曲线的第二个控制点
/// @param point3 贝塞尔曲线的终点
2020-02-19 12:09:50 +08:00
void AddBezier(const Point& point1, const Point& point2, const Point& point3);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加弧线
/// @param point 终点
/// @param radius 椭圆半径
/// @param rotation 椭圆旋转角度
/// @param clockwise 顺时针 or 逆时针
/// @param is_small 是否取小于 180° 的弧
2020-02-19 12:09:50 +08:00
void AddArc(const Point& point, const Size& radius, float rotation, bool clockwise = true, bool is_small = true);
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
void StrokePath();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 以填充的方式绘制路径
2020-01-21 10:09:55 +08:00
void FillPath();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 清空画布
2020-01-21 10:09:55 +08:00
void Clear();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 清空画布
/// @param clear_color 清空颜色
2020-02-19 12:09:50 +08:00
void Clear(const Color& clear_color);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置填充颜色
/// @param color 填充颜色
2020-02-19 12:09:50 +08:00
void SetFillColor(const Color& color);
2020-01-21 10:09:55 +08:00
/// \~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-02-19 12:09:50 +08:00
void SetStrokeColor(const Color& color);
2020-01-21 10:09:55 +08:00
/// \~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 设置轮廓样式
/// @param stroke_style 轮廓样式
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 设置画刷
/// @param[in] brush 画刷
2020-01-21 10:09:55 +08:00
void SetBrush(BrushPtr brush);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置画刷二维变换
/// @param transform 二维变换
2020-02-19 12:09:50 +08:00
void SetBrushTransform(const Transform& transform);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置画刷二维变换矩阵
/// @param transform 二维变换矩阵
2020-02-19 12:09:50 +08:00
void SetBrushTransform(const Matrix3x2& transform);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加一个图层
/// @param layer 图层
2020-02-16 20:14:01 +08:00
void PushLayer(LayerPtr layer);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 删除最近添加的图层
void PopLayer();
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加一个裁剪区域
/// @param clip_rect 裁剪矩形
2020-02-19 12:09:50 +08:00
void PushClipRect(const Rect& clip_rect);
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
void PopClipRect();
/// \~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;
2020-02-08 00:17:31 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 清空画布大小并重设画布大小
2020-02-08 00:17:31 +08:00
void ResizeAndClear(Size size);
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
TexturePtr ExportToTexture() const;
void OnRender(RenderContext& ctx) override;
private:
2020-02-08 00:17:31 +08:00
Canvas();
2020-01-21 10:09:55 +08:00
private:
StrokeStylePtr stroke_style_;
ShapeMaker shape_maker_;
BrushPtr fill_brush_;
BrushPtr stroke_brush_;
TexturePtr texture_cached_;
RenderContextPtr render_ctx_;
2020-01-21 10:09:55 +08:00
};
/** @} */
2020-02-06 16:54:47 +08:00
inline void Canvas::SetStrokeStyle(StrokeStylePtr stroke_style)
2020-01-21 10:09:55 +08:00
{
stroke_style_ = stroke_style;
}
2020-02-19 12:09:50 +08:00
inline void Canvas::SetStrokeColor(const Color& color)
2020-01-21 10:09:55 +08:00
{
2020-02-08 00:17:31 +08:00
if (!stroke_brush_)
{
stroke_brush_ = new Brush;
}
2020-01-21 10:09:55 +08:00
stroke_brush_->SetColor(color);
}
2019-12-27 23:42:51 +08:00
2020-02-19 12:09:50 +08:00
inline void Canvas::SetFillColor(const Color& color)
2020-01-21 10:09:55 +08:00
{
2020-02-08 00:17:31 +08:00
if (!fill_brush_)
{
fill_brush_ = new Brush;
}
2020-01-21 10:09:55 +08:00
fill_brush_->SetColor(color);
}
inline void Canvas::SetFillBrush(BrushPtr brush)
{
fill_brush_ = brush;
}
inline void Canvas::SetStrokeBrush(BrushPtr brush)
{
stroke_brush_ = brush;
}
inline BrushPtr Canvas::GetFillBrush() const
{
return fill_brush_;
}
inline BrushPtr Canvas::GetStrokeBrush() const
{
return stroke_brush_;
}
2020-02-08 00:17:31 +08:00
2020-01-21 10:09:55 +08:00
} // namespace kiwano