Mercurial > MadButterfly
annotate pyink/MBScene.py @ 956:167e359c9d5b
Re-indent MBScene.py
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Wed, 17 Nov 2010 14:17:59 +0800 |
parents | 53b0f8dc2284 |
children | 8e3e46c26137 |
rev | line source |
---|---|
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
1 #!/usr/bin/python |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
2 # -*- indent-tabs-mode: t; tab-width: 8; python-indent: 4; -*- |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
3 # vim: sw=4:ts=8:sts=4 |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
4 import pygtk |
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
5 import gtk |
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
6 from copy import deepcopy |
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
7 from lxml import etree |
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
8 import random |
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
9 import traceback |
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
10 |
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
11 # Please refer to http://www.assembla.com/wiki/show/MadButterfly/Inkscape_extention for the designed document. |
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
12 |
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
13 |
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
14 # Algorithm: |
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
15 # |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
16 # We will parse the first two level of the SVG DOM. collect a table of |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
17 # layer and scene. |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
18 # - 1. Collect the layer table which will be displayed as the first |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
19 # column of the grid. |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
20 # - 2. Get the maximum scene number. This will decide the size of the |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
21 # grid. |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
22 # - 3. When F6 is pressed, we will check if this scene has been |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
23 # defined. This can be done by scan all second level group and |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
24 # check if the current scene number is within the range specified |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
25 # by scene field. The function IsSceneDefined(scene) can be used |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
26 # for this purpose. |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
27 # - 4. If this is a new scene, we will append a new group which |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
28 # duplication the content of the last scene in the same |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
29 # group. The scene field will contain the number from the last |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
30 # scene number of the last scene to the current scenen |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
31 # number. For example, if the last scene is from 4-7 and the new |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
32 # scene is 10, we will set the scene field as "8-10". |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
33 # - 5. If this scene are filled screne, we will split the existing |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
34 # scene into two scenes with the same content. |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
35 # |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
36 |
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
37 class Layer: |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
38 def __init__(self,node): |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
39 self.scene = [] |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
40 self.node = node |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
41 self.nodes=[] |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
42 pass |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
43 pass |
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
44 |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
45 class Scene: |
956 | 46 def __init__(self, node, start,end): |
47 self.node = node | |
48 self.start = int(start) | |
49 self.end = int(end) | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
50 pass |
956 | 51 pass |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
52 |
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
53 |
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
54 class MBScene(): |
956 | 55 def __init__(self,desktop,win): |
56 self.desktop = desktop | |
57 self.window = win | |
58 self.layer = [] | |
59 self.layer.append(Layer(None)) | |
60 self.scenemap = None | |
61 pass | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
62 |
956 | 63 def confirm(self,msg): |
64 vbox = gtk.VBox() | |
65 vbox.pack_start(gtk.Label(msg)) | |
66 self.button = gtk.Button('OK') | |
67 vbox.pack_start(self.button) | |
68 self.button.connect("clicked", self.onQuit) | |
69 self.window.add(vbox) | |
70 pass | |
71 | |
72 def dumpattr(self,n): | |
73 s = "" | |
74 for a,v in n.attrib.items(): | |
75 s = s + ("%s=%s" % (a,v)) | |
76 pass | |
77 return s | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
78 |
956 | 79 def dump(self,node,l=0): |
80 print " " * l*2,"<", node.tag, self.dumpattr(node),">" | |
81 for n in node: | |
82 self.dump(n,l+1) | |
83 pass | |
84 print " " * l * 2,"/>" | |
85 pass | |
86 | |
87 def parseMetadata(self,node): | |
88 self.current = 1 | |
89 for n in node.childList(): | |
90 if n.repr.name() == 'ns0:scenes': | |
91 self.scenemap={} | |
92 cur = int(n.repr.attribute("current")) | |
93 self.current = cur | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
94 |
956 | 95 for s in n.childList(): |
96 print '--->',s.repr.name() | |
97 if s.repr.name() == 'ns0:scene': | |
98 try: | |
99 start = int(s.repr.attribute("start")) | |
100 except: | |
101 traceback.print_exc() | |
102 continue | |
103 try: | |
104 end = s.repr.attribute("end") | |
105 if end == None: | |
106 end = start | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
107 pass |
956 | 108 except: |
109 end = start | |
110 pass | |
111 link = s.repr.attribute("ref") | |
112 self.scenemap[link] = [int(start),int(end)] | |
113 print "scene %d to %d" % (self.scenemap[link][0],self.scenemap[link][1]) | |
114 if cur >= start and cur <= end: | |
115 self.currentscene = link | |
116 pass | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
117 pass |
956 | 118 pass |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
119 pass |
956 | 120 pass |
121 pass | |
122 | |
123 | |
124 def parseScene(self): | |
125 """ | |
126 In this function, we will collect all items for the current scene and then relocate them back to the appropriate scene object. | |
127 """ | |
128 self.layer = [] | |
129 self.scenemap = None | |
130 doc = self.desktop.doc().root() | |
131 | |
132 for node in doc.childList(): | |
133 if node.repr.name() == 'svg:metadata': | |
134 self.parseMetadata(node) | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
135 pass |
956 | 136 elif node.repr.name() == 'svg:g': |
137 oldscene = None | |
138 #print layer.attrib.get("id") | |
139 lyobj = Layer(node) | |
140 self.layer.append(lyobj) | |
141 lyobj.current_scene = [] | |
142 for scene in node.childList(): | |
143 if scene.repr.name() == 'svg:g': | |
144 try: | |
145 scmap = self.scenemap[scene.getId()] | |
146 if scmap == None: | |
147 lyobj.current_scene.append(scene) | |
148 continue | |
149 if self.current <= scmap[1] and self.current >= scmap[0]: | |
150 oldscene = scene | |
151 pass | |
152 except: | |
153 lyobj.current_scene.append(scene) | |
154 continue | |
155 | |
156 lyobj.scene.append(Scene(scene,scmap[0],scmap[1])) | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
157 pass |
956 | 158 else: |
159 lyobj.current_scene.append(scene) | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
160 pass |
956 | 161 pass |
162 | |
163 if oldscene != None: | |
164 # Put the objects back to the current scene | |
165 # for o in lyobj.current_scene: | |
166 # print o.tag | |
167 # oldscene.append(o) | |
168 pass | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
169 pass |
956 | 170 pass |
171 | |
172 self.collectID() | |
173 self.dumpID() | |
174 pass | |
175 | |
176 def collectID(self): | |
177 self.ID = {} | |
178 root = self.desktop.doc().root() | |
179 for n in root.childList(): | |
180 self.collectID_recursive(n) | |
181 pass | |
182 pass | |
183 | |
184 def collectID_recursive(self,node): | |
185 self.ID[node.getId()] = 1 | |
186 for n in node.childList(): | |
187 self.collectID_recursive(n) | |
188 pass | |
189 pass | |
190 | |
191 def newID(self): | |
192 while True: | |
193 n = 's%d' % int(random.random()*10000) | |
194 #print "try %s" % n | |
195 if self.ID.has_key(n) == False: | |
196 return n | |
197 pass | |
198 pass | |
199 | |
200 def dumpID(self): | |
201 for a,v in self.ID.items(): | |
202 print a | |
203 pass | |
204 pass | |
205 | |
206 def getLayer(self, layer): | |
207 for l in self.layer: | |
208 if l.node.getId() == layer: | |
209 return l | |
210 pass | |
211 return None | |
212 | |
213 | |
214 def insertKeyScene(self): | |
215 """ | |
216 Insert a new key scene into the stage. If the nth is always a key scene, we will return without changing anything. | |
217 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 | |
218 append a new scene. | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
219 |
956 | 220 """ |
221 nth = self.last_cell.nScene | |
222 layer = self.getLayer(self.last_cell.layer) | |
223 x = self.last_cell.nScene | |
224 y = self.last_cell.nLayer | |
225 if layer == None: return | |
226 for i in range(0,len(layer.scene)): | |
227 s = layer.scene[i] | |
228 if nth >= s.start and nth <= s.end: | |
229 if nth == s.start: return | |
230 newscene = Scene(DuplicateNode(s.node),nth,s.end) | |
231 newscene.node.setId(self.newID()) | |
232 layer.scene.insert(i+1,newscene) | |
233 layer.scene[i].end = nth-1 | |
234 btn = self.newCell('start.png') | |
235 btn.nScene = nth | |
236 btn.layer = layer | |
237 btn.nLayer = y | |
238 self.grid.remove(self.last_cell) | |
239 self.grid.attach(btn, x,x+1,y,y+1,0,0,0,0) | |
240 return | |
241 pass | |
242 | |
243 if len(layer.scene) > 0: | |
244 last = nth | |
245 lastscene = None | |
246 for s in layer.scene: | |
247 if s.end < nth and last < s.end: | |
248 last = s.end | |
249 lastscene = s | |
250 pass | |
251 pass | |
252 | |
253 for x in range(last+1, nth): | |
254 btn = self.newCell('fill.png') | |
255 btn.nScene = x | |
256 btn.layer = layer.node.getId() | |
257 btn.nLayer = y | |
258 self.grid.attach(btn, x, x+1, y , y+1,0,0,0,0) | |
259 pass | |
260 | |
261 if lastscene == None: | |
262 node = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene') | |
263 node.setId(self.newID()) | |
264 newscene = Scene(node,nth,nth) | |
265 else: | |
266 lastscene.end = nth-1 | |
267 newscene = Scene(DuplicateNode(lastscene.node),nth,nth) | |
268 newscene.node.setId(self.newID()) | |
269 pass | |
270 | |
271 layer.scene.append(newscene) | |
272 btn = self.newCell('start.png') | |
273 x = self.last_cell.nScene | |
274 y = self.last_cell.nLayer | |
275 btn.nScene = nth | |
276 btn.layer = layer.node.getId() | |
277 btn.nLayer = y | |
278 self.grid.attach(btn, x, x+1, y, y+1,0,0,0,0) | |
279 else: | |
280 # This is the first scene in the layer | |
281 node = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene') | |
282 node.repr.setId(self.newID()) | |
283 newscene = Scene(node,nth,nth) | |
284 layer.scene.append(newscene) | |
285 btn = self.newCell('start.png') | |
286 btn.nScene = nth | |
287 btn.layer = layer.node.getId() | |
288 btn.nLayer = y | |
289 self.grid.attach(btn, x, x+1, y, y+1,0,0,0,0) | |
290 pass | |
291 pass | |
292 | |
293 | |
294 def removeKeyScene(self): | |
295 nth = self.last_cell.nScene | |
296 try: | |
297 layer = self.getLayer(self.last_cell.layer) | |
298 except: | |
299 return | |
300 x = self.last_cell.nScene | |
301 y = self.last_cell.nLayer | |
302 for i in range(0,len(layer.scene)): | |
303 s = layer.scene[i] | |
304 if nth == s.start: | |
305 if i == 0: | |
306 for j in range(s.start,s.end+1): | |
307 btn = self.newCell('empty.png') | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
308 btn.nScene = nth |
956 | 309 btn.layer = layer |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
310 btn.nLayer = y |
956 | 311 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0) |
312 pass | |
313 layer.scene.remove(s) | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
314 else: |
956 | 315 if s.start == layer.scene[i-1].end+1: |
316 # If the start of the delete scene segment is the end of the last scene segmenet, convert all scenes in the deleted | |
317 # scene segmenet to the last one | |
318 layer.scene[i-1].end = s.end | |
319 layer.scene.remove(s) | |
320 btn = self.newCell('fill.png') | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
321 |
956 | 322 btn.nScene = nth |
323 btn.layer = layer | |
324 btn.nLayer = y | |
325 self.grid.attach(btn, x,x+1,y,y+1,0,0,0,0) | |
326 else: | |
327 # Convert all scenes into empty cell | |
328 layer.scene.remove(s) | |
329 for j in range(s.start,s.end+1): | |
330 btn = self.newCell('empty.png') | |
331 btn.nScene = nth | |
332 btn.layer = layer | |
333 btn.nLayer = y | |
334 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0) | |
335 pass | |
336 pass | |
337 pass | |
338 return | |
339 pass | |
340 pass | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
341 |
956 | 342 def extendScene(self): |
343 nth = self.last_cell.nScene | |
344 try: | |
345 layer = self.getLayer(self.last_cell.layer) | |
346 except: | |
347 traceback.print_exc() | |
348 return | |
349 x = self.last_cell.nScene | |
350 y = self.last_cell.nLayer | |
351 if layer == None: | |
352 return | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
353 |
956 | 354 for i in range(0,len(layer.scene)-1): |
355 s = layer.scene[i] | |
356 if nth >= layer.scene[i].start and nth <= layer.scene[i].end: | |
357 return | |
358 pass | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
359 |
956 | 360 for i in range(0,len(layer.scene)-1): |
361 s = layer.scene[i] | |
362 if nth >= layer.scene[i].start and nth < layer.scene[i+1].start: | |
363 for j in range(layer.scene[i].end+1, nth+1): | |
364 btn = self.newCell('fill.png') | |
365 btn.nScene = nth | |
366 btn.nLayer = y | |
367 btn.layer = self.last_cell.layer | |
368 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0) | |
369 layer.scene[i].end = nth | |
370 return | |
371 pass | |
372 if len(layer.scene) > 0 and nth > layer.scene[len(layer.scene)-1].end: | |
373 for j in range(layer.scene[len(layer.scene)-1].end+1, nth+1): | |
374 btn = self.newCell('fill.png') | |
375 btn.nScene = nth | |
376 btn.nLayer = y | |
377 btn.layer = self.last_cell.layer | |
378 self.grid.attach(btn, j,j+1,y,y+1,0,0,0,0) | |
379 pass | |
380 layer.scene[len(layer.scene)-1].end = nth | |
381 pass | |
382 pass | |
383 pass | |
384 | |
385 def setCurrentScene(self,nth): | |
386 self.current = nth | |
387 for layer in self.layer: | |
388 for s in layer.scene: | |
389 if nth >= s.start and nth <= s.end: | |
390 s.node.repr.setAttribute("style","",True) | |
391 # print "Put the elemenets out" | |
392 layer.nodes = [] | |
393 | |
394 # for o in s.node: | |
395 # print " ",o.tag | |
396 # layer.nodes.append(o) | |
397 # for o in s.node: | |
398 # s.node.remove(o) | |
399 else: | |
400 s.node.repr.setAttribute("style","display:none",True) | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
401 pass |
956 | 402 pass |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
403 pass |
956 | 404 pass |
405 pass | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
406 |
956 | 407 def generate(self): |
408 newdoc = deepcopy(self.document) | |
409 root = newdoc.getroot() | |
410 has_scene = False | |
411 for n in root: | |
412 if n.tag == '{http://www.w3.org/2000/svg}metadata': | |
413 for nn in n: | |
414 if nn.tag == '{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scenes': | |
415 nn.clear() | |
416 nn.set("current", "%d" % self.current) | |
417 scenes = [] | |
418 for l in self.layer: | |
419 for s in l.scene: | |
420 id = s.node.get("id") | |
421 scene = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene') | |
422 scene.set("ref", id) | |
423 if s.start == s.end: | |
424 scene.set("start", "%d" % s.start) | |
425 else: | |
426 scene.set("start", "%d" % s.start) | |
427 scene.set("end", "%d" % s.end) | |
428 pass | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
429 |
956 | 430 scenes.append(scene) |
431 pass | |
432 pass | |
433 for s in scenes: | |
434 nn.append(s) | |
435 pass | |
436 has_scene = True | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
437 pass |
956 | 438 pass |
439 if has_scene == False: | |
440 scenes = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scenes') | |
441 scenes.set("current","%d" % self.current) | |
442 for l in self.layer: | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
443 for s in l.scene: |
956 | 444 scene = etree.Element('{http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd}scene') |
445 scene.set("ref", s.node.get("id")) | |
446 if s.start == s.end: | |
447 scene.set("start", "%d" % s.start) | |
448 else: | |
449 scene.set("start", "%d" % s.start) | |
450 scene.set("end", "%d" % s.end) | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
451 pass |
956 | 452 scenes.append(scene) |
453 pass | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
454 pass |
956 | 455 n.append(scenes) |
456 pass | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
457 pass |
956 | 458 if n.tag == '{http://www.w3.org/2000/svg}g': |
459 root.remove(n) | |
460 pass | |
461 pass | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
462 |
956 | 463 for l in self.layer: |
464 # Duplicate all attribute of the layer | |
465 lnode = etree.Element("{http://www.w3.org/2000/svg}g") | |
466 for a,v in l.node.attrib.items(): | |
467 lnode.set(a,v) | |
468 pass | |
469 for n in l.nodes: | |
470 lnode.append(n) | |
471 pass | |
472 root.append(lnode) | |
473 for s in l.scene: | |
474 snode = etree.Element("{http://www.w3.org/2000/svg}g") | |
475 for a,v in s.node.attrib.items(): | |
476 snode.set(a,v) | |
477 pass | |
478 for n in s.node: | |
479 snode.append(deepcopy(n)) | |
480 pass | |
481 lnode.append(snode) | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
482 pass |
956 | 483 pass |
484 self.document = newdoc | |
485 pass | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
486 |
956 | 487 def newCell(self,file): |
488 img = gtk.Image() | |
489 img.set_from_file(file) | |
490 btn = gtk.EventBox() | |
491 btn.add(img) | |
492 btn.connect("button_press_event", self.cellSelect) | |
493 btn.modify_bg(gtk.STATE_NORMAL, btn.get_colormap().alloc_color("gray")) | |
494 return btn | |
495 | |
496 def showGrid(self): | |
497 max = 0 | |
498 for layer in self.layer: | |
499 for s in layer.scene: | |
500 if s.end > max: | |
501 max = s.end | |
502 pass | |
503 pass | |
504 pass | |
505 max = 50 | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
506 |
956 | 507 self.grid = gtk.Table(len(self.layer)+1, 50) |
508 self.scrollwin = gtk.ScrolledWindow() | |
509 self.scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) | |
510 self.scrollwin.add_with_viewport(self.grid) | |
511 self.scrollwin.set_size_request(-1,150) | |
512 for i in range(1,max): | |
513 self.grid.attach(gtk.Label('%d'% i), i,i+1,0,1,0,0,0,0) | |
514 pass | |
515 for i in range(1,len(self.layer)+1): | |
516 print "Layer", i | |
517 l = self.layer[i-1] | |
518 #self.grid.attach(gtk.Label(l.node.get('{http://www.inkscape.org/namespaces/inkscape}label')), 0, 1, i, i+1,0,0,10,0) | |
519 for s in l.scene: | |
520 btn = self.newCell('start.png') | |
521 btn.nScene = s.start | |
522 btn.layer = l.node.getId() | |
523 btn.nLayer = i | |
524 | |
525 self.grid.attach(btn, s.start, s.start+1, i, i+1,0,0,0,0) | |
526 for j in range(s.start+1,s.end+1): | |
527 btn = self.newCell('fill.png') | |
528 self.grid.attach(btn, j, j+1, i , i+1,0,0,0,0) | |
529 btn.modify_bg(gtk.STATE_NORMAL, btn.get_colormap().alloc_color("gray")) | |
530 btn.nScene = j | |
531 btn.layer = l.node.getId() | |
532 btn.nLayer = i | |
533 pass | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
534 pass |
956 | 535 if len(l.scene) == 0: |
536 start = 0 | |
537 else: | |
538 start = l.scene[len(l.scene)-1].end | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
539 pass |
956 | 540 |
541 for j in range(start,max): | |
542 btn = self.newCell('empty.png') | |
543 self.grid.attach(btn, j+1, j+2,i,i+1,0,0,0,0) | |
544 btn.modify_bg(gtk.STATE_NORMAL, btn.get_colormap().alloc_color("gray")) | |
545 btn.nScene = j+1 | |
546 btn.layer = l.node.getId() | |
547 btn.nLayer = i | |
548 pass | |
549 pass | |
550 self.last_cell = None | |
551 pass | |
552 | |
553 def cellSelect(self, cell, data): | |
554 if self.last_cell: | |
555 self.last_cell.modify_bg(gtk.STATE_NORMAL, self.last_cell.get_colormap().alloc_color("gray")) | |
556 pass | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
557 |
956 | 558 self.last_cell = cell |
559 cell.modify_bg(gtk.STATE_NORMAL, cell.get_colormap().alloc_color("green")) | |
560 pass | |
561 | |
562 def doEditScene(self,w): | |
563 self.setCurrentScene(self.last_cell.nScene) | |
564 pass | |
565 | |
566 def doInsertKeyScene(self,w): | |
567 # self.insertKeyScene() | |
568 # self.grid.show_all() | |
569 return | |
570 | |
571 def doRemoveScene(self,w): | |
572 # self.removeKeyScene() | |
573 # self.grid.show_all() | |
574 # self.generate() | |
575 return | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
576 |
956 | 577 |
578 def doExtendScene(self,w): | |
579 self.extendScene() | |
580 self.grid.show_all() | |
581 pass | |
582 | |
583 def addButtons(self,hbox): | |
584 btn = gtk.Button('Edit') | |
585 btn.connect('clicked', self.doEditScene) | |
586 hbox.pack_start(btn,expand=False,fill=False) | |
587 btn = gtk.Button('Insert Key') | |
588 btn.connect('clicked',self.doInsertKeyScene) | |
589 hbox.pack_start(btn,expand=False,fill=False) | |
590 btn=gtk.Button('Remove Key') | |
591 btn.connect('clicked', self.doRemoveScene) | |
592 hbox.pack_start(btn,expand=False,fill=False) | |
593 btn=gtk.Button('Extend scene') | |
594 btn.connect('clicked', self.doExtendScene) | |
595 hbox.pack_start(btn,expand=False,fill=False) | |
596 pass | |
597 | |
598 def onQuit(self, event): | |
599 self.OK = False | |
600 gtk.main_quit() | |
601 pass | |
602 | |
603 def onOK(self,event): | |
604 self.OK = True | |
605 gtk.main_quit() | |
606 pass | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
607 |
956 | 608 def onConfirmDelete(self): |
609 if self.scenemap == None: | |
610 vbox = gtk.VBox(False,0) | |
611 vbox.pack_start(gtk.Label('Convert the SVG into a MadButterfly SVG file. All current element will be delted')) | |
612 hbox = gtk.HBox(False,0) | |
613 self.button = gtk.Button('OK') | |
614 hbox.pack_start(self.button,expand=False,fill=False) | |
615 self.button.connect('clicked', self.onOK) | |
616 self.button = gtk.Button('Cancel') | |
617 hbox.pack_start(self.button,expand=False,fill=False) | |
618 self.button.connect("clicked", self.onQuit) | |
619 vbox.pack_start(hbox,expand=False,fill=False) | |
620 self.window.add(vbox) | |
621 self.window.show_all() | |
622 gtk.main() | |
623 self.window.remove(vbox) | |
624 pass | |
625 pass | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
626 |
956 | 627 def show(self): |
628 self.OK = True | |
629 self.parseScene() | |
630 self.showGrid() | |
631 vbox = gtk.VBox(False,0) | |
632 self.desktop.getToplevel().child.child.pack_end(vbox,expand=False) | |
633 self.window = vbox | |
634 # self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) | |
635 # self.window.connect("destroy", gtk.main_quit) | |
636 # self.window.set_position(gtk.WIN_POS_MOUSE) | |
637 if self.scenemap == None: | |
638 self.onConfirmDelete() | |
639 pass | |
640 if self.OK: | |
641 vbox = gtk.VBox(False,0) | |
642 self.window.pack_start(vbox,expand=False) | |
643 vbox.pack_start(self.scrollwin,expand=False) | |
644 self.vbox = vbox | |
645 hbox=gtk.HBox(False,0) | |
646 self.addButtons(hbox) | |
647 vbox.pack_start(hbox,expand=False) | |
648 else: | |
649 return | |
941
9ba94c577a6f
Add scene editor. This vewrsion can only switch scene. It can not change the scene yet.
wycc
parents:
diff
changeset
|
650 |
956 | 651 # self.window.set_size_request(600,200) |
652 | |
653 self.window.show_all() | |
955
53b0f8dc2284
Add tailing 'pass' commands to close indents
Thinker K.F. Li <thinker@codemud.net>
parents:
943
diff
changeset
|
654 pass |
956 | 655 pass |