Kiwano Engine v1.3.x
Actor.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/math/Math.h>
23#include <kiwano/core/Time.h>
24#include <kiwano/base/ObjectBase.h>
25#include <kiwano/base/component/ComponentManager.h>
26#include <kiwano/event/EventDispatcher.h>
27#include <kiwano/utils/TaskScheduler.h>
28#include <kiwano/2d/animation/Animator.h>
29
30namespace kiwano
31{
32class Stage;
33class Director;
34class RenderContext;
35
38typedef IntrusiveList<RefPtr<Actor>> ActorList;
39
56class KGE_API Actor
57 : public ObjectBase
58 , public Animator
59 , public TaskScheduler
60 , public EventDispatcher
61 , public ComponentManager
62 , protected IntrusiveListValue<RefPtr<Actor>>
63{
64 friend class Director;
65 friend class Transition;
67
68public:
72
73 Actor();
74
75 virtual ~Actor();
76
81 virtual void OnUpdate(Duration dt);
82
88 virtual void OnRender(RenderContext& ctx);
89
92 bool IsVisible() const;
93
96 bool IsCascadeOpacityEnabled() const;
97
100 bool IsEventDispatchEnabled() const;
101
104 size_t GetHashName() const;
105
108 int GetZOrder() const;
109
112 virtual Point GetPosition() const;
113
116 float GetPositionX() const;
117
120 float GetPositionY() const;
121
124 virtual Size GetSize() const;
125
128 float GetWidth() const;
129
132 float GetHeight() const;
133
136 float GetScaledWidth() const;
137
140 float GetScaledHeight() const;
141
144 Size GetScaledSize() const;
145
148 virtual Point GetAnchor() const;
149
152 float GetAnchorX() const;
153
156 float GetAnchorY() const;
157
160 virtual float GetOpacity() const;
161
164 float GetDisplayedOpacity() const;
165
168 virtual float GetRotation() const;
169
172 virtual Point GetScale() const;
173
176 float GetScaleX() const;
177
180 float GetScaleY() const;
181
184 virtual Point GetSkew() const;
185
188 float GetSkewX() const;
189
192 float GetSkewY() const;
193
196 Transform GetTransform() const;
197
200 Actor* GetParent() const;
201
204 Stage* GetStage() const;
205
208 virtual Rect GetBounds() const;
209
212 virtual Rect GetBoundingBox() const;
213
216 const Matrix3x2& GetTransformMatrix() const;
217
220 const Matrix3x2& GetTransformInverseMatrix() const;
221
224 const Matrix3x2& GetTransformMatrixToParent() const;
225
228 void SetVisible(bool val);
229
232 void SetName(StringView name);
233
236 virtual void SetPosition(const Point& point);
237
240 void SetPosition(float x, float y);
241
244 void SetPositionX(float x);
245
248 void SetPositionY(float y);
249
252 void MoveTo(const Point& p);
253
256 void MoveTo(float x, float y);
257
260 void MoveBy(const Vec2& trans);
261
264 void MoveBy(float trans_x, float trans_y);
265
268 virtual void SetScale(const Vec2& scale);
269
272 void SetScale(float scalex, float scaley);
273
276 virtual void SetSkew(const Vec2& skew);
277
280 void SetSkew(float skewx, float skewy);
281
284 virtual void SetRotation(float rotation);
285
288 virtual void SetAnchor(const Vec2& anchor);
289
292 void SetAnchor(float anchorx, float anchory);
293
296 virtual void SetSize(const Size& size);
297
300 void SetSize(float width, float height);
301
304 void SetWidth(float width);
305
308 void SetHeight(float height);
309
312 virtual void SetOpacity(float opacity);
313
316 void SetCascadeOpacityEnabled(bool enabled);
317
320 void SetTransform(const Transform& transform);
321
324 void SetZOrder(int zorder);
325
328 void AddChild(RefPtr<Actor> child);
329
332 void AddChild(RefPtr<Actor> child, int zorder);
333
336 void AddChildren(const Vector<RefPtr<Actor>>& children);
337
340 RefPtr<Actor> GetChild(StringView name) const;
341
344 Vector<RefPtr<Actor>> GetChildren(StringView name) const;
345
348 ActorList& GetAllChildren();
349
352 const ActorList& GetAllChildren() const;
353
356 void RemoveChild(RefPtr<Actor> child);
357
360 void RemoveChildren(StringView child_name);
361
364 void RemoveAllChildren();
365
368 void RemoveFromParent();
369
372 void PauseUpdating();
373
376 void ResumeUpdating();
377
380 bool IsUpdatePausing() const;
381
384 void SetCallbackOnUpdate(const UpdateCallback& cb);
385
388 UpdateCallback GetCallbackOnUpdate() const;
389
392 virtual bool ContainsPoint(const Point& point) const;
393
396 Point ConvertToLocal(const Point& point) const;
397
400 Point ConvertToWorld(const Point& point) const;
401
404 void ShowBorder(bool show);
405
410 virtual bool DispatchEvent(Event* evt);
411
415 bool HandleEvent(Event* evt);
416
420 void SetEventDispatchEnabled(bool enabled);
421
424 void DoSerialize(Serializer* serializer) const override;
425
428 void DoDeserialize(Deserializer* deserializer) override;
429
432 static void SetDefaultAnchor(float anchor_x, float anchor_y);
433
434protected:
437 virtual void Update(Duration dt);
438
441 virtual void Render(RenderContext& ctx);
442
445 virtual void RenderBorder(RenderContext& ctx);
446
449 virtual bool CheckVisibility(RenderContext& ctx) const;
450
453 virtual void PrepareToRender(RenderContext& ctx);
454
457 void UpdateTransform() const;
458
462 void UpdateTransformUpwards() const;
463
466 void UpdateOpacity();
467
470 void Reorder();
471
474 void SetStage(Stage* stage);
475
476private:
477 bool visible_;
478 bool update_pausing_;
479 bool cascade_opacity_;
480 bool show_border_;
481 bool evt_dispatch_enabled_;
482 mutable bool visible_in_rt_;
483
484 enum DirtyFlag : uint8_t
485 {
486 Clean = 0,
487 DirtyTransform = 1,
488 DirtyTransformInverse = 1 << 1,
489 DirtyOpacity = 1 << 2,
490 DirtyVisibility = 1 << 3
491 };
492 mutable Flag<uint8_t> dirty_flag_;
493
494 int z_order_;
495 float opacity_;
496 float displayed_opacity_;
497 Actor* parent_;
498 Stage* stage_;
499 size_t hash_name_;
500 Point anchor_;
501 Size size_;
502 ActorList children_;
503 UpdateCallback cb_update_;
504 Transform transform_;
505
506 mutable Matrix3x2 transform_matrix_;
507 mutable Matrix3x2 transform_matrix_inverse_;
508 mutable Matrix3x2 transform_matrix_to_parent_;
509};
510
514{
515 KGE_NOT_USED(dt);
516}
517
519{
520 KGE_NOT_USED(ctx);
521}
522
523inline bool Actor::IsVisible() const
524{
525 return visible_;
526}
527
529{
530 return cascade_opacity_;
531}
532
534{
535 return evt_dispatch_enabled_;
536}
537
538inline size_t Actor::GetHashName() const
539{
540 return hash_name_;
541}
542
543inline int Actor::GetZOrder() const
544{
545 return z_order_;
546}
547
549{
550 return transform_.position;
551}
552
553inline float Actor::GetPositionX() const
554{
555 return GetPosition().x;
556}
557
558inline float Actor::GetPositionY() const
559{
560 return GetPosition().y;
561}
562
563inline Point Actor::GetScale() const
564{
565 return transform_.scale;
566}
567
568inline float Actor::GetScaleX() const
569{
570 return GetScale().x;
571}
572
573inline float Actor::GetScaleY() const
574{
575 return GetScale().y;
576}
577
578inline Point Actor::GetSkew() const
579{
580 return transform_.skew;
581}
582
583inline float Actor::GetSkewX() const
584{
585 return GetSkew().x;
586}
587
588inline float Actor::GetSkewY() const
589{
590 return GetSkew().y;
591}
592
593inline float Actor::GetRotation() const
594{
595 return transform_.rotation;
596}
597
598inline float Actor::GetWidth() const
599{
600 return GetSize().x;
601}
602
603inline float Actor::GetHeight() const
604{
605 return GetSize().y;
606}
607
608inline Size Actor::GetSize() const
609{
610 return size_;
611}
612
613inline float Actor::GetScaledWidth() const
614{
615 return GetWidth() * GetScaleX();
616}
617
618inline float Actor::GetScaledHeight() const
619{
620 return GetHeight() * GetScaleY();
621}
622
624{
625 return Size{ GetScaledWidth(), GetScaledHeight() };
626}
627
629{
630 return anchor_;
631}
632
633inline float Actor::GetAnchorX() const
634{
635 return GetAnchor().x;
636}
637
638inline float Actor::GetAnchorY() const
639{
640 return GetAnchor().y;
641}
642
643inline float Actor::GetOpacity() const
644{
645 return opacity_;
646}
647
648inline float Actor::GetDisplayedOpacity() const
649{
650 return displayed_opacity_;
651}
652
654{
655 return transform_;
656}
657
658inline Actor* Actor::GetParent() const
659{
660 return parent_;
661}
662
663inline Stage* Actor::GetStage() const
664{
665 return stage_;
666}
667
669{
670 update_pausing_ = true;
671}
672
674{
675 update_pausing_ = false;
676}
677
678inline bool Actor::IsUpdatePausing() const
679{
680 return update_pausing_;
681}
682
684{
685 cb_update_ = cb;
686}
687
689{
690 return cb_update_;
691}
692
693inline void Actor::ShowBorder(bool show)
694{
695 show_border_ = show;
696}
697
698inline void Actor::SetPosition(float x, float y)
699{
700 this->SetPosition(Point(x, y));
701}
702
703inline void Actor::SetPositionX(float x)
704{
705 this->SetPosition(Point(x, transform_.position.y));
706}
707
708inline void Actor::SetPositionY(float y)
709{
710 this->SetPosition(Point(transform_.position.x, y));
711}
712
713inline void Actor::MoveTo(const Point& p)
714{
715 this->SetPosition(p);
716}
717
718inline void Actor::MoveTo(float x, float y)
719{
720 this->SetPosition(Point(x, y));
721}
722
723inline void Actor::MoveBy(const Vec2& trans)
724{
725 this->SetPosition(transform_.position.x + trans.x, transform_.position.y + trans.y);
726}
727
728inline void Actor::MoveBy(float trans_x, float trans_y)
729{
730 this->MoveBy(Vec2(trans_x, trans_y));
731}
732
733inline void Actor::SetScale(float scalex, float scaley)
734{
735 this->SetScale(Vec2(scalex, scaley));
736}
737
738inline void Actor::SetAnchor(float anchorx, float anchory)
739{
740 this->SetAnchor(Vec2(anchorx, anchory));
741}
742
743inline void Actor::SetSize(float width, float height)
744{
745 this->SetSize(Size(width, height));
746}
747
748inline void Actor::SetWidth(float width)
749{
750 this->SetSize(Size(width, size_.y));
751}
752
753inline void Actor::SetHeight(float height)
754{
755 this->SetSize(Size(size_.x, height));
756}
757
758inline void Actor::SetSkew(float skewx, float skewy)
759{
760 this->SetSkew(Vec2(skewx, skewy));
761}
762
763} // namespace kiwano
角色
Definition: Actor.h:63
UpdateCallback GetCallbackOnUpdate() const
获取更新时的回调函数
Definition: Actor.h:688
Transform GetTransform() const
获取变换
Definition: Actor.h:653
Function< void(Duration)> UpdateCallback
角色更新回调函数
Definition: Actor.h:71
virtual void SetSize(const Size &size)
修改大小
Definition: Actor.cpp:421
bool IsVisible() const
获取显示状态
Definition: Actor.h:523
float GetPositionX() const
获取 x 坐标
Definition: Actor.h:553
virtual void OnRender(RenderContext &ctx)
渲染角色
Definition: Actor.h:518
virtual Point GetSkew() const
获取错切角度
Definition: Actor.h:578
bool IsCascadeOpacityEnabled() const
是否启用级联透明度
Definition: Actor.h:528
void MoveTo(const Point &p)
移动至坐标
Definition: Actor.h:713
virtual void SetPosition(const Point &point)
设置坐标
Definition: Actor.cpp:447
Stage * GetStage() const
获取所在舞台
Definition: Actor.h:663
float GetAnchorY() const
获取 y 方向锚点
Definition: Actor.h:638
size_t GetHashName() const
获取名称的 Hash 值
Definition: Actor.h:538
void SetCallbackOnUpdate(const UpdateCallback &cb)
设置更新时的回调函数
Definition: Actor.h:683
Actor * GetParent() const
获取父角色
Definition: Actor.h:658
virtual Point GetPosition() const
获取坐标
Definition: Actor.h:548
Size GetScaledSize() const
获取缩放后的大小
Definition: Actor.h:623
float GetDisplayedOpacity() const
获取显示透明度
Definition: Actor.h:648
virtual void SetSkew(const Vec2 &skew)
设置错切角度,默认为 (0, 0)
Definition: Actor.cpp:465
virtual void SetScale(const Vec2 &scale)
设置缩放比例,默认为 (1.0, 1.0)
Definition: Actor.cpp:456
bool IsUpdatePausing() const
角色更新是否暂停
Definition: Actor.h:678
float GetScaleY() const
获取纵向缩放比例
Definition: Actor.h:573
virtual Point GetAnchor() const
获取锚点
Definition: Actor.h:628
int GetZOrder() const
获取 Z 轴顺序
Definition: Actor.h:543
float GetScaledHeight() const
获取缩放后的高度
Definition: Actor.h:618
float GetSkewX() const
获取横向错切角度
Definition: Actor.h:583
void SetWidth(float width)
修改宽度
Definition: Actor.h:748
float GetScaleX() const
获取横向缩放比例
Definition: Actor.h:568
void ResumeUpdating()
继续角色更新
Definition: Actor.h:673
virtual Size GetSize() const
获取大小
Definition: Actor.h:608
void SetHeight(float height)
修改高度
Definition: Actor.h:753
float GetAnchorX() const
获取 x 方向锚点
Definition: Actor.h:633
void MoveBy(const Vec2 &trans)
移动相对坐标
Definition: Actor.h:723
bool IsEventDispatchEnabled() const
是否启用事件分发
Definition: Actor.h:533
float GetScaledWidth() const
获取缩放后的宽度
Definition: Actor.h:613
void ShowBorder(bool show)
渲染角色边界
Definition: Actor.h:693
virtual float GetOpacity() const
获取透明度
Definition: Actor.h:643
void SetPositionX(float x)
设置横坐标
Definition: Actor.h:703
float GetSkewY() const
获取纵向错切角度
Definition: Actor.h:588
float GetPositionY() const
获取 y 坐标
Definition: Actor.h:558
void PauseUpdating()
暂停角色更新
Definition: Actor.h:668
float GetWidth() const
获取宽度
Definition: Actor.h:598
virtual void OnUpdate(Duration dt)
更新角色
Definition: Actor.h:513
virtual void SetAnchor(const Vec2 &anchor)
设置锚点位置,默认为 (0, 0), 范围 [0, 1]
Definition: Actor.cpp:412
virtual float GetRotation() const
获取旋转角度
Definition: Actor.h:593
float GetHeight() const
获取高度
Definition: Actor.h:603
void SetPositionY(float y)
设置纵坐标
Definition: Actor.h:708
virtual Point GetScale() const
获取缩放比例
Definition: Actor.h:563
动画调度器
Definition: Animator.h:37
组件管理器
Definition: ComponentManager.h:42
导演
Definition: Director.h:38
事件分发器
Definition: EventDispatcher.h:36
事件
Definition: Event.h:43
Definition: Function.h:228
侵入式链表元素
Definition: IntrusiveList.h:434
侵入式链表
Definition: IntrusiveList.h:34
基础对象
Definition: ObjectBase.h:138
引用计数智能指针
Definition: RefBasePtr.hpp:35
渲染上下文
Definition: RenderContext.h:62
舞台
Definition: Stage.h:40
任务调度器
Definition: TaskScheduler.h:36
舞台过渡动画
Definition: Transition.h:45
float rotation
旋转
Definition: Transform.hpp:40
Vec2T< ValueType > skew
错切角度
Definition: Transform.hpp:43
Vec2T< ValueType > position
坐标
Definition: Transform.hpp:41
Vec2T< ValueType > scale
缩放
Definition: Transform.hpp:42
反序列化器
Definition: Serializable.h:147
时间段
Definition: Duration.h:48
序列化器
Definition: Serializable.h:40