Kiwano Engine v1.3.x
Canvas.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/2d/Actor.h>
23#include <kiwano/2d/SpriteFrame.h>
24#include <kiwano/render/ShapeMaker.h>
25#include <kiwano/render/RenderContext.h>
26
27namespace kiwano
28{
29class CanvasRenderContext;
30
41class KGE_API Canvas : public Actor
42{
43public:
47 Canvas();
48
52 Canvas(const PixelSize& size);
53
56 RefPtr<CanvasRenderContext> GetContext2D() const;
57
61 void ResizeAndClear(const PixelSize& size);
62
65 RefPtr<Texture> GetTexture() const;
66
67 void OnRender(RenderContext& ctx) override;
68
69private:
70 RefPtr<Texture> texture_cached_;
71 RefPtr<RenderContext> render_ctx_;
72};
73
76class KGE_API CanvasRenderContext : public ObjectBase
77{
78public:
81 void BeginDraw();
82
85 void EndDraw();
86
90 void DrawActor(RefPtr<Actor> actor);
91
95 void DrawShape(RefPtr<Shape> shape);
96
101 void DrawLine(const Point& begin, const Point& end);
102
107 void DrawCircle(const Point& center, float radius);
108
113 void DrawEllipse(const Point& center, const Vec2& radius);
114
118 void DrawRect(const Rect& rect);
119
124 void DrawRoundedRect(const Rect& rect, const Vec2& radius);
125
129 void FillShape(RefPtr<Shape> shape);
130
135 void FillCircle(const Point& center, float radius);
136
141 void FillEllipse(const Point& center, const Vec2& radius);
142
146 void FillRect(const Rect& rect);
147
152 void FillRoundedRect(const Rect& rect, const Vec2& radius);
153
159 void DrawTexture(RefPtr<Texture> texture, const Point& pos, const Rect* crop_rect = nullptr);
160
167 void DrawTexture(RefPtr<Texture> texture, const Point& pos, const Size& size, const Rect* crop_rect = nullptr);
168
173 void DrawSpriteFrame(const SpriteFrame& frame, const Point& pos);
174
180 void DrawSpriteFrame(const SpriteFrame& frame, const Point& pos, const Size& size);
181
187 void DrawTextLayout(StringView text, const TextStyle& style, const Point& point);
188
193 void DrawTextLayout(RefPtr<TextLayout> layout, const Point& point);
194
197 void Clear();
198
202 void Clear(const Color& clear_color);
203
207 void SetFillColor(const Color& color);
208
212 void SetFillBrush(RefPtr<Brush> brush);
213
216 RefPtr<Brush> GetFillBrush() const;
217
221 void SetStrokeColor(const Color& color);
222
226 void SetStrokeBrush(RefPtr<Brush> brush);
227
230 RefPtr<Brush> GetStrokeBrush() const;
231
235 void SetStrokeStyle(RefPtr<StrokeStyle> stroke_style);
236
239 RefPtr<StrokeStyle> GetStrokeStyle() const;
240
244 void PushClipRect(const Rect& clip_rect);
245
248 void PopClipRect();
249
253 void PushLayer(RefPtr<Layer> layer);
254
257 void PopLayer();
258
261 Size GetSize() const;
262
265 void SetTransform(const Matrix3x2& matrix);
266
269 void SetAntialiasMode(bool enabled);
270
273 void SetTextAntialiasMode(TextAntialiasMode mode);
274
275 virtual ~CanvasRenderContext();
276
277private:
278 friend class Canvas;
279
281
282private:
284 RefPtr<Brush> fill_brush_;
285 RefPtr<Brush> stroke_brush_;
286 RefPtr<StrokeStyle> stroke_style_;
287};
288
292{
293 return texture_cached_;
294}
295
297{
298 KGE_ASSERT(ctx_);
299 ctx_->BeginDraw();
300}
301
303{
304 KGE_ASSERT(ctx_);
305 ctx_->EndDraw();
306}
307
309{
310 KGE_ASSERT(ctx_);
311 if (actor)
312 {
313 actor->OnRender(*ctx_);
314 }
315}
316
318{
319 KGE_ASSERT(ctx_);
320 if (shape)
321 {
322 ctx_->SetCurrentBrush(stroke_brush_);
323 ctx_->SetCurrentStrokeStyle(stroke_style_);
324 ctx_->DrawShape(*shape);
325 }
326}
327
328inline void CanvasRenderContext::DrawLine(const Point& begin, const Point& end)
329{
330 KGE_ASSERT(ctx_);
331
332 ctx_->SetCurrentBrush(stroke_brush_);
333 ctx_->SetCurrentStrokeStyle(stroke_style_);
334 ctx_->DrawLine(begin, end);
335}
336
337inline void CanvasRenderContext::DrawCircle(const Point& center, float radius)
338{
339 KGE_ASSERT(ctx_);
340
341 ctx_->SetCurrentBrush(stroke_brush_);
342 ctx_->SetCurrentStrokeStyle(stroke_style_);
343 ctx_->DrawCircle(center, radius);
344}
345
346inline void CanvasRenderContext::DrawEllipse(const Point& center, const Vec2& radius)
347{
348 KGE_ASSERT(ctx_);
349
350 ctx_->SetCurrentBrush(stroke_brush_);
351 ctx_->SetCurrentStrokeStyle(stroke_style_);
352 ctx_->DrawEllipse(center, radius);
353}
354
355inline void CanvasRenderContext::DrawRect(const Rect& rect)
356{
357 KGE_ASSERT(ctx_);
358
359 ctx_->SetCurrentBrush(stroke_brush_);
360 ctx_->SetCurrentStrokeStyle(stroke_style_);
361 ctx_->DrawRectangle(rect);
362}
363
364inline void CanvasRenderContext::DrawRoundedRect(const Rect& rect, const Vec2& radius)
365{
366 KGE_ASSERT(ctx_);
367
368 ctx_->SetCurrentBrush(stroke_brush_);
369 ctx_->SetCurrentStrokeStyle(stroke_style_);
370 ctx_->DrawRoundedRectangle(rect, radius);
371}
372
374{
375 KGE_ASSERT(ctx_);
376 if (shape)
377 {
378 ctx_->SetCurrentBrush(fill_brush_);
379 ctx_->FillShape(*shape);
380 }
381}
382
383inline void CanvasRenderContext::FillCircle(const Point& center, float radius)
384{
385 KGE_ASSERT(ctx_);
386
387 ctx_->SetCurrentBrush(fill_brush_);
388 ctx_->FillCircle(center, radius);
389}
390
391inline void CanvasRenderContext::FillEllipse(const Point& center, const Vec2& radius)
392{
393 KGE_ASSERT(ctx_);
394
395 ctx_->SetCurrentBrush(fill_brush_);
396 ctx_->FillEllipse(center, radius);
397}
398
399inline void CanvasRenderContext::FillRect(const Rect& rect)
400{
401 KGE_ASSERT(ctx_);
402
403 ctx_->SetCurrentBrush(fill_brush_);
404 ctx_->FillRectangle(rect);
405}
406
407inline void CanvasRenderContext::FillRoundedRect(const Rect& rect, const Vec2& radius)
408{
409 KGE_ASSERT(ctx_);
410
411 ctx_->SetCurrentBrush(fill_brush_);
412 ctx_->FillRoundedRectangle(rect, radius);
413}
414
415inline void CanvasRenderContext::DrawTexture(RefPtr<Texture> texture, const Point& pos, const Rect* crop_rect)
416{
417 if (texture)
418 {
419 this->DrawTexture(texture, pos, texture->GetSize(), crop_rect);
420 }
421}
422
423inline void CanvasRenderContext::DrawTexture(RefPtr<Texture> texture, const Point& pos, const Size& size,
424 const Rect* crop_rect)
425{
426 KGE_ASSERT(ctx_);
427
428 if (texture)
429 {
430 Rect dest_rect(pos, pos + size);
431 ctx_->DrawTexture(*texture, crop_rect, &dest_rect);
432 }
433}
434
435inline void CanvasRenderContext::DrawSpriteFrame(const SpriteFrame& frame, const Point& pos)
436{
437 this->DrawSpriteFrame(frame, pos, frame.GetCropRect().GetSize());
438}
439
440inline void CanvasRenderContext::DrawSpriteFrame(const SpriteFrame& frame, const Point& pos, const Size& size)
441{
442 this->DrawTexture(frame.GetTexture(), pos, size, &frame.GetCropRect());
443}
444
445inline void CanvasRenderContext::DrawTextLayout(StringView text, const TextStyle& style, const Point& point)
446{
447 RefPtr<TextLayout> layout = MakePtr<TextLayout>(text, style);
448 this->DrawTextLayout(layout, point);
449}
450
452{
453 KGE_ASSERT(ctx_);
454 if (layout)
455 {
456 ctx_->SetCurrentBrush(fill_brush_);
457 ctx_->SetCurrentStrokeStyle(stroke_style_);
458 ctx_->DrawTextLayout(*layout, point, stroke_brush_);
459 }
460}
461
463{
464 KGE_ASSERT(ctx_);
465 ctx_->Clear();
466}
467
468inline void CanvasRenderContext::Clear(const Color& clear_color)
469{
470 KGE_ASSERT(ctx_);
471 ctx_->Clear(clear_color);
472}
473
475{
476 if (!fill_brush_)
477 {
478 fill_brush_ = MakePtr<Brush>();
479 }
480 fill_brush_->SetColor(color);
481}
482
484{
485 fill_brush_ = brush;
486}
487
489{
490 return fill_brush_;
491}
492
494{
495 stroke_brush_ = brush;
496}
497
499{
500 if (!stroke_brush_)
501 {
502 stroke_brush_ = MakePtr<Brush>();
503 }
504 stroke_brush_->SetColor(color);
505}
506
508{
509 return stroke_brush_;
510}
511
513{
514 stroke_style_ = stroke_style;
515}
516
518{
519 return stroke_style_;
520}
521
522inline void CanvasRenderContext::PushClipRect(const Rect& clip_rect)
523{
524 KGE_ASSERT(ctx_);
525 ctx_->PushClipRect(clip_rect);
526}
527
529{
530 KGE_ASSERT(ctx_);
531 ctx_->PopClipRect();
532}
533
535{
536 KGE_ASSERT(ctx_);
537 if (layer)
538 {
539 ctx_->PushLayer(*layer);
540 }
541}
542
544{
545 KGE_ASSERT(ctx_);
546 ctx_->PopLayer();
547}
548
550{
551 KGE_ASSERT(ctx_);
552 return ctx_->GetSize();
553}
554
556{
557 KGE_ASSERT(ctx_);
558 ctx_->SetTransform(matrix);
559}
560
562{
563 KGE_ASSERT(ctx_);
564 ctx_->SetAntialiasMode(enabled);
565}
566
568{
569 KGE_ASSERT(ctx_);
570 ctx_->SetTextAntialiasMode(mode);
571}
572
573} // namespace kiwano
角色
Definition: Actor.h:63
画布渲染上下文
Definition: Canvas.h:77
void DrawEllipse(const Point &center, const Vec2 &radius)
画椭圆形边框
Definition: Canvas.h:346
void SetStrokeBrush(RefPtr< Brush > brush)
设置轮廓画刷
Definition: Canvas.h:493
void FillEllipse(const Point &center, const Vec2 &radius)
填充椭圆形
Definition: Canvas.h:391
void FillRoundedRect(const Rect &rect, const Vec2 &radius)
填充圆角矩形
Definition: Canvas.h:407
void PushLayer(RefPtr< Layer > layer)
添加一个图层
Definition: Canvas.h:534
void DrawCircle(const Point &center, float radius)
画圆形边框
Definition: Canvas.h:337
void FillRect(const Rect &rect)
填充矩形
Definition: Canvas.h:399
Size GetSize() const
获取画布大小
Definition: Canvas.h:549
void SetFillBrush(RefPtr< Brush > brush)
设置填充画刷
Definition: Canvas.h:483
void DrawShape(RefPtr< Shape > shape)
画形状轮廓
Definition: Canvas.h:317
void PopLayer()
删除最近添加的图层
Definition: Canvas.h:543
void DrawActor(RefPtr< Actor > actor)
画角色
Definition: Canvas.h:308
void DrawLine(const Point &begin, const Point &end)
画线段
Definition: Canvas.h:328
void FillCircle(const Point &center, float radius)
填充圆形
Definition: Canvas.h:383
void SetAntialiasMode(bool enabled)
设置抗锯齿模式
Definition: Canvas.h:561
void DrawSpriteFrame(const SpriteFrame &frame, const Point &pos)
绘制精灵帧
Definition: Canvas.h:435
void PopClipRect()
删除最近添加的裁剪区域
Definition: Canvas.h:528
void Clear()
清空画布
Definition: Canvas.h:462
RefPtr< Brush > GetStrokeBrush() const
获取轮廓画刷
Definition: Canvas.h:507
void DrawRoundedRect(const Rect &rect, const Vec2 &radius)
画圆角矩形边框
Definition: Canvas.h:364
void EndDraw()
结束渲染
Definition: Canvas.h:302
void SetFillColor(const Color &color)
设置填充颜色
Definition: Canvas.h:474
void SetTextAntialiasMode(TextAntialiasMode mode)
设置文字抗锯齿模式
Definition: Canvas.h:567
RefPtr< StrokeStyle > GetStrokeStyle() const
获取轮廓样式
Definition: Canvas.h:517
void DrawTexture(RefPtr< Texture > texture, const Point &pos, const Rect *crop_rect=nullptr)
绘制纹理
Definition: Canvas.h:415
RefPtr< Brush > GetFillBrush() const
获取填充画刷
Definition: Canvas.h:488
void SetStrokeColor(const Color &color)
设置轮廓颜色
Definition: Canvas.h:498
void DrawRect(const Rect &rect)
画矩形边框
Definition: Canvas.h:355
void SetStrokeStyle(RefPtr< StrokeStyle > stroke_style)
设置轮廓样式
Definition: Canvas.h:512
void PushClipRect(const Rect &clip_rect)
添加一个裁剪区域
Definition: Canvas.h:522
void SetTransform(const Matrix3x2 &matrix)
设置渲染上下文的二维变换矩阵
Definition: Canvas.h:555
void BeginDraw()
开始渲染
Definition: Canvas.h:296
void DrawTextLayout(StringView text, const TextStyle &style, const Point &point)
绘制文字布局
Definition: Canvas.h:445
void FillShape(RefPtr< Shape > shape)
填充形状
Definition: Canvas.h:373
画布
Definition: Canvas.h:42
RefPtr< Texture > GetTexture() const
导出纹理
Definition: Canvas.h:291
Definition: Color.h:41
基础对象
Definition: ObjectBase.h:138
引用计数智能指针
Definition: RefBasePtr.hpp:35
渲染上下文
Definition: RenderContext.h:62
精灵帧
Definition: SpriteFrame.h:34
RefPtr< Texture > GetTexture() const
获取纹理
Definition: SpriteFrame.h:131
const Rect & GetCropRect() const
获取裁剪矩形
Definition: SpriteFrame.h:126
文本样式
Definition: TextStyle.h:51
TextAntialiasMode
文字抗锯齿模式
Definition: RenderContext.h:40