123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- #ifndef __CCSCHEDULER_H__
- #define __CCSCHEDULER_H__
- #include <functional>
- #include <mutex>
- #include <set>
- #include "base/CCRef.h"
- #include "base/CCVector.h"
- #include "base/uthash.h"
- NS_CC_BEGIN
- class Scheduler;
- typedef std::function<void(float)> ccSchedulerFunc;
- class CC_DLL Timer : public Ref
- {
- protected:
- Timer();
- public:
-
- float getInterval() const { return _interval; }
-
- void setInterval(float interval) { _interval = interval; }
-
- void setupTimerWithInterval(float seconds, unsigned int repeat, float delay);
- void setAborted() { _aborted = true; }
- bool isAborted() const { return _aborted; }
-
- virtual void trigger(float dt) = 0;
- virtual void cancel() = 0;
-
-
- void update(float dt);
-
- protected:
-
- Scheduler* _scheduler;
- float _elapsed;
- bool _runForever;
- bool _useDelay;
- unsigned int _timesExecuted;
- unsigned int _repeat;
- float _delay;
- float _interval;
- bool _aborted;
- };
- class CC_DLL TimerTargetSelector : public Timer
- {
- public:
- TimerTargetSelector();
-
- bool initWithSelector(Scheduler* scheduler, SEL_SCHEDULE selector, Ref* target, float seconds, unsigned int repeat, float delay);
-
- SEL_SCHEDULE getSelector() const { return _selector; }
-
- virtual void trigger(float dt) override;
- virtual void cancel() override;
-
- protected:
- Ref* _target;
- SEL_SCHEDULE _selector;
- };
- class CC_DLL TimerTargetCallback : public Timer
- {
- public:
- TimerTargetCallback();
-
-
- bool initWithCallback(Scheduler* scheduler, const ccSchedulerFunc& callback, void *target, const std::string& key, float seconds, unsigned int repeat, float delay);
-
- const ccSchedulerFunc& getCallback() const { return _callback; }
- const std::string& getKey() const { return _key; }
-
- virtual void trigger(float dt) override;
- virtual void cancel() override;
-
- protected:
- void* _target;
- ccSchedulerFunc _callback;
- std::string _key;
- };
- #if CC_ENABLE_SCRIPT_BINDING
- class CC_DLL TimerScriptHandler : public Timer
- {
- public:
- bool initWithScriptHandler(int handler, float seconds);
- int getScriptHandler() const { return _scriptHandler; }
-
- virtual void trigger(float dt) override;
- virtual void cancel() override;
-
- private:
- int _scriptHandler;
- };
- #endif
- struct _listEntry;
- struct _hashSelectorEntry;
- struct _hashUpdateEntry;
- #if CC_ENABLE_SCRIPT_BINDING
- class SchedulerScriptHandlerEntry;
- #endif
- class CC_DLL Scheduler : public Ref
- {
- public:
-
- static const int PRIORITY_SYSTEM;
-
-
- static const int PRIORITY_NON_SYSTEM_MIN;
-
-
- Scheduler();
-
-
- virtual ~Scheduler();
-
- float getTimeScale() { return _timeScale; }
-
- void setTimeScale(float timeScale) { _timeScale = timeScale; }
-
- void update(float dt);
-
-
-
-
-
- void schedule(const ccSchedulerFunc& callback, void *target, float interval, unsigned int repeat, float delay, bool paused, const std::string& key);
-
- void schedule(const ccSchedulerFunc& callback, void *target, float interval, bool paused, const std::string& key);
-
-
-
- void schedule(SEL_SCHEDULE selector, Ref *target, float interval, unsigned int repeat, float delay, bool paused);
-
-
- void schedule(SEL_SCHEDULE selector, Ref *target, float interval, bool paused);
-
-
- template <class T>
- void scheduleUpdate(T *target, int priority, bool paused)
- {
- this->schedulePerFrame([target](float dt){
- target->update(dt);
- }, target, priority, paused);
- }
- #if CC_ENABLE_SCRIPT_BINDING
-
-
- unsigned int scheduleScriptFunc(unsigned int handler, float interval, bool paused);
- #endif
-
-
-
-
- void unschedule(const std::string& key, void *target);
-
- void unschedule(SEL_SCHEDULE selector, Ref *target);
-
-
- void unscheduleUpdate(void *target);
-
-
- void unscheduleAllForTarget(void *target);
-
-
- void unscheduleAll();
-
-
- void unscheduleAllWithMinPriority(int minPriority);
-
- #if CC_ENABLE_SCRIPT_BINDING
-
- void unscheduleScriptEntry(unsigned int scheduleScriptEntryID);
- #endif
-
-
-
-
-
-
- bool isScheduled(const std::string& key, void *target);
-
-
- bool isScheduled(SEL_SCHEDULE selector, Ref *target);
-
-
-
-
- void pauseTarget(void *target);
-
- void resumeTarget(void *target);
-
- bool isTargetPaused(void *target);
-
- std::set<void*> pauseAllTargets();
-
- std::set<void*> pauseAllTargetsWithMinPriority(int minPriority);
-
- void resumeTargets(const std::set<void*>& targetsToResume);
-
- void performFunctionInCocosThread( const std::function<void()> &function);
-
-
- void removeAllFunctionsToBePerformedInCocosThread();
-
-
-
-
-
-
- CC_DEPRECATED_ATTRIBUTE void scheduleSelector(SEL_SCHEDULE selector, Ref *target, float interval, unsigned int repeat, float delay, bool paused)
- {
- schedule(selector, target, interval, repeat, delay, paused);
- }
-
-
- CC_DEPRECATED_ATTRIBUTE void scheduleSelector(SEL_SCHEDULE selector, Ref *target, float interval, bool paused)
- {
- schedule(selector, target, interval, paused);
- }
-
-
- template <class T>
- CC_DEPRECATED_ATTRIBUTE void scheduleUpdateForTarget(T* target, int priority, bool paused) { scheduleUpdate(target, priority, paused); };
-
-
- CC_DEPRECATED_ATTRIBUTE void unscheduleSelector(SEL_SCHEDULE selector, Ref *target) { unschedule(selector, target); };
-
-
- CC_DEPRECATED_ATTRIBUTE bool isScheduledForTarget(Ref *target, SEL_SCHEDULE selector) { return isScheduled(selector, target); };
-
-
- CC_DEPRECATED_ATTRIBUTE void unscheduleUpdateForTarget(Ref *target) { return unscheduleUpdate(target); };
-
- protected:
-
-
- void schedulePerFrame(const ccSchedulerFunc& callback, void *target, int priority, bool paused);
-
- void removeHashElement(struct _hashSelectorEntry *element);
- void removeUpdateFromHash(struct _listEntry *entry);
-
- 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);
- float _timeScale;
-
-
-
- struct _listEntry *_updatesNegList;
- struct _listEntry *_updates0List;
- struct _listEntry *_updatesPosList;
- struct _hashUpdateEntry *_hashForUpdates;
- std::vector<struct _listEntry *> _updateDeleteVector;
-
- struct _hashSelectorEntry *_hashForTimers;
- struct _hashSelectorEntry *_currentTarget;
- bool _currentTargetSalvaged;
-
- bool _updateHashLocked;
-
- #if CC_ENABLE_SCRIPT_BINDING
- Vector<SchedulerScriptHandlerEntry*> _scriptHandlerEntries;
- #endif
-
-
- std::vector<std::function<void()>> _functionsToPerform;
- std::mutex _performMutex;
- };
- NS_CC_END
- #endif
|