重做Scene,继承Node类

This commit is contained in:
Nomango 2018-08-13 23:47:10 +08:00
parent 857f13230f
commit adcd8ff1f2
18 changed files with 99 additions and 203 deletions

View File

@ -1,5 +1,6 @@
#include "..\e2dbase.h"
#include "..\e2dmanager.h"
#include "..\e2dnode.h"
#include <imm.h>
#pragma comment (lib ,"imm32.lib")

View File

@ -1,91 +0,0 @@
#include "..\e2dbase.h"
#include "..\e2dnode.h"
#include "..\e2dmanager.h"
e2d::Scene::Scene()
: _root(nullptr)
{
_root = new (e2d::autorelease) Node();
_root->retain();
_root->_setParentScene(this);
}
e2d::Scene::~Scene()
{
GC::getInstance()->safeRelease(_root);
}
void e2d::Scene::render()
{
_root->_render();
if (Game::getInstance()->getConfig().isOutlineVisible())
{
auto brush = Renderer::getInstance()->getSolidColorBrush();
brush->SetColor(D2D1::ColorF(D2D1::ColorF::Red, 0.6f));
brush->SetOpacity(1.f);
_root->_renderOutline();
}
if (Game::getInstance()->getConfig().isColliderVisible())
{
Renderer::getInstance()->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
_root->_renderCollider();
}
}
void e2d::Scene::dispatch(const MouseEvent & e)
{
auto dispatcher = dynamic_cast<EventHandler*>(this);
if (dispatcher)
dispatcher->handle(e);
_root->dispatch(e, false);
}
void e2d::Scene::dispatch(const KeyEvent & e)
{
auto dispatcher = dynamic_cast<EventHandler*>(this);
if (dispatcher)
dispatcher->handle(e);
_root->dispatch(e, false);
}
void e2d::Scene::add(Node * child, int order /* = 0 */)
{
_root->addChild(child, order);
}
void e2d::Scene::add(const std::vector<Node*>& nodes, int order)
{
for (const auto& node : nodes)
{
this->add(node, order);
}
}
bool e2d::Scene::remove(Node * child)
{
return _root->removeChild(child);
}
std::vector<e2d::Node*> e2d::Scene::getChildren(const String& name) const
{
return _root->getChildren(name);
}
e2d::Node * e2d::Scene::getChild(const String& name) const
{
return _root->getChild(name);
}
const std::vector<e2d::Node*>& e2d::Scene::getAllChildren() const
{
return _root->getAllChildren();
}
e2d::Node * e2d::Scene::getRoot() const
{
return _root;
}

View File

@ -1,5 +1,5 @@
#include "..\e2dmanager.h"
#include "..\e2dbase.h"
#include "..\e2dnode.h"
#include "..\e2dtransition.h"
@ -161,7 +161,7 @@ void e2d::SceneManager::render()
}
else if (_currScene)
{
_currScene->render();
_currScene->visit();
}
}
@ -172,7 +172,7 @@ void e2d::SceneManager::dispatch(const MouseEvent & e)
if (_currScene)
{
_currScene->dispatch(e);
_currScene->dispatch(e, false);
}
}
@ -183,6 +183,6 @@ void e2d::SceneManager::dispatch(const KeyEvent & e)
if (_currScene)
{
_currScene->dispatch(e);
_currScene->dispatch(e, false);
}
}

View File

@ -189,9 +189,9 @@ bool e2d::Button::dispatch(const MouseEvent & e, bool handled)
return Node::dispatch(e, handled);
}
void e2d::Button::_render()
void e2d::Button::visit()
{
Node::_render();
Node::visit();
if (_visible &&
!_enabled &&

View File

@ -78,7 +78,7 @@ e2d::Node::~Node()
}
}
void e2d::Node::_render()
void e2d::Node::visit()
{
if (!_visible)
return;
@ -116,7 +116,7 @@ void e2d::Node::_render()
// 访问 Order 小于零的节点
if (child->getOrder() < 0)
{
child->_render();
child->visit();
}
else
{
@ -129,7 +129,7 @@ void e2d::Node::_render()
// 访问剩余节点
for (; i < _children.size(); ++i)
_children[i]->_render();
_children[i]->visit();
}
if (_clipEnabled)

30
core/Node/Scene.cpp Normal file
View File

@ -0,0 +1,30 @@
#include "..\e2dbase.h"
#include "..\e2dnode.h"
#include "..\e2dmanager.h"
e2d::Scene::Scene()
{
}
e2d::Scene::~Scene()
{
}
void e2d::Scene::visit()
{
Node::visit();
if (Game::getInstance()->getConfig().isOutlineVisible())
{
auto brush = Renderer::getInstance()->getSolidColorBrush();
brush->SetColor(D2D1::ColorF(D2D1::ColorF::Red, 0.6f));
brush->SetOpacity(1.f);
Node::_renderOutline();
}
if (Game::getInstance()->getConfig().isColliderVisible())
{
Renderer::getInstance()->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
Node::_renderCollider();
}
}

View File

@ -34,8 +34,8 @@ bool e2d::MoveTransition::init(Scene * prev, Scene * next)
_startPos = Point(-width, 0);
}
if (_outScene) _outScene->getRoot()->setPos(0, 0);
_inScene->getRoot()->setPos(_startPos);
if (_outScene) _outScene->setPos(0, 0);
_inScene->setPos(_startPos);
return true;
}
return false;
@ -47,11 +47,11 @@ void e2d::MoveTransition::update()
if (_outScene)
{
_outScene->getRoot()->setPos(_posDelta * _delta);
_outScene->setPos(_posDelta * _delta);
}
if (_inScene)
{
_inScene->getRoot()->setPos(_startPos + _posDelta * _delta);
_inScene->setPos(_startPos + _posDelta * _delta);
}
if (_delta >= 1)
@ -62,7 +62,6 @@ void e2d::MoveTransition::update()
void e2d::MoveTransition::reset()
{
if (_outScene) _outScene->getRoot()->setPos(0, 0);
_inScene->getRoot()->setPos(0, 0);
if (_outScene) _outScene->setPos(0, 0);
_inScene->setPos(0, 0);
}

View File

@ -84,7 +84,7 @@ void e2d::Transition::render()
if (_outScene)
{
Point rootPos = _outScene->getRoot()->getPos();
Point rootPos = _outScene->getPos();
auto clipRect = D2D1::RectF(
std::max(rootPos.x, 0.f),
std::max(rootPos.y, 0.f),
@ -95,7 +95,7 @@ void e2d::Transition::render()
pRT->PushAxisAlignedClip(clipRect, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
pRT->PushLayer(_outLayerParam, _outLayer);
_outScene->render();
_outScene->visit();
pRT->PopLayer();
pRT->PopAxisAlignedClip();
@ -103,7 +103,7 @@ void e2d::Transition::render()
if (_inScene)
{
Point rootPos = _inScene->getRoot()->getPos();
Point rootPos = _inScene->getPos();
auto clipRect = D2D1::RectF(
std::max(rootPos.x, 0.f),
std::max(rootPos.y, 0.f),
@ -114,7 +114,7 @@ void e2d::Transition::render()
pRT->PushAxisAlignedClip(clipRect, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
pRT->PushLayer(_inLayerParam, _inLayer);
_inScene->render();
_inScene->visit();
pRT->PopLayer();
pRT->PopAxisAlignedClip();

View File

@ -982,79 +982,6 @@ protected:
};
// 场景
class Scene :
public Ref
{
public:
Scene();
virtual ~Scene();
// 进入场景
virtual void onEnter() {}
// 退出场景
virtual void onExit() {}
// 关闭窗口
// 说明:返回 false 将阻止窗口关闭
virtual bool onCloseWindow() { return true; }
// 添加节点到场景
void add(
Node * child, /* 要添加的节点 */
int zOrder = 0 /* 渲染顺序 */
);
// 添加多个节点到场景
virtual void add(
const std::vector<Node*>& nodes, /* 节点数组 */
int order = 0 /* 渲染顺序 */
);
// 删除子节点
bool remove(
Node * child
);
// 获取名称相同的子节点
Node* getChild(
const String& name
) const;
// 获取所有名称相同的子节点
std::vector<Node*> getChildren(
const String& name
) const;
// 获取所有子节点
const std::vector<Node*>& getAllChildren() const;
// 获取根节点
Node * getRoot() const;
// 渲染场景画面
void render();
// 分发鼠标消息
void dispatch(
const MouseEvent& e
);
// 分发按键消息
void dispatch(
const KeyEvent& e
);
protected:
E2D_DISABLE_COPY(Scene);
protected:
Node * _root;
};
}

View File

@ -7,8 +7,8 @@ namespace e2d
class Node;
class Scene;
class Action;
class Collider;
class Transition;

View File

@ -394,12 +394,12 @@ public:
bool handled
);
// 遍历节点
virtual void visit();
protected:
E2D_DISABLE_COPY(Node);
// 渲染节点
virtual void _render();
// 渲染节点轮廓
virtual void _renderOutline();
@ -449,6 +449,34 @@ protected:
};
// 场景
class Scene :
public Node
{
public:
Scene();
virtual ~Scene();
// 进入场景
virtual void onEnter() {}
// 退出场景
virtual void onExit() {}
// 关闭窗口
// 说明:返回 false 将阻止窗口关闭
virtual bool onCloseWindow() { return true; }
// 渲染场景
virtual void visit() override;
protected:
E2D_DISABLE_COPY(Scene);
};
// 精灵
class Sprite :
public Node
{
@ -808,15 +836,15 @@ public:
bool handled
) override;
// 渲染节点
virtual void visit() override;
protected:
E2D_DISABLE_COPY(Button);
// 按钮状态枚举
enum class Status { Normal, Mouseover, Selected };
// 渲染节点
virtual void _render() override;
// 设置按钮状态
virtual void _setStatus(Status status);

View File

@ -5,6 +5,8 @@ namespace e2d
{
class Scene;
// 场景过渡
class Transition :
public Ref

View File

@ -66,7 +66,6 @@
<ClCompile Include="..\..\core\Common\Resource.cpp" />
<ClCompile Include="..\..\core\Common\Point.cpp" />
<ClCompile Include="..\..\core\Common\Rect.cpp" />
<ClCompile Include="..\..\core\Common\Scene.cpp" />
<ClCompile Include="..\..\core\Common\Size.cpp" />
<ClCompile Include="..\..\core\Common\String.cpp" />
<ClCompile Include="..\..\core\Common\Time.cpp" />
@ -81,6 +80,7 @@
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
<ClCompile Include="..\..\core\Manager\SceneManager.cpp" />
<ClCompile Include="..\..\core\Node\Button.cpp" />
<ClCompile Include="..\..\core\Node\Scene.cpp" />
<ClCompile Include="..\..\core\Node\Menu.cpp" />
<ClCompile Include="..\..\core\Node\Node.cpp" />
<ClCompile Include="..\..\core\Node\Sprite.cpp" />

View File

@ -138,9 +138,6 @@
<ClCompile Include="..\..\core\Common\Rect.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Scene.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Size.cpp">
<Filter>Common</Filter>
</ClCompile>
@ -240,5 +237,8 @@
<ClCompile Include="..\..\core\Base\Config.cpp">
<Filter>Base</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Node\Scene.cpp">
<Filter>Node</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -210,7 +210,6 @@
<ClCompile Include="..\..\core\Common\Resource.cpp" />
<ClCompile Include="..\..\core\Common\Point.cpp" />
<ClCompile Include="..\..\core\Common\Rect.cpp" />
<ClCompile Include="..\..\core\Common\Scene.cpp" />
<ClCompile Include="..\..\core\Common\Size.cpp" />
<ClCompile Include="..\..\core\Common\String.cpp" />
<ClCompile Include="..\..\core\Common\Time.cpp" />
@ -225,6 +224,7 @@
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
<ClCompile Include="..\..\core\Manager\SceneManager.cpp" />
<ClCompile Include="..\..\core\Node\Button.cpp" />
<ClCompile Include="..\..\core\Node\Scene.cpp" />
<ClCompile Include="..\..\core\Node\Menu.cpp" />
<ClCompile Include="..\..\core\Node\Node.cpp" />
<ClCompile Include="..\..\core\Node\Sprite.cpp" />

View File

@ -138,9 +138,6 @@
<ClCompile Include="..\..\core\Common\Rect.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Scene.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Size.cpp">
<Filter>Common</Filter>
</ClCompile>
@ -240,5 +237,8 @@
<ClCompile Include="..\..\core\Base\Config.cpp">
<Filter>Base</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Node\Scene.cpp">
<Filter>Node</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -230,7 +230,6 @@
<ClCompile Include="..\..\core\Common\Rect.cpp" />
<ClCompile Include="..\..\core\Common\Ref.cpp" />
<ClCompile Include="..\..\core\Common\Resource.cpp" />
<ClCompile Include="..\..\core\Common\Scene.cpp" />
<ClCompile Include="..\..\core\Common\Size.cpp" />
<ClCompile Include="..\..\core\Common\String.cpp" />
<ClCompile Include="..\..\core\Common\Image.cpp" />
@ -246,6 +245,7 @@
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
<ClCompile Include="..\..\core\Manager\SceneManager.cpp" />
<ClCompile Include="..\..\core\Node\Button.cpp" />
<ClCompile Include="..\..\core\Node\Scene.cpp" />
<ClCompile Include="..\..\core\Node\ToggleButton.cpp" />
<ClCompile Include="..\..\core\Node\Menu.cpp" />
<ClCompile Include="..\..\core\Node\Node.cpp" />

View File

@ -39,9 +39,6 @@
<ClCompile Include="..\..\core\Common\String.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Scene.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Manager\ActionManager.cpp">
<Filter>Manager</Filter>
</ClCompile>
@ -234,6 +231,9 @@
<ClCompile Include="..\..\core\Base\Config.cpp">
<Filter>Base</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Node\Scene.cpp">
<Filter>Node</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\core\easy2d.h" />