axmol/core/base/Scheduler.cpp

940 lines
26 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
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.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
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 "base/Scheduler.h"
#include "base/Macros.h"
#include "base/Director.h"
#include "base/ScriptSupport.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
// implementation Timer
Timer::Timer()
2021-12-25 10:04:45 +08:00
: _scheduler(nullptr)
, _elapsed(-1)
, _runForever(false)
, _useDelay(false)
, _timesExecuted(0)
, _repeat(0)
, _delay(0.0f)
, _interval(0.0f)
, _aborted(false)
{}
2019-11-23 20:27:39 +08:00
void Timer::setupTimerWithInterval(float seconds, unsigned int repeat, float delay)
{
2021-12-25 10:04:45 +08:00
_elapsed = -1;
_interval = seconds;
_delay = delay;
_useDelay = (_delay > 0.0f) ? true : false;
_repeat = repeat;
2022-07-16 10:43:05 +08:00
_runForever = (_repeat == AX_REPEAT_FOREVER) ? true : false;
2019-11-23 20:27:39 +08:00
_timesExecuted = 0;
}
void Timer::update(float dt)
{
if (_elapsed == -1)
{
2021-12-25 10:04:45 +08:00
_elapsed = 0;
2019-11-23 20:27:39 +08:00
_timesExecuted = 0;
return;
}
// accumulate elapsed time
_elapsed += dt;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// deal with delay
if (_useDelay)
{
if (_elapsed < _delay)
{
return;
}
2021-12-25 10:04:45 +08:00
_timesExecuted += 1; // important to increment before call trigger
2019-11-23 20:27:39 +08:00
trigger(_delay);
2021-12-25 10:04:45 +08:00
_elapsed = _elapsed - _delay;
2019-11-23 20:27:39 +08:00
_useDelay = false;
// after delay, the rest time should compare with interval
if (isExhausted())
2021-12-25 10:04:45 +08:00
{ // unschedule timer
2019-11-23 20:27:39 +08:00
cancel();
return;
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// if _interval == 0, should trigger once every frame
float interval = (_interval > 0) ? _interval : _elapsed;
while ((_elapsed >= interval) && !_aborted)
{
2021-12-25 10:04:45 +08:00
_timesExecuted += 1; // important to increment before call trigger
2019-11-23 20:27:39 +08:00
trigger(interval);
_elapsed -= interval;
if (isExhausted())
{
cancel();
break;
}
if (_elapsed <= 0.f)
{
break;
}
}
}
bool Timer::isExhausted() const
{
return !_runForever && _timesExecuted > _repeat;
}
// TimerTargetSelector
2021-12-25 10:04:45 +08:00
TimerTargetSelector::TimerTargetSelector() : _target(nullptr), _selector(nullptr) {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
bool TimerTargetSelector::initWithSelector(Scheduler* scheduler,
SEL_SCHEDULE selector,
Ref* target,
float seconds,
unsigned int repeat,
float delay)
2019-11-23 20:27:39 +08:00
{
_scheduler = scheduler;
2021-12-25 10:04:45 +08:00
_target = target;
_selector = selector;
2019-11-23 20:27:39 +08:00
setupTimerWithInterval(seconds, repeat, delay);
return true;
}
void TimerTargetSelector::trigger(float dt)
{
if (_target && _selector)
{
(_target->*_selector)(dt);
}
}
void TimerTargetSelector::cancel()
{
_scheduler->unschedule(_selector, _target);
}
// TimerTargetCallback
2021-12-25 10:04:45 +08:00
TimerTargetCallback::TimerTargetCallback() : _target(nullptr), _callback(nullptr) {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
bool TimerTargetCallback::initWithCallback(Scheduler* scheduler,
const ccSchedulerFunc& callback,
void* target,
std::string_view key,
2021-12-25 10:04:45 +08:00
float seconds,
unsigned int repeat,
float delay)
2019-11-23 20:27:39 +08:00
{
_scheduler = scheduler;
2021-12-25 10:04:45 +08:00
_target = target;
_callback = callback;
_key = key;
2019-11-23 20:27:39 +08:00
setupTimerWithInterval(seconds, repeat, delay);
return true;
}
void TimerTargetCallback::trigger(float dt)
{
if (_callback)
{
_callback(dt);
}
}
void TimerTargetCallback::cancel()
{
_scheduler->unschedule(_key, _target);
}
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_SCRIPT_BINDING
2019-11-23 20:27:39 +08:00
// TimerScriptHandler
bool TimerScriptHandler::initWithScriptHandler(int handler, float seconds)
{
_scriptHandler = handler;
2021-12-25 10:04:45 +08:00
_elapsed = -1;
_interval = seconds;
2019-11-23 20:27:39 +08:00
return true;
}
void TimerScriptHandler::trigger(float dt)
{
if (0 != _scriptHandler)
{
2021-12-25 10:04:45 +08:00
SchedulerScriptData data(_scriptHandler, dt);
ScriptEvent event(kScheduleEvent, &data);
ScriptEngineManager::sendEventToLua(event);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
void TimerScriptHandler::cancel() {}
2019-11-23 20:27:39 +08:00
#endif
// implementation of Scheduler
// Priority level reserved for system services.
const int Scheduler::PRIORITY_SYSTEM = INT_MIN;
// Minimum priority level for user scheduling.
const int Scheduler::PRIORITY_NON_SYSTEM_MIN = PRIORITY_SYSTEM + 1;
Scheduler::Scheduler()
2021-12-25 10:04:45 +08:00
: _timeScale(1.0f)
, _currentTarget(nullptr)
, _currentTargetSalvaged(false)
, _indexMapLocked(false)
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_SCRIPT_BINDING
2021-12-25 10:04:45 +08:00
, _scriptHandlerEntries(20)
2019-11-23 20:27:39 +08:00
#endif
{
// I don't expect to have more than 30 functions to all per frame
_actionsToPerform.reserve(30);
2019-11-23 20:27:39 +08:00
}
Scheduler::~Scheduler()
{
unscheduleAll();
}
2021-12-25 10:04:45 +08:00
void Scheduler::schedule(const ccSchedulerFunc& callback,
void* target,
float interval,
bool paused,
std::string_view key)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
this->schedule(callback, target, interval, AX_REPEAT_FOREVER, 0.0f, paused, key);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void Scheduler::schedule(const ccSchedulerFunc& callback,
void* target,
float interval,
unsigned int repeat,
float delay,
bool paused,
std::string_view key)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(target, "Argument target must be non-nullptr");
AXASSERT(!key.empty(), "key should not be empty!");
2019-11-23 20:27:39 +08:00
auto timerIt = _timersMap.find(target);
if (timerIt == _timersMap.end())
2019-11-23 20:27:39 +08:00
{
timerIt = _timersMap.emplace(target, TimerHandle{}).first;
2019-11-23 20:27:39 +08:00
// Is this the 1st element ? Then set the pause level to all the selectors of this target
timerIt->second.paused = paused;
2019-11-23 20:27:39 +08:00
}
else
{
AXASSERT(timerIt->second.paused == paused, "element's paused should be paused!");
2019-11-23 20:27:39 +08:00
}
auto& timers = timerIt->second.timers;
if (timers.empty())
2019-11-23 20:27:39 +08:00
{
timers.reserve(10);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else
2019-11-23 20:27:39 +08:00
{
auto timerIt = std::find_if(timers.begin(), timers.end(), [&key](Timer* const itimer) {
TimerTargetCallback* timer = dynamic_cast<TimerTargetCallback*>(itimer);
return timer && !timer->isExhausted() && key == timer->getKey();
});
if (timerIt != timers.end())
2019-11-23 20:27:39 +08:00
{
AXLOG("Scheduler#schedule. Reiniting timer with interval %.4f, repeat %u, delay %.4f", interval, repeat,
delay);
(*timerIt)->setupTimerWithInterval(interval, repeat, delay);
return;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
TimerTargetCallback* timer = new TimerTargetCallback();
2019-11-23 20:27:39 +08:00
timer->initWithCallback(this, callback, target, key, interval, repeat, delay);
timers.pushBack(timer);
2019-11-23 20:27:39 +08:00
timer->release();
}
void Scheduler::unschedule(std::string_view key, void* target)
2019-11-23 20:27:39 +08:00
{
// explicit handle nil arguments when removing an object
if (target == nullptr || key.empty())
{
return;
}
auto timerIt = _timersMap.find(target);
if (timerIt != _timersMap.end())
2019-11-23 20:27:39 +08:00
{
auto& timerHandle = timerIt->second;
for (int i = 0; i < timerHandle.timers.size(); ++i)
2019-11-23 20:27:39 +08:00
{
TimerTargetCallback* timer = dynamic_cast<TimerTargetCallback*>(timerHandle.timers[i]);
2019-11-23 20:27:39 +08:00
if (timer && key == timer->getKey())
{
if (timer == timerHandle.currentTimer && (!timer->isAborted()))
2019-11-23 20:27:39 +08:00
{
timer->retain();
timer->setAborted();
}
timerHandle.timers.erase(i);
2019-11-23 20:27:39 +08:00
// update timerIndex in case we are in tick:, looping over the actions
if (timerHandle.timerIndex >= i)
2019-11-23 20:27:39 +08:00
{
timerHandle.timerIndex--;
2019-11-23 20:27:39 +08:00
}
if (timerHandle.timers.empty())
2019-11-23 20:27:39 +08:00
{
if (_currentTarget == &timerHandle)
2019-11-23 20:27:39 +08:00
{
_currentTargetSalvaged = true;
}
else
{
_timersMap.erase(timerIt);
2019-11-23 20:27:39 +08:00
}
}
return;
}
}
}
}
void Scheduler::priorityIn(axstd::pod_vector<SchedHandle*>& list,
const ccSchedulerFunc& callback,
void* target,
int priority,
bool paused)
2019-11-23 20:27:39 +08:00
{
auto sched = new SchedHandle(list, callback, target, priority, paused);
axstd::insert_sorted(list, sched,
[](const SchedHandle* lhs, const SchedHandle* rhs) { return lhs->priority < rhs->priority; });
2019-11-23 20:27:39 +08:00
_schedIndexMap.emplace(target, sched);
2019-11-23 20:27:39 +08:00
}
void Scheduler::appendIn(axstd::pod_vector<SchedHandle*>& list,
const ccSchedulerFunc& callback,
void* target,
bool paused)
2019-11-23 20:27:39 +08:00
{
auto sched = new SchedHandle(list, callback, target, 0, paused);
list.emplace_back(sched);
_schedIndexMap.emplace(target, sched);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void Scheduler::schedulePerFrame(const ccSchedulerFunc& callback, void* target, int priority, bool paused)
2019-11-23 20:27:39 +08:00
{
auto updateIt = _schedIndexMap.find(target);
if (updateIt != _schedIndexMap.end())
2019-11-23 20:27:39 +08:00
{
// change priority: should unschedule it first
if (updateIt->second->priority != priority)
2019-11-23 20:27:39 +08:00
{
unscheduleUpdate(target);
}
else
{
// don't add it again
return;
}
}
// most of the updates are going to be 0, that's way there
// is an special list for updates with priority 0
if (priority == 0)
{
appendIn(_updates0List, callback, target, paused);
2019-11-23 20:27:39 +08:00
}
else if (priority < 0)
{
priorityIn(_updatesNegList, callback, target, priority, paused);
2019-11-23 20:27:39 +08:00
}
else
{
// priority > 0
priorityIn(_updatesPosList, callback, target, priority, paused);
2019-11-23 20:27:39 +08:00
}
}
bool Scheduler::isScheduled(std::string_view key, const void* target) const
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(!key.empty(), "Argument key must not be empty");
AXASSERT(target, "Argument target must be non-nullptr");
2021-12-25 10:04:45 +08:00
auto timerIt = _timersMap.find(const_cast<void*>(target));
2021-12-25 10:04:45 +08:00
if (timerIt == _timersMap.end())
2019-11-23 20:27:39 +08:00
return false;
2021-12-25 10:04:45 +08:00
auto&& timers = timerIt->second.timers;
const bool found = !timers.empty() && std::find_if(timers.begin(), timers.end(), [&key](Timer* const itimer) {
auto timer = dynamic_cast<TimerTargetCallback*>(itimer);
return (timer && !timer->isExhausted() && key == timer->getKey());
}) != timers.end();
2021-12-25 10:04:45 +08:00
return found;
2019-11-23 20:27:39 +08:00
}
void Scheduler::unscheduleUpdate(void* target)
2019-11-23 20:27:39 +08:00
{
if (target == nullptr)
{
return;
}
2019-11-23 20:27:39 +08:00
auto updateIt = _schedIndexMap.find(target);
if (updateIt != _schedIndexMap.end())
2019-11-23 20:27:39 +08:00
{
auto& sched = updateIt->second;
axstd::erase(sched->owner, sched);
if (!_indexMapLocked)
AX_SAFE_DELETE(sched);
2019-11-23 20:27:39 +08:00
else
{
sched->markedForDeletion = true;
_updateDeleteVector.emplace_back(sched);
2019-11-23 20:27:39 +08:00
}
_schedIndexMap.erase(updateIt);
2019-11-23 20:27:39 +08:00
}
}
void Scheduler::unscheduleAll()
{
unscheduleAllWithMinPriority(PRIORITY_SYSTEM);
}
void Scheduler::unscheduleAllWithMinPriority(int minPriority)
{
// Custom Selectors
2023-10-16 20:24:38 +08:00
for (auto timerIt = _timersMap.begin(); timerIt != _timersMap.end();)
2019-11-23 20:27:39 +08:00
{
2023-10-16 20:24:38 +08:00
unscheduleAllForTarget(timerIt);
2019-11-23 20:27:39 +08:00
}
// Updates selectors
2021-12-25 10:04:45 +08:00
if (minPriority < 0)
2019-11-23 20:27:39 +08:00
{
for (auto&& entry : _updatesNegList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (entry->priority >= minPriority)
2019-11-23 20:27:39 +08:00
{
unscheduleUpdate(entry->target);
}
}
}
2021-12-25 10:04:45 +08:00
if (minPriority <= 0)
2019-11-23 20:27:39 +08:00
{
for (auto&& entry : _updates0List)
{
unscheduleUpdate(entry->target);
}
2019-11-23 20:27:39 +08:00
}
for (auto&& entry : _updatesPosList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (entry->priority >= minPriority)
2019-11-23 20:27:39 +08:00
{
unscheduleUpdate(entry->target);
}
}
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_SCRIPT_BINDING
2019-11-23 20:27:39 +08:00
_scriptHandlerEntries.clear();
#endif
}
2021-12-25 10:04:45 +08:00
void Scheduler::unscheduleAllForTarget(void* target)
2019-11-23 20:27:39 +08:00
{
// explicit nullptr handling
if (target == nullptr)
{
return;
}
// Custom Selectors
auto timerIt = _timersMap.find(target);
if (timerIt != _timersMap.end())
2023-10-16 20:24:38 +08:00
unscheduleAllForTarget(timerIt);
else
unscheduleUpdate(target);
}
void Scheduler::unscheduleAllForTarget(std::unordered_map<void*, TimerHandle>::iterator& timerIt)
{
auto const target = timerIt->first;
auto& timerHandle = timerIt->second;
if (timerHandle.timers.contains(timerHandle.currentTimer) && (!timerHandle.currentTimer->isAborted()))
2019-11-23 20:27:39 +08:00
{
2023-10-16 20:24:38 +08:00
timerHandle.currentTimer->retain();
timerHandle.currentTimer->setAborted();
}
timerHandle.timers.clear();
2019-11-23 20:27:39 +08:00
2023-10-16 20:24:38 +08:00
if (_currentTarget == &timerHandle)
{
_currentTargetSalvaged = true;
++timerIt;
}
else
{
timerIt = _timersMap.erase(timerIt);
2019-11-23 20:27:39 +08:00
}
unscheduleUpdate(target);
}
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_SCRIPT_BINDING
2019-11-23 20:27:39 +08:00
unsigned int Scheduler::scheduleScriptFunc(unsigned int handler, float interval, bool paused)
{
SchedulerScriptHandlerEntry* entry = SchedulerScriptHandlerEntry::create(handler, interval, paused);
_scriptHandlerEntries.pushBack(entry);
return entry->getEntryId();
}
void Scheduler::unscheduleScriptEntry(unsigned int scheduleScriptEntryID)
{
for (ssize_t i = _scriptHandlerEntries.size() - 1; i >= 0; i--)
{
SchedulerScriptHandlerEntry* entry = _scriptHandlerEntries.at(i);
if (entry->getEntryId() == (int)scheduleScriptEntryID)
{
entry->markedForDeletion();
break;
}
}
}
#endif
2021-12-25 10:04:45 +08:00
void Scheduler::resumeTarget(void* target)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(target != nullptr, "target can't be nullptr!");
2019-11-23 20:27:39 +08:00
// custom selectors
auto timerIt = _timersMap.find(target);
if (timerIt != _timersMap.end())
2019-11-23 20:27:39 +08:00
{
timerIt->second.paused = false;
2019-11-23 20:27:39 +08:00
}
// update selector
auto updateIt = _schedIndexMap.find(target);
if (updateIt != _schedIndexMap.end())
2019-11-23 20:27:39 +08:00
{
updateIt->second->paused = false;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
void Scheduler::pauseTarget(void* target)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(target != nullptr, "target can't be nullptr!");
2019-11-23 20:27:39 +08:00
// custom selectors
auto timerIt = _timersMap.find(target);
if (timerIt != _timersMap.end())
2019-11-23 20:27:39 +08:00
{
timerIt->second.paused = false;
2019-11-23 20:27:39 +08:00
}
// update selector
auto updateIt = _schedIndexMap.find(target);
if (updateIt != _schedIndexMap.end())
2019-11-23 20:27:39 +08:00
{
updateIt->second->paused = true;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
bool Scheduler::isTargetPaused(void* target)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(target != nullptr, "target must be non nil");
2019-11-23 20:27:39 +08:00
// Custom selectors
auto timerIt = _timersMap.find(target);
if (timerIt != _timersMap.end())
2019-11-23 20:27:39 +08:00
{
return timerIt->second.paused;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// We should check update selectors if target does not have custom selectors
auto updateIt = _schedIndexMap.find(target);
if (updateIt != _schedIndexMap.end())
2019-11-23 20:27:39 +08:00
{
return updateIt->second->paused;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false; // should never get here
}
std::set<void*> Scheduler::pauseAllTargets()
{
return pauseAllTargetsWithMinPriority(PRIORITY_SYSTEM);
}
std::set<void*> Scheduler::pauseAllTargetsWithMinPriority(int minPriority)
{
std::set<void*> idsWithSelectors;
// Custom Selectors
for (auto& [target, timerHandle] : _timersMap)
2019-11-23 20:27:39 +08:00
{
timerHandle.paused = true;
idsWithSelectors.insert(target);
2019-11-23 20:27:39 +08:00
}
// Updates selectors
2021-12-25 10:04:45 +08:00
if (minPriority < 0)
2019-11-23 20:27:39 +08:00
{
for (auto&& entry : _updatesNegList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (entry->priority >= minPriority)
2019-11-23 20:27:39 +08:00
{
entry->paused = true;
idsWithSelectors.insert(entry->target);
}
}
}
2021-12-25 10:04:45 +08:00
if (minPriority <= 0)
2019-11-23 20:27:39 +08:00
{
for (auto&& entry : _updates0List)
2019-11-23 20:27:39 +08:00
{
entry->paused = true;
idsWithSelectors.insert(entry->target);
}
}
for (auto&& entry : _updatesPosList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (entry->priority >= minPriority)
2019-11-23 20:27:39 +08:00
{
entry->paused = true;
idsWithSelectors.insert(entry->target);
}
}
return idsWithSelectors;
}
void Scheduler::resumeTargets(const std::set<void*>& targetsToResume)
{
2021-12-25 10:04:45 +08:00
for (const auto& obj : targetsToResume)
{
2019-11-23 20:27:39 +08:00
this->resumeTarget(obj);
}
}
void Scheduler::runOnAxmolThread(std::function<void()> action)
2019-11-23 20:27:39 +08:00
{
std::lock_guard<std::mutex> lock(_performMutex);
_actionsToPerform.emplace_back(std::move(action));
2019-11-23 20:27:39 +08:00
}
void Scheduler::removeAllPendingActions()
2019-11-23 20:27:39 +08:00
{
std::unique_lock<std::mutex> lock(_performMutex);
_actionsToPerform.clear();
2019-11-23 20:27:39 +08:00
}
// main loop
void Scheduler::update(float dt)
{
_indexMapLocked = true;
2019-11-23 20:27:39 +08:00
if (_timeScale != 1.0f)
{
dt *= _timeScale;
}
//
// Selector callbacks
//
// Iterate over all the Updates' selectors
// updates with priority < 0
for (auto&& entry : _updatesNegList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if ((!entry->paused) && (!entry->markedForDeletion))
2019-11-23 20:27:39 +08:00
{
entry->callback(dt);
}
}
// updates with priority == 0
for (auto&& entry : _updates0List)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if ((!entry->paused) && (!entry->markedForDeletion))
2019-11-23 20:27:39 +08:00
{
entry->callback(dt);
}
}
// updates with priority > 0
for (auto&& entry : _updatesPosList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if ((!entry->paused) && (!entry->markedForDeletion))
2019-11-23 20:27:39 +08:00
{
entry->callback(dt);
}
}
// Iterate over all the custom selectors
for (auto it = _timersMap.begin(); it != _timersMap.end();)
2019-11-23 20:27:39 +08:00
{
auto elt = &it->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 'timers' array may change while inside this loop
for (elt->timerIndex = 0; elt->timerIndex < elt->timers.size(); ++(elt->timerIndex))
2019-11-23 20:27:39 +08:00
{
elt->currentTimer = elt->timers[elt->timerIndex];
2022-07-16 10:43:05 +08:00
AXASSERT(!elt->currentTimer->isAborted(), "An aborted timer should not be updated");
2019-11-23 20:27:39 +08:00
elt->currentTimer->update(dt);
if (elt->currentTimer->isAborted())
{
// The currentTimer told the remove itself. To prevent the timer from
// accidentally deallocating itself before finishing its step, we retained
// it. Now that step is done, it's safe to release it.
elt->currentTimer->release();
}
elt->currentTimer = nullptr;
}
}
// only delete currentTarget if no actions were scheduled during the cycle (issue #481)
if (_currentTargetSalvaged && _currentTarget->timers.empty())
2019-11-23 20:27:39 +08:00
{
it = _timersMap.erase(it);
2019-11-23 20:27:39 +08:00
}
else
++it;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// delete all updates that are removed in update
for (auto&& e : _updateDeleteVector)
2019-11-23 20:27:39 +08:00
delete e;
_updateDeleteVector.clear();
_indexMapLocked = false;
_currentTarget = nullptr;
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_SCRIPT_BINDING
2019-11-23 20:27:39 +08:00
//
// Script callbacks
//
// Iterate over all the script callbacks
if (!_scriptHandlerEntries.empty())
{
for (auto i = _scriptHandlerEntries.size() - 1; i >= 0; i--)
{
SchedulerScriptHandlerEntry* eachEntry = _scriptHandlerEntries.at(i);
if (eachEntry->isMarkedForDeletion())
{
_scriptHandlerEntries.erase(i);
}
else if (!eachEntry->isPaused())
{
eachEntry->getTimer()->update(dt);
}
}
}
#endif
//
// Functions allocated from another thread
//
// Testing size is faster than locking / unlocking.
// And almost never there will be functions scheduled to be called.
if (!_actionsToPerform.empty())
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
_performMutex.lock();
2021-12-25 10:04:45 +08:00
// fixed #4123: Save the callback functions, they must be invoked after '_performMutex.unlock()', otherwise if
// new functions are added in callback, it will cause thread deadlock.
auto temp = std::move(_actionsToPerform);
2019-11-23 20:27:39 +08:00
_performMutex.unlock();
2021-12-25 10:04:45 +08:00
for (const auto& function : temp)
{
2019-11-23 20:27:39 +08:00
function();
}
}
}
2021-12-25 10:04:45 +08:00
void Scheduler::schedule(SEL_SCHEDULE selector,
Ref* target,
float interval,
unsigned int repeat,
float delay,
bool paused)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(target, "Argument target must be non-nullptr");
2021-12-25 10:04:45 +08:00
auto timerIt = _timersMap.find(target);
2021-12-25 10:04:45 +08:00
if (timerIt == _timersMap.end())
2019-11-23 20:27:39 +08:00
{
timerIt = _timersMap.emplace(target, TimerHandle{}).first;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Is this the 1st element ? Then set the pause level to all the selectors of this target
timerIt->second.paused = paused;
2019-11-23 20:27:39 +08:00
}
else
{
AXASSERT(timerIt->second.paused == paused, "element's paused should be paused.");
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
auto&& timers = timerIt->second.timers;
if (timers.empty())
2019-11-23 20:27:39 +08:00
{
timers.reserve(10);
2019-11-23 20:27:39 +08:00
}
else
{
auto timerIt = std::find_if(timers.begin(), timers.end(), [selector](Timer* const itimer) {
TimerTargetSelector* timer = dynamic_cast<TimerTargetSelector*>(itimer);
return timer && !timer->isExhausted() && selector == timer->getSelector();
});
if (timerIt != timers.end())
2019-11-23 20:27:39 +08:00
{
AXLOG("Scheduler#schedule. Reiniting timer with interval %.4f, repeat %u, delay %.4f", interval, repeat,
delay);
(*timerIt)->setupTimerWithInterval(interval, repeat, delay);
return;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
TimerTargetSelector* timer = new TimerTargetSelector();
2019-11-23 20:27:39 +08:00
timer->initWithSelector(this, selector, target, interval, repeat, delay);
timers.pushBack(timer);
2019-11-23 20:27:39 +08:00
timer->release();
}
2021-12-25 10:04:45 +08:00
void Scheduler::schedule(SEL_SCHEDULE selector, Ref* target, float interval, bool paused)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
this->schedule(selector, target, interval, AX_REPEAT_FOREVER, 0.0f, paused);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
bool Scheduler::isScheduled(SEL_SCHEDULE selector, const Ref* target) const
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(selector, "Argument selector must be non-nullptr");
AXASSERT(target, "Argument target must be non-nullptr");
2021-12-25 10:04:45 +08:00
auto timerIt = _timersMap.find(const_cast<Ref*>(target));
if (timerIt == _timersMap.end())
2019-11-23 20:27:39 +08:00
{
return false;
}
auto&& timers = timerIt->second.timers;
const auto found =
!timers.empty() && std::find_if(timers.begin(), timers.end(), [selector](Timer* const itimer) {
auto timer = dynamic_cast<TimerTargetSelector*>(itimer);
return (timer && !timer->isExhausted() && selector == timer->getSelector());
}) != timers.end();
2021-12-25 10:04:45 +08:00
return found;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void Scheduler::unschedule(SEL_SCHEDULE selector, Ref* target)
2019-11-23 20:27:39 +08:00
{
// explicit handle nil arguments when removing an object
if (target == nullptr || selector == nullptr)
{
return;
}
2021-12-25 10:04:45 +08:00
auto timerIt = _timersMap.find(target);
if (timerIt != _timersMap.end())
2019-11-23 20:27:39 +08:00
{
auto& timerHandle = timerIt->second;
auto&& timers = timerHandle.timers;
for (int i = 0; i < timers.size(); ++i)
2019-11-23 20:27:39 +08:00
{
TimerTargetSelector* timer = dynamic_cast<TimerTargetSelector*>(timers[i]);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (timer && selector == timer->getSelector())
{
if (timer == timerHandle.currentTimer && !timer->isAborted())
2019-11-23 20:27:39 +08:00
{
timer->retain();
timer->setAborted();
}
2021-12-25 10:04:45 +08:00
timers.erase(i);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// update timerIndex in case we are in tick:, looping over the actions
if (timerHandle.timerIndex >= i)
2019-11-23 20:27:39 +08:00
{
timerHandle.timerIndex--;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
if (timers.empty())
2019-11-23 20:27:39 +08:00
{
if (_currentTarget == &timerHandle)
2019-11-23 20:27:39 +08:00
{
_currentTargetSalvaged = true;
}
else
{
_timersMap.erase(timerIt);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return;
}
}
}
}
NS_AX_END