From f0a6f8adff1effebb2d8194aefca63e4a966689c Mon Sep 17 00:00:00 2001 From: Darren Ranalli Date: Fri, 14 Jul 2006 21:21:48 +0000 Subject: [PATCH] added DelayedCall and DelayedCallback --- direct/src/showbase/PythonUtil.py | 39 ++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/direct/src/showbase/PythonUtil.py b/direct/src/showbase/PythonUtil.py index f047d87a5a..508808aad4 100644 --- a/direct/src/showbase/PythonUtil.py +++ b/direct/src/showbase/PythonUtil.py @@ -71,7 +71,7 @@ def writeFsmTree(instance, indent = 0): #if __debug__: #RAU accdg to Darren its's ok that StackTrace is not protected by __debug__ -# DCR if somebody ends up using StackTrace in production, either +# DCR: if somebody ends up using StackTrace in production, either # A) it will be OK because it hardly ever gets called, or # B) it will be easy to track it down (grep for StackTrace) class StackTrace: @@ -2244,6 +2244,43 @@ def printNumberedTyped(items, maxLen=5000): objStr = '%s%s' % (objStr[:(maxLen-len(snip))], snip) print format % (i, itype(items[i]), objStr) +class DelayedCall: + """ calls a func after a specified delay """ + def __init__(self, func, name=None, delay=None): + if name is None: + name = 'anonymous' + if delay is None: + delay = .01 + self._func = func + taskMgr.doMethodLater(delay, self._doCallback, 'DelayedCallback-%s' % name) + def _doCallback(self, task): + func = self._func + del self._func + func() + +class DelayedCallback: + """ waits for this object to be called, then calls your callback after a delay """ + def __init__(self, callback, name=None, delay=None): + self._callback = callback + self._name = name + # FunctionInterval requires __name__ + self.__name__ = self._name + self._delay = delay + def _doCallback(self): + cb = Functor(self._callback, *self._args, **self._kwArgs) + del self._callback + del self._name + del self._delay + del self._args + del self._kwArgs + del self._delayedCall + del self.__name__ + cb() + def __call__(self, *args, **kwArgs): + self._args = args + self._kwArgs = kwArgs + self._delayedCall = DelayedCall(self._doCallback, self._name, self._delay) + import __builtin__ __builtin__.Functor = Functor __builtin__.Stack = Stack