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

360 lines
9.3 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>
2020-02-05 19:56:22 +08:00
#include <kiwano/render/ShapeSink.h>
2020-02-08 00:17:31 +08:00
#include <kiwano/render/TextureRenderContext.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-08 00:17:31 +08:00
static CanvasPtr Create(Size const& 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-01-21 10:09:55 +08:00
void DrawLine(Point const& begin, Point const& end);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 画圆形边框
/// @param center 圆形原点
/// @param radius 圆形半径
2020-01-21 10:09:55 +08:00
void DrawCircle(Point const& center, float radius);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 画椭圆形边框
/// @param center 椭圆原点
/// @param radius 椭圆半径
2020-01-21 10:09:55 +08:00
void DrawEllipse(Point const& center, Vec2 const& radius);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 画矩形边框
/// @param rect 矩形
2020-01-21 10:09:55 +08:00
void DrawRect(Rect const& rect);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 画圆角矩形边框
/// @param rect 矩形
/// @param radius 矩形圆角半径
2020-01-21 10:09:55 +08:00
void DrawRoundedRect(Rect const& rect, Vec2 const& radius);
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-01-21 10:09:55 +08:00
void FillCircle(Point const& center, float radius);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 填充椭圆形
/// @param center 椭圆原点
/// @param radius 椭圆半径
2020-01-21 10:09:55 +08:00
void FillEllipse(Point const& center, Vec2 const& radius);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 填充矩形
/// @param rect 矩形
2020-01-21 10:09:55 +08:00
void FillRect(Rect const& rect);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 填充圆角矩形
/// @param rect 矩形
/// @param radius 矩形圆角半径
2020-01-21 10:09:55 +08:00
void FillRoundedRect(Rect const& rect, Vec2 const& radius);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 绘制纹理
/// @param texture 纹理
/// @param src_rect 纹理裁剪区域
/// @param dest_rect 绘制目标区域
2020-01-21 10:09:55 +08:00
void DrawTexture(TexturePtr texture, const Rect* src_rect = nullptr, const Rect* dest_rect = nullptr);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 绘制文字布局
/// @param text 文字
/// @param point 绘制文字的位置
2020-01-21 10:09:55 +08:00
void DrawTextLayout(String const& text, Point const& point);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 绘制文字布局
/// @param layout 文字布局
/// @param point 绘制布局的位置
2020-01-21 10:09:55 +08:00
void DrawTextLayout(TextLayout const& layout, Point const& point);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 开始绘制路径
/// @param begin_pos 路径起始点
2020-01-21 10:09:55 +08:00
void BeginPath(Point const& begin_pos);
/// \~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-01-21 10:09:55 +08:00
void AddLine(Point const& point);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加多条线段
/// @param points 端点集合
2020-01-21 10:09:55 +08:00
void AddLines(Vector<Point> const& points);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加一条三次方贝塞尔曲线
/// @param point1 贝塞尔曲线的第一个控制点
/// @param point2 贝塞尔曲线的第二个控制点
/// @param point3 贝塞尔曲线的终点
2020-01-21 10:09:55 +08:00
void AddBezier(Point const& point1, Point const& point2, Point const& point3);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加弧线
/// @param point 终点
/// @param radius 椭圆半径
/// @param rotation 椭圆旋转角度
/// @param clockwise 顺时针 or 逆时针
/// @param is_small 是否取小于 180° 的弧
2020-01-21 10:09:55 +08:00
void AddArc(Point const& point, Size const& radius, float rotation, bool clockwise = true, bool is_small = true);
/// \~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-01-21 10:09:55 +08:00
void Clear(Color const& clear_color);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置填充颜色
/// @param color 填充颜色
2020-01-21 10:09:55 +08:00
void SetFillColor(Color const& color);
/// \~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-01-21 10:09:55 +08:00
void SetStrokeColor(Color const& color);
/// \~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 width 轮廓宽度
2020-01-21 10:09:55 +08:00
void SetStrokeWidth(float width);
/// \~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 text_style 文字画刷样式
2020-01-21 10:09:55 +08:00
void SetTextStyle(TextStyle const& text_style);
/// \~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-01-21 10:09:55 +08:00
void SetBrushTransform(Transform const& transform);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置画刷二维变换矩阵
/// @param transform 二维变换矩阵
2020-01-21 10:09:55 +08:00
void SetBrushTransform(Matrix3x2 const& transform);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 添加一个图层
/// @param layer 图层
void PushLayer(Layer& 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-01-21 10:09:55 +08:00
void PushClipRect(Rect const& clip_rect);
/// \~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
float GetStrokeWidth() const;
/// \~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
void UpdateCache() const;
private:
2020-02-06 16:54:47 +08:00
float stroke_width_;
TextStyle text_style_;
StrokeStylePtr stroke_style_;
ShapeSink shape_sink_;
BrushPtr fill_brush_;
BrushPtr stroke_brush_;
2020-01-21 10:09:55 +08:00
mutable bool cache_expired_;
mutable TexturePtr texture_cached_;
mutable TextureRenderContextPtr ctx_;
};
/** @} */
2020-02-08 00:17:31 +08:00
inline float Canvas::GetStrokeWidth() const
{
return stroke_width_;
}
2020-01-21 10:09:55 +08:00
inline void Canvas::SetStrokeWidth(float width)
{
stroke_width_ = std::max(width, 0.f);
}
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;
}
inline void Canvas::SetTextStyle(TextStyle const& text_style)
{
text_style_ = text_style;
}
inline void Canvas::SetStrokeColor(Color const& color)
{
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-01-21 10:09:55 +08:00
inline void Canvas::SetFillColor(Color const& color)
{
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