comparison inkscape/MB_Frame.py @ 234:889cdc5f23c5

When a scene is selected to0 edit, we copy all of its elements out of the scene so that inkscape can edit it directly. All elements add or delete by the inkspcae will be put back when we switch the scene. The svg2code.py must recognize this structure.
author wycc
date Wed, 24 Dec 2008 23:43:39 +0800
parents ec62453bbb2b
children 55f86e2f8d40
comparison
equal deleted inserted replaced
233:ec62453bbb2b 234:889cdc5f23c5
8 8
9 # In the inkscape, the top layer group are treated as layer. The Mad butter fly add another structure under the layer as frame. It means that each layer can has multiple frame. 9 # In the inkscape, the top layer group are treated as layer. The Mad butter fly add another structure under the layer as frame. It means that each layer can has multiple frame.
10 # Like the layer, the frames are represented by special groups. All groups directly under a layer will be treated as frames. However, a group may be spanned for more than one 10 # Like the layer, the frames are represented by special groups. All groups directly under a layer will be treated as frames. However, a group may be spanned for more than one
11 # frame. For example 11 # frame. For example
12 # <g id="layer1"> 12 # <g id="layer1">
13 # <g id="g1222">
14 # </g>
13 # <g id="g1234" scene="1"> 15 # <g id="g1234" scene="1">
14 # </g> 16 # </g>
15 # <g id="g1235" scene="2-7"> 17 # <g id="g1235" scene="2-7" current="3">
16 # </g> 18 # </g>
17 # <g id="g1236"> 19 # <g id="g1333"/>
18 # </g>
19 # </g> 20 # </g>
20 # This will stand for 7 scenes. Scene 1 and scene 2 are key scenes. 3,4,5,6,7 are filled scenes.In the above example, g1234 and g1236 are in the scene 1. g1235 and g1236 are in 21 # This will stand for 7 scenes. Scene 1 and scene 2 are key scenes. 3,4,5,6,7 are filled scenes. The current scene is defined by the 'current' attribute. In the above example, it is 3.
21 # scene 2-7 22 # All elements without scene attributes are items in the current scene.
23 # Therefore, when we switch scene, we will move all items into the current scene and then move items out from the new scene.
22 # 24 #
23 # In the inkscape extention, we will provide an grid for users to select the current scene or change the scene structure. Users are allowed to 25 # In the inkscape extention, we will provide an grid for users to select the current scene or change the scene structure. Users are allowed to
24 # Insert a new key scene 26 # Insert a new key scene
25 # Delete a key scene 27 # Delete a key scene
26 # Insert a filled scene 28 # Insert a filled scene
46 48
47 class Layer: 49 class Layer:
48 def __init__(self,node): 50 def __init__(self,node):
49 self.scene = [] 51 self.scene = []
50 self.node = node 52 self.node = node
53 self.nodes=[]
51 class Scene: 54 class Scene:
52 def __init__(self, node, start,end): 55 def __init__(self, node, start,end):
53 self.node = node 56 self.node = node
54 self.start = int(start) 57 self.start = int(start)
55 self.end = int(end) 58 self.end = int(end)
74 for n in node: 77 for n in node:
75 self.dump(n,l+1) 78 self.dump(n,l+1)
76 print " " * l * 2,"/>" 79 print " " * l * 2,"/>"
77 80
78 def parseScene(self): 81 def parseScene(self):
82 """
83 In this function, we will collect all items for the current scene and then relocate them back to the appropriate scene object.
84 """
79 self.layer = [] 85 self.layer = []
80 for layer in self.document.getroot(): 86 current_scene = []
81 if layer.tag == '{http://www.w3.org/2000/svg}g': 87 oldscene = None
88 for node in self.document.getroot():
89 if node.tag == '{http://www.w3.org/2000/svg}g':
82 #print layer.attrib.get("id") 90 #print layer.attrib.get("id")
83 lyobj = Layer(layer) 91 lyobj = Layer(node)
84 self.layer.append(lyobj) 92 self.layer.append(lyobj)
85 for scene in layer: 93 for scene in node:
86 if scene.tag == '{http://www.w3.org/2000/svg}g': 94 if scene.tag == '{http://www.w3.org/2000/svg}g':
87 range = scene.attrib.get("scene").split('-') 95 s = scene.get("scene")
96 if s == None:
97 # group without scene is part of the current scene
98 current_scene.append(scene)
99 continue
100 range = s.split('-')
101
102 cur = scene.get("current")
103 try:
104 self.current_scene = int(cur)
105 del scene.attrib["current"]
106 oldscene = scene
107 except:
108 pass
88 if len(range) == 1: 109 if len(range) == 1:
89 #print " scene %d" % int(range[0]) 110 #print " scene %d" % int(range[0])
90 lyobj.scene.append(Scene(scene,range[0],range[0])) 111 lyobj.scene.append(Scene(scene,range[0],range[0]))
91 elif len(range) == 2: 112 elif len(range) == 2:
92 #print " scene%d-%d" % (int(range[0]),int(range[1])) 113 #print " scene%d-%d" % (int(range[0]),int(range[1]))
93 lyobj.scene.append(Scene(scene,range[0],range[1])) 114 lyobj.scene.append(Scene(scene,range[0],range[1]))
115 else:
116 current_scene.append(scene)
117 pass
118 pass
119 pass
120 pass
121
122 if oldscene != None:
123 # Put the objects back to the current scene
124 #print "Add elements back"
125 for o in current_scene:
126 #print o.tag
127 oldscene.append(o)
128
94 self.collectID() 129 self.collectID()
95 #self.dumpID() 130 #self.dumpID()
96 def collectID(self): 131 def collectID(self):
97 self.ID = {} 132 self.ID = {}
98 root = self.document.getroot() 133 root = self.document.getroot()
199 def setCurrentScene(self,nth): 234 def setCurrentScene(self,nth):
200 for layer in self.layer: 235 for layer in self.layer:
201 for s in layer.scene: 236 for s in layer.scene:
202 if nth >= s.start and nth <= s.end: 237 if nth >= s.start and nth <= s.end:
203 s.node.set("style","") 238 s.node.set("style","")
239 s.node.set("current","%d"%nth)
240 #print "Put the elemenets out"
241 layer.nodes = []
242
243 for o in s.node:
244 #print " ",o.tag
245 layer.nodes.append(o)
246 for o in s.node:
247 s.node.remove(o)
204 else: 248 else:
205 s.node.set("style","display:none") 249 s.node.set("style","display:none")
206 def generate(self): 250 def generate(self):
207 newdoc = deepcopy(self.document) 251 newdoc = deepcopy(self.document)
208 root = newdoc.getroot() 252 root = newdoc.getroot()
213 for l in self.layer: 257 for l in self.layer:
214 # Duplicate all attribute of the layer 258 # Duplicate all attribute of the layer
215 lnode = etree.Element("{http://www.w3.org/2000/svg}g") 259 lnode = etree.Element("{http://www.w3.org/2000/svg}g")
216 for a,v in l.node.attrib.items(): 260 for a,v in l.node.attrib.items():
217 lnode.set(a,v) 261 lnode.set(a,v)
262 for n in l.nodes:
263 lnode.append(n)
218 root.append(lnode) 264 root.append(lnode)
219 for s in l.scene: 265 for s in l.scene:
220 snode = etree.Element("{http://www.w3.org/2000/svg}g") 266 snode = etree.Element("{http://www.w3.org/2000/svg}g")
221 for a,v in s.node.attrib.items(): 267 for a,v in s.node.attrib.items():
222 snode.set(a,v) 268 snode.set(a,v)