Browse Source

added BulletinBoard

Darren Ranalli 21 years ago
parent
commit
3b93c911ef

+ 40 - 0
direct/src/showbase/BulletinBoard.py

@@ -0,0 +1,40 @@
+from direct.directnotify import DirectNotifyGlobal
+
+# TODO: add callback mechanism when values change.
+# Should we announce every change through the messenger?
+# Should you be able to hang a hook on a particular name?
+
+class BulletinBoard:
+    """This class implements a global location for key/value pairs to be
+    stored. Intended to prevent coders from putting global variables directly
+    on showbase, so that potential name collisions can be more easily
+    detected."""
+    notify = DirectNotifyGlobal.directNotify.newCategory('BulletinBoard')
+
+    def __init__(self):
+        self._dict = {}
+
+    def get(self, name):
+        return self._dict.get(name)
+
+    def has(self, name):
+        return name in self._dict
+
+    def post(self, name, value=None):
+        if name in self._dict:
+            BulletinBoard.notify.warning('changing %s from %s to %s' % (
+                name, self._dict[name], value))
+        self.update(name, value)
+
+    def update(self, name, value):
+        """can use this to set value the first time"""
+        if name in self._dict:
+            BulletinBoard.notify.info('update: posting %s' % (name))
+        self._dict[name] = value
+        
+    def remove(self, name):
+        if name in self._dict:
+            del self._dict[name]
+        
+    def __repr__(self):
+        return str(self._dict)

+ 5 - 0
direct/src/showbase/BulletinBoardGlobal.py

@@ -0,0 +1,5 @@
+"""instantiate global BulletinBoard object"""
+
+import BulletinBoard
+
+bulletinBoard = BulletinBoard.BulletinBoard()

+ 3 - 0
direct/src/showbase/ShowBase.py

@@ -10,6 +10,7 @@ __builtins__["config"] = ConfigConfigureGetConfigConfigShowbase
 
 from direct.directnotify.DirectNotifyGlobal import *
 from MessengerGlobal import *
+from BulletinBoardGlobal import *
 from direct.task.TaskManagerGlobal import *
 from EventManagerGlobal import *
 from PythonUtil import *
@@ -221,6 +222,7 @@ class ShowBase(DirectObject.DirectObject):
         self.loader = Loader.Loader(self)
         self.eventMgr = eventMgr
         self.messenger = messenger
+        self.bboard = bulletinBoard
         self.taskMgr = taskMgr
 
         # Particle manager
@@ -252,6 +254,7 @@ class ShowBase(DirectObject.DirectObject):
         __builtins__["taskMgr"] = self.taskMgr
         __builtins__["eventMgr"] = self.eventMgr
         __builtins__["messenger"] = self.messenger
+        __builtins__["bboard"] = self.bboard
         # Config needs to be defined before ShowBase is constructed
         #__builtins__["config"] = self.config
         __builtins__["run"] = self.run