Go to the documentation of this file.00001 import pickle
00002 from Signal import Signal
00003
00004 class ActionSequence(object):
00005
00006 execute_single_finished_signal = Signal()
00007 execute_sequence_finished_signal = Signal()
00008
00009 executing_action_signal = Signal()
00010
00011 def __init__(self):
00012 super(ActionSequence, self).__init__()
00013 self._actions = []
00014 self._current_action = None
00015
00016 def actions(self):
00017 return self._actions
00018
00019 def get_duration(self):
00020 duration = 0.0
00021 for action in self._actions:
00022 duration += action.get_duration()
00023 return duration
00024
00025 def add_action(self, action, index = None):
00026 if index is None:
00027 self._actions.append(action)
00028 else:
00029 assert(index >=0 and index < len(self._actions))
00030 self._actions.insert(index, action)
00031
00032 def remove_action(self, index):
00033 assert(index >=0 and index < len(self._actions))
00034 del self._actions[index]
00035 if self._current_action >= index:
00036 self._current_action = self._current_action - 1
00037
00038 def remove_all_actions(self):
00039 self._actions = []
00040 self._current_action = None
00041
00042 def serialize(self, stream):
00043 stream.serialize_data(len(self._actions))
00044 for action in self._actions:
00045 stream.serialize_instance(action)
00046
00047 def deserialize(self, stream):
00048 self.remove_all_actions()
00049 count = stream.deserialize_data()
00050 for i in range(count):
00051 action = stream.deserialize_instance()
00052 self.add_action(action)
00053
00054 def execute_single(self, index):
00055 if self._current_action is not None:
00056 print('ActionSequence.execute_single() skipped because previous execute has not yet finished')
00057 return
00058 self._execute(index, self._execute_single_finished)
00059
00060 def execute_all(self, first_index = None):
00061
00062 if self._current_action is not None:
00063 print('ActionSequence.execute_all() skipped because previous execute has not yet finished')
00064 return
00065 if not self._actions:
00066 self.execute_sequence_finished_signal.emit()
00067 return
00068 if first_index is None:
00069 first_index = 0
00070 self._execute(first_index, self._execute_sequence_finished)
00071
00072 def stop(self):
00073 if self._current_action is not None:
00074 action = self._actions[self._current_action]
00075 action.stop()
00076 action.execute_finished_signal.disconnect(self._execute_single_finished)
00077 action.execute_finished_signal.disconnect(self._execute_sequence_finished)
00078 self._current_action = None
00079
00080 def _execute_single_finished(self):
00081 index = self._current_action
00082 self._execute_finished(self._execute_single_finished)
00083 self.execute_single_finished_signal.emit(index)
00084
00085 def _execute_sequence_finished(self):
00086
00087 index = self._current_action
00088 self._execute_finished(self._execute_sequence_finished)
00089 index += 1
00090 if index < len(self._actions):
00091 self.execute_all(index)
00092 else:
00093
00094 self.execute_sequence_finished_signal.emit()
00095
00096 def _execute(self, index, callback):
00097
00098 assert(index >= 0 and index < len(self._actions))
00099 self._current_action = index
00100 action = self._actions[self._current_action]
00101 self.executing_action_signal.emit(self._current_action)
00102 action.execute_finished_signal.connect(callback)
00103 action.execute()
00104
00105 def _execute_finished(self, callback):
00106 index = self._current_action
00107 self._current_action = None
00108
00109 action = self._actions[index]
00110 action.execute_finished_signal.disconnect(callback)