comparison pyink/MBScene.py @ 1150:6586cd10c92f

Refactory frameline.py
author Thinker K.F. Li <thinker@codemud.net>
date Sun, 26 Dec 2010 19:17:12 +0800
parents 0ffef2df6201
children 71c72e8d6755
comparison
equal deleted inserted replaced
1149:0ffef2df6201 1150:6586cd10c92f
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # -*- indent-tabs-mode: t; tab-width: 8; python-indent: 4; -*- 2 # -*- indent-tabs-mode: t; tab-width: 8; python-indent: 4; fill-column: 79 -*-
3 # vim: sw=4:ts=8:sts=4 3 # vim: sw=4:ts=8:sts=4:textwidth=79
4 import pygtk 4 import pygtk
5 import gtk 5 import gtk
6 import glib 6 import glib
7 from copy import deepcopy 7 from copy import deepcopy
8 from lxml import etree 8 from lxml import etree
10 import traceback 10 import traceback
11 import time 11 import time
12 import pybInkscape 12 import pybInkscape
13 import math 13 import math
14 from tween import TweenObject 14 from tween import TweenObject
15 from frameline import frameline, frameruler
15 16
16 # Please refer to 17 # Please refer to
17 # http://www.assembla.com/wiki/show/MadButterfly/Inkscape_extention 18 # http://www.assembla.com/wiki/show/MadButterfly/Inkscape_extention
18 # for the designed document. 19 # for the designed document.
19 20
139 def notifyAttributeChanged(self,node, name, old_value, new_value): 140 def notifyAttributeChanged(self,node, name, old_value, new_value):
140 self.ui.updateUI() 141 self.ui.updateUI()
141 pass 142 pass
142 143
143 class MBScene(): 144 class MBScene():
145 _tween_types = (frameline.TWEEN_TYPE_NONE,
146 frameline.TWEEN_TYPE_MOVE,
147 frameline.TWEEN_TYPE_SHAPE)
148 _tween_type_names = ('normal', 'relocate', 'scale')
149
144 def __init__(self, desktop, win, root=None): 150 def __init__(self, desktop, win, root=None):
145 self.desktop = desktop 151 self.desktop = desktop
146 self.window = win 152 self.window = win
147 self.layers = [] 153 self.layers = []
148 self.layers.append(Layer(None)) 154 self.layers.append(Layer(None))
155 self.tween=None 161 self.tween=None
156 self.document = None 162 self.document = None
157 self.root = root 163 self.root = root
158 self.framerate=12 164 self.framerate=12
159 self.maxframe=0 165 self.maxframe=0
166 self._disable_tween_type_selector = False
160 pass 167 pass
161 168
162 def show_selection(self,w,obj): 169 def show_selection(self,w,obj):
163 objs = self.desktop.selection.list() 170 objs = self.desktop.selection.list()
164 try: 171 try:
265 node.removeChild(t) 272 node.removeChild(t)
266 ns = rdoc.createElement("ns0:scenes") 273 ns = rdoc.createElement("ns0:scenes")
267 node.appendChild(ns) 274 node.appendChild(ns)
268 for layer in range(0,len(self._framelines)): 275 for layer in range(0,len(self._framelines)):
269 lobj = self._framelines[layer] 276 lobj = self._framelines[layer]
270 lobj.addScenes(rdoc,ns) 277 self.addScenes(lobj, ns)
271 pass 278 pass
272 pass 279 pass
273 pass 280 pass
274 pass 281 pass
275 pass 282 pass
283 """ 290 """
284 self.layers = [] 291 self.layers = []
285 self.scenemap = None 292 self.scenemap = None
286 doc = self.root 293 doc = self.root
287 294
288 #obs = pybInkscape.PYNodeObserver()
289 #obs = LayerAddRemoveWatcher(self)
290 #doc.addObserver(obs)
291 addEventListener(doc,'DOMNodeInserted',self.updateUI,None) 295 addEventListener(doc,'DOMNodeInserted',self.updateUI,None)
292 addEventListener(doc,'DOMNodeRemoved',self.updateUI,None) 296 addEventListener(doc,'DOMNodeRemoved',self.updateUI,None)
293 doc.childList() 297 doc.childList()
294 try: 298 try:
295 self.width = float(doc.getAttribute("width")) 299 self.width = float(doc.getAttribute("width"))
411 ns.setAttribute("inkscape:groupmode","layer") 415 ns.setAttribute("inkscape:groupmode","layer")
412 self.last_line.node.appendChild(ns) 416 self.last_line.node.appendChild(ns)
413 print 'Add key ', x 417 print 'Add key ', x
414 self.last_line.add_keyframe(x,ns) 418 self.last_line.add_keyframe(x,ns)
415 self.update() 419 self.update()
416 self.last_line.update() 420 pass
417
418 421
419 def removeKeyScene(self): 422 def removeKeyScene(self):
420 nth = self.last_frame 423 nth = self.last_frame
421 y = self.last_line 424 y = self.last_line
422 rdoc = self.document 425 rdoc = self.document
550 for layer in self._framelines: 553 for layer in self._framelines:
551 i=0 554 i=0
552 555
553 # Check the duplicated scene group and create it if it is not available 556 # Check the duplicated scene group and create it if it is not available
554 try: 557 try:
555 if layer.duplicateGroup: 558 layer.duplicateGroup.setAttribute("style","display:none")
556 layer.duplicateGroup.setAttribute("style","display:none")
557 except: 559 except:
558 print "*"*40 560 print "*"*40
559 layer.duplicateGroup = self.document.createElement("svg:g") 561 layer.duplicateGroup = self.document.createElement("svg:g")
560 layer.duplicateGroup.setAttribute("inkscape:label","dup") 562 layer.duplicateGroup.setAttribute("inkscape:label","dup")
561 layer.duplicateGroup.setAttribute("sodipodi:insensitive","1") 563 layer.duplicateGroup.setAttribute("sodipodi:insensitive","1")
572 s.ref.setAttribute("style","") 574 s.ref.setAttribute("style","")
573 else: 575 else:
574 s.ref.setAttribute("style","display:none") 576 s.ref.setAttribute("style","display:none")
575 i = i + 1 577 i = i + 1
576 continue 578 continue
579
577 if nth == s.idx + 1: 580 if nth == s.idx + 1:
578 s.ref.setAttribute("style","") 581 s.ref.setAttribute("style","")
579 else: 582 else:
580 if nth > (s.idx+1) and nth <= (layer._keys[i+1].idx+1): 583 if nth > (s.idx+1) and nth <= (layer._keys[i+1].idx+1):
581 if i+2 < len(layer._keys): 584 if i+2 < len(layer._keys):
582 #s.ref.parent().appendChild(layer.duplicateGroup)
583 s.ref.setAttribute("style","display:none") 585 s.ref.setAttribute("style","display:none")
584 layer.duplicateGroup.setAttribute("style","") 586 layer.duplicateGroup.setAttribute("style","")
585 self.tween.updateTweenContent(layer.duplicateGroup, layer.get_tween_type(s.idx),s, layer._keys[i+2], nth) 587 d = layer._keys[i + 2]
588 self.tween.\
589 updateTweenContent(layer.duplicateGroup,
590 layer.get_tween_type(s.idx),
591 s, d, nth)
586 else: 592 else:
587 layer.duplicateGroup.setAttribute("style","") 593 layer.duplicateGroup.setAttribute("style","")
588 #layer.duplicateGroup = s.ref.duplicate(self.document)
589 #layer.duplicateGroup.setAttribute("style","")
590 #layer.duplicateGroup.setAttribute("inkscape:label","dup")
591 #layer.duplicateGroup.setAttribute("sodipodi:insensitive","1")
592 s.ref.setAttribute("style","display:none") 594 s.ref.setAttribute("style","display:none")
593 #s.ref.parent().appendChild(layer.duplicateGroup)
594 pass 595 pass
595 else: 596 else:
596 s.ref.setAttribute("style","display:none") 597 s.ref.setAttribute("style","display:none")
597 i = i + 2 598 i = i + 2
598 pass 599 pass
599 pass 600 pass
600 pass 601 pass
601
602 def DOMtoItem(self,obj):
603 """
604 Find the corresponding PYSPObject object for a DOM object.
605 """
606 return self.DOMtoItem_recursive(self.desktop.doc().root(),obj)
607
608 def DOMtoItem_recursive(self,tree,obj):
609 nodes = tree.childList()
610 for s in nodes:
611 if s.getId() == obj.getAttribute('id'):
612 return s
613 d = self.DOMtoItem_recursive(s,obj)
614 if d != None: return d
615
616 602
617 def enterGroup(self,obj): 603 def enterGroup(self,obj):
618 for l in self.layers: 604 for l in self.layers:
619 for s in l.node.childList(): 605 for s in l.node.childList():
620 if s.getAttribute('id') == obj.getAttribute("id"): 606 if s.getAttribute('id') == obj.getAttribute("id"):
646 pass 632 pass
647 i = i + 2 633 i = i + 2
648 pass 634 pass
649 pass 635 pass
650 636
651 def setTweenType(self,typ): 637 def setTweenType(self, tween_type):
652 if typ == 'normal': 638 sel_type = MBScene._tween_types.index(tween_type)
653 self.tweenTypeSelector.set_active(0) 639 self._disable_tween_type_selector = True
654 elif typ == 'relocate': 640 self.tweenTypeSelector.set_active(sel_type)
655 self.tweenTypeSelector.set_active(1) 641 self._disable_tween_type_selector = False
656 elif typ == 'scale':
657 self.tweenTypeSelector.set_active(2)
658 pass
659 pass 642 pass
660 643
661 def newCell(self,file): 644 def newCell(self,file):
662 img = gtk.Image() 645 img = gtk.Image()
663 img.set_from_file(file) 646 img.set_from_file(file)
687 pass 670 pass
688 pass 671 pass
689 pass 672 pass
690 673
691 def _create_framelines(self): 674 def _create_framelines(self):
692 import frameline
693 self.scrollwin = gtk.ScrolledWindow() 675 self.scrollwin = gtk.ScrolledWindow()
694 self.scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) 676 self.scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
695 self.scrollwin.set_size_request(-1,150) 677 self.scrollwin.set_size_request(-1,150)
696 678
697 nframes = 100 679 nframes = 100
698 680
699 vbox = gtk.VBox() 681 vbox = gtk.VBox()
700 vbox.show() 682 vbox.show()
701 self.scrollwin.add_with_viewport(vbox) 683 self.scrollwin.add_with_viewport(vbox)
702 684
703 ruler = frameline.frameruler(nframes) 685 ruler = frameruler(nframes)
704 ruler.set_size_request(nframes * 10, 20) 686 ruler.set_size_request(nframes * 10, 20)
705 ruler.show() 687 ruler.show()
706 hbox = gtk.HBox() 688 hbox = gtk.HBox()
707 label=gtk.Label('') 689 label=gtk.Label('')
708 label.set_size_request(100,0) 690 label.set_size_request(100,0)
713 # 695 #
714 # Add a frameline for each layer 696 # Add a frameline for each layer
715 # 697 #
716 self._framelines = [] 698 self._framelines = []
717 for i in range(len(self.layers)-1,-1,-1): 699 for i in range(len(self.layers)-1,-1,-1):
718 line = frameline.frameline(nframes) 700 line = frameline(nframes)
719 hbox = gtk.HBox() 701 hbox = gtk.HBox()
720 label = gtk.Label(self.layers[i].node.getAttribute("inkscape:label")) 702 label = gtk.Label(self.layers[i].node.getAttribute("inkscape:label"))
721 label.set_size_request(100,0) 703 label.set_size_request(100,0)
722 hbox.pack_start(label,expand=False,fill=True) 704 hbox.pack_start(label,expand=False,fill=True)
723 hbox.pack_start(line) 705 hbox.pack_start(line)
744 frameline.label.set_text(frameline.node.getAttribute("inkscape:label")) 726 frameline.label.set_text(frameline.node.getAttribute("inkscape:label"))
745 for scene in layer.scenes: 727 for scene in layer.scenes:
746 frameline.add_keyframe(scene.start-1,scene.node) 728 frameline.add_keyframe(scene.start-1,scene.node)
747 if scene.start != scene.end: 729 if scene.start != scene.end:
748 frameline.add_keyframe(scene.end-1,scene.node) 730 frameline.add_keyframe(scene.end-1,scene.node)
749 frameline.tween(scene.start-1,scene.type) 731 tween_type_idx = self._tween_type_names.index(scene.type)
732 tween_type = self._tween_types[tween_type_idx]
733 frameline.tween(scene.start-1, tween_type)
750 pass 734 pass
751 pass 735 pass
752 pass 736 pass
753 737
754 def cellSelect(self, cell, data): 738 def cellSelect(self, cell, data):
893 btn.connect('clicked', self.doRun) 877 btn.connect('clicked', self.doRun)
894 self.btnRun = btn 878 self.btnRun = btn
895 hbox.pack_start(btn,expand=False,fill=False) 879 hbox.pack_start(btn,expand=False,fill=False)
896 self.addNameEditor(hbox) 880 self.addNameEditor(hbox)
897 self.addTweenTypeSelector(hbox) 881 self.addTweenTypeSelector(hbox)
898 882 pass
899 pass 883
900 884 def onTweenTypeChange(self, w):
901 def onTweenTypeChange(self,w): 885 if self._disable_tween_type_selector:
902 n = self.tweenTypeSelector.get_active() 886 return
887
903 if self.last_line == None: 888 if self.last_line == None:
904 return 889 return
905 frameline = self.last_line 890 frameline = self.last_line
906 i = 0 891 i = 0
907 found = -1 892 found = -1
908 while i < len(frameline._keys): 893 for start_idx, stop_idx, tween_type in frameline.get_frame_blocks():
909 s = frameline._keys[i] 894 if start_idx < stop_idx:
910 if s.right_tween is False: 895 n = self.tweenTypeSelector.get_active()
911 if self.last_frame == s.idx: 896 new_tween_type = MBScene._tween_types[n]
912 found = s.idx 897 self.last_line.set_tween_type(start_idx, new_tween_type)
913 break 898 self.update()
914 else:
915 pass
916 i = i + 1
917 continue
918
919 if self.last_frame >= s.idx and self.last_frame <= frameline._keys[i+1].idx:
920 found = s.idx
921 break 899 break
922 else: 900 pass
923 pass
924 i = i + 2
925 pass
926 pass
927 if found == -1: return
928 self.last_line.set_tween_type(found,self.tweenTypeSelector.get_active_text())
929 self.last_line.update()
930 self.update()
931 pass 901 pass
932 902
933 def addTweenTypeSelector(self,hbox): 903 def addTweenTypeSelector(self,hbox):
934 tweenbox = gtk.HBox() 904 tweenbox = gtk.HBox()
935 label = gtk.Label('Tween Type') 905 label = gtk.Label('Tween Type')
953 def onOK(self,event): 923 def onOK(self,event):
954 self.OK = True 924 self.OK = True
955 gtk.main_quit() 925 gtk.main_quit()
956 pass 926 pass
957 927
928 def addScenes(self, frameline, scenes_node):
929 doc = self.document
930 for start_idx, stop_idx, tween_type in frameline.get_frame_blocks():
931 ref = frameline.get_frame_data(start_idx)
932 tween_type_idx = self._tween_types.index(tween_type)
933 tween_type_name = self._tween_type_names[tween_type_idx]
934
935 scene_node = doc.createElement("ns0:scene")
936 scenes_node.appendChild(scene_node)
937 scene_node.setAttribute("start", str(start_idx + 1))
938 if start_idx != stop_idx:
939 scene_node.setAttribute("end", str(stop_idx + 1))
940 pass
941 scene_node.setAttribute("ref", ref.attribute("id"))
942 scene_node.setAttribute("type", tween_type_name)
943 pass
944 pass
945
958 def updateUI(self,node=None,arg=None): 946 def updateUI(self,node=None,arg=None):
959 if self.lockui: return 947 if self.lockui: return
960 self.lockui = True 948 self.lockui = True
961 self._updateUI() 949 self._updateUI()
962 self.lockui = False 950 self.lockui = False
982 if self.top == None: 970 if self.top == None:
983 self.top = gtk.VBox(False,0) 971 self.top = gtk.VBox(False,0)
984 self.desktop.getToplevel().child.child.pack_end(self.top,expand=False) 972 self.desktop.getToplevel().child.child.pack_end(self.top,expand=False)
985 else: 973 else:
986 self.top.remove(self.startWindow) 974 self.top.remove(self.startWindow)
975 pass
976
987 vbox = gtk.VBox(False,0) 977 vbox = gtk.VBox(False,0)
988 self.startWindow = vbox 978 self.startWindow = vbox
989 self.top.pack_start(vbox,expand=False) 979 self.top.pack_start(vbox,expand=False)
990 vbox.pack_start(self.scrollwin,expand=False) 980 vbox.pack_start(self.scrollwin,expand=False)
991 hbox=gtk.HBox(False,0) 981 hbox=gtk.HBox(False,0)
992 self.addButtons(hbox) 982 self.addButtons(hbox)
993 vbox.pack_start(hbox,expand=False) 983 vbox.pack_start(hbox,expand=False)
994 984
995 # self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
996 # self.window.connect("destroy", gtk.main_quit)
997 # self.window.set_position(gtk.WIN_POS_MOUSE)
998
999 self.top.show_all() 985 self.top.show_all()
1000 self.last_update = None 986 self.last_update = None
1001 return False 987 return False
1002 pass 988 pass