axmol/core/2d/ActionManager.cpp

458 lines
13 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2009 Valentin Milea
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
2021-12-25 10:04:45 +08:00
https://axmol.dev/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "2d/ActionManager.h"
#include "2d/Node.h"
#include "2d/Action.h"
#include "base/Scheduler.h"
#include "base/Macros.h"
2019-11-23 20:27:39 +08:00
namespace ax
{
2019-11-23 20:27:39 +08:00
//
// singleton stuff
//
ActionManager::ActionManager() : _currentTarget(nullptr), _currentTargetSalvaged(false) {}
2019-11-23 20:27:39 +08:00
ActionManager::~ActionManager()
{
AXLOGV("deallocing ActionManager: {}", fmt::ptr(this));
2019-11-23 20:27:39 +08:00
removeAllActions();
}
// private
void ActionManager::reserveActionCapacity(ActionHandle& element)
2019-11-23 20:27:39 +08:00
{
// 4 actions per Node by default
if (!element.actions.capacity())
2019-11-23 20:27:39 +08:00
{
element.actions.reserve(4);
2021-12-25 10:04:45 +08:00
}
else if (element.actions.size() == element.actions.capacity())
2019-11-23 20:27:39 +08:00
{
element.actions.reserve(element.actions.size() * 2);
2019-11-23 20:27:39 +08:00
}
}
void ActionManager::removeActionAtIndex(ssize_t index,
ActionHandle& element,
std::unordered_map<Node*, ActionHandle>::iterator actionIt)
2019-11-23 20:27:39 +08:00
{
Action* action = static_cast<Action*>(element.actions[index]);
2019-11-23 20:27:39 +08:00
if (action == element.currentAction && (!element.currentActionSalvaged))
2019-11-23 20:27:39 +08:00
{
element.currentAction->retain();
element.currentActionSalvaged = true;
2019-11-23 20:27:39 +08:00
}
element.actions.erase(index);
2019-11-23 20:27:39 +08:00
// update actionIndex in case we are in tick. looping over the actions
if (element.actionIndex >= index)
2019-11-23 20:27:39 +08:00
{
element.actionIndex--;
2019-11-23 20:27:39 +08:00
}
if (element.actions.empty())
2019-11-23 20:27:39 +08:00
{
if (_currentTarget == &element)
2019-11-23 20:27:39 +08:00
{
_currentTargetSalvaged = true;
}
else
{
eraseTargetActionHandle(actionIt);
2019-11-23 20:27:39 +08:00
}
}
}
// pause / resume
2021-12-25 10:04:45 +08:00
void ActionManager::pauseTarget(Node* target)
2019-11-23 20:27:39 +08:00
{
auto it = _targets.find(target);
if (it != _targets.end())
2019-11-23 20:27:39 +08:00
{
it->second.paused = true;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
void ActionManager::resumeTarget(Node* target)
2019-11-23 20:27:39 +08:00
{
auto it = _targets.find(target);
if (it != _targets.end())
2019-11-23 20:27:39 +08:00
{
it->second.paused = false;
2019-11-23 20:27:39 +08:00
}
}
Vector<Node*> ActionManager::pauseAllRunningActions()
{
Vector<Node*> idsWithActions;
2021-12-25 10:04:45 +08:00
for (auto& [target, element] : _targets)
2019-11-23 20:27:39 +08:00
{
element.paused = true;
idsWithActions.pushBack(const_cast<Node*>(target));
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
return idsWithActions;
}
void ActionManager::resumeTargets(const Vector<Node*>& targetsToResume)
{
2021-12-25 10:04:45 +08:00
for (const auto& node : targetsToResume)
2019-11-23 20:27:39 +08:00
{
this->resumeTarget(node);
}
}
// run
2021-12-25 10:04:45 +08:00
void ActionManager::addAction(Action* action, Node* target, bool paused)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(action != nullptr, "action can't be nullptr!");
AXASSERT(target != nullptr, "target can't be nullptr!");
2021-12-25 10:04:45 +08:00
if (action == nullptr || target == nullptr)
2019-11-23 20:27:39 +08:00
return;
auto actionIt = _targets.find(target);
if (actionIt == _targets.end())
2019-11-23 20:27:39 +08:00
{
actionIt = _targets.emplace(target, ActionHandle{}).first;
actionIt->second.paused = paused;
2019-11-23 20:27:39 +08:00
target->retain();
}
auto& actionHandle = actionIt->second;
reserveActionCapacity(actionHandle);
2021-12-25 10:04:45 +08:00
AXASSERT(!actionHandle.actions.contains(action), "action already be added!");
actionHandle.actions.pushBack(action);
2021-12-25 10:04:45 +08:00
action->startWithTarget(target);
2019-11-23 20:27:39 +08:00
}
// remove
void ActionManager::removeAllActions()
{
for (auto actionIt = _targets.begin(); actionIt != _targets.end();)
removeTargetActionHandle(actionIt);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void ActionManager::removeAllActionsFromTarget(Node* target)
2019-11-23 20:27:39 +08:00
{
// explicit null handling
if (target == nullptr)
{
return;
}
auto actionIt = _targets.find(target);
if (actionIt != _targets.end())
removeTargetActionHandle(actionIt);
}
void ActionManager::removeTargetActionHandle(std::unordered_map<Node*, ActionHandle>::iterator& actionIt)
{
auto& element = actionIt->second;
if (element.actions.contains(element.currentAction) && !element.currentActionSalvaged)
2019-11-23 20:27:39 +08:00
{
element.currentAction->retain();
element.currentActionSalvaged = true;
}
2019-11-23 20:27:39 +08:00
element.actions.clear();
if (_currentTarget == &element)
{
_currentTargetSalvaged = true;
++actionIt;
}
else
{
eraseTargetActionHandle(actionIt);
2019-11-23 20:27:39 +08:00
}
}
void ActionManager::eraseTargetActionHandle(std::unordered_map<Node*, ActionHandle>::iterator& actionIt)
{
actionIt->first->release();
actionIt = _targets.erase(actionIt);
}
2021-12-25 10:04:45 +08:00
void ActionManager::removeAction(Action* action)
2019-11-23 20:27:39 +08:00
{
// explicit null handling
if (action == nullptr)
{
return;
}
Object* target = action->getOriginalTarget();
auto actionIt = _targets.find(static_cast<Node*>(target));
if (actionIt != _targets.end())
2019-11-23 20:27:39 +08:00
{
auto i = actionIt->second.actions.getIndex(action);
2022-07-16 10:43:05 +08:00
if (i != AX_INVALID_INDEX)
2019-11-23 20:27:39 +08:00
{
removeActionAtIndex(i, actionIt->second, actionIt);
2019-11-23 20:27:39 +08:00
}
}
}
2021-12-25 10:04:45 +08:00
void ActionManager::removeActionByTag(int tag, Node* target)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(tag != Action::INVALID_TAG, "Invalid tag value!");
AXASSERT(target != nullptr, "target can't be nullptr!");
2019-11-23 20:27:39 +08:00
if (target == nullptr)
{
return;
}
auto actionIt = _targets.find(target);
if (actionIt != _targets.end())
2019-11-23 20:27:39 +08:00
{
auto& element = actionIt->second;
auto limit = element.actions.size();
2019-11-23 20:27:39 +08:00
for (int i = 0; i < limit; ++i)
{
Action* action = static_cast<Action*>(element.actions[i]);
2019-11-23 20:27:39 +08:00
if (action->getTag() == (int)tag && action->getOriginalTarget() == target)
{
removeActionAtIndex(i, element, actionIt);
2019-11-23 20:27:39 +08:00
break;
}
}
}
}
2021-12-25 10:04:45 +08:00
void ActionManager::removeAllActionsByTag(int tag, Node* target)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(tag != Action::INVALID_TAG, "Invalid tag value!");
AXASSERT(target != nullptr, "target can't be nullptr!");
2019-11-23 20:27:39 +08:00
if (target == nullptr)
{
return;
}
2021-12-25 10:04:45 +08:00
auto actionIt = _targets.find(target);
if (actionIt != _targets.end())
2019-11-23 20:27:39 +08:00
{
auto& element = actionIt->second;
auto limit = element.actions.size();
2019-11-23 20:27:39 +08:00
for (int i = 0; i < limit;)
{
Action* action = static_cast<Action*>(element.actions[i]);
2019-11-23 20:27:39 +08:00
if (action->getTag() == (int)tag && action->getOriginalTarget() == target)
{
removeActionAtIndex(i, element, actionIt);
2019-11-23 20:27:39 +08:00
--limit;
}
else
{
++i;
}
}
}
}
2021-12-25 10:04:45 +08:00
void ActionManager::removeActionsByFlags(unsigned int flags, Node* target)
2019-11-23 20:27:39 +08:00
{
if (flags == 0)
{
return;
}
2022-07-16 10:43:05 +08:00
AXASSERT(target != nullptr, "target can't be nullptr!");
2019-11-23 20:27:39 +08:00
if (target == nullptr)
{
return;
}
auto actionIt = _targets.find(target);
if (actionIt != _targets.end())
2019-11-23 20:27:39 +08:00
{
auto& element = actionIt->second;
auto limit = element.actions.size();
2019-11-23 20:27:39 +08:00
for (int i = 0; i < limit;)
{
Action* action = static_cast<Action*>(element.actions[i]);
2019-11-23 20:27:39 +08:00
if ((action->getFlags() & flags) != 0 && action->getOriginalTarget() == target)
{
removeActionAtIndex(i, element, actionIt);
2019-11-23 20:27:39 +08:00
--limit;
}
else
{
++i;
}
}
}
}
// get
// FIXME: Passing "const O *" instead of "const O&" because HASH_FIND_IT requires the address of a pointer
// and, it is not possible to get the address of a reference
2021-12-25 10:04:45 +08:00
Action* ActionManager::getActionByTag(int tag, const Node* target) const
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(tag != Action::INVALID_TAG, "Invalid tag value!");
2019-11-23 20:27:39 +08:00
auto it = _targets.find(const_cast<Node*>(target));
if (it != _targets.end())
2019-11-23 20:27:39 +08:00
{
auto& actions = it->second.actions;
if (!actions.empty())
2019-11-23 20:27:39 +08:00
{
for (auto action : actions)
2019-11-23 20:27:39 +08:00
{
if (action->getTag() == tag)
2019-11-23 20:27:39 +08:00
return action;
}
}
}
return nullptr;
}
// FIXME: Passing "const O *" instead of "const O&" because HASH_FIND_IT requires the address of a pointer
// and, it is not possible to get the address of a reference
2021-12-25 10:04:45 +08:00
ssize_t ActionManager::getNumberOfRunningActionsInTarget(const Node* target) const
2019-11-23 20:27:39 +08:00
{
auto it = _targets.find(const_cast<Node*>(target));
return (it != _targets.end()) ? it->second.actions.size() : 0;
2019-11-23 20:27:39 +08:00
}
// FIXME: Passing "const O *" instead of "const O&" because HASH_FIND_IT requires the address of a pointer
// and, it is not possible to get the address of a reference
2021-12-25 10:04:45 +08:00
size_t ActionManager::getNumberOfRunningActionsInTargetByTag(const Node* target, int tag)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(tag != Action::INVALID_TAG, "Invalid tag value!");
2019-11-23 20:27:39 +08:00
auto it = _targets.find(const_cast<Node*>(target));
if (it == _targets.end())
return 0;
auto& actions = it->second.actions;
2019-11-23 20:27:39 +08:00
if (actions.empty())
2019-11-23 20:27:39 +08:00
return 0;
2021-12-25 10:04:45 +08:00
int count = 0;
auto limit = actions.size();
2021-12-25 10:04:45 +08:00
for (int i = 0; i < limit; ++i)
2019-11-23 20:27:39 +08:00
{
auto action = actions[i];
2021-12-25 10:04:45 +08:00
if (action->getTag() == tag)
2019-11-23 20:27:39 +08:00
++count;
}
return count;
}
ssize_t ActionManager::getNumberOfRunningActions() const
{
ssize_t count = 0;
for (auto& [_, element] : _targets)
count += element.actions.size();
2019-11-23 20:27:39 +08:00
return count;
}
// main loop
void ActionManager::update(float dt)
{
for (auto actionIt = _targets.begin(); actionIt != _targets.end();)
2019-11-23 20:27:39 +08:00
{
auto elt = &actionIt->second;
2021-12-25 10:04:45 +08:00
_currentTarget = elt;
2019-11-23 20:27:39 +08:00
_currentTargetSalvaged = false;
2021-12-25 10:04:45 +08:00
if (!_currentTarget->paused)
2019-11-23 20:27:39 +08:00
{
// The 'actions' MutableArray may change while inside this loop.
for (_currentTarget->actionIndex = 0; _currentTarget->actionIndex < _currentTarget->actions.size();
2021-12-25 10:04:45 +08:00
_currentTarget->actionIndex++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_currentTarget->currentAction =
static_cast<Action*>(_currentTarget->actions[_currentTarget->actionIndex]);
2019-11-23 20:27:39 +08:00
if (_currentTarget->currentAction == nullptr)
{
continue;
}
_currentTarget->currentActionSalvaged = false;
_currentTarget->currentAction->step(dt);
if (_currentTarget->currentActionSalvaged)
{
// The currentAction told the node to remove it. To prevent the action from
// accidentally deallocating itself before finishing its step, we retained
// it. Now that step is done, it's safe to release it.
_currentTarget->currentAction->release();
2021-12-25 10:04:45 +08:00
}
else if (_currentTarget->currentAction->isDone())
2019-11-23 20:27:39 +08:00
{
_currentTarget->currentAction->stop();
2021-12-25 10:04:45 +08:00
Action* action = _currentTarget->currentAction;
2019-11-23 20:27:39 +08:00
// Make currentAction nil to prevent removeAction from salvaging it.
_currentTarget->currentAction = nullptr;
removeAction(action);
}
_currentTarget->currentAction = nullptr;
}
}
// elt, at this moment, is still valid
// so it is safe to ask this here (issue #490)
// elt = (tHashElement*)(elt->hh.next);
2019-11-23 20:27:39 +08:00
// only delete currentTarget if no actions were scheduled during the cycle (issue #481)
2021-12-25 10:04:45 +08:00
// if some node reference 'target', it's reference count >= 2 (issues #14050)
if ((_currentTargetSalvaged && _currentTarget->actions.empty()) || actionIt->first->getReferenceCount() == 1)
2019-11-23 20:27:39 +08:00
{
eraseTargetActionHandle(actionIt);
2019-11-23 20:27:39 +08:00
}
else
++actionIt;
2019-11-23 20:27:39 +08:00
}
// issue #635
_currentTarget = nullptr;
}
}