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

433 lines
8.9 KiB
C++

// Copyright (c) 2016-2018 Kiwano - Nomango
//
// 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:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// 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
#include <kiwano/2d/include-forwards.h>
#include <kiwano/2d/Transform.h>
#include <kiwano/2d/action/ActionManager.h>
#include <kiwano/base/TimerManager.h>
#include <kiwano/base/EventDispatcher.h>
namespace kiwano
{
class Director;
class RenderTarget;
// ½ÇÉ«
class KGE_API Actor
: public ObjectBase
, public TimerManager
, public ActionManager
, public EventDispatcher
, public intrusive_list_item<ActorPtr>
{
friend class Director;
friend class Transition;
friend class intrusive_list<ActorPtr>;
using Children = intrusive_list<ActorPtr>;
using UpdateCallback = Function<void(Duration)>;
public:
Actor();
// ¸üнÇÉ«
virtual void OnUpdate(Duration dt) { KGE_UNUSED(dt); }
// äÖȾ½ÇÉ«
virtual void OnRender(RenderTarget* rt) { KGE_UNUSED(rt); }
// »ñÈ¡ÏÔʾ״̬
bool IsVisible() const { return visible_; }
// »ñÈ¡ÏìӦ״̬
bool IsResponsible() const { return responsible_; }
// ÊÇ·ñÆôÓü¶ÁªÍ¸Ã÷¶È
bool IsCascadeOpacityEnabled() const { return cascade_opacity_; }
// »ñÈ¡Ãû³ÆµÄ Hash Öµ
size_t GetHashName() const { return hash_name_; }
// »ñÈ¡ Z Öá˳Ðò
int GetZOrder() const { return z_order_; }
// »ñÈ¡×ø±ê
Point GetPosition() const { return transform_.position; }
// »ñÈ¡ x ×ø±ê
float GetPositionX() const { return transform_.position.x; }
// »ñÈ¡ y ×ø±ê
float GetPositionY() const { return transform_.position.y; }
// »ñÈ¡Ëõ·Å±ÈÀý
Point GetScale() const { return transform_.scale; }
// »ñÈ¡ºáÏòËõ·Å±ÈÀý
float GetScaleX() const { return transform_.scale.x; }
// »ñÈ¡×ÝÏòËõ·Å±ÈÀý
float GetScaleY() const { return transform_.scale.y; }
// »ñÈ¡´íÇнǶÈ
Point GetSkew() const { return transform_.skew; }
// »ñÈ¡ºáÏò´íÇнǶÈ
float GetSkewX() const { return transform_.skew.x; }
// »ñÈ¡×ÝÏò´íÇнǶÈ
float GetSkewY() const { return transform_.skew.y; }
// »ñÈ¡Ðýת½Ç¶È
float GetRotation() const { return transform_.rotation; }
// »ñÈ¡¿í¶È
float GetWidth() const { return size_.x; }
// »ñÈ¡¸ß¶È
float GetHeight() const { return size_.y; }
// »ñÈ¡´óС
Size GetSize() const { return size_; }
// »ñÈ¡Ëõ·ÅºóµÄ¿í¶È
float GetScaledWidth() const { return size_.x * transform_.scale.x; }
// »ñÈ¡Ëõ·ÅºóµÄ¸ß¶È
float GetScaledHeight() const { return size_.y * transform_.scale.y; }
// »ñÈ¡Ëõ·ÅºóµÄ´óС
Size GetScaledSize() const { return Size{ GetScaledWidth(), GetScaledHeight() }; }
// »ñȡêµã
Point GetAnchor() const { return anchor_; }
// »ñÈ¡ x ·½Ïòêµã
float GetAnchorX() const { return anchor_.x; }
// »ñÈ¡ y ·½Ïòêµã
float GetAnchorY() const { return anchor_.y; }
// »ñȡ͸Ã÷¶È
float GetOpacity() const { return opacity_; }
// »ñÈ¡ÏÔʾ͸Ã÷¶È
float GetDisplayedOpacity() const { return displayed_opacity_; }
// »ñÈ¡±ä»»
Transform GetTransform() const { return transform_; }
// »ñÈ¡¸¸½ÇÉ«
inline Actor* GetParent() const { return parent_; }
// »ñÈ¡ËùÔÚÎę̀
inline Stage* GetStage() const { return stage_; }
// »ñÈ¡±ß¿ò
virtual Rect GetBounds() const;
// »ñÈ¡ÍâÇаüΧºÐ
virtual Rect GetBoundingBox() const;
// »ñÈ¡¶þά±ä»»¾ØÕó
Matrix3x2 const& GetTransformMatrix() const;
// »ñÈ¡¶þά±ä»»µÄÄæ¾ØÕó
Matrix3x2 const& GetTransformInverseMatrix() const;
// ÉèÖÃÊÇ·ñÏÔʾ
void SetVisible(
bool val
);
// ÉèÖÃÃû³Æ
void SetName(
String const& name
);
// ÉèÖÃ×ø±ê
virtual void SetPosition(
Point const& point
);
// ÉèÖÃ×ø±ê
inline void SetPosition(
float x,
float y
)
{
SetPosition(Point{ x, y });
}
// ÉèÖúá×ø±ê
void SetPositionX(
float x
);
// ÉèÖÃ×Ý×ø±ê
void SetPositionY(
float y
);
// ÒÆ¶¯×ø±ê
void Move(
Vec2 const& v
);
// ÒÆ¶¯×ø±ê
inline void Move(
float vx,
float vy
)
{
Move(Vec2{ vx, vy });
}
// ÉèÖÃËõ·Å±ÈÀý
// ĬÈÏΪ (1.0, 1.0)
virtual void SetScale(
Vec2 const& scale
);
// ÉèÖÃËõ·Å±ÈÀý
// ĬÈÏΪ (1.0, 1.0)
inline void SetScale(
float scalex,
float scaley
)
{
SetScale(Vec2{ scalex, scaley });
}
// ÉèÖôíÇнǶÈ
// ĬÈÏΪ (0, 0)
virtual void SetSkew(
Vec2 const& skew
);
// ÉèÖôíÇнǶÈ
// ĬÈÏΪ (0, 0)
inline void SetSkew(
float skewx,
float skewy
)
{
SetSkew(Vec2{ skewx, skewy });
}
// ÉèÖÃÐýת½Ç¶È
// ĬÈÏΪ 0
virtual void SetRotation(
float rotation
);
// ÉèÖÃêµãλÖÃ
// ĬÈÏΪ (0, 0), ·¶Î§ [0, 1]
virtual void SetAnchor(
Vec2 const& anchor
);
// ÉèÖÃêµãλÖÃ
// ĬÈÏΪ (0, 0), ·¶Î§ [0, 1]
inline void SetAnchor(
float anchorx,
float anchory
)
{
SetAnchor(Vec2{ anchorx, anchory });
}
// Ð޸Ŀí¶È
virtual void SetWidth(
float width
);
// Ð޸ĸ߶È
virtual void SetHeight(
float height
);
// Ð޸ĴóС
virtual void SetSize(
Size const& size
);
// Ð޸ĴóС
inline void SetSize(
float width,
float height
)
{
SetSize(Size{ width, height });
}
// ÉèÖÃ͸Ã÷¶È
// ĬÈÏΪ 1.0, ·¶Î§ [0, 1]
virtual void SetOpacity(
float opacity
);
// ÆôÓûò½ûÓü¶ÁªÍ¸Ã÷¶È
void SetCascadeOpacityEnabled(
bool enabled
);
// ÉèÖöþά·ÂÉä±ä»»
void SetTransform(
Transform const& transform
);
// ÉèÖà Z Öá˳Ðò
// ĬÈÏΪ 0
void SetZOrder(
int zorder
);
// ÊÇ·ñ¿ÉÏìÓ¦ (Êó±ê Hover | Out | Click ÏûÏ¢)
// ĬÈÏΪ false
void SetResponsible(
bool enable
);
// Ìí¼Ó×Ó½ÇÉ«
void AddChild(
ActorPtr child
);
// Ìí¼Ó¶à¸ö×Ó½ÇÉ«
void AddChildren(
Vector<ActorPtr> const& children
);
// »ñÈ¡ËùÓÐÃû³ÆÏàͬµÄ×Ó½ÇÉ«
Vector<ActorPtr> GetChildren(
String const& name
) const;
// »ñÈ¡Ãû³ÆÏàͬµÄ×Ó½ÇÉ«
ActorPtr GetChild(
String const& name
) const;
// »ñȡȫ²¿×Ó½ÇÉ«
Children const& GetChildren() const;
// ÒÆ³ý×Ó½ÇÉ«
void RemoveChild(
ActorPtr child
);
// ÒÆ³ý×Ó½ÇÉ«
void RemoveChild(
Actor* child
);
// ÒÆ³ýËùÓÐÃû³ÆÏàͬµÄ×Ó½ÇÉ«
void RemoveChildren(
String const& child_name
);
// ÒÆ³ýËùÓнÇÉ«
void RemoveAllChildren();
// ´Ó¸¸½ÇÉ«ÒÆ³ý
void RemoveFromParent();
// ÅжϵãÊÇ·ñÔÚ½ÇÉ«ÄÚ
virtual bool ContainsPoint(const Point& point) const;
// ʼþ·Ö·¢
void Dispatch(Event& evt) override;
// ÉèÖÃĬÈÏêµã
static void SetDefaultAnchor(
float anchor_x,
float anchor_y
);
// ÔÝÍ£½ÇÉ«¸üÐÂ
inline void PauseUpdating() { update_pausing_ = true; }
// ¼ÌÐø½ÇÉ«¸üÐÂ
inline void ResumeUpdating() { update_pausing_ = false; }
// ½ÇÉ«¸üÐÂÊÇ·ñÔÝÍ£
inline bool IsUpdatePausing() const { return update_pausing_; }
// ÉèÖøüÐÂʱµÄ»Øµ÷º¯Êý
inline void SetCallbackOnUpdate(UpdateCallback const& cb) { cb_update_ = cb; }
// »ñÈ¡¸üÐÂʱµÄ»Øµ÷º¯Êý
inline UpdateCallback GetCallbackOnUpdate() const { return cb_update_; }
// äÖȾ½ÇÉ«±ß½ç
inline void ShowBorder(bool show) { show_border_ = show; }
protected:
virtual void Update(Duration dt);
virtual void Render(RenderTarget* rt);
virtual void PrepareRender(RenderTarget* rt);
virtual void RenderBorder(RenderTarget* rt);
virtual bool CheckVisibilty(RenderTarget* rt) const;
void UpdateTransform() const;
void UpdateOpacity();
void Reorder();
void SetStage(Stage* stage);
protected:
bool visible_;
bool update_pausing_;
bool cascade_opacity_;
bool show_border_;
bool hover_;
bool pressed_;
bool responsible_;
int z_order_;
float opacity_;
float displayed_opacity_;
Actor* parent_;
Stage* stage_;
size_t hash_name_;
Point anchor_;
Size size_;
Children children_;
UpdateCallback cb_update_;
Transform transform_;
bool is_fast_transform_;
mutable bool visible_in_rt_;
mutable bool dirty_visibility_;
mutable bool dirty_transform_;
mutable bool dirty_transform_inverse_;
mutable Matrix3x2 transform_matrix_;
mutable Matrix3x2 transform_matrix_inverse_;
};
}