diff --git a/core/Base/Window.cpp b/core/Base/Window.cpp index dd525cab..b9c11575 100644 --- a/core/Base/Window.cpp +++ b/core/Base/Window.cpp @@ -1,5 +1,6 @@ #include "..\e2dbase.h" #include "..\e2dmanager.h" +#include "..\e2dnode.h" #include #pragma comment (lib ,"imm32.lib") diff --git a/core/Common/Scene.cpp b/core/Common/Scene.cpp deleted file mode 100644 index f5d495f2..00000000 --- a/core/Common/Scene.cpp +++ /dev/null @@ -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(this); - if (dispatcher) - dispatcher->handle(e); - - _root->dispatch(e, false); -} - -void e2d::Scene::dispatch(const KeyEvent & e) -{ - auto dispatcher = dynamic_cast(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& 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::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::Scene::getAllChildren() const -{ - return _root->getAllChildren(); -} - -e2d::Node * e2d::Scene::getRoot() const -{ - return _root; -} diff --git a/core/Manager/SceneManager.cpp b/core/Manager/SceneManager.cpp index 99d2c80b..dfe38125 100644 --- a/core/Manager/SceneManager.cpp +++ b/core/Manager/SceneManager.cpp @@ -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); } } diff --git a/core/Node/Button.cpp b/core/Node/Button.cpp index a5b97a2c..d493347f 100644 --- a/core/Node/Button.cpp +++ b/core/Node/Button.cpp @@ -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 && diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index 51dad4db..97d575c1 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -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) diff --git a/core/Node/Scene.cpp b/core/Node/Scene.cpp new file mode 100644 index 00000000..5eba909d --- /dev/null +++ b/core/Node/Scene.cpp @@ -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(); + } +} diff --git a/core/Transition/MoveTransition.cpp b/core/Transition/MoveTransition.cpp index 76e07495..3f90f106 100644 --- a/core/Transition/MoveTransition.cpp +++ b/core/Transition/MoveTransition.cpp @@ -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); } - diff --git a/core/Transition/Transition.cpp b/core/Transition/Transition.cpp index b487f316..92a97161 100644 --- a/core/Transition/Transition.cpp +++ b/core/Transition/Transition.cpp @@ -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(); diff --git a/core/e2dcommon.h b/core/e2dcommon.h index cec598b6..4f21e677 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -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& nodes, /* 节点数组 */ - int order = 0 /* 渲染顺序 */ - ); - - // 删除子节点 - bool remove( - Node * child - ); - - // 获取名称相同的子节点 - Node* getChild( - const String& name - ) const; - - // 获取所有名称相同的子节点 - std::vector getChildren( - const String& name - ) const; - - // 获取所有子节点 - const std::vector& 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; -}; - - } diff --git a/core/e2dmanager.h b/core/e2dmanager.h index 1fe957ee..32a2a7ce 100644 --- a/core/e2dmanager.h +++ b/core/e2dmanager.h @@ -7,8 +7,8 @@ namespace e2d class Node; +class Scene; class Action; -class Collider; class Transition; diff --git a/core/e2dnode.h b/core/e2dnode.h index f94ecd5d..19a7e79b 100644 --- a/core/e2dnode.h +++ b/core/e2dnode.h @@ -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); diff --git a/core/e2dtransition.h b/core/e2dtransition.h index 0d94a451..c6f6aee4 100644 --- a/core/e2dtransition.h +++ b/core/e2dtransition.h @@ -5,6 +5,8 @@ namespace e2d { +class Scene; + // 场景过渡 class Transition : public Ref diff --git a/project/vs2012/Easy2D.vcxproj b/project/vs2012/Easy2D.vcxproj index a1ee16e3..b3d57b97 100644 --- a/project/vs2012/Easy2D.vcxproj +++ b/project/vs2012/Easy2D.vcxproj @@ -66,7 +66,6 @@ - @@ -81,6 +80,7 @@ + diff --git a/project/vs2012/Easy2D.vcxproj.filters b/project/vs2012/Easy2D.vcxproj.filters index 75870946..23fb53cd 100644 --- a/project/vs2012/Easy2D.vcxproj.filters +++ b/project/vs2012/Easy2D.vcxproj.filters @@ -138,9 +138,6 @@ Common - - Common - Common @@ -240,5 +237,8 @@ Base + + Node + \ No newline at end of file diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index bafed80c..93a4c667 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -210,7 +210,6 @@ - @@ -225,6 +224,7 @@ + diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index 67d75b16..ceb284e8 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -138,9 +138,6 @@ Common - - Common - Common @@ -240,5 +237,8 @@ Base + + Node + \ No newline at end of file diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index cb54607a..5130057c 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -230,7 +230,6 @@ - @@ -246,6 +245,7 @@ + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index a687485f..fdfd2378 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -39,9 +39,6 @@ Common - - Common - Manager @@ -234,6 +231,9 @@ Base + + Node +