2012-02-01 16:45:23 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-02-01 16:45:23 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2012-02-01 16:45:23 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCSCHEDULER_H__
|
|
|
|
#define __CCSCHEDULER_H__
|
|
|
|
|
2013-12-06 18:16:58 +08:00
|
|
|
#include <functional>
|
|
|
|
#include <mutex>
|
2014-02-20 14:11:47 +08:00
|
|
|
#include <set>
|
2013-12-06 18:16:58 +08:00
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCRef.h"
|
2014-04-27 01:35:57 +08:00
|
|
|
#include "base/CCVector.h"
|
2022-01-04 11:18:44 +08:00
|
|
|
#include "uthash/uthash.h"
|
2014-02-20 10:53:49 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2014-03-03 17:51:27 +08:00
|
|
|
class Scheduler;
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
typedef std::function<void(float)> ccSchedulerFunc;
|
2015-03-18 20:37:43 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @cond
|
|
|
|
*/
|
2022-07-15 19:17:01 +08:00
|
|
|
class AX_DLL Timer : public Ref
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2014-03-01 13:30:20 +08:00
|
|
|
protected:
|
|
|
|
Timer();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
public:
|
2014-03-01 13:30:20 +08:00
|
|
|
void setupTimerWithInterval(float seconds, unsigned int repeat, float delay);
|
2017-03-07 11:16:30 +08:00
|
|
|
void setAborted() { _aborted = true; }
|
|
|
|
bool isAborted() const { return _aborted; }
|
2017-05-04 16:24:20 +08:00
|
|
|
bool isExhausted() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-13 12:55:07 +08:00
|
|
|
virtual void trigger(float dt) = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void cancel() = 0;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** triggers the timer */
|
2012-06-12 01:43:07 +08:00
|
|
|
void update(float dt);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
protected:
|
2021-12-25 10:04:45 +08:00
|
|
|
Scheduler* _scheduler; // weak ref
|
2013-06-15 14:03:30 +08:00
|
|
|
float _elapsed;
|
|
|
|
bool _runForever;
|
|
|
|
bool _useDelay;
|
|
|
|
unsigned int _timesExecuted;
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned int _repeat; // 0 = once, 1 is 2 x executed
|
2013-06-15 14:03:30 +08:00
|
|
|
float _delay;
|
|
|
|
float _interval;
|
2017-03-07 11:16:30 +08:00
|
|
|
bool _aborted;
|
2014-03-01 13:30:20 +08:00
|
|
|
};
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
class AX_DLL TimerTargetSelector : public Timer
|
2014-03-01 13:30:20 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
TimerTargetSelector();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/** Initializes a timer with a target, a selector and an interval in seconds, repeat in number of times to repeat,
|
|
|
|
* delay in seconds. */
|
|
|
|
bool initWithSelector(Scheduler* scheduler,
|
|
|
|
SEL_SCHEDULE selector,
|
|
|
|
Ref* target,
|
|
|
|
float seconds,
|
|
|
|
unsigned int repeat,
|
|
|
|
float delay);
|
|
|
|
|
2016-09-12 09:44:21 +08:00
|
|
|
SEL_SCHEDULE getSelector() const { return _selector; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-13 12:55:07 +08:00
|
|
|
virtual void trigger(float dt) override;
|
2014-03-01 13:30:20 +08:00
|
|
|
virtual void cancel() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-01 13:30:20 +08:00
|
|
|
protected:
|
|
|
|
Ref* _target;
|
|
|
|
SEL_SCHEDULE _selector;
|
|
|
|
};
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
class AX_DLL TimerTargetCallback : public Timer
|
2014-03-01 13:30:20 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
TimerTargetCallback();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// Initializes a timer with a target, a lambda and an interval in seconds, repeat in number of times to repeat,
|
|
|
|
// delay in seconds.
|
|
|
|
bool initWithCallback(Scheduler* scheduler,
|
|
|
|
const ccSchedulerFunc& callback,
|
|
|
|
void* target,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view key,
|
2021-12-25 10:04:45 +08:00
|
|
|
float seconds,
|
|
|
|
unsigned int repeat,
|
|
|
|
float delay);
|
|
|
|
|
2016-09-12 09:44:21 +08:00
|
|
|
const ccSchedulerFunc& getCallback() const { return _callback; }
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view getKey() const { return _key; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-13 12:55:07 +08:00
|
|
|
virtual void trigger(float dt) override;
|
2014-03-01 13:30:20 +08:00
|
|
|
virtual void cancel() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-01 13:30:20 +08:00
|
|
|
protected:
|
|
|
|
void* _target;
|
2014-02-20 10:53:49 +08:00
|
|
|
ccSchedulerFunc _callback;
|
2014-03-01 13:30:20 +08:00
|
|
|
std::string _key;
|
|
|
|
};
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_ENABLE_SCRIPT_BINDING
|
2014-03-01 13:30:20 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
class AX_DLL TimerScriptHandler : public Timer
|
2014-03-01 13:30:20 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool initWithScriptHandler(int handler, float seconds);
|
2016-09-12 09:44:21 +08:00
|
|
|
int getScriptHandler() const { return _scriptHandler; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-13 12:55:07 +08:00
|
|
|
virtual void trigger(float dt) override;
|
2014-03-01 13:30:20 +08:00
|
|
|
virtual void cancel() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-01 13:30:20 +08:00
|
|
|
private:
|
2013-06-15 14:03:30 +08:00
|
|
|
int _scriptHandler;
|
2012-02-01 16:45:23 +08:00
|
|
|
};
|
|
|
|
|
2014-03-01 13:30:20 +08:00
|
|
|
#endif
|
|
|
|
|
2015-03-18 20:37:43 +08:00
|
|
|
/**
|
|
|
|
* @endcond
|
|
|
|
*/
|
|
|
|
|
2015-03-24 11:15:40 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup base
|
|
|
|
* @{
|
|
|
|
*/
|
2015-03-18 20:37:43 +08:00
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
struct _listEntry;
|
|
|
|
struct _hashSelectorEntry;
|
|
|
|
struct _hashUpdateEntry;
|
2014-02-20 16:40:46 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_ENABLE_SCRIPT_BINDING
|
2013-12-07 14:25:24 +08:00
|
|
|
class SchedulerScriptHandlerEntry;
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-09-16 05:19:14 +08:00
|
|
|
/** @brief Scheduler is responsible for triggering the scheduled callbacks.
|
2014-03-03 11:00:30 +08:00
|
|
|
You should not use system timer for your game logic. Instead, use this class.
|
2012-02-01 16:45:23 +08:00
|
|
|
|
|
|
|
There are 2 different types of callbacks (selectors):
|
|
|
|
|
|
|
|
- update selector: the 'update' selector will be called every frame. You can customize the priority.
|
|
|
|
- custom selector: A custom selector will be called every frame, or with a custom interval of time
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
The 'custom selectors' should be avoided when possible. It is faster, and consumes less memory to use the 'update
|
|
|
|
selector'.
|
2012-02-01 16:45:23 +08:00
|
|
|
|
|
|
|
*/
|
2022-07-15 19:17:01 +08:00
|
|
|
class AX_DLL Scheduler : public Ref
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
|
|
|
public:
|
2021-12-25 10:04:45 +08:00
|
|
|
/** Priority level reserved for system services.
|
2015-03-18 20:37:43 +08:00
|
|
|
* @lua NA
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-07-25 21:04:32 +08:00
|
|
|
static const int PRIORITY_SYSTEM;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/** Minimum priority level for user scheduling.
|
2015-03-18 20:37:43 +08:00
|
|
|
* Priority level of user scheduling should bigger then this value.
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-07-25 21:04:32 +08:00
|
|
|
static const int PRIORITY_NON_SYSTEM_MIN;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
2015-03-18 20:37:43 +08:00
|
|
|
* Constructor
|
|
|
|
*
|
2013-09-13 16:46:31 +08:00
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
Scheduler();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
2015-03-18 20:37:43 +08:00
|
|
|
* Destructor
|
|
|
|
*
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-03-01 13:30:20 +08:00
|
|
|
virtual ~Scheduler();
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2015-03-18 20:37:43 +08:00
|
|
|
/**
|
|
|
|
* Gets the time scale of schedule callbacks.
|
|
|
|
* @see Scheduler::setTimeScale()
|
|
|
|
*/
|
2016-09-12 09:44:21 +08:00
|
|
|
float getTimeScale() { return _timeScale; }
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Modifies the time of all scheduled callbacks.
|
|
|
|
You can use this property to create a 'slow motion' or 'fast forward' effect.
|
|
|
|
Default is 1.0. To create a 'slow motion' effect, use values below 1.0.
|
|
|
|
To create a 'fast forward' effect, use values higher than 1.0.
|
|
|
|
@since v0.8
|
|
|
|
@warning It will affect EVERY scheduled selector / action.
|
|
|
|
*/
|
2016-09-12 09:44:21 +08:00
|
|
|
void setTimeScale(float timeScale) { _timeScale = timeScale; }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** 'update' the scheduler.
|
2015-03-18 20:37:43 +08:00
|
|
|
* You should NEVER call this method, unless you know what you are doing.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @lua NA
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2012-06-12 01:43:07 +08:00
|
|
|
void update(float dt);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-03-03 11:00:30 +08:00
|
|
|
/////////////////////////////////////
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-03 11:00:30 +08:00
|
|
|
// schedule
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** The scheduled method will be called every 'interval' seconds.
|
2013-08-01 16:39:42 +08:00
|
|
|
If paused is true, then it won't be called until it is resumed.
|
2014-03-03 11:12:04 +08:00
|
|
|
If 'interval' is 0, it will be called every frame, but if so, it's recommended to use 'scheduleUpdate' instead.
|
2021-12-25 10:04:45 +08:00
|
|
|
If the 'callback' is already scheduled, then only the interval parameter will be updated without re-scheduling it
|
2022-07-15 19:17:01 +08:00
|
|
|
again. repeat let the action be repeated repeat + 1 times, use AX_REPEAT_FOREVER to let the action run continuously
|
2015-03-18 20:37:43 +08:00
|
|
|
delay is the amount of time the action will wait before it'll start.
|
|
|
|
@param callback The callback function.
|
|
|
|
@param target The target of the callback function.
|
2021-12-25 10:04:45 +08:00
|
|
|
@param interval The interval to schedule the callback. If the value is 0, then the callback will be scheduled every
|
|
|
|
frame.
|
2015-03-18 20:37:43 +08:00
|
|
|
@param repeat repeat+1 times to schedule the callback.
|
2021-12-25 10:04:45 +08:00
|
|
|
@param delay Schedule call back after `delay` seconds. If the value is not 0, the first schedule will happen after
|
|
|
|
`delay` seconds. But it will only affect first schedule. After first schedule, the delay time is determined by
|
|
|
|
`interval`.
|
2015-03-18 20:37:43 +08:00
|
|
|
@param paused Whether or not to pause the schedule.
|
|
|
|
@param key The key to identify the callback function, because there is not way to identify a std::function<>.
|
2014-02-20 10:53:49 +08:00
|
|
|
@since v3.0
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void schedule(const ccSchedulerFunc& callback,
|
|
|
|
void* target,
|
|
|
|
float interval,
|
|
|
|
unsigned int repeat,
|
|
|
|
float delay,
|
|
|
|
bool paused,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view key);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-03-18 20:37:43 +08:00
|
|
|
/** The scheduled method will be called every 'interval' seconds for ever.
|
|
|
|
@param callback The callback function.
|
|
|
|
@param target The target of the callback function.
|
2021-12-25 10:04:45 +08:00
|
|
|
@param interval The interval to schedule the callback. If the value is 0, then the callback will be scheduled every
|
|
|
|
frame.
|
2015-03-18 20:37:43 +08:00
|
|
|
@param paused Whether or not to pause the schedule.
|
|
|
|
@param key The key to identify the callback function, because there is not way to identify a std::function<>.
|
2014-02-20 10:53:49 +08:00
|
|
|
@since v3.0
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void schedule(const ccSchedulerFunc& callback, void* target, float interval, bool paused, std::string_view key);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-18 20:37:43 +08:00
|
|
|
/** The scheduled method will be called every `interval` seconds.
|
2014-02-20 10:53:49 +08:00
|
|
|
If paused is true, then it won't be called until it is resumed.
|
2014-03-03 11:12:04 +08:00
|
|
|
If 'interval' is 0, it will be called every frame, but if so, it's recommended to use 'scheduleUpdate' instead.
|
2021-12-25 10:04:45 +08:00
|
|
|
If the selector is already scheduled, then only the interval parameter will be updated without re-scheduling it
|
2022-07-15 19:17:01 +08:00
|
|
|
again. repeat let the action be repeated repeat + 1 times, use AX_REPEAT_FOREVER to let the action run continuously
|
2014-02-20 10:53:49 +08:00
|
|
|
delay is the amount of time the action will wait before it'll start
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-18 20:37:43 +08:00
|
|
|
@param selector The callback function.
|
|
|
|
@param target The target of the callback function.
|
2021-12-25 10:04:45 +08:00
|
|
|
@param interval The interval to schedule the callback. If the value is 0, then the callback will be scheduled every
|
|
|
|
frame.
|
2015-03-18 20:37:43 +08:00
|
|
|
@param repeat repeat+1 times to schedule the callback.
|
2021-12-25 10:04:45 +08:00
|
|
|
@param delay Schedule call back after `delay` seconds. If the value is not 0, the first schedule will happen after
|
|
|
|
`delay` seconds. But it will only affect first schedule. After first schedule, the delay time is determined by
|
|
|
|
`interval`.
|
2015-03-18 20:37:43 +08:00
|
|
|
@param paused Whether or not to pause the schedule.
|
|
|
|
@since v3.0
|
2014-02-20 10:53:49 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void schedule(SEL_SCHEDULE selector, Ref* target, float interval, unsigned int repeat, float delay, bool paused);
|
|
|
|
|
2015-03-18 20:37:43 +08:00
|
|
|
/** The scheduled method will be called every `interval` seconds for ever.
|
|
|
|
@param selector The callback function.
|
|
|
|
@param target The target of the callback function.
|
2021-12-25 10:04:45 +08:00
|
|
|
@param interval The interval to schedule the callback. If the value is 0, then the callback will be scheduled every
|
|
|
|
frame.
|
2015-03-18 20:37:43 +08:00
|
|
|
@param paused Whether or not to pause the schedule.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void schedule(SEL_SCHEDULE selector, Ref* target, float interval, bool paused);
|
|
|
|
|
2014-03-01 13:30:20 +08:00
|
|
|
/** Schedules the 'update' selector for a given target with a given priority.
|
|
|
|
The 'update' selector will be called every frame.
|
|
|
|
The lower the priority, the earlier it is called.
|
2014-03-03 11:00:30 +08:00
|
|
|
@since v3.0
|
2014-03-01 13:30:20 +08:00
|
|
|
@lua NA
|
|
|
|
*/
|
2014-02-20 11:57:04 +08:00
|
|
|
template <class T>
|
2021-12-25 10:04:45 +08:00
|
|
|
void scheduleUpdate(T* target, int priority, bool paused)
|
2014-02-20 11:57:04 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
this->schedulePerFrame([target](float dt) { target->update(dt); }, target, priority, paused);
|
2014-02-20 11:57:04 +08:00
|
|
|
}
|
2014-03-03 11:00:30 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_ENABLE_SCRIPT_BINDING
|
2015-03-18 20:37:43 +08:00
|
|
|
// Schedule for script bindings.
|
2014-03-03 11:00:30 +08:00
|
|
|
/** The scheduled script callback will be called every 'interval' seconds.
|
|
|
|
If paused is true, then it won't be called until it is resumed.
|
|
|
|
If 'interval' is 0, it will be called every frame.
|
|
|
|
return schedule script entry ID, used for unscheduleScriptFunc().
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-18 20:37:43 +08:00
|
|
|
@warn Don't invoke this function unless you know what you are doing.
|
|
|
|
@js NA
|
|
|
|
@lua NA
|
2014-03-03 11:00:30 +08:00
|
|
|
*/
|
|
|
|
unsigned int scheduleScriptFunc(unsigned int handler, float interval, bool paused);
|
|
|
|
#endif
|
|
|
|
/////////////////////////////////////
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-03 11:00:30 +08:00
|
|
|
// unschedule
|
|
|
|
|
|
|
|
/** Unschedules a callback for a key and a given target.
|
|
|
|
If you want to unschedule the 'callbackPerFrame', use unscheduleUpdate.
|
2015-03-18 20:37:43 +08:00
|
|
|
@param key The key to identify the callback function, because there is not way to identify a std::function<>.
|
|
|
|
@param target The target to be unscheduled.
|
2014-03-03 11:00:30 +08:00
|
|
|
@since v3.0
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void unschedule(std::string_view key, void* target);
|
2014-03-03 11:00:30 +08:00
|
|
|
|
2015-03-18 20:37:43 +08:00
|
|
|
/** Unschedules a selector for a given target.
|
2017-01-18 15:41:18 +08:00
|
|
|
If you want to unschedule the "update", use `unscheduleUpdate()`.
|
2015-03-18 20:37:43 +08:00
|
|
|
@param selector The selector that is unscheduled.
|
|
|
|
@param target The target of the unscheduled selector.
|
2014-03-03 11:00:30 +08:00
|
|
|
@since v3.0
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void unschedule(SEL_SCHEDULE selector, Ref* target);
|
|
|
|
|
2014-03-01 13:30:20 +08:00
|
|
|
/** Unschedules the update selector for a given target
|
2015-03-18 20:37:43 +08:00
|
|
|
@param target The target to be unscheduled.
|
2014-02-20 10:53:49 +08:00
|
|
|
@since v0.99.3
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void unscheduleUpdate(void* target);
|
|
|
|
|
2014-03-01 13:30:20 +08:00
|
|
|
/** Unschedules all selectors for a given target.
|
|
|
|
This also includes the "update" selector.
|
2015-03-18 20:37:43 +08:00
|
|
|
@param target The target to be unscheduled.
|
2014-02-20 10:53:49 +08:00
|
|
|
@since v0.99.3
|
2014-03-01 13:30:20 +08:00
|
|
|
@lua NA
|
2014-02-20 10:53:49 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void unscheduleAllForTarget(void* target);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Unschedules all selectors from all targets.
|
|
|
|
You should NEVER call this method, unless you know what you are doing.
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
2015-03-18 20:37:43 +08:00
|
|
|
void unscheduleAll();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2012-06-14 05:26:28 +08:00
|
|
|
/** Unschedules all selectors from all targets with a minimum priority.
|
2015-03-18 20:37:43 +08:00
|
|
|
You should only call this with `PRIORITY_NON_SYSTEM_MIN` or higher.
|
|
|
|
@param minPriority The minimum priority of selector to be unscheduled. Which means, all selectors which
|
|
|
|
priority is higher than minPriority will be unscheduled.
|
2014-03-03 11:00:30 +08:00
|
|
|
@since v2.0.0
|
|
|
|
*/
|
2013-11-16 21:08:00 +08:00
|
|
|
void unscheduleAllWithMinPriority(int minPriority);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_ENABLE_SCRIPT_BINDING
|
2021-12-25 10:04:45 +08:00
|
|
|
/** Unschedule a script entry.
|
2015-03-27 11:59:10 +08:00
|
|
|
* @warning Don't invoke this function unless you know what you are doing.
|
2015-03-18 20:37:43 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-11-16 21:08:00 +08:00
|
|
|
void unscheduleScriptEntry(unsigned int scheduleScriptEntryID);
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-01 13:30:20 +08:00
|
|
|
/////////////////////////////////////
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-03 11:00:30 +08:00
|
|
|
// isScheduled
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-03 11:00:30 +08:00
|
|
|
/** Checks whether a callback associated with 'key' and 'target' is scheduled.
|
2015-03-18 20:37:43 +08:00
|
|
|
@param key The key to identify the callback function, because there is not way to identify a std::function<>.
|
|
|
|
@param target The target of the callback.
|
|
|
|
@return True if the specified callback is invoked, false if not.
|
2014-03-03 11:00:30 +08:00
|
|
|
@since v3.0.0
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
bool isScheduled(std::string_view key, const void* target) const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-12-03 20:00:51 +08:00
|
|
|
/** Checks whether a selector for a given target is scheduled.
|
2015-03-18 20:37:43 +08:00
|
|
|
@param selector The selector to be checked.
|
|
|
|
@param target The target of the callback.
|
|
|
|
@return True if the specified selector is invoked, false if not.
|
2014-03-03 11:00:30 +08:00
|
|
|
@since v3.0
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isScheduled(SEL_SCHEDULE selector, const Ref* target) const;
|
|
|
|
|
2014-03-03 11:00:30 +08:00
|
|
|
/////////////////////////////////////
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Pauses the target.
|
|
|
|
All scheduled selectors/update for a given target won't be 'ticked' until the target is resumed.
|
|
|
|
If the target is not present, nothing happens.
|
2015-03-18 20:37:43 +08:00
|
|
|
@param target The target to be paused.
|
2012-04-19 14:35:52 +08:00
|
|
|
@since v0.99.3
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void pauseTarget(void* target);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Resumes the target.
|
|
|
|
The 'target' will be unpaused, so all schedule selectors/update will be 'ticked' again.
|
|
|
|
If the target is not present, nothing happens.
|
2015-03-18 20:37:43 +08:00
|
|
|
@param target The target to be resumed.
|
2012-04-19 14:35:52 +08:00
|
|
|
@since v0.99.3
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void resumeTarget(void* target);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2015-03-18 20:37:43 +08:00
|
|
|
/** Returns whether or not the target is paused.
|
|
|
|
* @param target The target to be checked.
|
|
|
|
* @return True if the target is paused, false if not.
|
|
|
|
* @since v1.0.0
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isTargetPaused(void* target);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-06-14 05:26:28 +08:00
|
|
|
/** Pause all selectors from all targets.
|
|
|
|
You should NEVER call this method, unless you know what you are doing.
|
|
|
|
@since v2.0.0
|
|
|
|
*/
|
2014-02-20 14:11:47 +08:00
|
|
|
std::set<void*> pauseAllTargets();
|
2012-06-14 05:26:28 +08:00
|
|
|
|
|
|
|
/** Pause all selectors from all targets with a minimum priority.
|
2015-03-18 20:37:43 +08:00
|
|
|
You should only call this with PRIORITY_NON_SYSTEM_MIN or higher.
|
|
|
|
@param minPriority The minimum priority of selector to be paused. Which means, all selectors which
|
|
|
|
priority is higher than minPriority will be paused.
|
2012-06-14 05:26:28 +08:00
|
|
|
@since v2.0.0
|
|
|
|
*/
|
2014-02-20 14:11:47 +08:00
|
|
|
std::set<void*> pauseAllTargetsWithMinPriority(int minPriority);
|
2012-06-14 05:26:28 +08:00
|
|
|
|
|
|
|
/** Resume selectors on a set of targets.
|
|
|
|
This can be useful for undoing a call to pauseAllSelectors.
|
2015-03-18 20:37:43 +08:00
|
|
|
@param targetsToResume The set of targets to be resumed.
|
2012-06-14 05:26:28 +08:00
|
|
|
@since v2.0.0
|
|
|
|
*/
|
2014-02-20 14:11:47 +08:00
|
|
|
void resumeTargets(const std::set<void*>& targetsToResume);
|
2012-06-12 01:43:07 +08:00
|
|
|
|
2015-03-18 20:37:43 +08:00
|
|
|
/** Calls a function on the cocos2d thread. Useful when you need to call a cocos2d function from another thread.
|
2013-11-27 08:58:14 +08:00
|
|
|
This function is thread safe.
|
2015-03-18 20:37:43 +08:00
|
|
|
@param function The function to be run in cocos2d thread.
|
2013-11-27 08:58:14 +08:00
|
|
|
@since v3.0
|
2015-03-19 21:25:09 +08:00
|
|
|
@js NA
|
2013-11-27 08:58:14 +08:00
|
|
|
*/
|
2017-05-08 13:39:17 +08:00
|
|
|
void performFunctionInCocosThread(std::function<void()> function);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-12-08 11:58:58 +08:00
|
|
|
/**
|
|
|
|
* Remove all pending functions queued to be performed with Scheduler::performFunctionInCocosThread
|
|
|
|
* Functions unscheduled in this manner will not be executed
|
|
|
|
* This function is thread safe
|
|
|
|
* @since v3.14
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
void removeAllFunctionsToBePerformedInCocosThread();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-27 08:58:14 +08:00
|
|
|
protected:
|
2014-03-01 13:30:20 +08:00
|
|
|
/** Schedules the 'callback' function for a given target with a given priority.
|
|
|
|
The 'callback' selector will be called every frame.
|
|
|
|
The lower the priority, the earlier it is called.
|
|
|
|
@note This method is only for internal use.
|
|
|
|
@since v3.0
|
2015-03-19 21:25:09 +08:00
|
|
|
@js _schedulePerFrame
|
2014-03-01 13:30:20 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void schedulePerFrame(const ccSchedulerFunc& callback, void* target, int priority, bool paused);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void removeHashElement(struct _hashSelectorEntry* element);
|
|
|
|
void removeUpdateFromHash(struct _listEntry* entry);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// update specific
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void priorityIn(struct _listEntry** list, const ccSchedulerFunc& callback, void* target, int priority, bool paused);
|
|
|
|
void appendIn(struct _listEntry** list, const ccSchedulerFunc& callback, void* target, bool paused);
|
2013-11-27 08:58:14 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
float _timeScale;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// "updates with priority" stuff
|
|
|
|
//
|
2021-12-25 10:04:45 +08:00
|
|
|
struct _listEntry* _updatesNegList; // list of priority < 0
|
|
|
|
struct _listEntry* _updates0List; // list priority == 0
|
|
|
|
struct _listEntry* _updatesPosList; // list priority > 0
|
|
|
|
struct _hashUpdateEntry* _hashForUpdates; // hash used to fetch quickly the list entries for pause,delete,etc
|
|
|
|
std::vector<struct _listEntry*>
|
|
|
|
_updateDeleteVector; // the vector holds list entries that needs to be deleted after update
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Used for "selectors with interval"
|
2021-12-25 10:04:45 +08:00
|
|
|
struct _hashSelectorEntry* _hashForTimers;
|
|
|
|
struct _hashSelectorEntry* _currentTarget;
|
2013-06-15 14:03:30 +08:00
|
|
|
bool _currentTargetSalvaged;
|
2012-04-19 14:35:52 +08:00
|
|
|
// If true unschedule will not remove anything from a hash. Elements will only be marked for deletion.
|
2013-06-15 14:03:30 +08:00
|
|
|
bool _updateHashLocked;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_ENABLE_SCRIPT_BINDING
|
2013-12-07 14:25:24 +08:00
|
|
|
Vector<SchedulerScriptHandlerEntry*> _scriptHandlerEntries;
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-27 08:58:14 +08:00
|
|
|
// Used for "perform Function"
|
|
|
|
std::vector<std::function<void()>> _functionsToPerform;
|
|
|
|
std::mutex _performMutex;
|
2012-02-01 16:45:23 +08:00
|
|
|
};
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2015-03-24 11:15:40 +08:00
|
|
|
// end of base group
|
|
|
|
/** @} */
|
2012-06-20 18:09:11 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif // __CCSCHEDULER_H__
|