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/render/ShapeMaker.h>
24#include <kiwano/render/RenderContext.h>
25
26namespace kiwano
27{
28class CanvasRenderContext;
29
40class KGE_API Canvas : public Actor
41{
42public:
46 Canvas();
47
51 Canvas(const Size& size);
52
55 RefPtr<CanvasRenderContext> GetContext2D() const;
56
60 void ResizeAndClear(const Size& size);
61
64 RefPtr<Bitmap> GetTarget() const;
65
69 void EnableCommandListMode(bool enable);
70
71 void OnRender(RenderContext& ctx) override;
72
73private:
74 bool cmd_list_mode_;
75 RefPtr<Bitmap> cached_bitmap_;
76 RefPtr<RenderContext> render_ctx_;
77};
78
81class KGE_API CanvasRenderContext : public ObjectBase
82{
83public:
86 void BeginDraw();
87
90 void EndDraw();
91
95 void DrawActor(RefPtr<Actor> actor);
96
100 void DrawShape(RefPtr<Shape> shape);
101
106 void DrawLine(const Point& begin, const Point& end);
107
112 void DrawCircle(const Point& center, float radius);
113
118 void DrawEllipse(const Point& center, const Vec2& radius);
119
123 void DrawRect(const Rect& rect);
124
129 void DrawRoundedRect(const Rect& rect, const Vec2& radius);
130
134 void FillShape(RefPtr<Shape> shape);
135
140 void FillCircle(const Point& center, float radius);
141
146 void FillEllipse(const Point& center, const Vec2& radius);
147
151 void FillRect(const Rect& rect);
152
157 void FillRoundedRect(const Rect& rect, const Vec2& radius);
158
164 void DrawBitmap(RefPtr<Bitmap> bitmap, const Point& pos, const Rect* crop_rect = nullptr);
165
172 void DrawBitmap(RefPtr<Bitmap> bitmap, const Point& pos, const Size& size, const Rect* crop_rect = nullptr);
173
179 void DrawTextLayout(StringView text, const TextStyle& style, const Point& point);
180
185 void DrawTextLayout(RefPtr<TextLayout> layout, const Point& point);
186
189 void Clear();
190
194 void Clear(const Color& clear_color);
195
199 void SetFillColor(const Color& color);
200
204 void SetFillBrush(RefPtr<Brush> brush);
205
208 RefPtr<Brush> GetFillBrush() const;
209
213 void SetStrokeColor(const Color& color);
214
218 void SetStrokeBrush(RefPtr<Brush> brush);
219
222 RefPtr<Brush> GetStrokeBrush() const;
223
227 void SetStrokeStyle(RefPtr<StrokeStyle> stroke_style);
228
231 RefPtr<StrokeStyle> GetStrokeStyle() const;
232
236 void PushClipRect(const Rect& clip_rect);
237
240 void PopClipRect();
241
245 void PushLayer(Layer& layer);
246
249 void PopLayer();
250
253 Size GetSize() const;
254
257 void SetTransform(const Matrix3x2& matrix);
258
261 void SetAntialiasMode(bool enabled);
262
265 void SetTextAntialiasMode(TextAntialiasMode mode);
266
267 virtual ~CanvasRenderContext();
268
269private:
270 friend class Canvas;
271
273
274private:
276 RefPtr<Brush> fill_brush_;
277 RefPtr<Brush> stroke_brush_;
278 RefPtr<StrokeStyle> stroke_style_;
279};
280
284{
285 return cached_bitmap_;
286}
287
289{
290 KGE_ASSERT(ctx_);
291 ctx_->BeginDraw();
292}
293
295{
296 KGE_ASSERT(ctx_);
297 ctx_->EndDraw();
298}
299
301{
302 KGE_ASSERT(ctx_);
303 if (actor)
304 {
305 actor->OnRender(*ctx_);
306 }
307}
308
310{
311 KGE_ASSERT(ctx_);
312 if (shape)
313 {
314 ctx_->SetCurrentBrush(stroke_brush_);
315 ctx_->SetCurrentStrokeStyle(stroke_style_);
316 ctx_->DrawShape(*shape);
317 }
318}
319
320inline void CanvasRenderContext::DrawLine(const Point& begin, const Point& end)
321{
322 KGE_ASSERT(ctx_);
323
324 ctx_->SetCurrentBrush(stroke_brush_);
325 ctx_->SetCurrentStrokeStyle(stroke_style_);
326 ctx_->DrawLine(begin, end);
327}
328
329inline void CanvasRenderContext::DrawCircle(const Point& center, float radius)
330{
331 KGE_ASSERT(ctx_);
332
333 ctx_->SetCurrentBrush(stroke_brush_);
334 ctx_->SetCurrentStrokeStyle(stroke_style_);
335 ctx_->DrawCircle(center, radius);
336}
337
338inline void CanvasRenderContext::DrawEllipse(const Point& center, const Vec2& radius)
339{
340 KGE_ASSERT(ctx_);
341
342 ctx_->SetCurrentBrush(stroke_brush_);
343 ctx_->SetCurrentStrokeStyle(stroke_style_);
344 ctx_->DrawEllipse(center, radius);
345}
346
347inline void CanvasRenderContext::DrawRect(const Rect& rect)
348{
349 KGE_ASSERT(ctx_);
350
351 ctx_->SetCurrentBrush(stroke_brush_);
352 ctx_->SetCurrentStrokeStyle(stroke_style_);
353 ctx_->DrawRectangle(rect);
354}
355
356inline void CanvasRenderContext::DrawRoundedRect(const Rect& rect, const Vec2& radius)
357{
358 KGE_ASSERT(ctx_);
359
360 ctx_->SetCurrentBrush(stroke_brush_);
361 ctx_->SetCurrentStrokeStyle(stroke_style_);
362 ctx_->DrawRoundedRectangle(rect, radius);
363}
364
366{
367 KGE_ASSERT(ctx_);
368 if (shape)
369 {
370 ctx_->SetCurrentBrush(fill_brush_);
371 ctx_->FillShape(*shape);
372 }
373}
374
375inline void CanvasRenderContext::FillCircle(const Point& center, float radius)
376{
377 KGE_ASSERT(ctx_);
378
379 ctx_->SetCurrentBrush(fill_brush_);
380 ctx_->FillCircle(center, radius);
381}
382
383inline void CanvasRenderContext::FillEllipse(const Point& center, const Vec2& radius)
384{
385 KGE_ASSERT(ctx_);
386
387 ctx_->SetCurrentBrush(fill_brush_);
388 ctx_->FillEllipse(center, radius);
389}
390
391inline void CanvasRenderContext::FillRect(const Rect& rect)
392{
393 KGE_ASSERT(ctx_);
394
395 ctx_->SetCurrentBrush(fill_brush_);
396 ctx_->FillRectangle(rect);
397}
398
399inline void CanvasRenderContext::FillRoundedRect(const Rect& rect, const Vec2& radius)
400{
401 KGE_ASSERT(ctx_);
402
403 ctx_->SetCurrentBrush(fill_brush_);
404 ctx_->FillRoundedRectangle(rect, radius);
405}
406
407inline void CanvasRenderContext::DrawBitmap(RefPtr<Bitmap> bitmap, const Point& pos, const Rect* crop_rect)
408{
409 if (bitmap)
410 {
411 this->DrawBitmap(bitmap, pos, bitmap->GetSize(), crop_rect);
412 }
413}
414
415inline void CanvasRenderContext::DrawBitmap(RefPtr<Bitmap> bitmap, const Point& pos, const Size& size,
416 const Rect* crop_rect)
417{
418 KGE_ASSERT(ctx_);
419
420 if (bitmap)
421 {
422 Rect dest_rect(pos, pos + size);
423 ctx_->DrawBitmap(*bitmap, crop_rect, &dest_rect);
424 }
425}
426
427inline void CanvasRenderContext::DrawTextLayout(StringView text, const TextStyle& style, const Point& point)
428{
429 RefPtr<TextLayout> layout = MakePtr<TextLayout>(text, style);
430 this->DrawTextLayout(layout, point);
431}
432
434{
435 KGE_ASSERT(ctx_);
436 if (layout)
437 {
438 ctx_->SetCurrentBrush(fill_brush_);
439 ctx_->SetCurrentStrokeStyle(stroke_style_);
440 ctx_->DrawTextLayout(*layout, point, stroke_brush_);
441 }
442}
443
445{
446 KGE_ASSERT(ctx_);
447 ctx_->Clear();
448}
449
450inline void CanvasRenderContext::Clear(const Color& clear_color)
451{
452 KGE_ASSERT(ctx_);
453 ctx_->Clear(clear_color);
454}
455
457{
458 if (!fill_brush_)
459 {
460 fill_brush_ = MakePtr<Brush>();
461 }
462 fill_brush_->SetColor(color);
463}
464
466{
467 fill_brush_ = brush;
468}
469
471{
472 return fill_brush_;
473}
474
476{
477 stroke_brush_ = brush;
478}
479
481{
482 if (!stroke_brush_)
483 {
484 stroke_brush_ = MakePtr<Brush>();
485 }
486 stroke_brush_->SetColor(color);
487}
488
490{
491 return stroke_brush_;
492}
493
495{
496 stroke_style_ = stroke_style;
497}
498
500{
501 return stroke_style_;
502}
503
504inline void CanvasRenderContext::PushClipRect(const Rect& clip_rect)
505{
506 KGE_ASSERT(ctx_);
507 ctx_->PushClipRect(clip_rect);
508}
509
511{
512 KGE_ASSERT(ctx_);
513 ctx_->PopClipRect();
514}
515
517{
518 KGE_ASSERT(ctx_);
519 ctx_->PushLayer(layer);
520}
521
523{
524 KGE_ASSERT(ctx_);
525 ctx_->PopLayer();
526}
527
529{
530 KGE_ASSERT(ctx_);
531 return ctx_->GetSize();
532}
533
535{
536 KGE_ASSERT(ctx_);
537 ctx_->SetTransform(matrix);
538}
539
541{
542 KGE_ASSERT(ctx_);
543 ctx_->SetAntialiasMode(enabled);
544}
545
547{
548 KGE_ASSERT(ctx_);
549 ctx_->SetTextAntialiasMode(mode);
550}
551
552} // namespace kiwano
角色
Definition: Actor.h:63
画布渲染上下文
Definition: Canvas.h:82
void DrawEllipse(const Point &center, const Vec2 &radius)
画椭圆形边框
Definition: Canvas.h:338
void SetStrokeBrush(RefPtr< Brush > brush)
设置轮廓画刷
Definition: Canvas.h:475
void FillEllipse(const Point &center, const Vec2 &radius)
填充椭圆形
Definition: Canvas.h:383
void FillRoundedRect(const Rect &rect, const Vec2 &radius)
填充圆角矩形
Definition: Canvas.h:399
void DrawCircle(const Point &center, float radius)
画圆形边框
Definition: Canvas.h:329
void FillRect(const Rect &rect)
填充矩形
Definition: Canvas.h:391
Size GetSize() const
获取画布大小
Definition: Canvas.h:528
void SetFillBrush(RefPtr< Brush > brush)
设置填充画刷
Definition: Canvas.h:465
void DrawShape(RefPtr< Shape > shape)
画形状轮廓
Definition: Canvas.h:309
void PopLayer()
删除最近添加的图层
Definition: Canvas.h:522
void DrawActor(RefPtr< Actor > actor)
画角色
Definition: Canvas.h:300
void DrawLine(const Point &begin, const Point &end)
画线段
Definition: Canvas.h:320
void FillCircle(const Point &center, float radius)
填充圆形
Definition: Canvas.h:375
void SetAntialiasMode(bool enabled)
设置抗锯齿模式
Definition: Canvas.h:540
void PopClipRect()
删除最近添加的裁剪区域
Definition: Canvas.h:510
void Clear()
清空画布
Definition: Canvas.h:444
RefPtr< Brush > GetStrokeBrush() const
获取轮廓画刷
Definition: Canvas.h:489
void DrawRoundedRect(const Rect &rect, const Vec2 &radius)
画圆角矩形边框
Definition: Canvas.h:356
void EndDraw()
结束渲染
Definition: Canvas.h:294
void SetFillColor(const Color &color)
设置填充颜色
Definition: Canvas.h:456
void SetTextAntialiasMode(TextAntialiasMode mode)
设置文字抗锯齿模式
Definition: Canvas.h:546
RefPtr< StrokeStyle > GetStrokeStyle() const
获取轮廓样式
Definition: Canvas.h:499
RefPtr< Brush > GetFillBrush() const
获取填充画刷
Definition: Canvas.h:470
void SetStrokeColor(const Color &color)
设置轮廓颜色
Definition: Canvas.h:480
void DrawBitmap(RefPtr< Bitmap > bitmap, const Point &pos, const Rect *crop_rect=nullptr)
绘制位图
Definition: Canvas.h:407
void DrawRect(const Rect &rect)
画矩形边框
Definition: Canvas.h:347
void SetStrokeStyle(RefPtr< StrokeStyle > stroke_style)
设置轮廓样式
Definition: Canvas.h:494
void PushClipRect(const Rect &clip_rect)
添加一个裁剪区域
Definition: Canvas.h:504
void SetTransform(const Matrix3x2 &matrix)
设置渲染上下文的二维变换矩阵
Definition: Canvas.h:534
void BeginDraw()
开始渲染
Definition: Canvas.h:288
void PushLayer(Layer &layer)
添加一个图层
Definition: Canvas.h:516
void DrawTextLayout(StringView text, const TextStyle &style, const Point &point)
绘制文字布局
Definition: Canvas.h:427
void FillShape(RefPtr< Shape > shape)
填充形状
Definition: Canvas.h:365
画布
Definition: Canvas.h:41
RefPtr< Bitmap > GetTarget() const
获取输出目标
Definition: Canvas.h:283
Definition: Color.h:41
图层
Definition: Layer.h:37
基础对象
Definition: ObjectBase.h:138
引用计数智能指针
Definition: RefBasePtr.hpp:35
渲染上下文
Definition: RenderContext.h:62
文本样式
Definition: TextStyle.h:64
TextAntialiasMode
文字抗锯齿模式
Definition: RenderContext.h:40