annotate inkscape/MB_Frame.py @ 233:ec62453bbb2b

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