comparison pyink/MBScene.py @ 1245:ccbf0c5d01d1

Move code of setCurrentScene to tween.py. - setCurrentScene() is removed from MBScene. - Add a new class scenes_director in tween.py. - scenes_director.show_scene() replaces setCurrentScene(). - MBScene instantiates a scenes_director, and calls scenes_director.show_scene() to switch scenes.
author Thinker K.F. Li <thinker@codemud.net>
date Mon, 10 Jan 2011 19:07:26 +0800
parents b241f9768833
children 42c4874c8d1e
comparison
equal deleted inserted replaced
1244:b241f9768833 1245:ccbf0c5d01d1
4 import pygtk 4 import pygtk
5 import gtk 5 import gtk
6 import glib 6 import glib
7 import traceback 7 import traceback
8 import pybInkscape 8 import pybInkscape
9 from tween import TweenObject 9 from tween import scenes_director
10 from domview_ui import domview_ui 10 from domview_ui import domview_ui
11 11
12 # Please refer to 12 # Please refer to
13 # http://www.assembla.com/wiki/show/MadButterfly/Inkscape_extention 13 # http://www.assembla.com/wiki/show/MadButterfly/Inkscape_extention
14 # for the designed document. 14 # for the designed document.
57 # 57 #
58 # This method accepts user actions and involves domview_ui to update 58 # This method accepts user actions and involves domview_ui to update
59 # data on the document. 59 # data on the document.
60 # 60 #
61 class MBScene(object): 61 class MBScene(object):
62 _tween_obj_tween_types = (TweenObject.TWEEN_TYPE_NORMAL,
63 TweenObject.TWEEN_TYPE_SCALE)
64 _tween_type_names = ('normal', 'scale') 62 _tween_type_names = ('normal', 'scale')
65 63
66 _num_frames_of_line = 100 64 _num_frames_of_line = 100
67 65
68 def __init__(self, desktop, win, root=None): 66 def __init__(self, desktop, win, root=None):
73 self.top = None 71 self.top = None
74 self.last_update = None 72 self.last_update = None
75 pybInkscape.inkscape.connect('change_selection', self.on_selection) 73 pybInkscape.inkscape.connect('change_selection', self.on_selection)
76 self.last_select = None 74 self.last_select = None
77 self._lockui = False 75 self._lockui = False
78 self.tween = None 76 self._director = None
79 self.document = None 77 self.document = None
80 self._root = root 78 self._root = root
81 self.framerate = 12 79 self.framerate = 12
82 self._disable_tween_type_selector = False 80 self._disable_tween_type_selector = False
83 self.current = 0 81 self.current = 0
125 pass 123 pass
126 pass 124 pass
127 125
128 def removeKeyScene(self, layer_idx, frame_idx): 126 def removeKeyScene(self, layer_idx, frame_idx):
129 self._domview.unmark_key(layer_idx, frame_idx) 127 self._domview.unmark_key(layer_idx, frame_idx)
130 self.setCurrentScene(frame_idx) 128 self._director.show_scene(frame_idx)
131 pass 129 pass
132 130
133 def extendScene(self): 131 def extendScene(self):
134 layer_idx, frame_idx = self._domview.get_active_layer_frame() 132 layer_idx, frame_idx = self._domview.get_active_layer_frame()
135 start, end, tween_type = \ 133 start, end, tween_type = \
137 tween_len = frame_idx - start + 1 135 tween_len = frame_idx - start + 1
138 self._domview.tween(layer_idx, start, tween_len, tween_type) 136 self._domview.tween(layer_idx, start, tween_len, tween_type)
139 137
140 scene_group = self._domview.get_key_group(layer_idx, start) 138 scene_group = self._domview.get_key_group(layer_idx, start)
141 self._enterGroup(scene_group) 139 self._enterGroup(scene_group)
142 pass
143
144 def setCurrentScene(self, idx):
145 """
146 Update the scene group according to the curretn scene
147 data. There are a couple of cases.
148 1. If the type of the scene is normal, we display it when
149 it contains the current frame. Otherwise hide it.
150 2. If the type of the scene is relocate or scale, we need
151 to duplicate the scene group and then modify its
152 transform matrix according to the definition of the
153 scene. Then, hide the original scenr group and display
154 the duplciate scene group. In addition, we may need to
155 delete the old duplicated scene group as well.
156
157 For each layer, we will always use the duplicated scene
158 group whose name as dup.
159 We will put the duplicated scene group inside it. We will
160 create this group if it is not
161 available.
162 """
163 self.current = idx
164 self.tween.updateMapping()
165 for layer_idx in range(self._domview.get_layer_num()):
166 dup_group = self._domview.get_layer_dup_group(layer_idx)
167 dup_group.setAttribute('style', 'display: none')
168
169 all_key_tweens = self._domview.get_layer_keys(layer_idx)
170 for start, end, tween_type in all_key_tweens:
171 if start == idx: # at key frame
172 scene_group = \
173 self._domview.get_key_group(layer_idx, start)
174 scene_group.setAttribute('style', '')
175 elif start < idx and end >= idx: # in Tween
176 dup_group.setAttribute('style', '')
177 scene_group = \
178 self._domview.get_key_group(layer_idx, start)
179 scene_group.setAttribute('style', 'display: none')
180
181 try:
182 next_scene_group = \
183 self._domview.get_key_group(layer_idx, end + 1)
184 except: # no next key frame
185 next_scene_group = scene_group
186 pass
187
188 tween_obj_type = self._tween_obj_tween_types[tween_type]
189 nframes = end - start + 1
190 percent = float(idx - start) / nframes
191 self.tween.updateTweenContent(dup_group,
192 tween_obj_type,
193 scene_group,
194 next_scene_group,
195 percent)
196 pass
197 else: # this scene should not be showed.
198 scene_group = \
199 self._domview.get_key_group(layer_idx, start)
200 scene_group.setAttribute('style', 'display: none')
201 pass
202 pass
203 pass
204 pass 140 pass
205 141
206 def _enterGroup(self, scene_group): 142 def _enterGroup(self, scene_group):
207 self.desktop.setCurrentLayer(scene_group.spitem) 143 self.desktop.setCurrentLayer(scene_group.spitem)
208 pass 144 pass
243 left_scene_group = \ 179 left_scene_group = \
244 self._domview.get_key_group(layer_idx, left_start) 180 self._domview.get_key_group(layer_idx, left_start)
245 181
246 dup_group = self._duplicate_group(left_scene_group, scene_group) 182 dup_group = self._duplicate_group(left_scene_group, scene_group)
247 183
248 self.setCurrentScene(frame_idx) 184 self._director.show_scene(frame_idx)
249 pass 185 pass
250 186
251 ## \brief Duplicate children of a group. 187 ## \brief Duplicate children of a group.
252 # 188 #
253 # Duplicate children of a group, and append them to another group. 189 # Duplicate children of a group, and append them to another group.
322 pass 258 pass
323 259
324 def onCellClick(self, layer_idx, frame_idx): 260 def onCellClick(self, layer_idx, frame_idx):
325 self._lockui = True 261 self._lockui = True
326 262
327 self.setCurrentScene(frame_idx) 263 self._director.show_scene(frame_idx)
328 self.selectSceneObject(layer_idx, frame_idx) 264 self.selectSceneObject(layer_idx, frame_idx)
329 265
330 self._lockui = False 266 self._lockui = False
331 pass 267 pass
332 268
376 def doRunNext(self): 312 def doRunNext(self):
377 if self.current > self._domview.get_max_frame(): 313 if self.current > self._domview.get_max_frame():
378 self.current = 0 314 self.current = 0
379 pass 315 pass
380 try: 316 try:
381 self.setCurrentScene(self.current) 317 self._director.show_scene(self.current)
382 except: 318 except:
383 traceback.print_exc() 319 traceback.print_exc()
384 raise 320 raise
385 self.current = self.current + 1 321 self.current = self.current + 1
386 tmout = 1000 / self.framerate 322 tmout = 1000 / self.framerate
464 self._root = self.desktop.doc().root().repr 400 self._root = self.desktop.doc().root().repr
465 pass 401 pass
466 402
467 self.document = self.desktop.doc().rdoc 403 self.document = self.desktop.doc().rdoc
468 404
469 self.tween = TweenObject(self.document, self._root)
470 self._domview.handle_doc_root(self.document, self._root) 405 self._domview.handle_doc_root(self.document, self._root)
471 self._domview.register_active_frame_callback(self.onCellClick) 406 self._domview.register_active_frame_callback(self.onCellClick)
407 self._director = scenes_director(self._domview)
472 408
473 if self.top == None: 409 if self.top == None:
474 self.top = gtk.VBox(False, 0) 410 self.top = gtk.VBox(False, 0)
475 toplevel = self.desktop.getToplevel() 411 toplevel = self.desktop.getToplevel()
476 toplevel.child.child.pack_end(self.top, expand=False) 412 toplevel.child.child.pack_end(self.top, expand=False)