From 785dbf9b65a9da860cf8841a45e88747174b2c9d Mon Sep 17 00:00:00 2001 From: Nomango Date: Wed, 28 Mar 2018 17:56:04 +0800 Subject: [PATCH] The callback function is changed to a function object --- core/Action/Action.cpp | 118 +++++++++++++++- core/Action/ActionCallback.cpp | 14 +- core/Action/Animation.cpp | 5 +- core/Base/Game.cpp | 2 +- core/Base/Input.cpp | 13 +- core/Base/Window.cpp | 6 +- core/Common/Font.cpp | 2 +- core/Common/Image.cpp | 8 +- core/Common/Listener.cpp | 18 ++- core/Manager/ActionManager.cpp | 8 +- core/Manager/MusicManager.cpp | 12 +- core/Manager/TimerManager.cpp | 12 +- core/Node/Button.cpp | 20 +-- core/Node/ButtonToggle.cpp | 16 +-- core/Node/Node.cpp | 16 +-- core/Node/Sprite.cpp | 6 +- core/Node/Text.cpp | 8 +- core/Tool/Data.cpp | 64 +++++++++ core/Tool/File.cpp | 153 -------------------- core/Tool/Music.cpp | 4 +- core/Tool/Path.cpp | 193 +++++++++++++++++++------- core/Tool/Timer.cpp | 10 +- core/eactions.h | 27 ++-- core/easy2d.h | 3 +- core/ebase.h | 28 ++-- core/ecommon.h | 47 +++---- core/emanagers.h | 32 ++--- core/enodes.h | 154 ++++++++++---------- core/etools.h | 84 +++++------ project/vs2012/Easy2D.vcxproj | 10 +- project/vs2012/Easy2D.vcxproj.filters | 30 ++-- project/vs2017/Easy2D.vcxproj | 2 +- project/vs2017/Easy2D.vcxproj.filters | 12 +- 33 files changed, 629 insertions(+), 508 deletions(-) create mode 100644 core/Tool/Data.cpp delete mode 100644 core/Tool/File.cpp diff --git a/core/Action/Action.cpp b/core/Action/Action.cpp index 94f987a2..68aac364 100644 --- a/core/Action/Action.cpp +++ b/core/Action/Action.cpp @@ -58,7 +58,7 @@ e2d::String e2d::Action::getName() const return m_sName; } -void e2d::Action::setName(const String & name) +void e2d::Action::setName(String& name) { m_sName = name; } @@ -100,3 +100,119 @@ void e2d::Action::_resetTime() { m_fLast = Time::getTotalTime(); } + +e2d::ActionMoveBy * e2d::action::MoveBy(double duration, Vector vector) +{ + return new (std::nothrow) ActionMoveBy(duration, vector); +} + +e2d::ActionMoveTo * e2d::action::MoveTo(double duration, Point pos) +{ + return new (std::nothrow) ActionMoveTo(duration, pos); +} + +e2d::ActionScaleBy * e2d::action::ScaleBy(double duration, double scale) +{ + return new (std::nothrow) ActionScaleBy(duration, scale); +} + +e2d::ActionScaleBy * e2d::action::ScaleBy(double duration, double scaleX, double scaleY) +{ + return new (std::nothrow) ActionScaleBy(duration, scaleX, scaleY); +} + +e2d::ActionScaleTo * e2d::action::ScaleTo(double duration, double scale) +{ + return new (std::nothrow) ActionScaleTo(duration, scale); +} + +e2d::ActionScaleTo * e2d::action::ScaleTo(double duration, double scaleX, double scaleY) +{ + return new (std::nothrow) ActionScaleTo(duration, scaleX, scaleY); +} + +e2d::ActionOpacityBy * e2d::action::OpacityBy(double duration, double opacity) +{ + return new (std::nothrow) ActionOpacityBy(duration, opacity); +} + +e2d::ActionOpacityTo * e2d::action::OpacityTo(double duration, double opacity) +{ + return new (std::nothrow) ActionOpacityTo(duration, opacity); +} + +e2d::ActionFadeIn * e2d::action::FadeIn(double duration) +{ + return new (std::nothrow) ActionFadeIn(duration); +} + +e2d::ActionFadeOut * e2d::action::FadeOut(double duration) +{ + return new (std::nothrow) ActionFadeOut(duration); +} + +e2d::ActionRotateBy * e2d::action::RotateBy(double duration, double rotation) +{ + return new (std::nothrow) ActionRotateBy(duration, rotation); +} + +e2d::ActionRotateTo * e2d::action::RotateTo(double duration, double rotation) +{ + return new (std::nothrow) ActionRotateTo(duration, rotation); +} + +e2d::ActionTwo * e2d::action::Two(Action * pActionFirst, Action * pActionSecond, bool bAtSameTime) +{ + return new (std::nothrow) ActionTwo(pActionFirst, pActionSecond, bAtSameTime); +} + +e2d::ActionSequence * e2d::action::Sequence(int number, Action * action1, ...) +{ + auto action = new (std::nothrow) ActionSequence(); + if (action) + { + Action ** ppAction = &action1; + + while (number > 0) + { + ASSERT((*ppAction) != nullptr, "ActionSequence NULL pointer exception!"); + action->add(*ppAction); + ppAction++; + number--; + } + } + return action; +} + +e2d::ActionDelay * e2d::action::Delay(double duration) +{ + return new (std::nothrow) ActionDelay(duration); +} + +e2d::ActionLoop * e2d::action::Loop(Action * action, int times) +{ + return new (std::nothrow) ActionLoop(action, times); +} + +e2d::Animation * e2d::action::Animate(double interval, int number, Image * frame, ...) +{ + auto animation = new (std::nothrow) Animation(interval); + if (animation) + { + Image ** ppImage = &frame; + + while (number > 0) + { + ASSERT((*ppImage) != nullptr, "Animation NULL pointer exception!"); + animation->addKeyframe(*ppImage); + ppImage++; + number--; + } + } + return animation; +} + +e2d::ActionFunc * e2d::action::Func(Function func) +{ + return new (std::nothrow) ActionFunc(func); +} diff --git a/core/Action/ActionCallback.cpp b/core/Action/ActionCallback.cpp index b64c02a0..28b5e06d 100644 --- a/core/Action/ActionCallback.cpp +++ b/core/Action/ActionCallback.cpp @@ -1,21 +1,21 @@ #include "..\eactions.h" -e2d::ActionCallback::ActionCallback(VoidFunction callback) : - m_Callback(callback) +e2d::ActionFunc::ActionFunc(Function func) : + m_Callback(func) { } -e2d::ActionCallback * e2d::ActionCallback::clone() const +e2d::ActionFunc * e2d::ActionFunc::clone() const { - return new ActionCallback(m_Callback); + return new ActionFunc(m_Callback); } -void e2d::ActionCallback::_init() +void e2d::ActionFunc::_init() { - // 执行回调函数的动作不需要初始化 + // 执行函数对象的动作不需要初始化 } -void e2d::ActionCallback::_update() +void e2d::ActionFunc::_update() { m_Callback(); this->stop(); diff --git a/core/Action/Animation.cpp b/core/Action/Animation.cpp index 8e6ad590..1f47ede0 100644 --- a/core/Action/Animation.cpp +++ b/core/Action/Animation.cpp @@ -2,6 +2,7 @@ e2d::Animation::Animation() : m_nFrameIndex(0) + , m_fInterval(1) { } @@ -11,9 +12,9 @@ e2d::Animation::Animation(double interval) { } -e2d::Animation::Animation(double interval, int number, Image * frame, ...) +e2d::Animation::Animation(int number, Image * frame, ...) : m_nFrameIndex(0) - , m_fInterval(interval) + , m_fInterval(1) { Image ** ppImage = &frame; diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index 81910dca..f7bb623a 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -12,7 +12,7 @@ static bool s_bInitialized = false; static e2d::String s_sAppName; -bool e2d::Game::init(const String & sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID, const String & sAppname) +bool e2d::Game::init(String& sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID, String sAppname) { if (s_bInitialized) { diff --git a/core/Base/Input.cpp b/core/Base/Input.cpp index 7f00d455..bc33e2ce 100644 --- a/core/Base/Input.cpp +++ b/core/Base/Input.cpp @@ -186,13 +186,12 @@ void e2d::Input::__add(Listener * pListener) } } -void e2d::Input::add(ListenerCallback callback, const String & name) +void e2d::Input::add(Function func, String name) { - auto pListener = new Listener(callback, name); - pListener->start(); + (new Listener(func, name))->start(); } -void e2d::Input::start(const String & name) +void e2d::Input::start(String& name) { for (const auto & pListener : s_vListeners) { @@ -203,7 +202,7 @@ void e2d::Input::start(const String & name) } } -void e2d::Input::stop(const String & name) +void e2d::Input::stop(String& name) { for (const auto & pListener : s_vListeners) { @@ -214,7 +213,7 @@ void e2d::Input::stop(const String & name) } } -void e2d::Input::clear(const String & name) +void e2d::Input::clear(String& name) { for (const auto & pListener : s_vListeners) { @@ -249,7 +248,7 @@ void e2d::Input::clearAll() } } -std::vector e2d::Input::get(const String & name) +std::vector e2d::Input::get(String& name) { std::vector vListeners; for (auto pListener : s_vListeners) diff --git a/core/Base/Window.cpp b/core/Base/Window.cpp index b74fc247..18ce644f 100644 --- a/core/Base/Window.cpp +++ b/core/Base/Window.cpp @@ -9,7 +9,7 @@ static HWND s_HWnd = nullptr; static bool s_bShowConsole = false; -bool e2d::Window::__init(const String & sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID /*= nullptr*/) +bool e2d::Window::__init(String& sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID /*= nullptr*/) { if (!Window::__initMutex(sTitle)) { @@ -106,7 +106,7 @@ bool e2d::Window::__init(const String & sTitle, UINT32 nWidth, UINT32 nHeight, L return SUCCEEDED(hr); } -bool e2d::Window::__initMutex(const String & sTitle) +bool e2d::Window::__initMutex(String& sTitle) { // 创建进程互斥体 HANDLE m_hMutex = ::CreateMutex(NULL, TRUE, L"Easy2DApp-" + sTitle); @@ -204,7 +204,7 @@ void e2d::Window::setSize(UINT32 width, UINT32 height) ::MoveWindow(s_HWnd, (screenWidth - width) / 2, (screenHeight - height) / 2, width, height, TRUE); } -void e2d::Window::setTitle(const String &title) +void e2d::Window::setTitle(String&title) { // 设置窗口标题 ::SetWindowText(s_HWnd, title); diff --git a/core/Common/Font.cpp b/core/Common/Font.cpp index e32c0f89..9b4d9b17 100644 --- a/core/Common/Font.cpp +++ b/core/Common/Font.cpp @@ -50,7 +50,7 @@ bool e2d::Font::isItalic() const return m_bItalic; } -void e2d::Font::setFamily(const String & fontFamily) +void e2d::Font::setFamily(String& fontFamily) { m_sFontFamily = fontFamily; m_bRecreateNeeded = true; diff --git a/core/Common/Image.cpp b/core/Common/Image.cpp index 4ff1d456..ace6088e 100644 --- a/core/Common/Image.cpp +++ b/core/Common/Image.cpp @@ -13,12 +13,12 @@ e2d::Image::Image() { } -e2d::Image::Image(const String & strFileName) +e2d::Image::Image(String& strFileName) { this->open(strFileName); } -e2d::Image::Image(const String & strFileName, double nClipX, double nClipY, double nClipWidth, double nClipHeight) +e2d::Image::Image(String& strFileName, double nClipX, double nClipY, double nClipWidth, double nClipHeight) { this->open(strFileName); this->clip(nClipX, nClipY, nClipWidth, nClipHeight); @@ -28,7 +28,7 @@ e2d::Image::~Image() { } -void e2d::Image::open(const String & strFilePath) +void e2d::Image::open(String& strFilePath) { WARN_IF(strFilePath.isEmpty(), "Image cannot load bitmap from NULL file name."); @@ -124,7 +124,7 @@ e2d::Point e2d::Image::getClipPos() const return Point(m_fSourceClipX, m_fSourceClipY); } -bool e2d::Image::preload(const String & fileName) +bool e2d::Image::preload(String& fileName) { if (s_mBitmapsFromFile.find(fileName.getHashCode()) != s_mBitmapsFromFile.end()) { diff --git a/core/Common/Listener.cpp b/core/Common/Listener.cpp index 2be72528..01c01cd4 100644 --- a/core/Common/Listener.cpp +++ b/core/Common/Listener.cpp @@ -9,10 +9,18 @@ e2d::Listener::Listener() Input::__add(this); } -e2d::Listener::Listener(ListenerCallback callback, const String & name) +e2d::Listener::Listener(Function func) + : m_bRunning(false) + , m_callback(func) + , m_bClear(false) +{ + Input::__add(this); +} + +e2d::Listener::Listener(Function func, String& name) : m_bRunning(false) , m_sName(name) - , m_callback(callback) + , m_callback(func) , m_bClear(false) { Input::__add(this); @@ -44,14 +52,14 @@ e2d::String e2d::Listener::getName() return m_sName; } -void e2d::Listener::setName(const String & name) +void e2d::Listener::setName(String& name) { m_sName = name; } -void e2d::Listener::setCallback(ListenerCallback callback) +void e2d::Listener::setFunction(Function func) { - m_callback = callback; + m_callback = func; } void e2d::Listener::update() diff --git a/core/Manager/ActionManager.cpp b/core/Manager/ActionManager.cpp index 7dcbe7bc..88e18e01 100644 --- a/core/Manager/ActionManager.cpp +++ b/core/Manager/ActionManager.cpp @@ -114,7 +114,7 @@ void e2d::ActionManager::__stopAllBindedWith(Node * pTargetNode) } } -void e2d::ActionManager::resume(const String & strActionName) +void e2d::ActionManager::resume(String& strActionName) { for (auto action : s_vRunningActions) { @@ -125,7 +125,7 @@ void e2d::ActionManager::resume(const String & strActionName) } } -void e2d::ActionManager::pause(const String & strActionName) +void e2d::ActionManager::pause(String& strActionName) { for (auto action : s_vRunningActions) { @@ -136,7 +136,7 @@ void e2d::ActionManager::pause(const String & strActionName) } } -void e2d::ActionManager::stop(const String & strActionName) +void e2d::ActionManager::stop(String& strActionName) { for (auto action : s_vRunningActions) { @@ -191,7 +191,7 @@ void e2d::ActionManager::stopAll() } } -std::vector e2d::ActionManager::get(const String & strActionName) +std::vector e2d::ActionManager::get(String& strActionName) { std::vector vActions; for (const auto action : s_vActions) diff --git a/core/Manager/MusicManager.cpp b/core/Manager/MusicManager.cpp index 01a9530b..21eb51ab 100644 --- a/core/Manager/MusicManager.cpp +++ b/core/Manager/MusicManager.cpp @@ -15,7 +15,7 @@ static MusicList& getMusicList() } -bool e2d::MusicManager::preload(const String & strFilePath) +bool e2d::MusicManager::preload(String& strFilePath) { UINT nRet = strFilePath.getHashCode(); @@ -42,7 +42,7 @@ bool e2d::MusicManager::preload(const String & strFilePath) return false; } -bool e2d::MusicManager::play(const String & strFilePath, int nLoopCount) +bool e2d::MusicManager::play(String& strFilePath, int nLoopCount) { if (MusicManager::preload(strFilePath)) { @@ -56,7 +56,7 @@ bool e2d::MusicManager::play(const String & strFilePath, int nLoopCount) return false; } -void e2d::MusicManager::pause(const String & strFilePath) +void e2d::MusicManager::pause(String& strFilePath) { auto music = MusicManager::get(strFilePath); if (music) @@ -65,7 +65,7 @@ void e2d::MusicManager::pause(const String & strFilePath) } } -void e2d::MusicManager::resume(const String & strFilePath) +void e2d::MusicManager::resume(String& strFilePath) { auto music = MusicManager::get(strFilePath); if (music) @@ -74,7 +74,7 @@ void e2d::MusicManager::resume(const String & strFilePath) } } -void e2d::MusicManager::stop(const String & strFilePath) +void e2d::MusicManager::stop(String& strFilePath) { auto music = MusicManager::get(strFilePath); if (music) @@ -83,7 +83,7 @@ void e2d::MusicManager::stop(const String & strFilePath) } } -e2d::Music * e2d::MusicManager::get(const String & strFilePath) +e2d::Music * e2d::MusicManager::get(String& strFilePath) { if (strFilePath.isEmpty()) return nullptr; diff --git a/core/Manager/TimerManager.cpp b/core/Manager/TimerManager.cpp index 2bef25e1..bb5c5e3b 100644 --- a/core/Manager/TimerManager.cpp +++ b/core/Manager/TimerManager.cpp @@ -28,9 +28,9 @@ void e2d::TimerManager::__update() } } -void e2d::TimerManager::start(double timeOut, TimerCallback callback) +void e2d::TimerManager::start(double timeOut, Function func) { - (new Timer(L"", callback, timeOut, 1, false, true))->start(); + (new Timer(func, L"", timeOut, 1, false, true))->start(); } void e2d::TimerManager::__add(Timer * pTimer) @@ -62,7 +62,7 @@ void e2d::TimerManager::__add(Timer * pTimer) } } -void e2d::TimerManager::start(const String & name) +void e2d::TimerManager::start(String& name) { for (auto timer : s_vTimers) { @@ -73,7 +73,7 @@ void e2d::TimerManager::start(const String & name) } } -void e2d::TimerManager::stop(const String & name) +void e2d::TimerManager::stop(String& name) { for (auto timer : s_vTimers) { @@ -84,7 +84,7 @@ void e2d::TimerManager::stop(const String & name) } } -void e2d::TimerManager::clear(const String & name) +void e2d::TimerManager::clear(String& name) { for (auto timer : s_vTimers) { @@ -95,7 +95,7 @@ void e2d::TimerManager::clear(const String & name) } } -std::vector e2d::TimerManager::get(const String & name) +std::vector e2d::TimerManager::get(String& name) { std::vector vTimers; for (auto timer : s_vTimers) diff --git a/core/Node/Button.cpp b/core/Node/Button.cpp index 66da1a6e..a9395f37 100644 --- a/core/Node/Button.cpp +++ b/core/Node/Button.cpp @@ -16,7 +16,7 @@ e2d::Button::Button() { } -e2d::Button::Button(Node * normal, ButtonCallback callback) +e2d::Button::Button(Node * normal, Function func) : m_Callback(nullptr) , m_eBtnState(Button::NORMAL) , m_bEnable(true) @@ -27,10 +27,10 @@ e2d::Button::Button(Node * normal, ButtonCallback callback) , m_pDisabled(nullptr) { this->setNormal(normal); - this->setCallback(callback); + this->setFunction(func); } -e2d::Button::Button(Node * normal, Node * selected, ButtonCallback callback) +e2d::Button::Button(Node * normal, Node * selected, Function func) : m_Callback(nullptr) , m_eBtnState(Button::NORMAL) , m_bEnable(true) @@ -42,10 +42,10 @@ e2d::Button::Button(Node * normal, Node * selected, ButtonCallback callback) { this->setNormal(normal); this->setSelected(selected); - this->setCallback(callback); + this->setFunction(func); } -e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, ButtonCallback callback) +e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Function func) : m_Callback(nullptr) , m_eBtnState(Button::NORMAL) , m_bEnable(true) @@ -58,10 +58,10 @@ e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, ButtonCall this->setNormal(normal); this->setMouseOver(mouseover); this->setSelected(selected); - this->setCallback(callback); + this->setFunction(func); } -e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Node * disabled, ButtonCallback callback) +e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Node * disabled, Function func) : m_Callback(nullptr) , m_eBtnState(Button::NORMAL) , m_bEnable(true) @@ -75,7 +75,7 @@ e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Node * dis this->setMouseOver(mouseover); this->setSelected(selected); this->setDisabled(disabled); - this->setCallback(callback); + this->setFunction(func); } bool e2d::Button::isEnable() const @@ -170,11 +170,11 @@ void e2d::Button::setEnable(bool bEnable) } } -void e2d::Button::setCallback(ButtonCallback callback) +void e2d::Button::setFunction(Function func) { WARN_IF(m_pNormal == nullptr, "Button cannot work without anything to show. Please set its normal displayed."); - m_Callback = callback; + m_Callback = func; } void e2d::Button::onFixedUpdate() diff --git a/core/Node/ButtonToggle.cpp b/core/Node/ButtonToggle.cpp index 5b0723fa..f492af49 100644 --- a/core/Node/ButtonToggle.cpp +++ b/core/Node/ButtonToggle.cpp @@ -14,7 +14,7 @@ e2d::ButtonToggle::ButtonToggle() { } -e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, ButtonCallback callback) +e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, Function func) : Button() , m_bState(true) , m_pNormalOn(nullptr) @@ -28,10 +28,10 @@ e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, B { this->setNormal(toggleOnNormal); this->setNormalOff(toggleOffNormal); - this->setCallback(callback); + this->setFunction(func); } -e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, Node * toggleOnSelected, Node * toggleOffSelected, ButtonCallback callback) +e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, Node * toggleOnSelected, Node * toggleOffSelected, Function func) : Button() , m_bState(true) , m_pNormalOn(nullptr) @@ -47,10 +47,10 @@ e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, N this->setNormalOff(toggleOffNormal); this->setSelected(toggleOnSelected); this->setSelectedOff(toggleOffSelected); - this->setCallback(callback); + this->setFunction(func); } -e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, Node * toggleOnMouseOver, Node * toggleOffMouseOver, Node * toggleOnSelected, Node * toggleOffSelected, ButtonCallback callback) +e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, Node * toggleOnMouseOver, Node * toggleOffMouseOver, Node * toggleOnSelected, Node * toggleOffSelected, Function func) : Button() , m_bState(true) , m_pNormalOn(nullptr) @@ -68,10 +68,10 @@ e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, N this->setMouseOverOff(toggleOffMouseOver); this->setSelected(toggleOnSelected); this->setSelectedOff(toggleOffSelected); - this->setCallback(callback); + this->setFunction(func); } -e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, Node * toggleOnMouseOver, Node * toggleOffMouseOver, Node * toggleOnSelected, Node * toggleOffSelected, Node * toggleOnDisabled, Node * toggleOffDisabled, ButtonCallback callback) +e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, Node * toggleOnMouseOver, Node * toggleOffMouseOver, Node * toggleOnSelected, Node * toggleOffSelected, Node * toggleOnDisabled, Node * toggleOffDisabled, Function func) : Button() , m_bState(true) , m_pNormalOn(nullptr) @@ -91,7 +91,7 @@ e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, N this->setSelectedOff(toggleOffSelected); this->setDisabled(toggleOnDisabled); this->setDisabledOff(toggleOffDisabled); - this->setCallback(callback); + this->setFunction(func); } bool e2d::ButtonToggle::getState() const diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index ce60b62f..8cf9f0de 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -614,7 +614,7 @@ e2d::Scene * e2d::Node::getParentScene() const return m_pParentScene; } -std::vector e2d::Node::getChildren(const String & name) +std::vector e2d::Node::getChildren(String& name) { std::vector vChildren; unsigned int hash = name.getHashCode(); @@ -684,7 +684,7 @@ bool e2d::Node::removeChild(Node * child) return false; } -void e2d::Node::removeChildren(const String & childName) +void e2d::Node::removeChildren(String& childName) { WARN_IF(childName.isEmpty(), "Invalid Node name."); @@ -753,7 +753,7 @@ void e2d::Node::runAction(Action * action) } } -void e2d::Node::resumeAction(const String & strActionName) +void e2d::Node::resumeAction(String& strActionName) { auto actions = ActionManager::get(strActionName); for (auto action : actions) @@ -765,7 +765,7 @@ void e2d::Node::resumeAction(const String & strActionName) } } -void e2d::Node::pauseAction(const String & strActionName) +void e2d::Node::pauseAction(String& strActionName) { auto actions = ActionManager::get(strActionName); for (auto action : actions) @@ -777,7 +777,7 @@ void e2d::Node::pauseAction(const String & strActionName) } } -void e2d::Node::stopAction(const String & strActionName) +void e2d::Node::stopAction(String& strActionName) { auto actions = ActionManager::get(strActionName); for (auto action : actions) @@ -789,7 +789,7 @@ void e2d::Node::stopAction(const String & strActionName) } } -e2d::Action * e2d::Node::getAction(const String & strActionName) +e2d::Action * e2d::Node::getAction(String& strActionName) { auto actions = ActionManager::get(strActionName); for (auto action : actions) @@ -802,7 +802,7 @@ e2d::Action * e2d::Node::getAction(const String & strActionName) return nullptr; } -std::vector e2d::Node::getActions(const String & strActionName) +std::vector e2d::Node::getActions(String& strActionName) { std::vector::iterator iter; auto actions = ActionManager::get(strActionName); @@ -970,7 +970,7 @@ void e2d::Node::setVisiable(bool value) m_bVisiable = value; } -void e2d::Node::setName(const String & name) +void e2d::Node::setName(String& name) { WARN_IF(name.isEmpty(), "Invalid Node name."); diff --git a/core/Node/Sprite.cpp b/core/Node/Sprite.cpp index 09283ce4..baaf32f9 100644 --- a/core/Node/Sprite.cpp +++ b/core/Node/Sprite.cpp @@ -12,13 +12,13 @@ e2d::Sprite::Sprite(Image * image) open(image); } -e2d::Sprite::Sprite(const String & imageFileName) +e2d::Sprite::Sprite(String& imageFileName) : m_pImage(nullptr) { open(imageFileName); } -e2d::Sprite::Sprite(const String & imageFileName, double x, double y, double width, double height) +e2d::Sprite::Sprite(String& imageFileName, double x, double y, double width, double height) : m_pImage(nullptr) { open(imageFileName); @@ -42,7 +42,7 @@ void e2d::Sprite::open(Image * image) } } -void e2d::Sprite::open(const String & imageFileName) +void e2d::Sprite::open(String& imageFileName) { open(new Image(imageFileName)); } diff --git a/core/Node/Text.cpp b/core/Node/Text.cpp index 45408ff4..8dc79098 100644 --- a/core/Node/Text.cpp +++ b/core/Node/Text.cpp @@ -8,7 +8,7 @@ e2d::Text::Text() this->setFont(new Font()); } -e2d::Text::Text(const String & text) +e2d::Text::Text(String& text) : m_bWordWrapping(false) , m_pFont(nullptr) , m_fWordWrappingWidth(0) @@ -25,7 +25,7 @@ e2d::Text::Text(Font * font) this->setFont(font); } -e2d::Text::Text(const String & text, Font * font) +e2d::Text::Text(String& text, Font * font) : m_bWordWrapping(false) , m_pFont(nullptr) , m_fWordWrappingWidth(0) @@ -34,7 +34,7 @@ e2d::Text::Text(const String & text, Font * font) this->setFont(font); } -e2d::Text::Text(const String & text, String fontFamily, double fontSize, UINT32 color, UINT32 fontWeight, bool italic) +e2d::Text::Text(String& text, String fontFamily, double fontSize, UINT32 color, UINT32 fontWeight, bool italic) : m_bWordWrapping(false) , m_pFont(nullptr) , m_fWordWrappingWidth(0) @@ -68,7 +68,7 @@ e2d::Font * e2d::Text::getFont() const return m_pFont; } -void e2d::Text::setText(const String & text) +void e2d::Text::setText(String& text) { m_sText = text; _initTextLayout(); diff --git a/core/Tool/Data.cpp b/core/Tool/Data.cpp new file mode 100644 index 00000000..3e9cc481 --- /dev/null +++ b/core/Tool/Data.cpp @@ -0,0 +1,64 @@ +#include "..\etools.h" + +static e2d::String s_sDefaultFileName = L"DefaultData.ini"; + +void e2d::Data::saveInt(String& key, int value, String field) +{ + ::WritePrivateProfileString(field, key, String::toString(value), Data::getDataFilePath()); +} + +void e2d::Data::saveDouble(String& key, double value, String field) +{ + ::WritePrivateProfileString(field, key, String::toString(value), Data::getDataFilePath()); +} + +void e2d::Data::saveBool(String& key, bool value, String field) +{ + const wchar_t* sValue = value ? L"1" : L"0"; + ::WritePrivateProfileString(field, key, sValue, Data::getDataFilePath()); +} + +void e2d::Data::saveString(String& key, String& value, String field) +{ + ::WritePrivateProfileString(field, key, value, Data::getDataFilePath()); +} + +int e2d::Data::getInt(String& key, int defaultValue, String field) +{ + return ::GetPrivateProfileInt(field, key, defaultValue, Data::getDataFilePath()); +} + +double e2d::Data::getDouble(String& key, double defaultValue, String field) +{ + wchar_t temp[32] = { 0 }; + ::GetPrivateProfileString(field, key, String::toString(defaultValue), temp, 31, Data::getDataFilePath()); + return std::stof(temp); +} + +bool e2d::Data::getBool(String& key, bool defaultValue, String field) +{ + int nDefaultValue = defaultValue ? 1 : 0; + int nValue = ::GetPrivateProfileInt(field, key, nDefaultValue, Data::getDataFilePath()); + return nValue != 0; +} + +e2d::String e2d::Data::getString(String& key, String& defaultValue, String field) +{ + wchar_t temp[256] = { 0 }; + ::GetPrivateProfileString(field, key, defaultValue, temp, 255, Data::getDataFilePath()); + return temp; +} + +void e2d::Data::setDataFileName(String& fileName) +{ + if (!fileName.isEmpty()) + { + s_sDefaultFileName.clear(); + s_sDefaultFileName << fileName << L".ini"; + } +} + +e2d::String e2d::Data::getDataFilePath() +{ + return Path::getDefaultSavePath() + s_sDefaultFileName; +} diff --git a/core/Tool/File.cpp b/core/Tool/File.cpp deleted file mode 100644 index 026efa41..00000000 --- a/core/Tool/File.cpp +++ /dev/null @@ -1,153 +0,0 @@ -#include "..\etools.h" -#include -#include - -#define DEFINE_KNOWN_FOLDER(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ - EXTERN_C const GUID DECLSPEC_SELECTANY name \ - = { l, w1, w2,{ b1, b2, b3, b4, b5, b6, b7, b8 } } - -DEFINE_KNOWN_FOLDER(FOLDERID_LocalAppData, 0xF1B32785, 0x6FBA, 0x4FCF, 0x9D, 0x55, 0x7B, 0x8E, 0x7F, 0x15, 0x70, 0x91); - - -e2d::String e2d::Path::getLocalAppDataPath() -{ - typedef HRESULT(WINAPI* pFunSHGetKnownFolderPath)(const GUID& rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath); - - // 获取 AppData\Local 文件夹的路径 - PWSTR pszPath = NULL; - HMODULE hModule = LoadLibrary(L"shell32.dll"); - pFunSHGetKnownFolderPath SHGetKnownFolderPath = (pFunSHGetKnownFolderPath)GetProcAddress(hModule, "SHGetKnownFolderPath"); - HRESULT hr = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &pszPath); - - if (SUCCEEDED(hr)) - { - String path = pszPath; - CoTaskMemFree(pszPath); - return path; - } - return L""; -} - -e2d::String e2d::Path::getTempPath() -{ - // 获取临时文件目录 - wchar_t path[_MAX_PATH]; - if (0 == ::GetTempPath(_MAX_PATH, path)) - { - return L""; - } - - // 创建临时文件目录 - e2d::String tempFilePath; - tempFilePath << path << L"Easy2DGameTemp\\"; - // 创建文件夹 - if (!Path::createFolder(tempFilePath)) - { - return path; - } - - // 获取 AppName - String sAppName = Game::getAppName(); - if (!sAppName.isEmpty()) - { - // 创建文件夹 - if (!Path::createFolder(tempFilePath + sAppName + L"\\")) - { - return std::move(tempFilePath); - } - tempFilePath << sAppName << L"\\"; - } - return std::move(tempFilePath); -} - -e2d::String e2d::Path::getDefaultSavePath() -{ - // 获取 AppData 路径 - String path = Path::getLocalAppDataPath(); - - if (path.isEmpty()) - { - WARN_IF(true, "Cannot get local AppData path!"); - return std::move(path); - } - - // 创建文件夹 - if (!Path::createFolder(path + L"\\Easy2DGameData")) - { - return std::move(path); - } - path << L"\\Easy2DGameData"; - - // 获取 AppName - String sAppName = Game::getAppName(); - if (!sAppName.isEmpty()) - { - // 创建文件夹 - if (!Path::createFolder(path + L"\\" + sAppName)) - { - return std::move(path); - } - path << L"\\" << sAppName; - } - path << L"\\"; - - return std::move(path); -} - -e2d::String e2d::Path::getFileExtension(const String & filePath) -{ - String fileExtension; - // 找到文件名中的最后一个 '.' 的位置 - int pos = filePath.findLastOf(L'.'); - // 判断 pos 是否是个有效位置 - if (pos != -1) - { - // 截取扩展名 - fileExtension = filePath.subtract(pos); - // 转换为小写字母 - fileExtension = fileExtension.toLower(); - } - - return fileExtension; -} - -e2d::String e2d::Path::getSaveFilePath(const String & title, const String & defExt) -{ - // 弹出保存对话框 - OPENFILENAME ofn = { 0 }; - wchar_t strFilename[MAX_PATH] = { 0 }; // 用于接收文件名 - ofn.lStructSize = sizeof(OPENFILENAME); // 结构体大小 - ofn.hwndOwner = Window::getHWnd(); // 窗口句柄 - ofn.lpstrFilter = L"所有文件\0*.*\0\0"; // 设置过滤 - ofn.nFilterIndex = 1; // 过滤器索引 - ofn.lpstrFile = strFilename; // 接收返回的文件路径和文件名 - ofn.nMaxFile = sizeof(strFilename); // 缓冲区长度 - ofn.lpstrInitialDir = NULL; // 初始目录为默认 - ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT; - ofn.lpstrTitle = title; // 标题 - ofn.lpstrDefExt = defExt; // 默认追加的扩展名 - - if (GetSaveFileName(&ofn)) - { - return strFilename; - } - return L""; -} - -bool e2d::Path::createFolder(const String & strDirPath) -{ - if (strDirPath.isEmpty()) - { - WARN_IF(true, "Path::createFolder Failed: Invalid directory path!"); - return false; - } - - if (-1 == ::_waccess(strDirPath, 0)) - { - if (0 != ::_wmkdir(strDirPath)) - { - return false; - } - } - return true; -} diff --git a/core/Tool/Music.cpp b/core/Tool/Music.cpp index 2a2db6bc..1e65b1aa 100644 --- a/core/Tool/Music.cpp +++ b/core/Tool/Music.cpp @@ -35,7 +35,7 @@ Music::Music() { } -e2d::Music::Music(const String & strFileName) +e2d::Music::Music(String& strFileName) : m_bOpened(false) , m_bPlaying(false) , m_pwfx(nullptr) @@ -53,7 +53,7 @@ Music::~Music() close(); } -bool Music::open(const String & strFileName) +bool Music::open(String& strFileName) { if (m_bOpened) { diff --git a/core/Tool/Path.cpp b/core/Tool/Path.cpp index a63e10ea..97e26929 100644 --- a/core/Tool/Path.cpp +++ b/core/Tool/Path.cpp @@ -1,64 +1,153 @@ #include "..\etools.h" +#include +#include -static e2d::String s_sDefaultFileName = L"DefaultData.ini"; +#define DEFINE_KNOWN_FOLDER(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ + EXTERN_C const GUID DECLSPEC_SELECTANY name \ + = { l, w1, w2,{ b1, b2, b3, b4, b5, b6, b7, b8 } } -void e2d::Data::saveInt(const String & key, int value, const String & field) +DEFINE_KNOWN_FOLDER(FOLDERID_LocalAppData, 0xF1B32785, 0x6FBA, 0x4FCF, 0x9D, 0x55, 0x7B, 0x8E, 0x7F, 0x15, 0x70, 0x91); + + +e2d::String e2d::Path::getLocalAppDataPath() { - ::WritePrivateProfileString(field, key, String::toString(value), Data::getDataFilePath()); -} + typedef HRESULT(WINAPI* pFunSHGetKnownFolderPath)(const GUID& rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath); -void e2d::Data::saveDouble(const String & key, double value, const String & field) -{ - ::WritePrivateProfileString(field, key, String::toString(value), Data::getDataFilePath()); -} + // 获取 AppData\Local 文件夹的路径 + PWSTR pszPath = NULL; + HMODULE hModule = LoadLibrary(L"shell32.dll"); + pFunSHGetKnownFolderPath SHGetKnownFolderPath = (pFunSHGetKnownFolderPath)GetProcAddress(hModule, "SHGetKnownFolderPath"); + HRESULT hr = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &pszPath); -void e2d::Data::saveBool(const String & key, bool value, const String & field) -{ - const wchar_t* sValue = value ? L"1" : L"0"; - ::WritePrivateProfileString(field, key, sValue, Data::getDataFilePath()); -} - -void e2d::Data::saveString(const String & key, const String & value, const String & field) -{ - ::WritePrivateProfileString(field, key, value, Data::getDataFilePath()); -} - -int e2d::Data::getInt(const String & key, int defaultValue, const String & field) -{ - return ::GetPrivateProfileInt(field, key, defaultValue, Data::getDataFilePath()); -} - -double e2d::Data::getDouble(const String & key, double defaultValue, const String & field) -{ - wchar_t temp[32] = { 0 }; - ::GetPrivateProfileString(field, key, String::toString(defaultValue), temp, 31, Data::getDataFilePath()); - return std::stof(temp); -} - -bool e2d::Data::getBool(const String & key, bool defaultValue, const String & field) -{ - int nDefaultValue = defaultValue ? 1 : 0; - int nValue = ::GetPrivateProfileInt(field, key, nDefaultValue, Data::getDataFilePath()); - return nValue != 0; -} - -e2d::String e2d::Data::getString(const String & key, const String & defaultValue, const String & field) -{ - wchar_t temp[256] = { 0 }; - ::GetPrivateProfileString(field, key, defaultValue, temp, 255, Data::getDataFilePath()); - return temp; -} - -void e2d::Data::setDataFileName(const String & fileName) -{ - if (!fileName.isEmpty()) + if (SUCCEEDED(hr)) { - s_sDefaultFileName.clear(); - s_sDefaultFileName << fileName << L".ini"; + String path = pszPath; + CoTaskMemFree(pszPath); + return path; } + return L""; } -e2d::String e2d::Data::getDataFilePath() +e2d::String e2d::Path::getTempPath() { - return Path::getDefaultSavePath() + s_sDefaultFileName; + // 获取临时文件目录 + wchar_t path[_MAX_PATH]; + if (0 == ::GetTempPath(_MAX_PATH, path)) + { + return L""; + } + + // 创建临时文件目录 + e2d::String tempFilePath; + tempFilePath << path << L"Easy2DGameTemp\\"; + // 创建文件夹 + if (!Path::createFolder(tempFilePath)) + { + return path; + } + + // 获取 AppName + String sAppName = Game::getAppName(); + if (!sAppName.isEmpty()) + { + // 创建文件夹 + if (!Path::createFolder(tempFilePath + sAppName + L"\\")) + { + return std::move(tempFilePath); + } + tempFilePath << sAppName << L"\\"; + } + return std::move(tempFilePath); +} + +e2d::String e2d::Path::getDefaultSavePath() +{ + // 获取 AppData 路径 + String path = Path::getLocalAppDataPath(); + + if (path.isEmpty()) + { + WARN_IF(true, "Cannot get local AppData path!"); + return std::move(path); + } + + // 创建文件夹 + if (!Path::createFolder(path + L"\\Easy2DGameData")) + { + return std::move(path); + } + path << L"\\Easy2DGameData"; + + // 获取 AppName + String sAppName = Game::getAppName(); + if (!sAppName.isEmpty()) + { + // 创建文件夹 + if (!Path::createFolder(path + L"\\" + sAppName)) + { + return std::move(path); + } + path << L"\\" << sAppName; + } + path << L"\\"; + + return std::move(path); +} + +e2d::String e2d::Path::getFileExtension(String& filePath) +{ + String fileExtension; + // 找到文件名中的最后一个 '.' 的位置 + int pos = filePath.findLastOf(L'.'); + // 判断 pos 是否是个有效位置 + if (pos != -1) + { + // 截取扩展名 + fileExtension = filePath.subtract(pos); + // 转换为小写字母 + fileExtension = fileExtension.toLower(); + } + + return fileExtension; +} + +e2d::String e2d::Path::getSaveFilePath(const String& title, const String& defExt) +{ + // 弹出保存对话框 + OPENFILENAME ofn = { 0 }; + wchar_t strFilename[MAX_PATH] = { 0 }; // 用于接收文件名 + ofn.lStructSize = sizeof(OPENFILENAME); // 结构体大小 + ofn.hwndOwner = Window::getHWnd(); // 窗口句柄 + ofn.lpstrFilter = L"所有文件\0*.*\0\0"; // 设置过滤 + ofn.nFilterIndex = 1; // 过滤器索引 + ofn.lpstrFile = strFilename; // 接收返回的文件路径和文件名 + ofn.nMaxFile = sizeof(strFilename); // 缓冲区长度 + ofn.lpstrInitialDir = NULL; // 初始目录为默认 + ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT; + ofn.lpstrTitle = title; // 标题 + ofn.lpstrDefExt = defExt; // 默认追加的扩展名 + + if (GetSaveFileName(&ofn)) + { + return strFilename; + } + return L""; +} + +bool e2d::Path::createFolder(String& strDirPath) +{ + if (strDirPath.isEmpty()) + { + WARN_IF(true, "Path::createFolder Failed: Invalid directory path!"); + return false; + } + + if (-1 == ::_waccess(strDirPath, 0)) + { + if (0 != ::_wmkdir(strDirPath)) + { + return false; + } + } + return true; } diff --git a/core/Tool/Timer.cpp b/core/Tool/Timer.cpp index e76996bc..06b10b16 100644 --- a/core/Tool/Timer.cpp +++ b/core/Tool/Timer.cpp @@ -2,7 +2,7 @@ #include "..\enodes.h" #include "..\emanagers.h" -e2d::Timer::Timer(const String & name, TimerCallback callback, double interval /* = 0 */, int updateTimes /* = -1 */, bool atOnce /* = false */, bool autoRelease /* = false */) +e2d::Timer::Timer(Function func, String name, double interval /* = 0 */, int updateTimes /* = -1 */, bool atOnce /* = false */, bool autoRelease /* = false */) : m_bRunning(false) , m_nRunTimes(0) , m_Callback(nullptr) @@ -14,7 +14,7 @@ e2d::Timer::Timer(const String & name, TimerCallback callback, double interval / , m_bClear(true) { this->setName(name); - this->setCallback(callback); + this->setFunction(func); this->setUpdateTimes(updateTimes); this->setInterval(interval); m_bAutoRelease = autoRelease; @@ -49,7 +49,7 @@ e2d::String e2d::Timer::getName() const return m_sName; } -void e2d::Timer::setName(const String & name) +void e2d::Timer::setName(String& name) { m_sName = name; } @@ -59,9 +59,9 @@ void e2d::Timer::setInterval(double interval) m_fInterval = max(interval, 0); } -void e2d::Timer::setCallback(TimerCallback callback) +void e2d::Timer::setFunction(Function func) { - m_Callback = callback; + m_Callback = func; } void e2d::Timer::setUpdateTimes(int updateTimes) diff --git a/core/eactions.h b/core/eactions.h index 9d79ff4e..2b8622bd 100644 --- a/core/eactions.h +++ b/core/eactions.h @@ -48,15 +48,15 @@ public: // 设置动作名称 virtual void setName( - const String &name + String&name ); - // 获取一个新的拷贝动作 - virtual Action * clone() const = 0; - // 获取一个新的逆向动作 virtual Action * reverse() const; + // 获取一个新的拷贝动作 + virtual Action * clone() const = 0; + // 重置动作 virtual void reset(); @@ -514,7 +514,6 @@ public: // 创建帧动画 Animation( - double interval, /* 帧间隔(秒) */ int number, /* 帧数量 */ Image * frame, /* 第一帧 */ ... @@ -555,17 +554,17 @@ protected: }; -class ActionCallback : +class ActionFunc : public Action { public: - // 创建执行回调函数的动作 - ActionCallback( - VoidFunction callback /* 回调函数 */ + // 创建执行函数对象的动作 + ActionFunc( + Function func /* 函数对象 */ ); // 获取该动作的拷贝对象 - virtual ActionCallback * clone() const override; + virtual ActionFunc * clone() const override; protected: // 初始化动作 @@ -575,7 +574,7 @@ protected: virtual void _update() override; protected: - VoidFunction m_Callback; + Function m_Callback; }; } @@ -689,9 +688,9 @@ namespace e2d ... ); - // 创建执行回调函数的动作 - ActionCallback* Callback( - VoidFunction callback /* 回调函数 */ + // 创建执行函数对象的动作 + ActionFunc* Func( + Function func /* 函数对象 */ ); } } \ No newline at end of file diff --git a/core/easy2d.h b/core/easy2d.h index 22ed1ea5..8291a36a 100644 --- a/core/easy2d.h +++ b/core/easy2d.h @@ -35,4 +35,5 @@ #endif -using namespace e2d; \ No newline at end of file +using namespace e2d; +using namespace e2d::action; \ No newline at end of file diff --git a/core/ebase.h b/core/ebase.h index ce1ba7e3..325cad7b 100644 --- a/core/ebase.h +++ b/core/ebase.h @@ -14,11 +14,11 @@ class Game public: // 初始化游戏 static bool init( - const String & sTitle, /* 窗口标题 */ - UINT32 nWidth, /* 窗口宽度 */ - UINT32 nHeight, /* 窗口高度 */ - LPCTSTR pIconID = nullptr, /* 窗口图标 */ - const String & sAppname = L"" /* AppName */ + String& sTitle, /* 窗口标题 */ + UINT32 nWidth = 640U, /* 窗口宽度 */ + UINT32 nHeight = 480U, /* 窗口高度 */ + LPCTSTR pIconID = nullptr, /* 窗口图标 */ + String sAppname = L"" /* AppName */ ); // 启动游戏 @@ -73,7 +73,7 @@ public: // 设置窗口标题 static void setTitle( - const String & sTitle + String& sTitle ); // 打开/隐藏控制台 @@ -89,7 +89,7 @@ public: private: // 初始化窗口 static bool __init( - const String & sTitle, + String& sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID @@ -97,7 +97,7 @@ private: // 创建进程互斥体 static bool __initMutex( - const String & sTitle + String& sTitle ); // 重置窗口属性 @@ -153,23 +153,23 @@ class Input public: // 添加输入监听 static void add( - ListenerCallback callback, /* 回调函数 */ - const String & name = L"" /* 监听器名称 */ + Function func, /* 监听到消息时的执行函数 */ + String name = L"" /* 监听器名称 */ ); // 启动输入监听 static void start( - const String & name + String& name ); // 停止输入监听 static void stop( - const String & name + String& name ); // 清除输入监听 static void clear( - const String & name + String& name ); // 启动所有监听器 @@ -183,7 +183,7 @@ public: // 获取监听器 static std::vector get( - const String & name + String& name ); // 获取全部监听器 diff --git a/core/ecommon.h b/core/ecommon.h index 4b785361..08f78fa1 100644 --- a/core/ecommon.h +++ b/core/ecommon.h @@ -9,26 +9,19 @@ namespace e2d { -// 返回值和参数列表都为空的函数 -typedef std::function VoidFunction; - -// 监听器回调函数 -typedef VoidFunction ListenerCallback; - -// 定时器回调函数 -typedef VoidFunction TimerCallback; - -// 按钮点击回调函数 -typedef VoidFunction ButtonCallback; +// 函数对象 +typedef std::function Function; +// 创建函数对象 template -inline VoidFunction CreateCallback(Func&& func) +inline Function CreateFunc(Func&& func) { return std::bind(func); } +// 创建函数对象 template -inline VoidFunction CreateCallback(Object&& obj, Func&& func) +inline Function CreateFunc(Object&& obj, Func&& func) { return std::bind(func, obj); } @@ -463,7 +456,7 @@ public: // 设置字体 void setFamily( - const String & fontFamily + String& fontFamily ); // 设置字号 @@ -514,12 +507,12 @@ public: // 从本地文件中读取资源 Image( - const String & strFilePath /* 图片文件路径 */ + String& strFilePath /* 图片文件路径 */ ); // 从本地文件中读取资源 Image( - const String & strFilePath,/* 图片文件路径 */ + String& strFilePath,/* 图片文件路径 */ double nClipX, /* 裁剪位置 X 坐标 */ double nClipY, /* 裁剪位置 Y 坐标 */ double nClipWidth, /* 裁剪宽度 */ @@ -530,7 +523,7 @@ public: // 从本地文件中读取图片 void open( - const String & strFilePath + String& strFilePath ); // 裁剪图片 @@ -573,7 +566,7 @@ public: // 预加载资源 static bool preload( - const String & strFileName /* 图片文件路径 */ + String& strFileName /* 图片文件路径 */ ); // 清空缓存 @@ -673,8 +666,12 @@ public: Listener(); Listener( - ListenerCallback callback, /* 回调函数 */ - const String & name = L"" /* 监听器名称 */ + Function func /* 监听到消息时的执行函数 */ + ); + + Listener( + Function func, /* 监听到消息时的执行函数 */ + String& name /* 监听器名称 */ ); // 启动 @@ -694,12 +691,12 @@ public: // 修改名称 void setName( - const String & name + String& name ); - // 修改回调函数 - void setCallback( - ListenerCallback callback + // 设置监听到消息时的执行函数 + void setFunction( + Function func ); // 更新 @@ -709,7 +706,7 @@ protected: String m_sName; bool m_bRunning; bool m_bClear; - ListenerCallback m_callback; + Function m_callback; }; // String 类模板函数定义 diff --git a/core/emanagers.h b/core/emanagers.h index 48e8ef11..c2273240 100644 --- a/core/emanagers.h +++ b/core/emanagers.h @@ -95,28 +95,28 @@ class TimerManager public: // 等待一段时间后执行指定函数 static void start( - double timeOut, /* 等待的时长(秒) */ - TimerCallback callback /* 执行的函数 */ + double timeOut, /* 等待的时长(秒) */ + Function func /* 执行的函数 */ ); // 启动具有相同名称的定时器 static void start( - const String &name + String&name ); // 停止具有相同名称的定时器 static void stop( - const String &name + String&name ); // 删除具有相同名称的定时器 static void clear( - const String &name + String&name ); // 获取名称相同的定时器 static std::vector get( - const String & name + String& name ); // 启动所有定时器 @@ -158,17 +158,17 @@ class ActionManager public: // 继续名称相同的所有动作 static void resume( - const String & strActionName + String& strActionName ); // 暂停名称相同的所有动作 static void pause( - const String & strActionName + String& strActionName ); // 停止名称相同的所有动作 static void stop( - const String & strActionName + String& strActionName ); // 继续所有动作 @@ -182,7 +182,7 @@ public: // 获取所有名称相同的动作 static std::vector get( - const String & strActionName + String& strActionName ); // 获取所有动作 @@ -241,33 +241,33 @@ class MusicManager public: // 预加载音乐资源 static bool preload( - const String & strFilePath /* 音乐文件路径 */ + String& strFilePath /* 音乐文件路径 */ ); // 播放音乐 static bool play( - const String & strFilePath, /* 音乐文件路径 */ + String& strFilePath, /* 音乐文件路径 */ int nLoopCount = 0 /* 重复播放次数,设置 -1 为循环播放 */ ); // 暂停音乐 static void pause( - const String & strFilePath /* 音乐文件路径 */ + String& strFilePath /* 音乐文件路径 */ ); // 继续播放音乐 static void resume( - const String & strFilePath /* 音乐文件路径 */ + String& strFilePath /* 音乐文件路径 */ ); // 停止音乐 static void stop( - const String & strFilePath /* 音乐文件路径 */ + String& strFilePath /* 音乐文件路径 */ ); // 获取指定音乐的 Music 对象 static Music * get( - const String & strFilePath /* 音乐文件路径 */ + String& strFilePath /* 音乐文件路径 */ ); // 暂停所有音乐 diff --git a/core/enodes.h b/core/enodes.h index aab76fca..7c1169ed 100644 --- a/core/enodes.h +++ b/core/enodes.h @@ -123,7 +123,7 @@ public: // 获取所有名称相同的子节点 virtual std::vector getChildren( - const String & name + String& name ); // 获取所有子节点 @@ -139,7 +139,7 @@ public: // 移除所有名称相同的子节点 virtual void removeChildren( - const String & childName + String& childName ); // 从父节点移除 @@ -160,7 +160,7 @@ public: // 设置节点名称 virtual void setName( - const String & name + String& name ); // 设置节点横坐标 @@ -325,27 +325,27 @@ public: // 继续动画 virtual void resumeAction( - const String & strActionName + String& strActionName ); // 暂停动画 virtual void pauseAction( - const String & strActionName + String& strActionName ); // 停止动画 virtual void stopAction( - const String & strActionName + String& strActionName ); // 获取名称相同的动画 virtual Action * getAction( - const String & strActionName + String& strActionName ); // 获取所有名称相同的动画 virtual std::vector getActions( - const String & strActionName + String& strActionName ); // 继续所有暂停动画 @@ -449,12 +449,12 @@ public: // 从文件图片创建精灵 Sprite( - const String & imageFileName + String& imageFileName ); // 从文件图片创建精灵并裁剪 Sprite( - const String & imageFileName, + String& imageFileName, double x, double y, double width, @@ -465,7 +465,7 @@ public: // 从本地文件加载图片 virtual void open( - const String & imageFileName + String& imageFileName ); // 加载图片 @@ -499,7 +499,7 @@ public: Text(); Text( - const String & text /* 文字内容 */ + String& text /* 文字内容 */ ); Text( @@ -507,12 +507,12 @@ public: ); Text( - const String & text,/* 文字内容 */ + String& text,/* 文字内容 */ Font * font /* 字体样式 */ ); Text( - const String & text, /* 文字内容*/ + String& text, /* 文字内容*/ String fontFamily, /* 字体 */ double fontSize = 22, /* 字号 */ UINT32 color = Color::WHITE, /* 颜色 */ @@ -536,7 +536,7 @@ public: // 设置文本 void setText( - const String & text + String& text ); // 设置字体 @@ -578,32 +578,32 @@ public: // 创建按钮 Button( - Node * normal, /* 普通状态 */ - ButtonCallback callback = nullptr + Node * normal, /* 普通状态 */ + Function func = nullptr /* 按钮点击后的执行函数 */ ); // 创建按钮 Button( - Node * normal, /* 普通状态 */ - Node * selected, /* 鼠标按下状态 */ - ButtonCallback callback = nullptr + Node * normal, /* 普通状态 */ + Node * selected, /* 鼠标按下状态 */ + Function func = nullptr /* 按钮点击后的执行函数 */ ); // 创建按钮 Button( - Node * normal, /* 普通状态 */ - Node * mouseover, /* 鼠标移入状态 */ - Node * selected, /* 鼠标按下状态 */ - ButtonCallback callback = nullptr + Node * normal, /* 普通状态 */ + Node * mouseover, /* 鼠标移入状态 */ + Node * selected, /* 鼠标按下状态 */ + Function func = nullptr /* 按钮点击后的执行函数 */ ); // 创建按钮 Button( - Node * normal, /* 普通状态 */ - Node * mouseover, /* 鼠标移入状态 */ - Node * selected, /* 鼠标移入状态 */ - Node * disabled, /* 按钮禁用状态 */ - ButtonCallback callback = nullptr + Node * normal, /* 普通状态 */ + Node * mouseover, /* 鼠标移入状态 */ + Node * selected, /* 鼠标移入状态 */ + Node * disabled, /* 按钮禁用状态 */ + Function func = nullptr /* 按钮点击后的执行函数 */ ); // 获取按钮状态是启用还是禁用 @@ -624,7 +624,7 @@ public: Node * mouseover ); - // 设置鼠标选中按钮时显示的按钮 + // 设置鼠标按下按钮时显示的按钮 virtual void setSelected( Node * selected ); @@ -634,9 +634,9 @@ public: Node * disabled ); - // 设置回调函数 - void setCallback( - ButtonCallback callback + // 设置按钮点击后的执行函数 + void setFunction( + Function func ); // 更新按钮状态 @@ -651,18 +651,18 @@ protected: // 刷新按钮显示 virtual void _updateVisiable(); - // 执行按钮回调函数 + // 执行按钮函数对象 virtual void _runCallback(); protected: - Node * m_pNormal; - Node * m_pMouseover; - Node * m_pSelected; - Node * m_pDisabled; - bool m_bEnable; - bool m_bIsSelected; - BTN_STATE m_eBtnState; - ButtonCallback m_Callback; + Node * m_pNormal; + Node * m_pMouseover; + Node * m_pSelected; + Node * m_pDisabled; + bool m_bEnable; + bool m_bIsSelected; + BTN_STATE m_eBtnState; + Function m_Callback; }; @@ -675,42 +675,42 @@ public: // 创建开关按钮 ButtonToggle( - Node * onNormal, - Node * offNormal, - ButtonCallback callback = nullptr + Node * onNormal, /* 按钮打开时,普通状态 */ + Node * offNormal, /* 按钮关闭时,普通状态 */ + Function func = nullptr /* 按钮点击后的执行函数 */ ); // 创建开关按钮 ButtonToggle( - Node * onNormal, - Node * offNormal, - Node * onSelected, - Node * offSelected, - ButtonCallback callback = nullptr + Node * onNormal, /* 按钮打开时,普通状态 */ + Node * offNormal, /* 按钮关闭时,普通状态 */ + Node * onSelected, /* 按钮打开时,鼠标按下状态 */ + Node * offSelected, /* 按钮关闭时,鼠标按下状态 */ + Function func = nullptr /* 按钮点击后的执行函数 */ ); // 创建开关按钮 ButtonToggle( - Node * onNormal, - Node * offNormal, - Node * onMouseOver, - Node * offMouseOver, - Node * onSelected, - Node * offSelected, - ButtonCallback callback = nullptr + Node * onNormal, /* 按钮打开时,普通状态 */ + Node * offNormal, /* 按钮关闭时,普通状态 */ + Node * onMouseOver, /* 按钮打开时,鼠标移入状态 */ + Node * offMouseOver, /* 按钮关闭时,鼠标移入状态 */ + Node * onSelected, /* 按钮打开时,鼠标按下状态 */ + Node * offSelected, /* 按钮关闭时,鼠标按下状态 */ + Function func = nullptr /* 按钮点击后的执行函数 */ ); // 创建开关按钮 ButtonToggle( - Node * onNormal, - Node * offNormal, - Node * onMouseOver, - Node * offMouseOver, - Node * onSelected, - Node * offSelected, - Node * onDisabled, - Node * offDisabled, - ButtonCallback callback = nullptr + Node * onNormal, /* 按钮打开时,普通状态 */ + Node * offNormal, /* 按钮关闭时,普通状态 */ + Node * onMouseOver, /* 按钮打开时,鼠标移入状态 */ + Node * offMouseOver, /* 按钮关闭时,鼠标移入状态 */ + Node * onSelected, /* 按钮打开时,鼠标按下状态 */ + Node * offSelected, /* 按钮关闭时,鼠标按下状态 */ + Node * onDisabled, /* 按钮打开时,禁用状态 */ + Node * offDisabled, /* 按钮关闭时,禁用状态 */ + Function func = nullptr /* 按钮点击后的执行函数 */ ); // 获取开关状态(打开或关闭) @@ -731,7 +731,7 @@ public: Node * mouseover ) override; - // 设置按钮打开状态下,鼠标选中按钮时显示的按钮 + // 设置按钮打开状态下,鼠标按下按钮时显示的按钮 virtual void setSelected( Node * selected ) override; @@ -751,7 +751,7 @@ public: Node * mouseover ); - // 设置按钮关闭状态下,鼠标选中按钮时显示的按钮 + // 设置按钮关闭状态下,鼠标按下按钮时显示的按钮 void setSelectedOff( Node * selected ); @@ -765,18 +765,18 @@ protected: // 刷新按钮开关 virtual void _updateState(); - // 执行按钮回调函数 + // 执行按钮函数对象 virtual void _runCallback() override; protected: - Node * m_pNormalOn; - Node * m_pNormalOff; - Node * m_pMouseoverOn; - Node * m_pMouseoverOff; - Node * m_pSelectedOn; - Node * m_pSelectedOff; - Node * m_pDisabledOn; - Node * m_pDisabledOff; + Node * m_pNormalOn; + Node * m_pNormalOff; + Node * m_pMouseoverOn; + Node * m_pMouseoverOff; + Node * m_pSelectedOn; + Node * m_pSelectedOff; + Node * m_pDisabledOn; + Node * m_pDisabledOff; bool m_bState; }; diff --git a/core/etools.h b/core/etools.h index 861ca374..7cc5128d 100644 --- a/core/etools.h +++ b/core/etools.h @@ -48,12 +48,12 @@ class Timer : public: Timer( - const String &name = L"", /* 定时器名称 */ - TimerCallback callback = nullptr, /* 定时器回调函数 */ - double interval = 0, /* 时间间隔(秒) */ - int times = -1, /* 执行次数(设 -1 为永久执行) */ - bool atOnce = false, /* 是否立即执行 */ - bool autoRelease = false /* 自动清除 */ + Function func = nullptr, /* 定时器执行函数 */ + String name = L"", /* 定时器名称 */ + double interval = 0, /* 时间间隔(秒) */ + int times = -1, /* 执行次数(设 -1 为永久执行) */ + bool atOnce = false, /* 是否立即执行 */ + bool autoRelease = false /* 自动清除 */ ); // 启动定时器 @@ -79,7 +79,7 @@ public: // 设置定时器名称 void setName( - const String &name + String&name ); // 设置定时器执行间隔 @@ -87,9 +87,9 @@ public: double fInterval /* 时间间隔(秒) */ ); - // 设置定时器回调函数 - void setCallback( - TimerCallback callback + // 设置定时器的执行函数 + void setFunction( + Function func ); // 设置定时器执行次数 @@ -112,7 +112,7 @@ protected: int m_nUpdateTimes; double m_fInterval; double m_fLast; - TimerCallback m_Callback; + Function m_Callback; }; @@ -122,67 +122,67 @@ class Data public: // 保存 int 类型的值 static void saveInt( - const String & key, /* 键值 */ - int value, /* 数据 */ - const String & field = L"Defalut" /* 字段名称 */ + String& key, /* 键值 */ + int value, /* 数据 */ + String field = L"Defalut" /* 字段名称 */ ); // 保存 double 类型的值 static void saveDouble( - const String & key, /* 键值 */ - double value, /* 数据 */ - const String & field = L"Defalut" /* 字段名称 */ + String& key, /* 键值 */ + double value, /* 数据 */ + String field = L"Defalut" /* 字段名称 */ ); // 保存 bool 类型的值 static void saveBool( - const String & key, /* 键值 */ - bool value, /* 数据 */ - const String & field = L"Defalut" /* 字段名称 */ + String& key, /* 键值 */ + bool value, /* 数据 */ + String field = L"Defalut" /* 字段名称 */ ); // 保存 字符串 类型的值 static void saveString( - const String & key, /* 键值 */ - const String & value, /* 数据 */ - const String & field = L"Defalut" /* 字段名称 */ + String& key, /* 键值 */ + String& value, /* 数据 */ + String field = L"Defalut" /* 字段名称 */ ); // 获取 int 类型的值 // (若不存在则返回 defaultValue 参数的值) static int getInt( - const String & key, /* 键值 */ - int defaultValue, /* 默认值 */ - const String & field = L"Defalut" /* 字段名称 */ + String& key, /* 键值 */ + int defaultValue, /* 默认值 */ + String field = L"Defalut" /* 字段名称 */ ); // 获取 double 类型的值 // (若不存在则返回 defaultValue 参数的值) static double getDouble( - const String & key, /* 键值 */ - double defaultValue, /* 默认值 */ - const String & field = L"Defalut" /* 字段名称 */ + String& key, /* 键值 */ + double defaultValue, /* 默认值 */ + String field = L"Defalut" /* 字段名称 */ ); // 获取 bool 类型的值 // (若不存在则返回 defaultValue 参数的值) static bool getBool( - const String & key, /* 键值 */ - bool defaultValue, /* 默认值 */ - const String & field = L"Defalut" /* 字段名称 */ + String& key, /* 键值 */ + bool defaultValue, /* 默认值 */ + String field = L"Defalut" /* 字段名称 */ ); // 获取 字符串 类型的值 // (若不存在则返回 defaultValue 参数的值) static String getString( - const String & key, /* 键值 */ - const String & defaultValue, /* 默认值 */ - const String & field = L"Defalut" /* 字段名称 */ + String& key, /* 键值 */ + String& defaultValue, /* 默认值 */ + String field = L"Defalut" /* 字段名称 */ ); // 修改数据文件的名称 static void setDataFileName( - const String & strFileName /* 文件名称 */ + String& strFileName /* 文件名称 */ ); // 获取数据文件的完整路径 @@ -205,18 +205,18 @@ public: // 获取文件扩展名 static String getFileExtension( - const String & filePath + String& filePath ); // 打开保存文件对话框 static String getSaveFilePath( - const String & title = L"保存到", /* 对话框标题 */ - const String & defExt = L"" /* 默认扩展名 */ + const String& title = L"保存到", /* 对话框标题 */ + const String& defExt = L"" /* 默认扩展名 */ ); // 创建文件夹 static bool createFolder( - const String & strDirPath /* 文件夹路径 */ + String& strDirPath /* 文件夹路径 */ ); }; @@ -231,14 +231,14 @@ public: Music(); Music( - const String & strFileName /* 音乐文件路径 */ + String& strFileName /* 音乐文件路径 */ ); virtual ~Music(); // 打开音乐文件 bool open( - const String & strFileName /* 音乐文件路径 */ + String& strFileName /* 音乐文件路径 */ ); // 播放 diff --git a/project/vs2012/Easy2D.vcxproj b/project/vs2012/Easy2D.vcxproj index 46d1bbc7..afbbc96d 100644 --- a/project/vs2012/Easy2D.vcxproj +++ b/project/vs2012/Easy2D.vcxproj @@ -76,15 +76,15 @@ - + - - - - + + + + {722EA245-ADD5-4296-8C85-8FF42C0335D3} diff --git a/project/vs2012/Easy2D.vcxproj.filters b/project/vs2012/Easy2D.vcxproj.filters index b9bb655d..af474b94 100644 --- a/project/vs2012/Easy2D.vcxproj.filters +++ b/project/vs2012/Easy2D.vcxproj.filters @@ -138,18 +138,6 @@ Tool - - Transition - - - Transition - - - Transition - - - Transition - Node @@ -183,9 +171,6 @@ Manager - - Tool - Shape @@ -201,5 +186,20 @@ Tool + + Transition + + + Transition + + + Transition + + + Transition + + + Tool + \ No newline at end of file diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index c657b98e..a8f939c4 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -235,8 +235,8 @@ + - diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 9c947391..653e91be 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -144,9 +144,6 @@ Tool - - Tool - Manager @@ -174,9 +171,6 @@ Common - - Tool - Transition @@ -189,6 +183,12 @@ Transition + + Tool + + + Tool +