Mercurial > MadButterfly
comparison pyink/MBScene.py @ 973:84f502fb3e40
Rewrite the MBScene to use framelines. The old layers/scenes data structure is used to load the scenes only. We should remove it completely in the future.
author | wycc |
---|---|
date | Sat, 20 Nov 2010 11:45:17 +0800 |
parents | 1028f6278ead |
children | ed7e8e309d55 |
comparison
equal
deleted
inserted
replaced
972:8a7e5914a84b | 973:84f502fb3e40 |
---|---|
5 import gtk | 5 import gtk |
6 from copy import deepcopy | 6 from copy import deepcopy |
7 from lxml import etree | 7 from lxml import etree |
8 import random | 8 import random |
9 import traceback | 9 import traceback |
10 import time | |
10 | 11 |
11 # Please refer to | 12 # Please refer to |
12 # http://www.assembla.com/wiki/show/MadButterfly/Inkscape_extention | 13 # http://www.assembla.com/wiki/show/MadButterfly/Inkscape_extention |
13 # for the designed document. | 14 # for the designed document. |
14 | 15 |
95 self.scenemap={} | 96 self.scenemap={} |
96 cur = int(n.repr.attribute("current")) | 97 cur = int(n.repr.attribute("current")) |
97 self.current = cur | 98 self.current = cur |
98 | 99 |
99 for s in n.childList(): | 100 for s in n.childList(): |
100 print '--->',s.repr.name() | |
101 if s.repr.name() == 'ns0:scene': | 101 if s.repr.name() == 'ns0:scene': |
102 try: | 102 try: |
103 start = int(s.repr.attribute("start")) | 103 start = int(s.repr.attribute("start")) |
104 except: | 104 except: |
105 traceback.print_exc() | 105 traceback.print_exc() |
122 pass | 122 pass |
123 pass | 123 pass |
124 pass | 124 pass |
125 pass | 125 pass |
126 pass | 126 pass |
127 def update(self): | |
128 doc = self.desktop.doc().root() | |
129 rdoc = self.desktop.doc().rdoc | |
130 for node in doc.childList(): | |
131 if node.repr.name() == 'svg:metadata': | |
132 for t in node.childList(): | |
133 if t.repr.name() == "ns0:scenes": | |
134 node.repr.removeChild(t.repr) | |
135 ns = rdoc.createElement("ns0:scenes") | |
136 node.repr.appendChild(ns) | |
137 for layer in range(0,len(self._framelines)): | |
138 lobj = self._framelines[layer] | |
139 lobj.addScenes(rdoc,ns) | |
127 | 140 |
128 | 141 |
129 def parseScene(self): | 142 def parseScene(self): |
130 """ | 143 """ |
131 In this function, we will collect all items for the current | 144 In this function, we will collect all items for the current |
205 pass | 218 pass |
206 pass | 219 pass |
207 | 220 |
208 def dumpID(self): | 221 def dumpID(self): |
209 for a,v in self.ID.items(): | 222 for a,v in self.ID.items(): |
210 print a | |
211 pass | 223 pass |
212 pass | 224 pass |
213 | 225 |
214 def getLayer(self, layer): | 226 def getLayer(self, layer): |
215 for l in self.layers: | 227 for l in self.layers: |
226 nth is a filled scene, we will break the original scene into | 238 nth is a filled scene, we will break the original scene into |
227 two parts. If the nth is out of any scene, we will append a | 239 two parts. If the nth is out of any scene, we will append a |
228 new scene. | 240 new scene. |
229 | 241 |
230 """ | 242 """ |
231 nth = self.last_cell.nScene | 243 x = self.last_frame |
232 layer = self.getLayer(self.last_cell.layer) | 244 y = self.last_line |
233 x = self.last_cell.nScene | 245 rdoc = self.desktop.doc().rdoc |
234 y = self.last_cell.nLayer | 246 ns = rdoc.createElement("svg:g") |
235 if layer == None: return | 247 txt = rdoc.createElement("svg:rect") |
236 for i in range(0,len(layer.scenes)): | 248 txt.setAttribute("x","0",True) |
237 s = layer.scenes[i] | 249 txt.setAttribute("y","0",True) |
238 if nth >= s.start and nth <= s.end: | 250 txt.setAttribute("width","100",True) |
239 if nth == s.start: return | 251 txt.setAttribute("height","100",True) |
240 newscene = Scene(DuplicateNode(s.node),nth,s.end) | 252 txt.setAttribute("style","fill:#ff00",True) |
241 newscene.node.setId(self.newID()) | 253 ns.appendChild(txt) |
242 layer.scenes.insert(i+1,newscene) | 254 gid = self.newID() |
243 layer.scenes[i].end = nth-1 | 255 ns.setAttribute("id",gid,True) |
244 btn = self.newCell('start.png') | 256 self.last_line.node.repr.appendChild(ns) |
245 btn.nScene = nth | 257 self.last_line.add_keyframe(x,ns) |
246 btn.layer = layer | 258 self.update() |
247 btn.nLayer = y | 259 |
248 self.grid.remove(self.last_cell) | 260 |
249 self.grid.attach(btn, x,x+1,y,y+1,0,0,0,0) | 261 def removeKeyScene(self): |
262 nth = self.last_frame | |
263 y = self.last_line | |
264 rdoc = self.desktop.doc().rdoc | |
265 i = 0 | |
266 layer = self.last_line | |
267 while i < len(layer._keys): | |
268 s = layer._keys[i] | |
269 print "nth:%d idx %d" % (nth,s.idx) | |
270 if nth > s.idx: | |
271 if i == len(layer._keys)-1: | |
272 return | |
273 if nth == s.idx: | |
274 if s.left_tween: | |
275 # This is left tween, we move the keyframe one frame ahead | |
276 if s.idx == layer._keys[i-1].idx: | |
277 layer._keys[i].ref.parent().removeChild(layer._keys[i].ref) | |
278 self.last_line.rm_keyframe(nth) | |
279 self.last_line.rm_keyframe(nth-1) | |
280 else: | |
281 s.idx = s.idx-1 | |
282 else: | |
283 layer._keys[i].ref.parent().removeChild(layer._keys[i].ref) | |
284 if s.right_tween: | |
285 self.last_line.rm_keyframe(layer._keys[i+1].idx) | |
286 self.last_line.rm_keyframe(nth) | |
287 else: | |
288 self.last_line.rm_keyframe(nth) | |
289 | |
290 self.update() | |
291 self.last_line._draw_all_frames() | |
250 return | 292 return |
251 pass | 293 i = i + 1 |
252 | 294 def extendScene(self): |
253 if len(layer.scenes) > 0: | 295 nth = self.last_frame |
254 last = nth | 296 layer = self.last_line |
255 lastscene = None | 297 i = 0 |
256 for s in layer.scenes: | 298 while i < len(layer._keys): |
257 if s.end < nth and last < s.end: | 299 s = layer._keys[i] |
258 last = s.end | 300 if s.right_tween: |
259 lastscene = s | 301 if nth > s.idx: |
260 pass | 302 if nth <= layer._keys[i+1].idx: |
261 pass | 303 return |
262 | 304 try: |
263 for x in range(last+1, nth): | 305 if nth <= layer._keys[i+2].idx: |
264 btn = self.newCell('fill.png') | 306 layer._keys[i+1].idx = nth |
265 btn.nScene = x | 307 layer.draw_all_frames() |
266 btn.layer = layer.node.getId() | 308 self.update() |
267 btn.nLayer = y | 309 self.setCurrentScene(nth) |
268 self.grid.attach(btn, x, x+1, y , y+1,0,0,0,0) | 310 return |
269 pass | 311 else: |
270 | 312 # We may in the next scene |
271 if lastscene == None: | 313 i = i + 2 |
272 node = etree.Element(_scene) | 314 pass |
273 node.setId(self.newID()) | 315 except: |
274 newscene = Scene(node,nth,nth) | 316 # This is the last keyframe, extend the keyframe by |
317 # relocate the location of the keyframe | |
318 layer._keys[i+1].idx = nth | |
319 layer._draw_all_frames() | |
320 self.update() | |
321 self.setCurrentScene(nth) | |
322 return | |
323 else: | |
324 # We are in the front of all keyframes | |
325 return | |
275 else: | 326 else: |
276 lastscene.end = nth-1 | 327 # This is a single keyframe |
277 newscene = Scene(DuplicateNode(lastscene.node),nth,nth) | 328 if nth < s.idx: |
278 newscene.node.setId(self.newID()) | 329 return |
279 pass | 330 if nth == s.idx: |
280 | 331 return |
281 layer.scenes.append(newscene) | 332 try: |
282 btn = self.newCell('start.png') | 333 if nth < layer._keys[i+1].idx: |
283 x = self.last_cell.nScene | 334 # We are after a single keyframe and no scene is |
284 y = self.last_cell.nLayer | 335 # available here. Create a new tween here |
285 btn.nScene = nth | 336 idx = layer._keys[i].idx |
286 btn.layer = layer.node.getId() | 337 layer.add_keyframe(nth,layer._keys[i].ref) |
287 btn.nLayer = y | 338 layer.tween(idx) |
288 self.grid.attach(btn, x, x+1, y, y+1,0,0,0,0) | 339 layer._draw_all_frames() |
289 else: | 340 self.update() |
290 # This is the first scene in the layer | 341 self.setCurrentScene(nth) |
291 node = etree.Element(_scene) | 342 return |
292 node.repr.setId(self.newID()) | 343 else: |
293 newscene = Scene(node,nth,nth) | 344 # We may in the next scene |
294 layer.scenes.append(newscene) | 345 i = i + 1 |
295 btn = self.newCell('start.png') | |
296 btn.nScene = nth | |
297 btn.layer = layer.node.getId() | |
298 btn.nLayer = y | |
299 self.grid.attach(btn, x, x+1, y, y+1,0,0,0,0) | |
300 pass | |
301 pass | |
302 | |
303 | |
304 def removeKeyScene(self): | |
305 nth = self.last_cell.nScene | |
306 try: | |
307 layer = self.getLayer(self.last_cell.layer) | |
308 except: | |
309 return | |
310 x = self.last_cell.nScene | |
311 y = self.last_cell.nLayer | |
312 for i in range(0,len(layer.scenes)): | |
313 s = layer.scenes[i] | |
314 if nth == s.start: | |
315 if i == 0: | |
316 for j in range(s.start,s.end+1): | |
317 btn = self.newCell('empty.png') | |
318 btn.nScene = nth | |
319 btn.layer = layer | |
320 btn.nLayer = y | |
321 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0) | |
322 pass | 346 pass |
323 layer.scenes.remove(s) | 347 pass |
324 else: | 348 except: |
325 if s.start == layer.scenes[i-1].end+1: | 349 # This is the last scene, create a new one |
326 # If the start of the delete scene segment is | 350 idx = layer._keys[i].idx |
327 # the end of the last scene segmenet, convert | 351 layer.add_keyframe(nth,layer._keys[i].ref) |
328 # all scenes in the deleted scene segmenet to | 352 layer.tween(idx) |
329 # the last one | 353 layer._draw_all_frames() |
330 layer.scenes[i-1].end = s.end | 354 self.update() |
331 layer.scenes.remove(s) | 355 self.setCurrentScene(nth) |
332 btn = self.newCell('fill.png') | |
333 | |
334 btn.nScene = nth | |
335 btn.layer = layer | |
336 btn.nLayer = y | |
337 self.grid.attach(btn, x,x+1,y,y+1,0,0,0,0) | |
338 else: | |
339 # Convert all scenes into empty cell | |
340 layer.scenes.remove(s) | |
341 for j in range(s.start,s.end+1): | |
342 btn = self.newCell('empty.png') | |
343 btn.nScene = nth | |
344 btn.layer = layer | |
345 btn.nLayer = y | |
346 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0) | |
347 pass | |
348 pass | |
349 pass | |
350 return | |
351 pass | |
352 pass | |
353 | |
354 def extendScene(self): | |
355 nth = self.last_cell.nScene | |
356 try: | |
357 layer = self.getLayer(self.last_cell.layer) | |
358 except: | |
359 traceback.print_exc() | |
360 return | |
361 x = self.last_cell.nScene | |
362 y = self.last_cell.nLayer | |
363 if layer == None: | |
364 return | |
365 | |
366 for i in range(0,len(layer.scenes)-1): | |
367 s = layer.scenes[i] | |
368 if nth >= layer.scenes[i].start and nth <= layer.scenes[i].end: | |
369 return | |
370 pass | |
371 | |
372 for i in range(0,len(layer.scenes)-1): | |
373 s = layer.scenes[i] | |
374 if nth >= layer.scenes[i].start and nth < layer.scenes[i+1].start: | |
375 for j in range(layer.scenes[i].end+1, nth+1): | |
376 btn = self.newCell('fill.png') | |
377 btn.nScene = nth | |
378 btn.nLayer = y | |
379 btn.layer = self.last_cell.layer | |
380 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0) | |
381 layer.scenes[i].end = nth | |
382 return | 356 return |
383 pass | 357 pass |
384 if len(layer.scenes) > 0 and \ | 358 pass |
385 nth > layer.scenes[len(layer.scenes)-1].end: | 359 pass |
386 for j in range(layer.scenes[len(layer.scenes)-1].end+1, nth+1): | 360 |
387 btn = self.newCell('fill.png') | |
388 btn.nScene = nth | |
389 btn.nLayer = y | |
390 btn.layer = self.last_cell.layer | |
391 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0) | |
392 pass | |
393 layer.scenes[len(layer.scenes)-1].end = nth | |
394 pass | |
395 pass | |
396 pass | |
397 | 361 |
398 def setCurrentScene(self,nth): | 362 def setCurrentScene(self,nth): |
399 self.current = nth | 363 self.current = nth |
400 for layer in self.layers: | 364 for layer in self._framelines: |
401 for s in layer.scenes: | 365 i=0 |
402 if nth >= s.start and nth <= s.end: | 366 while i < len(layer._keys): |
403 s.node.repr.setAttribute("style","",True) | 367 s = layer._keys[i] |
404 # print "Put the elemenets out" | 368 print s.ref.attribute("id"),s.idx,s.left_tween,s.right_tween |
405 layer.nodes = [] | 369 if s.right_tween is False: |
406 | 370 if nth == s.idx: |
407 # for o in s.node: | 371 s.ref.setAttribute("style","",True) |
408 # print " ",o.tag | 372 else: |
409 # layer.nodes.append(o) | 373 s.ref.setAttribute("style","display:none",True) |
410 # for o in s.node: | 374 i = i + 1 |
411 # s.node.remove(o) | 375 continue |
376 | |
377 if nth >= (s.idx+1) and nth <= (layer._keys[i+1].idx+1): | |
378 s.ref.setAttribute("style","",True) | |
412 else: | 379 else: |
413 s.node.repr.setAttribute("style","display:none",True) | 380 s.ref.setAttribute("style","display:none",True) |
414 pass | 381 i = i + 2 |
415 pass | 382 pass |
416 pass | 383 pass |
417 pass | 384 pass |
418 | 385 |
419 def generate(self): | 386 def generate(self): |
503 btn.add(img) | 470 btn.add(img) |
504 btn.connect("button_press_event", self.cellSelect) | 471 btn.connect("button_press_event", self.cellSelect) |
505 btn.modify_bg(gtk.STATE_NORMAL, btn.get_colormap().alloc_color("gray")) | 472 btn.modify_bg(gtk.STATE_NORMAL, btn.get_colormap().alloc_color("gray")) |
506 return btn | 473 return btn |
507 | 474 |
475 def onCellClick(self,line,frame,but): | |
476 self.last_line = line | |
477 self.last_frame = frame | |
478 self.doEditScene(frame) | |
479 | |
480 | |
508 def _create_framelines(self): | 481 def _create_framelines(self): |
509 import frameline | 482 import frameline |
510 self.scrollwin = gtk.ScrolledWindow() | 483 self.scrollwin = gtk.ScrolledWindow() |
511 self.scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) | 484 self.scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) |
512 self.scrollwin.set_size_request(-1,150) | 485 self.scrollwin.set_size_request(-1,150) |
529 for i in range(len(self.layers)): | 502 for i in range(len(self.layers)): |
530 line = frameline.frameline(nframes) | 503 line = frameline.frameline(nframes) |
531 line.set_size_request(nframes * 10, 20) | 504 line.set_size_request(nframes * 10, 20) |
532 vbox.pack_start(line, False) | 505 vbox.pack_start(line, False) |
533 self._framelines.append(line) | 506 self._framelines.append(line) |
507 line.connect(line.FRAME_BUT_PRESS, self.onCellClick) | |
508 line.nLayer = i | |
509 line.node = self.layers[i].node | |
534 pass | 510 pass |
535 pass | 511 pass |
536 | 512 |
537 ## \brief Update conetent of frameliens according layers. | 513 ## \brief Update conetent of frameliens according layers. |
538 # | 514 # |
539 def _update_framelines(self): | 515 def _update_framelines(self): |
540 for layer_i, layer in enumerate(self.layers): | 516 for layer_i, layer in enumerate(self.layers): |
541 for scene in layer.scenes: | 517 for scene in layer.scenes: |
542 frameline = self._framelines[layer_i] | 518 frameline = self._framelines[layer_i] |
543 for scene_i in range(scene.start, scene.stop + 1): | 519 frameline.add_keyframe(scene.start-1,scene.node.repr) |
544 frameline.add_keyframe(scene_i) | 520 if scene.start != scene.end: |
545 pass | 521 frameline.add_keyframe(scene.end-1,scene.node.repr) |
522 frameline.tween(scene.start-1) | |
546 pass | 523 pass |
547 pass | 524 pass |
548 pass | 525 pass |
549 | 526 |
550 def cellSelect(self, cell, data): | 527 def cellSelect(self, cell, data): |
557 color = cell.get_colormap().alloc_color("green") | 534 color = cell.get_colormap().alloc_color("green") |
558 cell.modify_bg(gtk.STATE_NORMAL, color) | 535 cell.modify_bg(gtk.STATE_NORMAL, color) |
559 pass | 536 pass |
560 | 537 |
561 def doEditScene(self,w): | 538 def doEditScene(self,w): |
562 self.setCurrentScene(self.last_cell.nScene) | 539 self.setCurrentScene(self.last_frame+1) |
563 pass | 540 pass |
564 | 541 |
565 def doInsertKeyScene(self,w): | 542 def doInsertKeyScene(self,w): |
566 # self.insertKeyScene() | 543 self.insertKeyScene() |
567 # self.grid.show_all() | 544 # self.grid.show_all() |
568 return | 545 return |
569 | 546 |
570 def doRemoveScene(self,w): | 547 def doRemoveScene(self,w): |
571 # self.removeKeyScene() | 548 self.removeKeyScene() |
572 # self.grid.show_all() | 549 # self.grid.show_all() |
573 # self.generate() | 550 # self.generate() |
574 return | 551 return |
575 | 552 |
576 | 553 |
577 def doExtendScene(self,w): | 554 def doExtendScene(self,w): |
578 self.extendScene() | 555 self.extendScene() |
579 self.grid.show_all() | 556 #self.grid.show_all() |
580 pass | 557 pass |
581 | 558 |
582 def addButtons(self,hbox): | 559 def addButtons(self,hbox): |
583 btn = gtk.Button('Edit') | 560 #btn = gtk.Button('Edit') |
584 btn.connect('clicked', self.doEditScene) | 561 #btn.connect('clicked', self.doEditScene) |
585 hbox.pack_start(btn,expand=False,fill=False) | 562 #hbox.pack_start(btn,expand=False,fill=False) |
586 btn = gtk.Button('Insert Key') | 563 btn = gtk.Button('Insert Key') |
587 btn.connect('clicked',self.doInsertKeyScene) | 564 btn.connect('clicked',self.doInsertKeyScene) |
588 hbox.pack_start(btn,expand=False,fill=False) | 565 hbox.pack_start(btn,expand=False,fill=False) |
589 btn=gtk.Button('Remove Key') | 566 btn=gtk.Button('Remove Key') |
590 btn.connect('clicked', self.doRemoveScene) | 567 btn.connect('clicked', self.doRemoveScene) |