From fe52ebf45134197674a704a7207b2a4e8fb2ef36 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Sun, 12 Aug 2018 14:30:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Action/Animation.cpp | 4 +-- core/Action/Sequence.cpp | 8 ++--- core/Action/Spawn.cpp | 10 +++--- core/Base/GC.cpp | 2 +- core/Common/Image.cpp | 2 +- core/Common/Scene.cpp | 2 +- core/Common/String.cpp | 6 ++-- core/Manager/ActionManager.cpp | 18 +++++------ core/Manager/CollisionManager.cpp | 4 +-- core/Node/Menu.cpp | 4 +-- core/Node/Node.cpp | 52 +++++++++++++++++-------------- core/Tool/File.cpp | 2 +- core/Tool/Player.cpp | 12 +++---- core/Tool/Timer.cpp | 16 +++++----- core/e2dcommon.h | 6 ++-- 15 files changed, 77 insertions(+), 71 deletions(-) diff --git a/core/Action/Animation.cpp b/core/Action/Animation.cpp index d87ca612..642a2627 100644 --- a/core/Action/Animation.cpp +++ b/core/Action/Animation.cpp @@ -24,7 +24,7 @@ e2d::Animation::Animation(float interval, const std::vector& frames) e2d::Animation::~Animation() { - for (auto frame : _frames) + for (const auto& frame : _frames) { GC::getInstance()->safeRelease(frame); } @@ -67,7 +67,7 @@ e2d::Animation * e2d::Animation::clone() const auto animation = new (e2d::autorelease) Animation(_interval); if (animation) { - for (auto frame : _frames) + for (const auto& frame : _frames) { animation->add(frame); } diff --git a/core/Action/Sequence.cpp b/core/Action/Sequence.cpp index a2ced564..693c8117 100644 --- a/core/Action/Sequence.cpp +++ b/core/Action/Sequence.cpp @@ -13,7 +13,7 @@ e2d::Sequence::Sequence(const std::vector& actions) e2d::Sequence::~Sequence() { - for (auto action : _actions) + for (const auto& action : _actions) { GC::getInstance()->safeRelease(action); } @@ -25,7 +25,7 @@ void e2d::Sequence::_init() // 将所有动作与目标绑定 if (_target) { - for (auto action : _actions) + for (const auto& action : _actions) { action->_target = _target; } @@ -58,7 +58,7 @@ void e2d::Sequence::_update() void e2d::Sequence::reset() { Action::reset(); - for (auto action : _actions) + for (const auto& action : _actions) { action->reset(); } @@ -67,7 +67,7 @@ void e2d::Sequence::reset() void e2d::Sequence::_resetTime() { - for (auto action : _actions) + for (const auto& action : _actions) { action->_resetTime(); } diff --git a/core/Action/Spawn.cpp b/core/Action/Spawn.cpp index 697cba7e..97adf2e9 100644 --- a/core/Action/Spawn.cpp +++ b/core/Action/Spawn.cpp @@ -11,7 +11,7 @@ e2d::Spawn::Spawn(const std::vector& actions) e2d::Spawn::~Spawn() { - for (auto action : _actions) + for (const auto& action : _actions) { GC::getInstance()->safeRelease(action); } @@ -23,7 +23,7 @@ void e2d::Spawn::_init() if (_target) { - for (auto action : _actions) + for (const auto& action : _actions) { action->_target = _target; action->_init(); @@ -36,7 +36,7 @@ void e2d::Spawn::_update() Action::_update(); size_t doneNum = 0; - for (auto action : _actions) + for (const auto& action : _actions) { if (action->_isDone()) { @@ -57,7 +57,7 @@ void e2d::Spawn::_update() void e2d::Spawn::reset() { Action::reset(); - for (auto action : _actions) + for (const auto& action : _actions) { action->reset(); } @@ -65,7 +65,7 @@ void e2d::Spawn::reset() void e2d::Spawn::_resetTime() { - for (auto action : _actions) + for (const auto& action : _actions) { action->_resetTime(); } diff --git a/core/Base/GC.cpp b/core/Base/GC.cpp index c9c1970b..158b95db 100644 --- a/core/Base/GC.cpp +++ b/core/Base/GC.cpp @@ -80,7 +80,7 @@ void e2d::GC::clear() Timer::getInstance()->clearAllTasks(); ActionManager::getInstance()->clearAll(); - for (auto ref : _pool) + for (const auto& ref : _pool) { delete ref; } diff --git a/core/Common/Image.cpp b/core/Common/Image.cpp index 5c0e77e0..a039c3a1 100644 --- a/core/Common/Image.cpp +++ b/core/Common/Image.cpp @@ -299,7 +299,7 @@ void e2d::Image::clearCache() if (_bitmapCache.empty()) return; - for (auto bitmap : _bitmapCache) + for (const auto& bitmap : _bitmapCache) { bitmap.second->Release(); } diff --git a/core/Common/Scene.cpp b/core/Common/Scene.cpp index fac317d7..809c89dc 100644 --- a/core/Common/Scene.cpp +++ b/core/Common/Scene.cpp @@ -72,7 +72,7 @@ void e2d::Scene::add(Node * child, int order /* = 0 */) void e2d::Scene::add(const std::vector& nodes, int order) { - for (auto node : nodes) + for (const auto& node : nodes) { this->add(node, order); } diff --git a/core/Common/String.cpp b/core/Common/String.cpp index 42e418af..7a81d63e 100644 --- a/core/Common/String.cpp +++ b/core/Common/String.cpp @@ -195,21 +195,21 @@ wchar_t &e2d::String::operator[](int index) return _str[size_t(index)]; } -e2d::String e2d::String::operator+(const wchar_t *str) +e2d::String e2d::String::operator+(const wchar_t *str) const { String temp; temp._str = _str + str; return std::move(temp); } -e2d::String e2d::String::operator+(const char *str) +e2d::String e2d::String::operator+(const char *str) const { String temp; temp._str = _str + static_cast(_bstr_t(str)); return std::move(temp); } -e2d::String e2d::String::operator+(const e2d::String &str) +e2d::String e2d::String::operator+(const e2d::String &str) const { String temp; temp._str = _str + str._str; diff --git a/core/Manager/ActionManager.cpp b/core/Manager/ActionManager.cpp index 8d924c2f..76b68a4d 100644 --- a/core/Manager/ActionManager.cpp +++ b/core/Manager/ActionManager.cpp @@ -87,7 +87,7 @@ void e2d::ActionManager::resumeAllBindedWith(Node * target) if (_runningActions.empty() || target == nullptr) return; - for (auto action : _runningActions) + for (const auto& action : _runningActions) { if (action->getTarget() == target) { @@ -101,7 +101,7 @@ void e2d::ActionManager::pauseAllBindedWith(Node * target) if (_runningActions.empty() || target == nullptr) return; - for (auto action : _runningActions) + for (const auto& action : _runningActions) { if (action->getTarget() == target) { @@ -115,7 +115,7 @@ void e2d::ActionManager::stopAllBindedWith(Node * target) if (_runningActions.empty() || target == nullptr) return; - for (auto action : _runningActions) + for (const auto& action : _runningActions) { if (action->getTarget() == target) { @@ -154,7 +154,7 @@ void e2d::ActionManager::resume(const String& name) if (_runningActions.empty() || name.isEmpty()) return; - for (auto action : _runningActions) + for (const auto& action : _runningActions) { if (action->getName() == name) { @@ -168,7 +168,7 @@ void e2d::ActionManager::pause(const String& name) if (_runningActions.empty() || name.isEmpty()) return; - for (auto action : _runningActions) + for (const auto& action : _runningActions) { if (action->getName() == name) { @@ -182,7 +182,7 @@ void e2d::ActionManager::stop(const String& name) if (_runningActions.empty() || name.isEmpty()) return; - for (auto action : _runningActions) + for (const auto& action : _runningActions) { if (action->getName() == name) { @@ -215,7 +215,7 @@ void e2d::ActionManager::clearAll() { if (!_runningActions.empty()) { - for (auto action : _runningActions) + for (const auto& action : _runningActions) { action->release(); } @@ -228,7 +228,7 @@ void e2d::ActionManager::clearAll() std::vector e2d::ActionManager::get(const String& name) { std::vector actions; - for (auto action : _actions) + for (const auto& action : _actions) { if (action->getName() == name) { @@ -245,7 +245,7 @@ const std::vector& e2d::ActionManager::getAll() void e2d::ActionManager::updateTime() { - for (auto action : _runningActions) + for (const auto& action : _runningActions) { action->_resetTime(); } diff --git a/core/Manager/CollisionManager.cpp b/core/Manager/CollisionManager.cpp index 179858af..34f40b0c 100644 --- a/core/Manager/CollisionManager.cpp +++ b/core/Manager/CollisionManager.cpp @@ -92,7 +92,7 @@ void e2d::CollisionManager::addName(const String & name1, const String & name2) void e2d::CollisionManager::addName(const std::vector >& names) { - for (auto& name : names) + for (const auto& name : names) { if (!name.first.isEmpty() && !name.second.isEmpty()) { @@ -112,7 +112,7 @@ bool e2d::CollisionManager::isCollidable(const String & name1, const String & na hashName2 = name2.getHashCode(); auto pair1 = std::make_pair(hashName1, hashName2), pair2 = std::make_pair(hashName2, hashName1); - for (auto& pair : _collisionList) + for (const auto& pair : _collisionList) { if (pair == pair1 || pair == pair2) { diff --git a/core/Node/Menu.cpp b/core/Node/Menu.cpp index fa8da8c6..a0ee2780 100644 --- a/core/Node/Menu.cpp +++ b/core/Node/Menu.cpp @@ -8,7 +8,7 @@ e2d::Menu::Menu() e2d::Menu::Menu(const std::vector& buttons) : _enabled(true) { - for (auto button : buttons) + for (const auto& button : buttons) { this->addButton(button); } @@ -30,7 +30,7 @@ void e2d::Menu::setEnabled(bool enabled) { _enabled = enabled; - for (auto button : _buttons) + for (const auto& button : _buttons) { button->setEnabled(enabled); } diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index fc0e77a4..302dd4f3 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -73,7 +73,7 @@ e2d::Node::Node() e2d::Node::~Node() { ActionManager::getInstance()->clearAllBindedWith(this); - for (auto child : _children) + for (const auto& child : _children) { GC::getInstance()->safeRelease(child); } @@ -209,7 +209,7 @@ void e2d::Node::_renderOutline() ); // 渲染所有子节点的轮廓 - for (auto child : _children) + for (const auto& child : _children) { child->_renderOutline(); } @@ -223,7 +223,7 @@ void e2d::Node::_renderCollider() _collider.render(); // 绘制所有子节点的几何碰撞体 - for (auto child : _children) + for (const auto& child : _children) { child->_renderCollider(); } @@ -268,7 +268,7 @@ void e2d::Node::updateTransform() _collider.recreate(); // 通知子节点进行转换 - for (auto& child : _children) + for (const auto& child : _children) { child->_needTransform = true; } @@ -276,25 +276,31 @@ void e2d::Node::updateTransform() bool e2d::Node::dispatch(const MouseEvent & e) { - if (onMouseEvent(e)) - return true; - - for (auto iter = _children.rbegin(); iter != _children.rend(); ++iter) - if ((*iter)->dispatch(e)) + if (_visible) + { + if (onMouseEvent(e)) return true; + for (auto iter = _children.rbegin(); iter != _children.rend(); ++iter) + if ((*iter)->dispatch(e)) + return true; + } + return false; } bool e2d::Node::dispatch(const KeyEvent & e) { - if (onKeyEvent(e)) - return true; - - for (auto iter = _children.rbegin(); iter != _children.rend(); ++iter) - if ((*iter)->dispatch(e)) + if (_visible) + { + if (onKeyEvent(e)) return true; + for (auto iter = _children.rbegin(); iter != _children.rend(); ++iter) + if ((*iter)->dispatch(e)) + return true; + } + return false; } @@ -318,7 +324,7 @@ void e2d::Node::_updateOpacity() { _displayOpacity = _realOpacity * _parent->_displayOpacity; } - for (auto child : _children) + for (const auto& child : _children) { child->_updateOpacity(); } @@ -685,7 +691,7 @@ void e2d::Node::addChild(Node * child, int order /* = 0 */) void e2d::Node::addChild(const std::vector& nodes, int order) { - for (auto node : nodes) + for (const auto& node : nodes) { this->addChild(node, order); } @@ -706,7 +712,7 @@ std::vector e2d::Node::getChildren(const String& name) const std::vector vChildren; size_t hash = name.getHashCode(); - for (auto child : _children) + for (const auto& child : _children) { // 不同的名称可能会有相同的 Hash 值,但是先比较 Hash 可以提升搜索速度 if (child->_hashName == hash && child->_name == name) @@ -721,7 +727,7 @@ e2d::Node * e2d::Node::getChild(const String& name) const { size_t hash = name.getHashCode(); - for (auto child : _children) + for (const auto& child : _children) { // 不同的名称可能会有相同的 Hash 值,但是先比较 Hash 可以提升搜索速度 if (child->_hashName == hash && child->_name == name) @@ -811,7 +817,7 @@ void e2d::Node::removeChildren(const String& childName) void e2d::Node::removeAllChildren() { // 所有节点的引用计数减一 - for (auto child : _children) + for (const auto& child : _children) { child->release(); } @@ -827,7 +833,7 @@ void e2d::Node::runAction(Action * action) void e2d::Node::resumeAction(const String& name) { auto& actions = ActionManager::getInstance()->get(name); - for (auto action : actions) + for (const auto& action : actions) { if (action->getTarget() == this) { @@ -839,7 +845,7 @@ void e2d::Node::resumeAction(const String& name) void e2d::Node::pauseAction(const String& name) { auto& actions = ActionManager::getInstance()->get(name); - for (auto action : actions) + for (const auto& action : actions) { if (action->getTarget() == this) { @@ -851,7 +857,7 @@ void e2d::Node::pauseAction(const String& name) void e2d::Node::stopAction(const String& name) { auto& actions = ActionManager::getInstance()->get(name); - for (auto action : actions) + for (const auto& action : actions) { if (action->getTarget() == this) { @@ -991,7 +997,7 @@ void e2d::Node::setName(const String& name) void e2d::Node::_setParentScene(Scene * scene) { _parentScene = scene; - for (auto child : _children) + for (const auto& child : _children) { child->_setParentScene(scene); } diff --git a/core/Tool/File.cpp b/core/Tool/File.cpp index 7c2f9389..951475c8 100644 --- a/core/Tool/File.cpp +++ b/core/Tool/File.cpp @@ -39,7 +39,7 @@ bool e2d::File::open(const String & fileName) } else { - for (auto& resPath : _searchPaths) + for (const auto& resPath : _searchPaths) { if (FindFile(resPath + fileName)) { diff --git a/core/Tool/Player.cpp b/core/Tool/Player.cpp index 7ac615f0..294d0e94 100644 --- a/core/Tool/Player.cpp +++ b/core/Tool/Player.cpp @@ -24,7 +24,7 @@ e2d::Player::~Player() { if (!_musicList.empty()) { - for (auto pair : _musicList) + for (const auto& pair : _musicList) { delete pair.second; } @@ -113,7 +113,7 @@ float e2d::Player::getVolume() void e2d::Player::setVolume(float volume) { _volume = std::min(std::max(volume, -224.f), 224.f); - for (auto pair : _musicList) + for (const auto& pair : _musicList) { pair.second->setVolume(_volume); } @@ -121,7 +121,7 @@ void e2d::Player::setVolume(float volume) void e2d::Player::pauseAll() { - for (auto pair : _musicList) + for (const auto& pair : _musicList) { pair.second->pause(); } @@ -129,7 +129,7 @@ void e2d::Player::pauseAll() void e2d::Player::resumeAll() { - for (auto pair : _musicList) + for (const auto& pair : _musicList) { pair.second->resume(); } @@ -137,7 +137,7 @@ void e2d::Player::resumeAll() void e2d::Player::stopAll() { - for (auto pair : _musicList) + for (const auto& pair : _musicList) { pair.second->stop(); } @@ -154,7 +154,7 @@ void e2d::Player::setEnabled(bool enabled) void e2d::Player::clearCache() { - for (auto pair : _musicList) + for (const auto& pair : _musicList) { delete pair.second; } diff --git a/core/Tool/Timer.cpp b/core/Tool/Timer.cpp index a7e72f82..f9c06a41 100644 --- a/core/Tool/Timer.cpp +++ b/core/Tool/Timer.cpp @@ -43,7 +43,7 @@ void e2d::Timer::addTask(Task * task) void e2d::Timer::stopTasks(const String& name) { - for (auto task : _tasks) + for (const auto& task : _tasks) { if (task->getName() == name) { @@ -54,7 +54,7 @@ void e2d::Timer::stopTasks(const String& name) void e2d::Timer::startTasks(const String& name) { - for (auto task : _tasks) + for (const auto& task : _tasks) { if (task->getName() == name) { @@ -65,7 +65,7 @@ void e2d::Timer::startTasks(const String& name) void e2d::Timer::removeTasks(const String& name) { - for (auto task : _tasks) + for (const auto& task : _tasks) { if (task->getName() == name) { @@ -76,7 +76,7 @@ void e2d::Timer::removeTasks(const String& name) void e2d::Timer::stopAllTasks() { - for (auto task : _tasks) + for (const auto& task : _tasks) { task->stop(); } @@ -84,7 +84,7 @@ void e2d::Timer::stopAllTasks() void e2d::Timer::startAllTasks() { - for (auto task : _tasks) + for (const auto& task : _tasks) { task->start(); } @@ -92,7 +92,7 @@ void e2d::Timer::startAllTasks() void e2d::Timer::removeAllTasks() { - for (auto task : _tasks) + for (const auto& task : _tasks) { task->_stopped = true; } @@ -103,7 +103,7 @@ void e2d::Timer::clearAllTasks() if (_tasks.empty()) return; - for (auto task : _tasks) + for (const auto& task : _tasks) { task->release(); } @@ -139,7 +139,7 @@ void e2d::Timer::update() void e2d::Timer::updateTime() { - for (auto task : _tasks) + for (const auto& task : _tasks) { task->_lastTime = Time::now(); } diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 0ac13060..43e0f7b4 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -233,9 +233,9 @@ public: String& operator+= (const String &); String& operator+= (const char *); String& operator+= (const wchar_t *); - String operator+ (const String &); - String operator+ (const char *); - String operator+ (const wchar_t *); + String operator+ (const String &) const; + String operator+ (const char *) const; + String operator+ (const wchar_t *) const; // 友元运算符 friend String operator+ (const char *, const String &);