Kiwano Engine  v1.2.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 
30 namespace kiwano
31 {
32 class Stage;
33 class Director;
34 class RenderContext;
35 
36 namespace physics
37 {
38 class PhysicBody;
39 }
40 
41 KGE_DECLARE_SMART_PTR(Actor);
42 
45 typedef IntrusiveList<ActorPtr> ActorList;
46 
63 class KGE_API Actor
64  : public ObjectBase
65  , public Animator
66  , public TaskScheduler
67  , public EventDispatcher
68  , public ComponentManager
69  , protected IntrusiveListValue<ActorPtr>
70 {
71  friend class Director;
72  friend class Transition;
74 
75 public:
79 
80  Actor();
81 
82  virtual ~Actor();
83 
88  virtual void OnUpdate(Duration dt);
89 
95  virtual void OnRender(RenderContext& ctx);
96 
99  bool IsVisible() const;
100 
103  bool IsCascadeOpacityEnabled() const;
104 
107  bool IsEventDispatchEnabled() const;
108 
111  size_t GetHashName() const;
112 
115  int GetZOrder() const;
116 
119  virtual Point GetPosition() const;
120 
123  float GetPositionX() const;
124 
127  float GetPositionY() const;
128 
131  virtual Size GetSize() const;
132 
135  float GetWidth() const;
136 
139  float GetHeight() const;
140 
143  float GetScaledWidth() const;
144 
147  float GetScaledHeight() const;
148 
151  Size GetScaledSize() const;
152 
155  virtual Point GetAnchor() const;
156 
159  float GetAnchorX() const;
160 
163  float GetAnchorY() const;
164 
167  virtual float GetOpacity() const;
168 
171  float GetDisplayedOpacity() const;
172 
175  virtual float GetRotation() const;
176 
179  virtual Point GetScale() const;
180 
183  float GetScaleX() const;
184 
187  float GetScaleY() const;
188 
191  virtual Point GetSkew() const;
192 
195  float GetSkewX() const;
196 
199  float GetSkewY() const;
200 
203  Transform GetTransform() const;
204 
207  Actor* GetParent() const;
208 
211  Stage* GetStage() const;
212 
215  virtual Rect GetBounds() const;
216 
219  virtual Rect GetBoundingBox() const;
220 
223  const Matrix3x2& GetTransformMatrix() const;
224 
227  const Matrix3x2& GetTransformInverseMatrix() const;
228 
231  const Matrix3x2& GetTransformMatrixToParent() const;
232 
235  void SetVisible(bool val);
236 
239  void SetName(const String& name);
240 
243  virtual void SetPosition(const Point& point);
244 
247  void SetPosition(float x, float y);
248 
251  void SetPositionX(float x);
252 
255  void SetPositionY(float y);
256 
259  void MoveTo(const Point& p);
260 
263  void MoveTo(float x, float y);
264 
267  void MoveBy(const Vec2& trans);
268 
271  void MoveBy(float trans_x, float trans_y);
272 
275  virtual void SetScale(const Vec2& scale);
276 
279  void SetScale(float scalex, float scaley);
280 
283  virtual void SetSkew(const Vec2& skew);
284 
287  void SetSkew(float skewx, float skewy);
288 
291  virtual void SetRotation(float rotation);
292 
295  virtual void SetAnchor(const Vec2& anchor);
296 
299  void SetAnchor(float anchorx, float anchory);
300 
303  virtual void SetSize(const Size& size);
304 
307  void SetSize(float width, float height);
308 
311  void SetWidth(float width);
312 
315  void SetHeight(float height);
316 
319  virtual void SetOpacity(float opacity);
320 
323  void SetCascadeOpacityEnabled(bool enabled);
324 
327  void SetTransform(const Transform& transform);
328 
331  void SetZOrder(int zorder);
332 
335  void AddChild(ActorPtr child);
336 
339  void AddChild(ActorPtr child, int zorder);
340 
343  void AddChildren(const Vector<ActorPtr>& children);
344 
347  ActorPtr GetChild(const String& name) const;
348 
351  Vector<ActorPtr> GetChildren(const String& name) const;
352 
355  ActorList& GetAllChildren();
356 
359  const ActorList& GetAllChildren() const;
360 
363  void RemoveChild(ActorPtr child);
364 
367  void RemoveChildren(const String& child_name);
368 
371  void RemoveAllChildren();
372 
375  void RemoveFromParent();
376 
379  void PauseUpdating();
380 
383  void ResumeUpdating();
384 
387  bool IsUpdatePausing() const;
388 
391  void SetCallbackOnUpdate(const UpdateCallback& cb);
392 
395  UpdateCallback GetCallbackOnUpdate() const;
396 
399  virtual bool ContainsPoint(const Point& point) const;
400 
403  Point ConvertToLocal(const Point& point) const;
404 
407  Point ConvertToWorld(const Point& point) const;
408 
411  void ShowBorder(bool show);
412 
417  virtual bool DispatchEvent(Event* evt);
418 
422  bool HandleEvent(Event* evt);
423 
427  void SetEventDispatchEnabled(bool enabled);
428 
431  physics::PhysicBody* GetPhysicBody() const;
432 
435  void DoSerialize(Serializer* serializer) const override;
436 
439  void DoDeserialize(Deserializer* deserializer) override;
440 
443  static void SetDefaultAnchor(float anchor_x, float anchor_y);
444 
445 protected:
448  virtual void Update(Duration dt);
449 
452  virtual void Render(RenderContext& ctx);
453 
456  virtual void RenderBorder(RenderContext& ctx);
457 
460  virtual bool CheckVisibility(RenderContext& ctx) const;
461 
464  virtual void PrepareToRender(RenderContext& ctx);
465 
468  void UpdateTransform() const;
469 
472  void UpdateOpacity();
473 
476  void Reorder();
477 
480  void SetStage(Stage* stage);
481 
484  void SetPhysicBody(physics::PhysicBody* body);
485 
486  friend physics::PhysicBody;
487 
488 private:
489  bool visible_;
490  bool update_pausing_;
491  bool cascade_opacity_;
492  bool show_border_;
493  bool evt_dispatch_enabled_;
494  mutable bool visible_in_rt_;
495 
496  enum DirtyFlag : uint8_t
497  {
498  Clean = 0,
499  DirtyTransform = 1,
500  DirtyTransformInverse = 1 << 1,
501  DirtyOpacity = 1 << 2,
502  DirtyVisibility = 1 << 3
503  };
504  mutable Flag<uint8_t> dirty_flag_;
505 
506  int z_order_;
507  float opacity_;
508  float displayed_opacity_;
509  Actor* parent_;
510  Stage* stage_;
511  physics::PhysicBody* physic_body_;
512  size_t hash_name_;
513  Point anchor_;
514  Size size_;
515  ActorList children_;
516  UpdateCallback cb_update_;
517  Transform transform_;
518 
519  mutable Matrix3x2 transform_matrix_;
520  mutable Matrix3x2 transform_matrix_inverse_;
521  mutable Matrix3x2 transform_matrix_to_parent_;
522 };
523 
526 inline void Actor::OnUpdate(Duration dt)
527 {
528  KGE_NOT_USED(dt);
529 }
530 
532 {
533  KGE_NOT_USED(ctx);
534 }
535 
536 inline bool Actor::IsVisible() const
537 {
538  return visible_;
539 }
540 
542 {
543  return cascade_opacity_;
544 }
545 
546 inline bool Actor::IsEventDispatchEnabled() const
547 {
548  return evt_dispatch_enabled_;
549 }
550 
551 inline size_t Actor::GetHashName() const
552 {
553  return hash_name_;
554 }
555 
556 inline int Actor::GetZOrder() const
557 {
558  return z_order_;
559 }
560 
561 inline Point Actor::GetPosition() const
562 {
563  return transform_.position;
564 }
565 
566 inline float Actor::GetPositionX() const
567 {
568  return GetPosition().x;
569 }
570 
571 inline float Actor::GetPositionY() const
572 {
573  return GetPosition().y;
574 }
575 
576 inline Point Actor::GetScale() const
577 {
578  return transform_.scale;
579 }
580 
581 inline float Actor::GetScaleX() const
582 {
583  return GetScale().x;
584 }
585 
586 inline float Actor::GetScaleY() const
587 {
588  return GetScale().y;
589 }
590 
591 inline Point Actor::GetSkew() const
592 {
593  return transform_.skew;
594 }
595 
596 inline float Actor::GetSkewX() const
597 {
598  return GetSkew().x;
599 }
600 
601 inline float Actor::GetSkewY() const
602 {
603  return GetSkew().y;
604 }
605 
606 inline float Actor::GetRotation() const
607 {
608  return transform_.rotation;
609 }
610 
611 inline float Actor::GetWidth() const
612 {
613  return GetSize().x;
614 }
615 
616 inline float Actor::GetHeight() const
617 {
618  return GetSize().y;
619 }
620 
621 inline Size Actor::GetSize() const
622 {
623  return size_;
624 }
625 
626 inline float Actor::GetScaledWidth() const
627 {
628  return GetWidth() * GetScaleX();
629 }
630 
631 inline float Actor::GetScaledHeight() const
632 {
633  return GetHeight() * GetScaleY();
634 }
635 
637 {
638  return Size{ GetScaledWidth(), GetScaledHeight() };
639 }
640 
641 inline Point Actor::GetAnchor() const
642 {
643  return anchor_;
644 }
645 
646 inline float Actor::GetAnchorX() const
647 {
648  return GetAnchor().x;
649 }
650 
651 inline float Actor::GetAnchorY() const
652 {
653  return GetAnchor().y;
654 }
655 
656 inline float Actor::GetOpacity() const
657 {
658  return opacity_;
659 }
660 
661 inline float Actor::GetDisplayedOpacity() const
662 {
663  return displayed_opacity_;
664 }
665 
667 {
668  return transform_;
669 }
670 
671 inline Actor* Actor::GetParent() const
672 {
673  return parent_;
674 }
675 
676 inline Stage* Actor::GetStage() const
677 {
678  return stage_;
679 }
680 
681 inline void Actor::PauseUpdating()
682 {
683  update_pausing_ = true;
684 }
685 
687 {
688  update_pausing_ = false;
689 }
690 
691 inline bool Actor::IsUpdatePausing() const
692 {
693  return update_pausing_;
694 }
695 
697 {
698  cb_update_ = cb;
699 }
700 
702 {
703  return cb_update_;
704 }
705 
706 inline void Actor::ShowBorder(bool show)
707 {
708  show_border_ = show;
709 }
710 
711 inline void Actor::SetPosition(float x, float y)
712 {
713  this->SetPosition(Point(x, y));
714 }
715 
716 inline void Actor::SetPositionX(float x)
717 {
718  this->SetPosition(Point(x, transform_.position.y));
719 }
720 
721 inline void Actor::SetPositionY(float y)
722 {
723  this->SetPosition(Point(transform_.position.x, y));
724 }
725 
726 inline void Actor::MoveTo(const Point& p)
727 {
728  this->SetPosition(p);
729 }
730 
731 inline void Actor::MoveTo(float x, float y)
732 {
733  this->SetPosition(Point(x, y));
734 }
735 
736 inline void Actor::MoveBy(const Vec2& trans)
737 {
738  this->SetPosition(transform_.position.x + trans.x, transform_.position.y + trans.y);
739 }
740 
741 inline void Actor::MoveBy(float trans_x, float trans_y)
742 {
743  this->MoveBy(Vec2(trans_x, trans_y));
744 }
745 
746 inline void Actor::SetScale(float scalex, float scaley)
747 {
748  this->SetScale(Vec2(scalex, scaley));
749 }
750 
751 inline void Actor::SetAnchor(float anchorx, float anchory)
752 {
753  this->SetAnchor(Vec2(anchorx, anchory));
754 }
755 
756 inline void Actor::SetSize(float width, float height)
757 {
758  this->SetSize(Size(width, height));
759 }
760 
761 inline void Actor::SetWidth(float width)
762 {
763  this->SetSize(Size(width, size_.y));
764 }
765 
766 inline void Actor::SetHeight(float height)
767 {
768  this->SetSize(Size(size_.x, height));
769 }
770 
771 inline void Actor::SetSkew(float skewx, float skewy)
772 {
773  this->SetSkew(Vec2(skewx, skewy));
774 }
775 
777 {
778  return physic_body_;
779 }
780 
782 {
783  physic_body_ = body;
784 }
785 
786 } // namespace kiwano
float GetDisplayedOpacity() const
获取显示透明度
Definition: Actor.h:661
virtual Point GetAnchor() const
获取锚点
Definition: Actor.h:641
事件分发器
Definition: EventDispatcher.h:35
float GetPositionX() const
获取 x 坐标
Definition: Actor.h:566
void ShowBorder(bool show)
渲染角色边界
Definition: Actor.h:706
bool IsVisible() const
获取显示状态
Definition: Actor.h:536
float GetHeight() const
获取高度
Definition: Actor.h:616
bool IsEventDispatchEnabled() const
是否启用事件分发
Definition: Actor.h:546
侵入式链表元素
Definition: IntrusiveList.h:434
组件管理器
Definition: ComponentManager.h:41
导演
Definition: Director.h:35
任务调度器
Definition: TaskScheduler.h:35
virtual void SetAnchor(const Vec2 &anchor)
设置锚点位置,默认为 (0, 0), 范围 [0, 1]
Definition: Actor.cpp:398
virtual Point GetSkew() const
获取错切角度
Definition: Actor.h:591
void SetCallbackOnUpdate(const UpdateCallback &cb)
设置更新时的回调函数
Definition: Actor.h:696
float GetScaleX() const
获取横向缩放比例
Definition: Actor.h:581
序列化器
Definition: Serializable.h:39
virtual Point GetScale() const
获取缩放比例
Definition: Actor.h:576
virtual float GetRotation() const
获取旋转角度
Definition: Actor.h:606
virtual Point GetPosition() const
获取坐标
Definition: Actor.h:561
void SetPositionX(float x)
设置横坐标
Definition: Actor.h:716
void SetHeight(float height)
修改高度
Definition: Actor.h:766
float GetSkewY() const
获取纵向错切角度
Definition: Actor.h:601
float GetScaledWidth() const
获取缩放后的宽度
Definition: Actor.h:626
int GetZOrder() const
获取 Z 轴顺序
Definition: Actor.h:556
Size GetScaledSize() const
获取缩放后的大小
Definition: Actor.h:636
角色
Definition: Actor.h:63
void SetWidth(float width)
修改宽度
Definition: Actor.h:761
virtual void SetSkew(const Vec2 &skew)
设置错切角度,默认为 (0, 0)
Definition: Actor.cpp:451
舞台过渡动画
Definition: Transition.h:46
float GetAnchorY() const
获取 y 方向锚点
Definition: Actor.h:651
bool IsUpdatePausing() const
角色更新是否暂停
Definition: Actor.h:691
virtual float GetOpacity() const
获取透明度
Definition: Actor.h:656
void SetPositionY(float y)
设置纵坐标
Definition: Actor.h:721
virtual void SetSize(const Size &size)
修改大小
Definition: Actor.cpp:407
void MoveBy(const Vec2 &trans)
移动相对坐标
Definition: Actor.h:736
Definition: Actor.cpp:26
时间段
Definition: Duration.h:47
Stage * GetStage() const
获取所在舞台
Definition: Actor.h:676
事件
Definition: Event.h:43
UpdateCallback GetCallbackOnUpdate() const
获取更新时的回调函数
Definition: Actor.h:701
size_t GetHashName() const
获取名称的 Hash 值
Definition: Actor.h:551
virtual Size GetSize() const
获取大小
Definition: Actor.h:621
float GetWidth() const
获取宽度
Definition: Actor.h:611
void MoveTo(const Point &p)
移动至坐标
Definition: Actor.h:726
void SetPhysicBody(physics::PhysicBody *body)
设置物理身体
Definition: Actor.h:781
渲染上下文
Definition: RenderContext.h:52
bool IsCascadeOpacityEnabled() const
是否启用级联透明度
Definition: Actor.h:541
物体
Definition: PhysicBody.h:39
virtual void SetScale(const Vec2 &scale)
设置缩放比例,默认为 (1.0, 1.0)
Definition: Actor.cpp:442
virtual void OnRender(RenderContext &ctx)
渲染角色
Definition: Actor.h:531
physics::PhysicBody * GetPhysicBody() const
获取物理身体
Definition: Actor.h:776
float GetScaledHeight() const
获取缩放后的高度
Definition: Actor.h:631
virtual void SetPosition(const Point &point)
设置坐标
Definition: Actor.cpp:433
Transform GetTransform() const
获取变换
Definition: Actor.h:666
float GetAnchorX() const
获取 x 方向锚点
Definition: Actor.h:646
Function< void(Duration)> UpdateCallback
角色更新回调函数
Definition: Actor.h:78
float GetScaleY() const
获取纵向缩放比例
Definition: Actor.h:586
舞台
Definition: Stage.h:40
float GetPositionY() const
获取 y 坐标
Definition: Actor.h:571
virtual void OnUpdate(Duration dt)
更新角色
Definition: Actor.h:526
float GetSkewX() const
获取横向错切角度
Definition: Actor.h:596
void ResumeUpdating()
继续角色更新
Definition: Actor.h:686
void PauseUpdating()
暂停角色更新
Definition: Actor.h:681
反序列化器
Definition: Serializable.h:146
动画调度器
Definition: Animator.h:36
基础对象
Definition: ObjectBase.h:134
Actor * GetParent() const
获取父角色
Definition: Actor.h:671