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