瀏覽代碼

added state change broadcast support

Darren Ranalli 21 年之前
父節點
當前提交
bab3da450c
共有 1 個文件被更改,包括 17 次插入0 次删除
  1. 17 0
      direct/src/fsm/FSM.py

+ 17 - 0
direct/src/fsm/FSM.py

@@ -123,8 +123,13 @@ class FSM(DirectObject.DirectObject):
 
     notify = DirectNotifyGlobal.directNotify.newCategory("FSM")
 
+    SerialNum = 0
+
     def __init__(self, name):
         self.name = name
+        self._serialNum = FSM.SerialNum
+        FSM.SerialNum += 1
+        self._broadcastStateChanges = False
         # Initially, we are in the Off state by convention.
         self.state = 'Off'
 
@@ -155,6 +160,15 @@ class FSM(DirectObject.DirectObject):
         if self.state != 'Off':
             self.__setState('Off')
 
+    def setBroadcastStateChanges(self, doBroadcast):
+        self._broadcastStateChanges = doBroadcast
+    def getStateChangeEvent(self):
+        # if setBroadcastStateChanges(True), this event will be sent through
+        # the messenger on every state change. The new and old states are
+        # accessible as self.oldState and self.newState, and the transition
+        # functions will already have been called.
+        return 'FSM-%s-%s-stateChange' % (self._serialNum, self.name)
+
     def getCurrentOrNextState(self):
         # Returns the current state if we are in a state now, or the
         # state we are transitioning into if we are currently within
@@ -355,6 +369,9 @@ class FSM(DirectObject.DirectObject):
             del self.oldState
             del self.newState
             raise
+
+        if self._broadcastStateChanges:
+            messenger.send(self.getStateChangeEvent())
         
         self.state = newState
         del self.oldState