Magic_Game/core/base/Node.h

273 lines
5.3 KiB
C
Raw Normal View History

// Copyright (c) 2016-2018 Easy2D - 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
2018-11-16 15:53:39 +08:00
#include "base.hpp"
#include "time.h"
2018-11-20 01:20:06 +08:00
#include "Unit.h"
2018-11-15 23:40:13 +08:00
#include "TaskManager.h"
2018-11-22 19:31:44 +08:00
#include "ActionManager.h"
#include "EventDispatcher.h"
2018-11-16 15:53:39 +08:00
#include "intrusive/List.hpp"
namespace easy2d
{
class Game;
// <20>ڵ<EFBFBD>
class Node
2018-11-20 01:20:06 +08:00
: public Unit
, public TaskManager
2018-11-22 19:31:44 +08:00
, public ActionManager
, public EventDispatcher
2018-11-16 15:53:39 +08:00
, protected intrusive::ListItem<spNode>
{
friend class Game;
friend class Scene;
friend class Transition;
2018-11-16 15:53:39 +08:00
friend class intrusive::List<spNode>;
2018-11-16 15:53:39 +08:00
using Nodes = std::vector<spNode>;
using Children = intrusive::List<spNode>;
2018-11-15 23:40:13 +08:00
public:
Node();
2018-11-21 19:24:18 +08:00
// <20><>ʼ<EFBFBD><CABC><EFBFBD>ڵ<EFBFBD>
virtual void Init();
// <20><><EFBFBD>½ڵ<C2BD>
2018-11-21 19:24:18 +08:00
virtual void Update(Duration const& dt);
// <20><>Ⱦ<EFBFBD>ڵ<EFBFBD>
virtual void OnRender();
2018-11-22 19:31:44 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
virtual void HandleEvent(Event* e);
2018-11-20 01:20:06 +08:00
// <20><>ȡ<EFBFBD><C8A1>ʾ״̬
2018-11-18 20:26:41 +08:00
bool IsVisible() const { return visible_; }
2018-11-20 01:20:06 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
2018-11-18 20:26:41 +08:00
String const& GetName() const { return name_; }
2018-11-20 01:20:06 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>Ƶ<EFBFBD> Hash ֵ
2018-11-18 20:26:41 +08:00
size_t GetHashName() const { return hash_name_; }
2018-11-21 19:24:18 +08:00
// <20><>ȡ Z <20><>˳<EFBFBD><CBB3>
int GetZOrder() const { return z_order_; }
2018-11-20 01:20:06 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
virtual float GetWidth() const { return size_.width * transform_.scale.x; }
2018-11-20 01:20:06 +08:00
// <20><>ȡ<EFBFBD>߶<EFBFBD>
virtual float GetHeight() const { return size_.height * transform_.scale.y; }
2018-11-20 01:20:06 +08:00
// <20><>ȡ<EFBFBD><C8A1>С
2018-11-18 20:26:41 +08:00
Size GetSize() const { return Size{ GetWidth(), GetHeight() }; }
2018-11-20 01:20:06 +08:00
// <20><>ȡ x <20><><EFBFBD><EFBFBD>֧<EFBFBD><D6A7>
virtual float GetPivotX() const { return pivot_.x; }
2018-11-20 01:20:06 +08:00
// <20><>ȡ y <20><><EFBFBD><EFBFBD>֧<EFBFBD><D6A7>
virtual float GetPivotY() const { return pivot_.y; }
2018-11-20 01:20:06 +08:00
// <20><>ȡ͸<C8A1><CDB8><EFBFBD><EFBFBD>
2018-11-18 20:26:41 +08:00
virtual float GetOpacity() const { return opacity_; }
// <20><>ȡ<EFBFBD><C8A1>Χ<EFBFBD><CEA7>
virtual Rect GetBounds();
2018-11-20 01:20:06 +08:00
// <20><>ȡ<EFBFBD><C8A1>ά<EFBFBD><EFBFBD><E4BBBB><EFBFBD><EFBFBD>
virtual math::Matrix const& GetTransformMatrix() override;
2018-11-18 20:26:41 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ڵ<EFBFBD>
2018-11-22 19:31:44 +08:00
spNode GetParent() const;
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ڳ<EFBFBD><DAB3><EFBFBD>
spScene GetScene() const;
2018-11-18 20:26:41 +08:00
2018-11-20 01:20:06 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ʾ
void SetVisible(
bool val
);
2018-11-20 01:20:06 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void SetName(
String const& name
);
// <20><><EFBFBD><EFBFBD>֧<EFBFBD><D6A7><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD>λ<EFBFBD><CEBB>
// Ĭ<><C4AC>Ϊ 0, <20><>Χ [0, 1]
void SetPivotX(
float pivot_x
);
// <20><><EFBFBD><EFBFBD>֧<EFBFBD><D6A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
// Ĭ<><C4AC>Ϊ 0, <20><>Χ [0, 1]
void SetPivotY(
float pivot_y
);
// <20><><EFBFBD><EFBFBD>֧<EFBFBD><D6A7>λ<EFBFBD><CEBB>
// Ĭ<><C4AC>Ϊ (0, 0), <20><>Χ [0, 1]
virtual void SetPivot(
float pivot_x,
float pivot_y
);
2018-11-20 01:20:06 +08:00
// <20>޸Ŀ<DEB8><C4BF><EFBFBD>
2018-11-18 20:26:41 +08:00
void SetWidth(
float width
);
2018-11-20 01:20:06 +08:00
// <20>޸ĸ߶<C4B8>
void SetHeight(
float height
);
2018-11-20 01:20:06 +08:00
// <20>޸Ĵ<DEB8>С
void SetSize(
float width,
float height
);
2018-11-20 01:20:06 +08:00
// <20>޸Ĵ<DEB8>С
void SetSize(
const Size & size
);
2018-11-20 01:20:06 +08:00
virtual void SetTransform(
Transform const& transform
) override;
// <20><><EFBFBD><EFBFBD>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>
// Ĭ<><C4AC>Ϊ 1.0, <20><>Χ [0, 1]
void SetOpacity(
float opacity
);
2018-11-21 19:24:18 +08:00
// <20><><EFBFBD><EFBFBD> Z <20><>˳<EFBFBD><CBB3>
2018-11-20 01:20:06 +08:00
// Ĭ<><C4AC>Ϊ 0
2018-11-21 19:24:18 +08:00
void SetZOrder(
2018-11-20 01:20:06 +08:00
int order
);
2018-11-20 01:20:06 +08:00
// <20><><EFBFBD>ñ߿<C3B1><DFBF><EFBFBD>ɫ
void SetBorderColor(
const Color& color
);
// <20>жϵ<D0B6><CFB5>Ƿ<EFBFBD><C7B7>ڽڵ<DABD><DAB5><EFBFBD>
bool ContainsPoint(
const Point& point
);
// <20><><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD>
void AddChild(
spNode const& child,
2018-11-21 19:24:18 +08:00
int z_order = 0 /* Z <20><>˳<EFBFBD><CBB3> */
);
// <20><><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD>ӽڵ<D3BD>
void AddChild(
const Nodes& nodes, /* <20>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD> */
2018-11-21 19:24:18 +08:00
int z_order = 0 /* Z <20><>˳<EFBFBD><CBB3> */
);
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD>ӽڵ<D3BD>
Nodes GetChildren(
String const& name
) const;
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD>ӽڵ<D3BD>
spNode GetChild(
String const& name
) const;
2018-11-15 23:40:13 +08:00
// <20><>ȡȫ<C8A1><C8AB><EFBFBD>ӽڵ<D3BD>
Children const& GetChildren() const;
// <20>Ƴ<EFBFBD><C6B3>ӽڵ<D3BD>
bool RemoveChild(
spNode const& child
);
// <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD>ӽڵ<D3BD>
void RemoveChildren(
String const& child_name
);
// <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD>нڵ<D0BD>
void RemoveAllChildren();
// <20>Ӹ<EFBFBD><D3B8>ڵ<EFBFBD><DAB5>Ƴ<EFBFBD>
void RemoveFromParent();
2018-11-22 19:31:44 +08:00
virtual void DispatchEvent(Event* e) override;
2018-11-15 14:35:19 +08:00
// <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>֧<EFBFBD><D6A7>
static void SetDefaultPivot(
float pivot_x,
float pivot_y
);
2018-11-21 19:24:18 +08:00
// <20><><EFBFBD>ñ߿<C3B1><DFBF>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>
static void EnableBorder();
2018-11-21 19:24:18 +08:00
// <20><><EFBFBD>ñ߿<C3B1><DFBF>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>
static void DisableBorder();
protected:
2018-11-21 19:24:18 +08:00
void Render();
2018-11-21 19:24:18 +08:00
void DrawBorder();
2018-11-20 01:20:06 +08:00
2018-11-21 19:24:18 +08:00
void SortChildren();
2018-11-20 01:20:06 +08:00
void UpdateBorder();
void UpdateTransform();
void UpdateOpacity();
2018-11-22 19:31:44 +08:00
void SetScene(Scene* scene);
2018-11-21 19:24:18 +08:00
protected:
2018-11-21 19:24:18 +08:00
bool inited_;
2018-11-17 20:27:04 +08:00
bool visible_;
bool dirty_sort_;
2018-11-21 19:24:18 +08:00
int z_order_;
float opacity_;
float display_opacity_;
String name_;
size_t hash_name_;
2018-11-17 20:27:04 +08:00
Node* parent_;
2018-11-22 19:31:44 +08:00
Scene* scene_;
2018-11-17 20:27:04 +08:00
Color border_color_;
Children children_;
cpGeometry border_;
2018-11-20 01:20:06 +08:00
Point pivot_;
Size size_;
2018-11-17 20:27:04 +08:00
math::Matrix initial_matrix_;
math::Matrix final_matrix_;
};
}