Mercurial > MadButterfly
annotate 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 |
rev | line source |
---|---|
233 | 1 #!/usr/bin/python |
2 import inkex | |
3 import pygtk | |
4 import gtk | |
5 from copy import deepcopy | |
6 from lxml import etree | |
7 import random | |
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. | |
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 | |
12 # <g id="layer1"> | |
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.
wycc
parents:
233
diff
changeset
|
13 # <g id="g1222"> |
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.
wycc
parents:
233
diff
changeset
|
14 # </g> |
233 | 15 # <g id="g1234" scene="1"> |
16 # </g> | |
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.
wycc
parents:
233
diff
changeset
|
17 # <g id="g1235" scene="2-7" current="3"> |
233 | 18 # </g> |
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.
wycc
parents:
233
diff
changeset
|
19 # <g id="g1333"/> |
233 | 20 # </g> |
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.
wycc
parents:
233
diff
changeset
|
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. |
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.
wycc
parents:
233
diff
changeset
|
22 # All elements without scene attributes are items in the current scene. |
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.
wycc
parents:
233
diff
changeset
|
23 # Therefore, when we switch scene, we will move all items into the current scene and then move items out from the new scene. |
233 | 24 # |
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 | |
26 # Insert a new key scene | |
27 # Delete a key scene | |
28 # Insert a filled scene | |
29 # Delete a filled scene | |
30 # Select a scene for edit. | |
31 # | |
32 # When user select a scene to edit, we will hide all scenes which is not in the selected scene. For example, if we select scene 4, g1234 will be hidden and g1235 and g1236 will | |
33 # be displayed. | |
34 | |
35 | |
36 | |
37 # Algorithm: | |
38 # | |
39 # We will parse the first two level of the SVG DOM. collect a table of layer and scene. | |
40 # 1. Collect the layer table which will be displayed as the first column of the grid. | |
41 # 2. Get the maximum scene number. This will decide the size of the grid. | |
42 # 3. When F6 is pressed, we will check if this scene has been defined. This can be done by scan all second level group and check if the current scene number is within the | |
43 # range specified by scene field. The function IsSceneDefined(scene) can be used for this purpose. | |
44 # 4. If this is a new scene, we will append a new group which duplication the content of the last scene in the same group. The scene field will contain the number from the | |
45 # last scene number of the last scene to the current scenen number. For example, if the last scene is from 4-7 and the new scene is 10, we will set the scene field as | |
46 # "8-10". | |
47 # 5. If this scene are filled screne, we will split the existing scene into two scenes with the same content. | |
48 | |
49 class Layer: | |
50 def __init__(self,node): | |
51 self.scene = [] | |
52 self.node = node | |
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.
wycc
parents:
233
diff
changeset
|
53 self.nodes=[] |
233 | 54 class Scene: |
55 def __init__(self, node, start,end): | |
56 self.node = node | |
57 self.start = int(start) | |
58 self.end = int(end) | |
59 | |
60 | |
61 class MBScene(inkex.Effect): | |
62 def confirm(self,msg): | |
63 vbox = gtk.VBox() | |
64 vbox.pack_start(gtk.Label(msg)) | |
65 self.button = gtk.Button('OK') | |
66 vbox.pack_start(self.button) | |
67 self.button.connect("clicked", self.onQuit) | |
68 self.window.add(vbox) | |
69 def dumpattr(self,n): | |
70 s = "" | |
71 for a,v in n.attrib.items(): | |
72 s = s + ("%s=%s" % (a,v)) | |
73 return s | |
74 | |
75 def dump(self,node,l=0): | |
76 print " " * l*2,"<", node.tag, self.dumpattr(node),">" | |
77 for n in node: | |
78 self.dump(n,l+1) | |
79 print " " * l * 2,"/>" | |
80 | |
81 def parseScene(self): | |
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.
wycc
parents:
233
diff
changeset
|
82 """ |
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.
wycc
parents:
233
diff
changeset
|
83 In this function, we will collect all items for the current scene and then relocate them back to the appropriate scene object. |
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.
wycc
parents:
233
diff
changeset
|
84 """ |
233 | 85 self.layer = [] |
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.
wycc
parents:
233
diff
changeset
|
86 current_scene = [] |
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.
wycc
parents:
233
diff
changeset
|
87 oldscene = None |
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.
wycc
parents:
233
diff
changeset
|
88 for node in self.document.getroot(): |
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.
wycc
parents:
233
diff
changeset
|
89 if node.tag == '{http://www.w3.org/2000/svg}g': |
233 | 90 #print layer.attrib.get("id") |
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.
wycc
parents:
233
diff
changeset
|
91 lyobj = Layer(node) |
233 | 92 self.layer.append(lyobj) |
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.
wycc
parents:
233
diff
changeset
|
93 for scene in node: |
233 | 94 if scene.tag == '{http://www.w3.org/2000/svg}g': |
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.
wycc
parents:
233
diff
changeset
|
95 s = scene.get("scene") |
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.
wycc
parents:
233
diff
changeset
|
96 if s == None: |
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.
wycc
parents:
233
diff
changeset
|
97 # group without scene is part of the current scene |
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.
wycc
parents:
233
diff
changeset
|
98 current_scene.append(scene) |
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.
wycc
parents:
233
diff
changeset
|
99 continue |
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.
wycc
parents:
233
diff
changeset
|
100 range = s.split('-') |
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.
wycc
parents:
233
diff
changeset
|
101 |
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.
wycc
parents:
233
diff
changeset
|
102 cur = scene.get("current") |
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.
wycc
parents:
233
diff
changeset
|
103 try: |
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.
wycc
parents:
233
diff
changeset
|
104 self.current_scene = int(cur) |
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.
wycc
parents:
233
diff
changeset
|
105 del scene.attrib["current"] |
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.
wycc
parents:
233
diff
changeset
|
106 oldscene = scene |
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.
wycc
parents:
233
diff
changeset
|
107 except: |
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.
wycc
parents:
233
diff
changeset
|
108 pass |
233 | 109 if len(range) == 1: |
110 #print " scene %d" % int(range[0]) | |
111 lyobj.scene.append(Scene(scene,range[0],range[0])) | |
112 elif len(range) == 2: | |
113 #print " scene%d-%d" % (int(range[0]),int(range[1])) | |
114 lyobj.scene.append(Scene(scene,range[0],range[1])) | |
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.
wycc
parents:
233
diff
changeset
|
115 else: |
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.
wycc
parents:
233
diff
changeset
|
116 current_scene.append(scene) |
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.
wycc
parents:
233
diff
changeset
|
117 pass |
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.
wycc
parents:
233
diff
changeset
|
118 pass |
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.
wycc
parents:
233
diff
changeset
|
119 pass |
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.
wycc
parents:
233
diff
changeset
|
120 pass |
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.
wycc
parents:
233
diff
changeset
|
121 |
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.
wycc
parents:
233
diff
changeset
|
122 if oldscene != None: |
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.
wycc
parents:
233
diff
changeset
|
123 # Put the objects back to the current scene |
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.
wycc
parents:
233
diff
changeset
|
124 #print "Add elements back" |
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.
wycc
parents:
233
diff
changeset
|
125 for o in current_scene: |
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.
wycc
parents:
233
diff
changeset
|
126 #print o.tag |
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.
wycc
parents:
233
diff
changeset
|
127 oldscene.append(o) |
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.
wycc
parents:
233
diff
changeset
|
128 |
233 | 129 self.collectID() |
130 #self.dumpID() | |
131 def collectID(self): | |
132 self.ID = {} | |
133 root = self.document.getroot() | |
134 for n in root: | |
135 self.collectID_recursive(n) | |
136 def collectID_recursive(self,node): | |
137 self.ID[node.get("id")] = 1 | |
138 for n in node: | |
139 self.collectID_recursive(n) | |
140 def newID(self): | |
141 while True: | |
142 n = 's%d' % int(random.random()*10000) | |
143 #print "try %s" % n | |
144 if self.ID.has_key(n) == False: | |
145 return n | |
146 def dumpID(self): | |
147 for a,v in self.ID.items(): | |
148 print a | |
149 | |
150 | |
151 def getLayer(self, layer): | |
152 for l in self.layer: | |
153 if l.node.attrib.get("id") == layer: | |
154 return l | |
155 return None | |
156 | |
157 | |
158 def insertKeyScene(self): | |
159 """ | |
160 Insert a new key scene into the stage. If the nth is always a key scene, we will return without changing anything. | |
161 If the nth is a filled scene, we will break the original scene into two parts. If the nth is out of any scene, we will | |
162 append a new scene. | |
163 | |
164 """ | |
165 nth = self.last_cell.nScene | |
166 layer = self.getLayer(self.last_cell.layer) | |
167 x = self.last_cell.nScene | |
168 y = self.last_cell.nLayer | |
169 if layer == None: return | |
170 for i in range(0,len(layer.scene)): | |
171 s = layer.scene[i] | |
172 if nth >= s.start and nth <= s.end: | |
173 if nth == s.start: return | |
174 newscene = Scene(deepcopy(s.node),nth,s.end) | |
175 newscene.node.set("id", self.newID()) | |
176 layer.scene.insert(i+1,newscene) | |
177 layer.scene[i].end = nth-1 | |
178 btn = self.newCell('start.png') | |
179 btn.nScene = nth | |
180 btn.layer = layer | |
181 btn.nLayer = y | |
182 self.grid.remove(self.last_cell) | |
183 self.grid.attach(btn, x,x+1,y,y+1,0,0,0,0) | |
184 return | |
185 if len(layer.scene) > 0: | |
186 last = layer.scene[len(layer.scene)-1] | |
187 for x in range(last.end+1, nth): | |
188 btn = self.newCell('fill.png') | |
189 btn.nScene = x | |
190 btn.layer = layer | |
191 btn.nLayer = y | |
192 self.grid.attach(btn, x, x+1, y , y+1,0,0,0,0) | |
193 last.end = nth-1 | |
194 newscene = Scene(deepcopy(s.node),nth,nth) | |
195 newscene.node.set("id",self.newID()) | |
196 layer.scene.append(newscene) | |
197 btn = self.newCell('start.png') | |
198 x = self.last_cell.nScene | |
199 y = self.last_cell.nLayer | |
200 btn.nScene = nth | |
201 btn.layer = layer | |
202 btn.nLayer = y | |
203 self.grid.attach(btn, x, x+1, y, y+1,0,0,0,0) | |
204 | |
205 | |
206 def removeKeyScene(self): | |
207 nth = self.last_cell.nScene | |
208 layer = self.getLayer(self.last_cell.layer) | |
209 x = self.last_cell.nScene | |
210 y = self.last_cell.nLayer | |
211 # We can not remove the key scene at the first scene | |
212 if nth == 1: return | |
213 for i in range(0,len(layer.scene)): | |
214 s = layer.scene[i] | |
215 if nth == s.start: | |
216 layer.scene[i-1].end = s.end | |
217 layer.scene.remove(s) | |
218 btn = self.newCell('fill.png') | |
219 btn.nScene = nth | |
220 btn.layer = layer | |
221 btn.nLayer = y | |
222 self.grid.attach(btn, x,x+1,y,y+1,0,0,0,0) | |
223 return | |
224 | |
225 def extendScene(self,layer,nth): | |
226 layer = self.getLayer(layer) | |
227 if layer == None: return | |
228 for i in range(0,len(layer.scene)-1): | |
229 s = layer.scene[i] | |
230 if nth >= layer.scene[i].start and nth < layer.scene[i+1].start: | |
231 layer.scene[i].end = nth | |
232 if len(layer.scene) > 0: | |
233 layer.scene[len(layer.scene)-1].end = nth | |
234 def setCurrentScene(self,nth): | |
235 for layer in self.layer: | |
236 for s in layer.scene: | |
237 if nth >= s.start and nth <= s.end: | |
238 s.node.set("style","") | |
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.
wycc
parents:
233
diff
changeset
|
239 s.node.set("current","%d"%nth) |
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.
wycc
parents:
233
diff
changeset
|
240 #print "Put the elemenets out" |
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.
wycc
parents:
233
diff
changeset
|
241 layer.nodes = [] |
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.
wycc
parents:
233
diff
changeset
|
242 |
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.
wycc
parents:
233
diff
changeset
|
243 for o in s.node: |
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.
wycc
parents:
233
diff
changeset
|
244 #print " ",o.tag |
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.
wycc
parents:
233
diff
changeset
|
245 layer.nodes.append(o) |
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.
wycc
parents:
233
diff
changeset
|
246 for o in s.node: |
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.
wycc
parents:
233
diff
changeset
|
247 s.node.remove(o) |
233 | 248 else: |
249 s.node.set("style","display:none") | |
250 def generate(self): | |
251 newdoc = deepcopy(self.document) | |
252 root = newdoc.getroot() | |
253 for n in root: | |
254 if n.tag == '{http://www.w3.org/2000/svg}g': | |
255 root.remove(n) | |
256 | |
257 for l in self.layer: | |
258 # Duplicate all attribute of the layer | |
259 lnode = etree.Element("{http://www.w3.org/2000/svg}g") | |
260 for a,v in l.node.attrib.items(): | |
261 lnode.set(a,v) | |
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.
wycc
parents:
233
diff
changeset
|
262 for n in l.nodes: |
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.
wycc
parents:
233
diff
changeset
|
263 lnode.append(n) |
233 | 264 root.append(lnode) |
265 for s in l.scene: | |
266 snode = etree.Element("{http://www.w3.org/2000/svg}g") | |
267 for a,v in s.node.attrib.items(): | |
268 snode.set(a,v) | |
269 if s.start == s.end: | |
270 snode.set("scene", "%d" % s.start) | |
271 else: | |
272 snode.set("scene","%d-%d" % (s.start,s.end)) | |
273 for n in s.node: | |
274 snode.append(deepcopy(n)) | |
275 lnode.append(snode) | |
276 self.document = newdoc | |
277 def newCell(self,file): | |
278 img = gtk.Image() | |
279 img.set_from_file(file) | |
280 btn = gtk.EventBox() | |
281 btn.add(img) | |
282 btn.connect("button_press_event", self.cellSelect) | |
283 btn.modify_bg(gtk.STATE_NORMAL, btn.get_colormap().alloc_color("gray")) | |
284 return btn | |
285 def showGrid(self): | |
286 max = 0 | |
287 for layer in self.layer: | |
288 for s in layer.scene: | |
289 if s.end > max: | |
290 max = s.end | |
291 max = 50 | |
292 | |
293 self.grid = gtk.Table(len(self.layer)+1, 50) | |
294 self.scrollwin = gtk.ScrolledWindow() | |
295 self.scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) | |
296 self.scrollwin.add_with_viewport(self.grid) | |
297 for i in range(1,max): | |
298 self.grid.attach(gtk.Label('%d'% i), i,i+1,0,1,0,0,0,0) | |
299 for i in range(1,len(self.layer)+1): | |
300 l = self.layer[i-1] | |
301 self.grid.attach(gtk.Label(l.node.get('id')), 0, 1, i, i+1,0,0,10,0) | |
302 for s in l.scene: | |
303 btn = self.newCell('start.png') | |
304 btn.nScene = s.start | |
305 btn.layer = l.node.get('id') | |
306 btn.nLayer = i | |
307 | |
308 self.grid.attach(btn, s.start, s.start+1, i, i+1,0,0,0,0) | |
309 for j in range(s.start+1,s.end+1): | |
310 btn = self.newCell('fill.png') | |
311 self.grid.attach(btn, j, j+1, i , i+1,0,0,0,0) | |
312 btn.modify_bg(gtk.STATE_NORMAL, btn.get_colormap().alloc_color("gray")) | |
313 btn.nScene = j | |
314 btn.layer = l.node.get('id') | |
315 btn.nLayer = i | |
316 if len(l.scene) == 0: | |
317 start = 0 | |
318 else: | |
319 start = l.scene[len(l.scene)-1].end | |
320 for j in range(start,max): | |
321 btn = self.newCell('empty.png') | |
322 self.grid.attach(btn, j+1, j+2,i,i+1,0,0,0,0) | |
323 btn.modify_bg(gtk.STATE_NORMAL, btn.get_colormap().alloc_color("gray")) | |
324 btn.nScene = j+1 | |
325 btn.layer = l.node.get('id') | |
326 btn.nLayer = i | |
327 self.last_cell = None | |
328 def cellSelect(self, cell, data): | |
329 if self.last_cell: | |
330 self.last_cell.modify_bg(gtk.STATE_NORMAL, self.last_cell.get_colormap().alloc_color("gray")) | |
331 | |
332 self.last_cell = cell | |
333 cell.modify_bg(gtk.STATE_NORMAL, cell.get_colormap().alloc_color("green")) | |
334 | |
335 def doEditScene(self,w): | |
336 self.setCurrentScene(self.last_cell.nScene) | |
337 self.generate() | |
338 gtk.main_quit() | |
339 def doInsertKeyScene(self,w): | |
340 self.insertKeyScene() | |
341 self.grid.show_all() | |
342 self.generate() | |
343 | |
344 def doRemoveScene(self,w): | |
345 self.removeKeyScene() | |
346 self.grid.show_all() | |
347 self.generate() | |
348 def addButtons(self,hbox): | |
349 btn = gtk.Button('Edit') | |
350 btn.connect('clicked', self.doEditScene) | |
351 hbox.pack_start(btn) | |
352 btn = gtk.Button('Insert Key') | |
353 btn.connect('clicked',self.doInsertKeyScene) | |
354 hbox.pack_start(btn) | |
355 btn=gtk.Button('Remove Key') | |
356 btn.connect('clicked', self.doRemoveScene) | |
357 hbox.pack_start(btn) | |
358 def effect(self): | |
359 self.parseScene() | |
360 self.showGrid() | |
361 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) | |
362 self.window.connect("destroy", gtk.main_quit) | |
363 self.window.set_position(gtk.WIN_POS_MOUSE) | |
364 vbox = gtk.VBox() | |
365 self.window.add(vbox) | |
366 vbox.add(self.scrollwin) | |
367 self.vbox = vbox | |
368 hbox=gtk.HBox() | |
369 self.addButtons(hbox) | |
370 vbox.add(hbox) | |
371 self.window.set_size_request(600,200) | |
372 self.window.show_all() | |
373 gtk.main() | |
374 | |
375 | |
376 | |
377 | |
378 | |
379 A = MBScene() | |
380 A.affect() | |
381 | |
382 |