From 02d94cc8d2b3bc03cd8954af9e5634413936dadd Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 31 Oct 2008 05:02:57 +0000 Subject: [PATCH] recalcWakeTime() --- direct/src/task/TaskOrig.py | 23 +++++++++++++++++++++++ panda/src/event/asyncTask.I | 3 ++- panda/src/event/asyncTask.cxx | 29 +++++++++++++++++++++++++++++ panda/src/event/asyncTask.h | 1 + 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/direct/src/task/TaskOrig.py b/direct/src/task/TaskOrig.py index b5f9b878a9..3a3cb62fc9 100644 --- a/direct/src/task/TaskOrig.py +++ b/direct/src/task/TaskOrig.py @@ -187,6 +187,29 @@ class Task: def setDelay(self, delay): self.delayTime = delay + def recalcWakeTime(self): + """If the task is currently sleeping, this resets its wake + time to the current time + self.delayTime. It is as if the + task had suddenly returned task.again. The task will sleep + for its current delay seconds before running again. This + method may therefore be used to make the task wake up sooner + or later than it would have otherwise. + + If the task is not already sleeping, this method has no + effect.""" + + # This is a little bit hacky--we're poking around in global + # space more than we should--but this code will be going away + # soon in favor of the new task manager anyway. + + now = globalClock.getFrameTime() + self.wakeTime = now + self.delayTime + + # Very important that we re-sort the priority queue after + # monkeying with the wake times. + heapify(taskMgr._TaskManager__doLaterList) + + def setStartTimeFrame(self, startTime, startFrame): self.starttime = startTime self.startframe = startFrame diff --git a/panda/src/event/asyncTask.I b/panda/src/event/asyncTask.I index 50e88e5f9a..5d2df64c60 100644 --- a/panda/src/event/asyncTask.I +++ b/panda/src/event/asyncTask.I @@ -79,7 +79,8 @@ get_manager() const { // added will not affect the task's wake time; it will // only affect the task if it is re-added to the queue // in the future, for instance if the task returns -// DS_again. +// DS_again. Howver, see recalc_wake_time() if you wish +// to apply the delay effect immediately. //////////////////////////////////////////////////////////////////// INLINE void AsyncTask:: set_delay(double delay) { diff --git a/panda/src/event/asyncTask.cxx b/panda/src/event/asyncTask.cxx index e5c97805fb..6d8f8621cc 100644 --- a/panda/src/event/asyncTask.cxx +++ b/panda/src/event/asyncTask.cxx @@ -114,6 +114,35 @@ get_wake_time() const { return 0.0; } +//////////////////////////////////////////////////////////////////// +// Function: AsyncTask::recalc_wake_time +// Access: Published +// Description: If the task is currently sleeping on a task +// chain, this resets its wake time to the current time +// + get_delay(). It is as if the task had suddenly +// returned DS_again. The task will sleep for its +// current delay seconds before running again. This +// method may therefore be used to make the task wake up +// sooner or later than it would have otherwise. +// +// If the task is not already sleeping, this method has +// no effect. +//////////////////////////////////////////////////////////////////// +void AsyncTask:: +recalc_wake_time() { + if (_manager != (AsyncTaskManager *)NULL) { + MutexHolder holder(_manager->_lock); + if (_state == S_sleeping) { + double now = _manager->_clock->get_frame_time(); + _wake_time = now + _delay; + _start_time = _wake_time; + + make_heap(_chain->_sleeping.begin(), _chain->_sleeping.end(), + AsyncTaskChain::AsyncTaskSortWakeTime()); + } + } +} + //////////////////////////////////////////////////////////////////// // Function: AsyncTask::get_elapsed_time // Access: Published diff --git a/panda/src/event/asyncTask.h b/panda/src/event/asyncTask.h index 9d881001dc..32e3922133 100644 --- a/panda/src/event/asyncTask.h +++ b/panda/src/event/asyncTask.h @@ -79,6 +79,7 @@ PUBLISHED: INLINE bool has_delay() const; INLINE double get_delay() const; double get_wake_time() const; + void recalc_wake_time(); INLINE double get_start_time() const; double get_elapsed_time() const;