From 556f8f76deea5e668272a3dd581b8d8e6070e0b7 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Tue, 6 Feb 2018 21:11:54 +0800 Subject: [PATCH] fixed bugs about buttons. --- core/Action/Action.cpp | 3 +-- core/Base/Game.cpp | 6 +----- core/Common/Scene.cpp | 18 +++++------------ core/Manager/ActionManager.cpp | 29 ++++++++++----------------- core/Manager/SceneManager.cpp | 11 +++++++++-- core/Node/Button.cpp | 24 ++++++++++------------- core/Node/ButtonToggle.cpp | 1 + core/Node/Node.cpp | 36 ++++++++++++++++------------------ core/Tool/Music.cpp | 3 +++ core/emanagers.h | 8 +++----- core/enodes.h | 15 +++++--------- core/etools.h | 23 ++++++++-------------- project/vs2017/Easy2D.vcxproj | 2 +- 13 files changed, 74 insertions(+), 105 deletions(-) diff --git a/core/Action/Action.cpp b/core/Action/Action.cpp index a3de133e..d5199685 100644 --- a/core/Action/Action.cpp +++ b/core/Action/Action.cpp @@ -1,5 +1,4 @@ #include "..\eactions.h" -#include "..\emanagers.h" e2d::EAction::EAction() : m_bRunning(false) @@ -13,7 +12,6 @@ e2d::EAction::EAction() e2d::EAction::~EAction() { - EActionManager::__destroyAction(this); } bool e2d::EAction::isRunning() @@ -30,6 +28,7 @@ void e2d::EAction::startWith(ENode* pTarget) { m_bRunning = true; m_pTarget = pTarget; + this->reset(); } void e2d::EAction::resume() diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index 786af78a..9a5fd61d 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -23,11 +23,7 @@ bool e2d::EGame::init(LPCTSTR sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIc do { // 初始化 COM 组件 - if (FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED))) - { - WARN_IF(true, "CoInitializeEx Failed!"); - break; - } + CoInitializeEx(NULL, COINIT_MULTITHREADED); // 创建设备无关资源 if (!ERenderer::__createDeviceIndependentResources()) diff --git a/core/Common/Scene.cpp b/core/Common/Scene.cpp index 05574c80..bcd7796d 100644 --- a/core/Common/Scene.cpp +++ b/core/Common/Scene.cpp @@ -39,21 +39,13 @@ void e2d::EScene::_render() void e2d::EScene::_update() { - if (!EGame::isPaused()) + // 执行 onUpdate 函数 + if (m_bAutoUpdate) { - // 执行 onUpdate 函数 - if (m_bAutoUpdate) - { - this->onUpdate(); - } - // 更新根节点 - m_pRoot->_update(false); - } - else - { - // 更新根节点 - m_pRoot->_update(true); + this->onUpdate(); } + // 更新根节点 + m_pRoot->_update(); } void e2d::EScene::setAutoUpdate(bool bAutoUpdate) diff --git a/core/Manager/ActionManager.cpp b/core/Manager/ActionManager.cpp index 42b9bbc6..366d3235 100644 --- a/core/Manager/ActionManager.cpp +++ b/core/Manager/ActionManager.cpp @@ -90,18 +90,6 @@ void e2d::EActionManager::__clearAllActionsBindedWith(ENode * pTargetNode) } } -void e2d::EActionManager::__destroyAction(EAction * pAction) -{ - for (size_t i = 0; i < s_vActions.size(); i++) - { - if (pAction == s_vActions[i]) - { - s_vActions.erase(s_vActions.begin() + i); - return; - } - } -} - void e2d::EActionManager::resumeAllActions() { for (auto child : ESceneManager::getCurrentScene()->getRoot()->getChildren()) @@ -140,23 +128,26 @@ void e2d::EActionManager::__update() return; // 循环遍历所有正在运行的动作 - for (auto &action : s_vActions) + for (size_t i = 0; i < s_vActions.size(); i++) { + auto &action = s_vActions[i]; // 获取动作运行状态 if (action->isRunning() && action->getTarget() && action->getTarget()->getParentScene() == ESceneManager::getCurrentScene()) { - if (action->_isEnding()) - { - // 动作已经结束 - action->release(); - } - else + if (!action->_isEnding()) { // 执行动作 action->_update(); } + else + { + // 动作已经结束 + action->release(); + action->m_pTarget = nullptr; + s_vActions.erase(s_vActions.begin() + i); + } } } } diff --git a/core/Manager/SceneManager.cpp b/core/Manager/SceneManager.cpp index 5dd2935b..5c09f738 100644 --- a/core/Manager/SceneManager.cpp +++ b/core/Manager/SceneManager.cpp @@ -77,6 +77,11 @@ e2d::EScene * e2d::ESceneManager::getCurrentScene() return s_pCurrentScene; } +bool e2d::ESceneManager::isTransitioning() +{ + return s_pTransition != nullptr; +} + void e2d::ESceneManager::__update() { // 更新场景内容 @@ -147,8 +152,10 @@ void e2d::ESceneManager::__render() bool e2d::ESceneManager::__init() { - if (s_pNextScene == nullptr) - return false; + if (!s_pNextScene) + { + s_pNextScene = new EScene(); + } s_pCurrentScene = s_pNextScene; s_pCurrentScene->onEnter(); diff --git a/core/Node/Button.cpp b/core/Node/Button.cpp index 4869894e..d35ca975 100644 --- a/core/Node/Button.cpp +++ b/core/Node/Button.cpp @@ -1,4 +1,5 @@ #include "..\enodes.h" +#include "..\emanagers.h" e2d::EButton::EButton() : m_Callback((const BtnClkCallback &)nullptr) @@ -93,6 +94,7 @@ void e2d::EButton::setNormal(ENode * normal) { this->addChild(normal); normal->setPivot(m_fPivotX, m_fPivotY); + this->_setSize(normal->getWidth(), normal->getHeight()); } m_pNormal = normal; @@ -203,18 +205,18 @@ void e2d::EButton::setPivot(float pivotX, float pivotY) if (m_pDisabled) m_pDisabled->setPivot(pivotX, pivotY); } -void e2d::EButton::onUpdate() +void e2d::EButton::onFixedUpdate() { - if (m_bEnable && m_pNormal) - { - ENode * pMouseover = m_pMouseover ? m_pMouseover : m_pNormal; - ENode * pSelected = m_pSelected ? m_pSelected : m_pNormal; + if (ESceneManager::isTransitioning()) + return; + if (m_bEnable && m_bVisiable && m_pNormal) + { if (EInput::isMouseLButtonRelease()) { // 鼠标左键抬起时,判断鼠标坐标是否在按钮内部 if (m_bIsSelected && - pSelected->isPointIn(EInput::getMousePos())) + m_pNormal->isPointIn(EInput::getMousePos())) { _runCallback(); } @@ -224,18 +226,17 @@ void e2d::EButton::onUpdate() if (EInput::isMouseLButtonPress()) { - if (pMouseover->isPointIn(EInput::getMousePos())) + if (m_pNormal->isPointIn(EInput::getMousePos())) { // 鼠标左键按下,且位于按钮内时,标记 m_bIsSelected 为 true m_bIsSelected = true; - _setState(EButton::SELECTED); return; } } if (m_bIsSelected && EInput::isMouseLButtonDown()) { - if (pSelected->isPointIn(EInput::getMousePos())) + if (m_pNormal->isPointIn(EInput::getMousePos())) { _setState(EButton::SELECTED); return; @@ -251,11 +252,6 @@ void e2d::EButton::onUpdate() } } -void e2d::EButton::onPause() -{ - this->onUpdate(); -} - void e2d::EButton::_setState(BTN_STATE state) { if (m_eBtnState != state) diff --git a/core/Node/ButtonToggle.cpp b/core/Node/ButtonToggle.cpp index 0b611228..3942d8a9 100644 --- a/core/Node/ButtonToggle.cpp +++ b/core/Node/ButtonToggle.cpp @@ -134,6 +134,7 @@ void e2d::EButtonToggle::setNormal(ENode * normal) { this->addChild(normal); normal->setPivot(m_fPivotX, m_fPivotY); + this->_setSize(normal->getWidth(), normal->getHeight()); } m_pNormalOn = normal; diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index 8b49c04c..8374ce1d 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -45,7 +45,7 @@ e2d::ENode::~ENode() } } -void e2d::ENode::_update(bool bPaused) +void e2d::ENode::_update() { if (m_bTransformNeeded) { @@ -77,7 +77,7 @@ void e2d::ENode::_update(bool bPaused) // 访问 Order 小于零的节点 if (child->getOrder() < 0) { - child->_update(bPaused); + child->_update(); } else { @@ -85,28 +85,28 @@ void e2d::ENode::_update(bool bPaused) } } - if (bPaused) + if (m_bAutoUpdate) { - this->onPause(); - } - else if (m_bAutoUpdate) - { - this->onUpdate(); + if (!EGame::isPaused()) + { + this->onUpdate(); + } + this->onFixedUpdate(); } // 访问剩余节点 for (; i < size; i++) - m_vChildren[i]->_update(bPaused); + m_vChildren[i]->_update(); } else { - if (bPaused) + if (m_bAutoUpdate) { - this->onPause(); - } - else if (m_bAutoUpdate) - { - this->onUpdate(); + if (!EGame::isPaused()) + { + this->onUpdate(); + } + this->onFixedUpdate(); } } } @@ -765,15 +765,13 @@ bool e2d::ENode::isPointIn(EPoint point) // 为节点创建一个形状 ID2D1RectangleGeometry * rect; ERenderer::getID2D1Factory()->CreateRectangleGeometry( - D2D1::RectF(0, 0, getRealWidth(), getRealHeight()), + D2D1::RectF(0, 0, getWidth(), getHeight()), &rect ); // 判断点是否在形状内 BOOL ret; rect->FillContainsPoint( - D2D1::Point2F( - point.x, - point.y), + D2D1::Point2F(point.x, point.y), &m_MatriFinal, &ret ); diff --git a/core/Tool/Music.cpp b/core/Tool/Music.cpp index 90e7f1cb..3c48a16d 100644 --- a/core/Tool/Music.cpp +++ b/core/Tool/Music.cpp @@ -122,6 +122,9 @@ bool EMusic::play(int nLoopCount) stop(); } + nLoopCount = min(nLoopCount, XAUDIO2_LOOP_INFINITE - 1); + nLoopCount = (nLoopCount < 0) ? XAUDIO2_LOOP_INFINITE : nLoopCount; + // 提交 wave 样本数据 XAUDIO2_BUFFER buffer = { 0 }; buffer.pAudioData = m_pbWaveData; diff --git a/core/emanagers.h b/core/emanagers.h index ea68844d..243347d7 100644 --- a/core/emanagers.h +++ b/core/emanagers.h @@ -62,6 +62,9 @@ public: // 获取当前场景 static EScene * getCurrentScene(); + // 是否正在进行转场动画 + static bool isTransitioning(); + private: // 更新场景内容 static void __update(); @@ -198,11 +201,6 @@ private: ENode * pTargetNode ); - // 删除指定的动作 - static void __destroyAction( - EAction * pAction - ); - // 重置所有动作状态 static void __resetAllActions(); }; diff --git a/core/enodes.h b/core/enodes.h index 3707e44d..50a5dca6 100644 --- a/core/enodes.h +++ b/core/enodes.h @@ -30,6 +30,9 @@ public: // 更新节点 virtual void onUpdate() {} + // 固定地更新(游戏暂停时仍然运行) + virtual void onFixedUpdate() {} + // 渲染节点 virtual void onRender() {} @@ -39,9 +42,6 @@ public: int nRelation /* 碰撞关系,取值为 ERelation::VALUE 中的一种 */ ) {} - // 游戏暂停时的处理 - virtual void onPause() {} - // 节点被销毁时的处理 virtual void onDestroy() {} @@ -337,9 +337,7 @@ public: protected: // 更新节点 - void _update( - bool bPaused - ); + void _update(); // 渲染节点 void _render(); @@ -632,10 +630,7 @@ public: ) override; // 更新按钮状态 - virtual void onUpdate() override; - - // 更新游戏暂停时的按钮状态 - virtual void onPause() override; + virtual void onFixedUpdate() override; protected: enum BTN_STATE { NORMAL, MOUSEOVER, SELECTED }; diff --git a/core/etools.h b/core/etools.h index 1e5cc61c..d5a8a535 100644 --- a/core/etools.h +++ b/core/etools.h @@ -223,7 +223,7 @@ class EMusic public: // 播放 bool play( - int nLoopCount = 0 /* 重复播放次数,设置为 255 时循环播放 */ + int nLoopCount = 0 /* 重复播放次数,设置 -1 为循环播放 */ ); // 暂停 @@ -262,28 +262,21 @@ protected: virtual ~EMusic(); - // 打开音乐文件 - bool _open( - LPWSTR strFileName - ); + EMusic(const EMusic &) = delete; + + EMusic &operator =(const EMusic &) = delete; + + bool _open(LPWSTR strFileName); - // 关闭该播放器 void _close(); bool _readMMIO(); bool _resetFile(); - bool _read( - BYTE* pBuffer, - DWORD dwSizeToRead - ); + bool _read(BYTE* pBuffer, DWORD dwSizeToRead); - bool _findMediaFileCch( - WCHAR* strDestPath, - int cchDest, - LPCWSTR strFilename - ); + bool _findMediaFileCch(WCHAR* strDestPath, int cchDest, LPCWSTR strFilename); protected: bool m_bOpened; diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index 50224617..a1d75d22 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -101,7 +101,7 @@ Disabled WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true - None + EditAndContinue false