2012-02-01 16:45:23 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-02-01 16:45:23 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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-10-14 14:01:00 +08:00
|
|
|
#include "CCObject.h"
|
|
|
|
#include "uthash.h"
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup global
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class Set;
|
2012-02-01 16:45:23 +08:00
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
// Timer
|
2012-02-01 16:45:23 +08:00
|
|
|
//
|
2012-09-16 05:19:14 +08:00
|
|
|
/** @brief Light-weight timer */
|
2012-11-14 18:05:15 +08:00
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL Timer : public Object
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-07-18 07:56:19 +08:00
|
|
|
/** Allocates a timer with a target and a selector. */
|
2013-07-19 07:30:19 +08:00
|
|
|
static Timer* create(Object *target, SEL_SCHEDULE selector);
|
2013-07-18 07:56:19 +08:00
|
|
|
/** Allocates a timer with a target, a selector and an interval in seconds. */
|
2013-07-19 07:30:19 +08:00
|
|
|
static Timer* create(Object *target, SEL_SCHEDULE selector, float seconds);
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Allocates a timer with a script callback function and an interval in seconds.
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-11-16 21:08:00 +08:00
|
|
|
static Timer* createWithScriptHandler(int handler, float seconds);
|
2013-07-18 07:56:19 +08:00
|
|
|
|
2013-07-19 07:30:19 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static Timer* timerWithTarget(Object *target, SEL_SCHEDULE selector) { return Timer::create(target, selector); }
|
|
|
|
CC_DEPRECATED_ATTRIBUTE static Timer* timerWithTarget(Object *target, SEL_SCHEDULE selector, float seconds) { return Timer::create(target, selector, seconds); }
|
2013-11-16 21:08:00 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static Timer* timerWithScriptHandler(int handler, float seconds) { return Timer::createWithScriptHandler(handler, seconds); }
|
2013-07-18 07:56:19 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Timer(void);
|
2013-07-18 07:56:19 +08:00
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
/** Initializes a timer with a target and a selector. */
|
2013-07-18 07:56:19 +08:00
|
|
|
bool initWithTarget(Object *target, SEL_SCHEDULE selector);
|
2012-04-19 14:35:52 +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. */
|
2013-11-16 21:08:00 +08:00
|
|
|
bool initWithTarget(Object *target, SEL_SCHEDULE selector, float seconds, unsigned int repeat, float delay);
|
2012-02-02 15:58:10 +08:00
|
|
|
/** Initializes a timer with a script callback function and an interval in seconds. */
|
2013-11-16 21:08:00 +08:00
|
|
|
bool initWithScriptHandler(int handler, float seconds);
|
2013-07-18 07:56:19 +08:00
|
|
|
|
|
|
|
/** get interval in seconds */
|
|
|
|
float getInterval() const;
|
|
|
|
/** set interval in seconds */
|
|
|
|
void setInterval(float interval);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-18 07:56:19 +08:00
|
|
|
SEL_SCHEDULE getSelector() const;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** triggers the timer */
|
2012-06-12 01:43:07 +08:00
|
|
|
void update(float dt);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
inline int getScriptHandler() const { return _scriptHandler; };
|
2012-09-11 14:02:33 +08:00
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
protected:
|
2013-06-20 14:13:12 +08:00
|
|
|
Object *_target;
|
2013-06-15 14:03:30 +08:00
|
|
|
float _elapsed;
|
|
|
|
bool _runForever;
|
|
|
|
bool _useDelay;
|
|
|
|
unsigned int _timesExecuted;
|
|
|
|
unsigned int _repeat; //0 = once, 1 is 2 x executed
|
|
|
|
float _delay;
|
|
|
|
float _interval;
|
|
|
|
SEL_SCHEDULE _selector;
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
int _scriptHandler;
|
2012-02-01 16:45:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
// Scheduler
|
2012-02-01 16:45:23 +08:00
|
|
|
//
|
|
|
|
struct _listEntry;
|
|
|
|
struct _hashSelectorEntry;
|
|
|
|
struct _hashUpdateEntry;
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class Array;
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-09-16 05:19:14 +08:00
|
|
|
/** @brief Scheduler is responsible for triggering the scheduled callbacks.
|
2012-02-01 16:45:23 +08:00
|
|
|
You should not use NSTimer. Instead use this class.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
The 'custom selectors' should be avoided when possible. It is faster, and consumes less memory to use the 'update selector'.
|
|
|
|
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL Scheduler : public Object
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-07-25 21:04:32 +08:00
|
|
|
// Priority level reserved for system services.
|
|
|
|
static const int PRIORITY_SYSTEM;
|
|
|
|
|
|
|
|
// Minimum priority level for user scheduling.
|
|
|
|
static const int PRIORITY_NON_SYSTEM_MIN;
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
Scheduler();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
~Scheduler(void);
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
inline 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.
|
|
|
|
*/
|
2013-11-16 21:08:00 +08:00
|
|
|
inline void setTimeScale(float timeScale) { _timeScale = timeScale; }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** 'update' the scheduler.
|
|
|
|
You should NEVER call this method, unless you know what you are doing.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @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
|
|
|
|
|
|
|
/** 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.
|
2012-09-16 06:12:28 +08:00
|
|
|
If 'interval' is 0, it will be called every frame, but if so, it's recommended to use 'scheduleUpdateForTarget:' instead.
|
2012-04-19 14:35:52 +08:00
|
|
|
If the selector is already scheduled, then only the interval parameter will be updated without re-scheduling it again.
|
2013-06-20 14:13:12 +08:00
|
|
|
repeat let the action be repeated repeat + 1 times, use kRepeatForever to let the action run continuously
|
2012-04-19 14:35:52 +08:00
|
|
|
delay is the amount of time the action will wait before it'll start
|
|
|
|
|
|
|
|
@since v0.99.3, repeat and delay added in v1.1
|
|
|
|
*/
|
2013-11-16 21:08:00 +08:00
|
|
|
void scheduleSelector(SEL_SCHEDULE selector, Object *target, float interval, unsigned int repeat, float delay, bool paused);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** calls scheduleSelector with kRepeatForever and a 0 delay */
|
2013-11-16 21:08:00 +08:00
|
|
|
void scheduleSelector(SEL_SCHEDULE selector, Object *target, float interval, bool paused);
|
2012-04-19 14:35:52 +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.
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
2013-11-16 21:08:00 +08:00
|
|
|
void scheduleUpdateForTarget(Object *target, int priority, bool paused);
|
2013-06-27 12:05:47 +08:00
|
|
|
|
|
|
|
/** Checks whether a selector for a given taget is scheduled.
|
|
|
|
@since v3.0.0
|
|
|
|
*/
|
2013-07-18 07:56:19 +08:00
|
|
|
bool isScheduledForTarget(SEL_SCHEDULE selector, Object *target);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Unschedule a selector for a given target.
|
|
|
|
If you want to unschedule the "update", use unscheudleUpdateForTarget.
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
2013-07-18 07:56:19 +08:00
|
|
|
void unscheduleSelector(SEL_SCHEDULE selector, Object *target);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Unschedules the update selector for a given target
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
2013-07-18 07:56:19 +08:00
|
|
|
void unscheduleUpdateForTarget(const Object *target);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Unschedules all selectors for a given target.
|
|
|
|
This also includes the "update" selector.
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
2013-07-18 07:56:19 +08:00
|
|
|
void unscheduleAllForTarget(Object *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
|
|
|
|
*/
|
2012-11-14 18:05:15 +08:00
|
|
|
void unscheduleAll(void);
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-06-14 05:26:28 +08:00
|
|
|
/** Unschedules all selectors from all targets with a minimum priority.
|
2013-06-20 14:13:12 +08:00
|
|
|
You should only call this with kPriorityNonSystemMin or higher.
|
2012-06-14 05:26:28 +08:00
|
|
|
@since v2.0.0
|
|
|
|
*/
|
2013-11-16 21:08:00 +08:00
|
|
|
void unscheduleAllWithMinPriority(int minPriority);
|
2012-06-12 01:43:07 +08:00
|
|
|
|
2012-02-02 15:58:10 +08:00
|
|
|
/** The scheduled script callback 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.
|
2012-04-19 14:35:52 +08:00
|
|
|
If 'interval' is 0, it will be called every frame.
|
2012-02-02 15:58:10 +08:00
|
|
|
return schedule script entry ID, used for unscheduleScriptFunc().
|
|
|
|
*/
|
2013-11-16 21:08:00 +08:00
|
|
|
unsigned int scheduleScriptFunc(unsigned int handler, float interval, bool paused);
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Unschedule a script entry. */
|
2013-11-16 21:08:00 +08:00
|
|
|
void unscheduleScriptEntry(unsigned int scheduleScriptEntryID);
|
2012-02-01 16:45:23 +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.
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
2013-07-18 07:56:19 +08:00
|
|
|
void pauseTarget(Object *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.
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
2013-07-18 07:56:19 +08:00
|
|
|
void resumeTarget(Object *target);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
|
|
|
/** Returns whether or not the target is paused
|
|
|
|
@since v1.0.0
|
2013-09-13 11:41:20 +08:00
|
|
|
* In js: var isTargetPaused(var jsObject)
|
|
|
|
* @lua NA
|
2012-02-01 16:45:23 +08:00
|
|
|
*/
|
2013-07-18 07:56:19 +08:00
|
|
|
bool isTargetPaused(Object *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
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
Set* pauseAllTargets();
|
2012-06-14 05:26:28 +08:00
|
|
|
|
|
|
|
/** Pause all selectors from all targets with a minimum priority.
|
2013-06-20 14:13:12 +08:00
|
|
|
You should only call this with kPriorityNonSystemMin or higher.
|
2012-06-14 05:26:28 +08:00
|
|
|
@since v2.0.0
|
|
|
|
*/
|
2013-11-16 21:08:00 +08:00
|
|
|
Set* 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.
|
|
|
|
@since v2.0.0
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void resumeTargets(Set* targetsToResume);
|
2012-06-12 01:43:07 +08:00
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
private:
|
2013-11-16 21:08:00 +08:00
|
|
|
void removeHashElement(struct _hashSelectorEntry *element);
|
2012-04-19 14:35:52 +08:00
|
|
|
void removeUpdateFromHash(struct _listEntry *entry);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// update specific
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
void priorityIn(struct _listEntry **list, Object *target, int priority, bool paused);
|
|
|
|
void appendIn(struct _listEntry **list, Object *target, bool paused);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
|
|
|
protected:
|
2013-06-15 14:03:30 +08:00
|
|
|
float _timeScale;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// "updates with priority" stuff
|
|
|
|
//
|
2013-06-15 14:03:30 +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
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Used for "selectors with interval"
|
2013-06-15 14:03:30 +08:00
|
|
|
struct _hashSelectorEntry *_hashForTimers;
|
|
|
|
struct _hashSelectorEntry *_currentTarget;
|
|
|
|
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;
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* _scriptHandlerEntries;
|
2012-02-01 16:45:23 +08:00
|
|
|
};
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of global group
|
|
|
|
/// @}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|
2012-02-01 16:45:23 +08:00
|
|
|
|
|
|
|
#endif // __CCSCHEDULER_H__
|