forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectObject.py
More file actions
115 lines (92 loc) · 3.88 KB
/
DirectObject.py
File metadata and controls
115 lines (92 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""Defines the DirectObject class, a convenient class to inherit from if the
object needs to be able to respond to events."""
__all__ = ['DirectObject']
from direct.directnotify.DirectNotifyGlobal import directNotify
from .MessengerGlobal import messenger
class DirectObject:
"""
This is the class that all Direct/SAL classes should inherit from
"""
def __init__(self):
pass
#def __del__(self):
# This next line is useful for debugging leaks
#print "Destructing: ", self.__class__.__name__
# Wrapper functions to have a cleaner, more object oriented approach to
# the messenger functionality.
def accept(self, event, method, extraArgs=[]):
return messenger.accept(event, self, method, extraArgs, 1)
def acceptOnce(self, event, method, extraArgs=[]):
return messenger.accept(event, self, method, extraArgs, 0)
def ignore(self, event):
return messenger.ignore(event, self)
def ignoreAll(self):
return messenger.ignoreAll(self)
def isAccepting(self, event):
return messenger.isAccepting(event, self)
def getAllAccepting(self):
return messenger.getAllAccepting(self)
def isIgnoring(self, event):
return messenger.isIgnoring(event, self)
#This function must be used if you want a managed task
def addTask(self, *args, **kwargs):
if(not hasattr(self,"_taskList")):
self._taskList = {}
kwargs['owner']=self
task = taskMgr.add(*args, **kwargs)
return task
def doMethodLater(self, *args, **kwargs):
if(not hasattr(self,"_taskList")):
self._taskList ={}
kwargs['owner']=self
task = taskMgr.doMethodLater(*args, **kwargs)
return task
def removeTask(self, taskOrName):
if type(taskOrName) == type(''):
# we must use a copy, since task.remove will modify self._taskList
if hasattr(self, '_taskList'):
taskListValues = list(self._taskList.values())
for task in taskListValues:
if task.name == taskOrName:
task.remove()
else:
taskOrName.remove()
def removeAllTasks(self):
if hasattr(self,'_taskList'):
for task in list(self._taskList.values()):
task.remove()
def _addTask(self, task):
self._taskList[task.id] = task
def _clearTask(self, task):
del self._taskList[task.id]
def detectLeaks(self):
if not __dev__:
return
# call this after the DirectObject instance has been destroyed
# if it's leaking, will notify user
# make sure we're not still listening for messenger events
events = messenger.getAllAccepting(self)
# make sure we're not leaking tasks
# TODO: include tasks that were added directly to the taskMgr
tasks = []
if hasattr(self, '_taskList'):
tasks = [task.name for task in self._taskList.values()]
if len(events) or len(tasks):
estr = ('listening to events: %s' % events if len(events) else '')
andStr = (' and ' if len(events) and len(tasks) else '')
tstr = ('%srunning tasks: %s' % (andStr, tasks) if len(tasks) else '')
notify = directNotify.newCategory('LeakDetect')
crash = getattr(getRepository(), '_crashOnProactiveLeakDetect', False)
func = (self.notify.error if crash else self.notify.warning)
func('destroyed %s instance is still %s%s' % (self.__class__.__name__, estr, tstr))
#snake_case alias:
add_task = addTask
do_method_later = doMethodLater
detect_leaks = detectLeaks
accept_once = acceptOnce
ignore_all = ignoreAll
get_all_accepting = getAllAccepting
is_ignoring = isIgnoring
remove_all_tasks = removeAllTasks
remove_task = removeTask
is_accepting = isAccepting