2012-02-01 16:45:23 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
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__
|
|
|
|
|
|
|
|
#include "CCObject.h"
|
|
|
|
#include "support/data_support/uthash.h"
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
// Priority level reserved for system services.
|
|
|
|
#define kCCPrioritySystem INT_MIN
|
|
|
|
|
|
|
|
// Minimum priority level for user scheduling.
|
2012-06-08 14:11:48 +08:00
|
|
|
#define kCCPriorityNonSystemMin (kCCPrioritySystem+1)
|
|
|
|
|
|
|
|
class CCSet;
|
2012-02-01 16:45:23 +08:00
|
|
|
//
|
2012-06-12 17:07:54 +08:00
|
|
|
// CCTimer
|
2012-02-01 16:45:23 +08:00
|
|
|
//
|
|
|
|
/** @brief Light weight timer */
|
2012-06-12 17:07:54 +08:00
|
|
|
class CC_DLL CCTimer : public CCObject
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
|
|
|
public:
|
2012-06-12 17:07:54 +08:00
|
|
|
CCTimer(void);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** get interval in seconds */
|
2012-06-08 13:55:28 +08:00
|
|
|
inline float getInterval(void) { return m_fInterval; }
|
2012-04-19 14:35:52 +08:00
|
|
|
/** set interval in seconds */
|
2012-06-08 13:55:28 +08:00
|
|
|
inline void setInterval(float fInterval){ m_fInterval = fInterval; }
|
2012-02-01 16:45:23 +08:00
|
|
|
|
|
|
|
/** Initializes a timer with a target and a selector. */
|
2012-04-19 14:35:52 +08:00
|
|
|
bool initWithTarget(CCObject *pTarget, SEL_SCHEDULE pfnSelector);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
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. */
|
2012-06-08 13:55:28 +08:00
|
|
|
bool initWithTarget(CCObject *pTarget, SEL_SCHEDULE pfnSelector, float fSeconds, unsigned int nRepeat, float fDelay);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-02-02 15:58:10 +08:00
|
|
|
/** Initializes a timer with a script callback function and an interval in seconds. */
|
2012-06-08 13:55:28 +08:00
|
|
|
bool initWithScriptHandler(int nHandler, float fSeconds);
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** triggers the timer */
|
2012-06-08 13:55:28 +08:00
|
|
|
void update(float dt);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
|
|
|
public:
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Allocates a timer with a target and a selector. */
|
2012-06-12 17:07:54 +08:00
|
|
|
static CCTimer* timerWithTarget(CCObject *pTarget, SEL_SCHEDULE pfnSelector);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Allocates a timer with a target, a selector and an interval in seconds. */
|
2012-06-12 17:07:54 +08:00
|
|
|
static CCTimer* timerWithTarget(CCObject *pTarget, SEL_SCHEDULE pfnSelector, float fSeconds);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-02-02 15:58:10 +08:00
|
|
|
/** Allocates a timer with a script callback function and an interval in seconds. */
|
2012-06-12 17:07:54 +08:00
|
|
|
static CCTimer* timerWithScriptHandler(int nHandler, float fSeconds);
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
public:
|
2012-04-19 14:35:52 +08:00
|
|
|
SEL_SCHEDULE m_pfnSelector;
|
2012-06-08 13:55:28 +08:00
|
|
|
float m_fInterval;
|
2012-02-01 16:45:23 +08:00
|
|
|
|
|
|
|
protected:
|
2012-04-19 14:35:52 +08:00
|
|
|
CCObject *m_pTarget;
|
2012-06-08 13:55:28 +08:00
|
|
|
float m_fElapsed;
|
2012-04-19 14:35:52 +08:00
|
|
|
bool m_bRunForever;
|
|
|
|
bool m_bUseDelay;
|
|
|
|
unsigned int m_nTimesExecuted;
|
|
|
|
unsigned int m_nRepeat; //0 = once, 1 is 2 x executed
|
2012-06-08 13:55:28 +08:00
|
|
|
float m_fDelay;
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2012-02-07 11:43:29 +08:00
|
|
|
int m_nScriptHandler;
|
2012-02-01 16:45:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// CCScheduler
|
|
|
|
//
|
|
|
|
struct _listEntry;
|
|
|
|
struct _hashSelectorEntry;
|
|
|
|
struct _hashUpdateEntry;
|
|
|
|
|
2012-02-02 15:58:10 +08:00
|
|
|
class CCArray;
|
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
/** @brief Scheduler is responsible of triggering the scheduled callbacks.
|
|
|
|
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'.
|
|
|
|
|
|
|
|
*/
|
|
|
|
class CC_DLL CCScheduler : public CCObject
|
|
|
|
{
|
|
|
|
public:
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScheduler();
|
2012-02-01 16:45:23 +08:00
|
|
|
~CCScheduler(void);
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
inline float getTimeScale(void) { return m_fTimeScale; }
|
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.
|
|
|
|
*/
|
2012-06-08 13:55:28 +08:00
|
|
|
inline void setTimeScale(float fTimeScale) { m_fTimeScale = fTimeScale; }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** 'update' the scheduler.
|
|
|
|
You should NEVER call this method, unless you know what you are doing.
|
|
|
|
*/
|
2012-06-08 13:55:28 +08:00
|
|
|
void update(float dt);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** The scheduled method will be called every 'interval' seconds.
|
|
|
|
If paused is YES, then it won't be called until it is resumed.
|
|
|
|
If 'interval' is 0, it will be called every frame, but if so, it recommened to use 'scheduleUpdateForTarget:' instead.
|
|
|
|
If the selector is already scheduled, then only the interval parameter will be updated without re-scheduling it again.
|
|
|
|
repeat let the action be repeated repeat + 1 times, use kCCRepeatForever to let the action run continiously
|
|
|
|
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
|
|
|
|
*/
|
2012-06-08 13:55:28 +08:00
|
|
|
void scheduleSelector(SEL_SCHEDULE pfnSelector, CCObject *pTarget, float fInterval, bool bPaused, unsigned int repeat, float delay);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** calls scheduleSelector with kCCRepeatForever and a 0 delay */
|
2012-06-08 13:55:28 +08:00
|
|
|
void scheduleSelector(SEL_SCHEDULE pfnSelector, CCObject *pTarget, float fInterval, bool bPaused);
|
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
|
|
|
|
*/
|
|
|
|
void scheduleUpdateForTarget(CCObject *pTarget, int nPriority, bool bPaused);
|
|
|
|
|
|
|
|
/** Unschedule a selector for a given target.
|
|
|
|
If you want to unschedule the "update", use unscheudleUpdateForTarget.
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
|
|
|
void unscheduleSelector(SEL_SCHEDULE pfnSelector, CCObject *pTarget);
|
|
|
|
|
|
|
|
/** Unschedules the update selector for a given target
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
|
|
|
void unscheduleUpdateForTarget(const CCObject *pTarget);
|
|
|
|
|
|
|
|
/** Unschedules all selectors for a given target.
|
|
|
|
This also includes the "update" selector.
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
|
|
|
void unscheduleAllSelectorsForTarget(CCObject *pTarget);
|
|
|
|
|
|
|
|
/** Unschedules all selectors from all targets.
|
|
|
|
You should NEVER call this method, unless you know what you are doing.
|
|
|
|
|
|
|
|
@since v0.99.3
|
|
|
|
*/
|
|
|
|
void unscheduleAllSelectors(void);
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
/** Unschedules all selectors from all targets with a minimum priority.
|
|
|
|
You should only call this with kCCPriorityNonSystemMin or higher.
|
|
|
|
@since v2.0.0
|
|
|
|
*/
|
|
|
|
void unscheduleAllSelectorsWithMinPriority(int nMinPriority);
|
|
|
|
|
2012-02-02 15:58:10 +08:00
|
|
|
/** The scheduled script callback will be called every 'interval' seconds.
|
2012-04-19 14:35:52 +08:00
|
|
|
If paused is YES, then it won't be called until it is resumed.
|
|
|
|
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().
|
|
|
|
*/
|
2012-06-08 13:55:28 +08:00
|
|
|
unsigned int scheduleScriptFunc(unsigned int nHandler, float fInterval, bool bPaused);
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Unschedule a script entry. */
|
2012-02-02 15:58:10 +08:00
|
|
|
void unscheduleScriptEntry(unsigned int uScheduleScriptEntryID);
|
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
|
|
|
|
*/
|
|
|
|
void pauseTarget(CCObject *pTarget);
|
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
|
|
|
|
*/
|
|
|
|
void resumeTarget(CCObject *pTarget);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
|
|
|
/** Returns whether or not the target is paused
|
|
|
|
@since v1.0.0
|
|
|
|
*/
|
|
|
|
bool isTargetPaused(CCObject *pTarget);
|
|
|
|
|
2012-06-08 14:11:48 +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
|
|
|
|
*/
|
|
|
|
CCSet* pauseAllTargets();
|
|
|
|
|
|
|
|
/** Pause all selectors from all targets with a minimum priority.
|
|
|
|
You should only call this with kCCPriorityNonSystemMin or higher.
|
|
|
|
@since v2.0.0
|
|
|
|
*/
|
|
|
|
CCSet* pauseAllTargetsWithMinPriority(int nMinPriority);
|
|
|
|
|
|
|
|
/** Resume selectors on a set of targets.
|
|
|
|
This can be useful for undoing a call to pauseAllSelectors.
|
|
|
|
@since v2.0.0
|
|
|
|
*/
|
|
|
|
void resumeTargets(CCSet* targetsToResume);
|
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
private:
|
2012-04-19 14:35:52 +08:00
|
|
|
void removeHashElement(struct _hashSelectorEntry *pElement);
|
|
|
|
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
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void priorityIn(struct _listEntry **ppList, CCObject *pTarget, int nPriority, bool bPaused);
|
|
|
|
void appendIn(struct _listEntry **ppList, CCObject *pTarget, bool bPaused);
|
2012-02-01 16:45:23 +08:00
|
|
|
|
|
|
|
protected:
|
2012-06-08 13:55:28 +08:00
|
|
|
float m_fTimeScale;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// "updates with priority" stuff
|
|
|
|
//
|
|
|
|
struct _listEntry *m_pUpdatesNegList; // list of priority < 0
|
|
|
|
struct _listEntry *m_pUpdates0List; // list priority == 0
|
|
|
|
struct _listEntry *m_pUpdatesPosList; // list priority > 0
|
|
|
|
struct _hashUpdateEntry *m_pHashForUpdates; // hash used to fetch quickly the list entries for pause,delete,etc
|
|
|
|
|
|
|
|
// Used for "selectors with interval"
|
|
|
|
struct _hashSelectorEntry *m_pHashForSelectors;
|
|
|
|
struct _hashSelectorEntry *m_pCurrentTarget;
|
|
|
|
bool m_bCurrentTargetSalvaged;
|
|
|
|
// If true unschedule will not remove anything from a hash. Elements will only be marked for deletion.
|
|
|
|
bool m_bUpdateHashLocked;
|
2012-02-07 11:43:29 +08:00
|
|
|
CCArray* m_pScriptHandlerEntries;
|
2012-02-01 16:45:23 +08:00
|
|
|
};
|
2012-04-18 18:43:45 +08:00
|
|
|
|
|
|
|
NS_CC_END
|
2012-02-01 16:45:23 +08:00
|
|
|
|
|
|
|
#endif // __CCSCHEDULER_H__
|