From 3b93c911efd8561d6811e5ecbc7eab46c89f9a1b Mon Sep 17 00:00:00 2001 From: Darren Ranalli Date: Sat, 16 Oct 2004 01:37:54 +0000 Subject: [PATCH] added BulletinBoard --- direct/src/showbase/BulletinBoard.py | 40 ++++++++++++++++++++++ direct/src/showbase/BulletinBoardGlobal.py | 5 +++ direct/src/showbase/ShowBase.py | 3 ++ 3 files changed, 48 insertions(+) create mode 100755 direct/src/showbase/BulletinBoard.py create mode 100755 direct/src/showbase/BulletinBoardGlobal.py diff --git a/direct/src/showbase/BulletinBoard.py b/direct/src/showbase/BulletinBoard.py new file mode 100755 index 0000000000..ee522bd661 --- /dev/null +++ b/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) diff --git a/direct/src/showbase/BulletinBoardGlobal.py b/direct/src/showbase/BulletinBoardGlobal.py new file mode 100755 index 0000000000..c3096c6a25 --- /dev/null +++ b/direct/src/showbase/BulletinBoardGlobal.py @@ -0,0 +1,5 @@ +"""instantiate global BulletinBoard object""" + +import BulletinBoard + +bulletinBoard = BulletinBoard.BulletinBoard() diff --git a/direct/src/showbase/ShowBase.py b/direct/src/showbase/ShowBase.py index 888f8eb0fb..ebb76b1f9d 100644 --- a/direct/src/showbase/ShowBase.py +++ b/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