comparison engine/extensions/savers.py @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 1fa74d3229d5
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 import os
2 from xml.sax.saxutils import XMLGenerator
3 from xml.sax.xmlreader import AttributesNSImpl
4 from serializers import *
5
6 import fife
7
8 MAPFORMAT = '1.0'
9
10 fileExtensions = ('xml',)
11 class ModelSaver:
12
13 def __init__(self, filepath, engine, state = 0, datastate = 0):
14 self.SModel, self.SMap, self.SLayer, self.SInstances, self.SObject, self.SAction = range(6)
15
16 self.engine = engine
17 self.model = self.engine.getModel()
18 self.pool = self.engine.getImagePool()
19 self.anim_pool = self.engine.getAnimationPool()
20
21 if (state):
22 self.state = state
23 if (state == self.SMap):
24 self.map = datastate
25 else:
26 assert 0, "Invalid initialization state."
27 else:
28 self.state = self.SModel
29
30 self.stack = [ self.SModel ]
31 self.datastack = [ ]
32
33 self.file = open(filepath, 'w')
34 self.xmlout = XMLGenerator(self.file, 'ascii')
35 self.xmlout.startDocument()
36
37 self.indent_level = ''
38
39 self.nspace = None
40
41 def startElement(self, name, attrs):
42 self.file.write(self.indent_level)
43 self.xmlout.startElementNS((None, name), name, attrs)
44 self.file.write('\n')
45 self.indent_level = self.indent_level + '\t'
46
47 def endElement(self, name):
48 self.indent_level = self.indent_level[0:(len(self.indent_level) - 1)]
49 self.file.write(self.indent_level)
50 self.xmlout.endElementNS((None, name), name)
51 self.file.write('\n')
52
53 def write_map(self, map, importList):
54 assert self.state == self.SModel, "Declaration of <map> not at the top level."
55
56 attr_vals = {
57 (None, 'id'): map.getId(),
58 (None, 'format'): MAPFORMAT,
59 }
60 attr_names = {
61 (None, 'id'): 'id',
62 (None, 'format'): 'format',
63 }
64 attrs = AttributesNSImpl(attr_vals, attr_names)
65 self.startElement('map', attrs)
66 self.state = self.SMap
67 self.write_imports(map, importList)
68 self.write_layers(map)
69 self.write_camera(map)
70 self.endElement('map')
71
72 def write_imports(self, map, importList):
73 for importdir in importList:
74 self.write_importdir(root_subfile(map.getResourceFile(), importdir))
75
76 imports = []
77 for layer in map.getLayers():
78 for instance in layer.getInstances():
79 file = instance.getObject().getResourceFile()
80 if not (file in imports):
81 if not self.have_superdir(file, importList):
82 imports.append(file)
83 self.write_import(root_subfile(map.getResourceFile(), file))
84
85 def have_superdir(self, file, importList):
86 '''returns true, if file is in directories given in importList'''
87 for dir in importList:
88 have = True
89 for test in zip(dir.split(os.path.sep), file.split(os.path.sep)):
90 if test[0] != test[1]: have = False
91 if have: return True
92
93 return False
94
95 def write_import(self, file):
96 attr_vals = {
97 (None, 'file'): file,
98 }
99 attr_names = {
100 (None, 'file'): 'file',
101 }
102 attrs = AttributesNSImpl(attr_vals, attr_names)
103 self.file.write(self.indent_level)
104 self.xmlout.startElementNS((None, 'import'), 'import', attrs)
105 self.xmlout.endElementNS((None, 'import'), 'import')
106 self.file.write('\n')
107
108 def write_importdir(self, dir):
109 attr_vals = {
110 (None, 'dir'): dir,
111 }
112 attr_names = {
113 (None, 'dir'): 'dir',
114 }
115 attrs = AttributesNSImpl(attr_vals, attr_names)
116 self.file.write(self.indent_level)
117 self.xmlout.startElementNS((None, 'import'), 'import', attrs)
118 self.xmlout.endElementNS((None, 'import'), 'import')
119 self.file.write('\n')
120
121 def pathing_val_to_str(self, val):
122 if val == fife.CELL_EDGES_AND_DIAGONALS:
123 return "cell_edges_and_diagonals"
124 if val == fife.FREEFORM:
125 return "freeform"
126 return "cell_edges_only"
127
128 def write_layers(self, map):
129 for layer in map.getLayers():
130 cellgrid = layer.getCellGrid()
131 attr_vals = {
132 (None, 'id'): layer.getId(),
133 (None, 'grid_type'): cellgrid.getType(),
134 (None, 'x_scale'): str(cellgrid.getXScale()),
135 (None, 'y_scale'): str(cellgrid.getYScale()),
136 (None, 'rotation'): str(cellgrid.getRotation()),
137 (None, 'x_offset'): str(cellgrid.getXShift()),
138 (None, 'y_offset'): str(cellgrid.getYShift()),
139 (None, 'pathing'): self.pathing_val_to_str(layer.getPathingStrategy()),
140 }
141 attr_names = {
142 (None, 'id'): 'id',
143 (None, 'grid_type'): 'grid_type',
144 (None, 'scaling'): 'scaling',
145 (None, 'rotation'): 'rotation',
146 (None, 'x_offset'): 'x_offset',
147 (None, 'y_offset'): 'y_offset',
148 (None, 'pathing'): 'pathing',
149 }
150 attrs = AttributesNSImpl(attr_vals, attr_names)
151 self.startElement('layer', attrs)
152 self.write_instances(layer)
153 self.endElement('layer')
154
155 def write_instances(self, layer):
156 attrs = AttributesNSImpl({}, {})
157 self.startElement('instances', attrs)
158
159 for inst in layer.getInstances():
160 position = inst.getLocationRef().getExactLayerCoordinates()
161 attr_vals = {
162 (None, 'o'): inst.getObject().getId(),
163 (None, 'x'): str(position.x),
164 (None, 'y'): str(position.y),
165 (None, 'z'): str(position.z),
166 (None, 'r'): str(inst.getRotation()),
167 }
168 attr_names = {
169 (None, 'o'): 'o',
170 (None, 'x'): 'x',
171 (None, 'y'): 'y',
172 (None, 'z'): 'z',
173 (None, 'r'): 'r',
174 }
175
176 nspace = inst.getObject().getNamespace()
177 if nspace != self.nspace:
178 attr_vals[(None, 'ns')] = inst.getObject().getNamespace()
179 attr_names[(None, 'ns')] = 'ns'
180 self.nspace = nspace
181
182 instId = inst.getId()
183 if instId:
184 attr_vals[(None, 'id')] = inst.getId()
185 attr_names[(None, 'id')] = 'id'
186
187 attrs = AttributesNSImpl(attr_vals, attr_names)
188 self.file.write(self.indent_level)
189 self.xmlout.startElementNS((None, 'i'), 'i', attrs)
190 self.xmlout.endElementNS((None, 'i'), 'i')
191 self.file.write('\n')
192
193 self.endElement('instances')
194
195 # Save the linked camera of a map.
196 def write_camera( self, map ):
197 cameralist = self.engine.getView().getCameras()
198
199 for cam in cameralist:
200 if cam.getLocationRef().getMap().getId() == map.getId():
201 celldimensions = cam.getCellImageDimensions()
202 viewport = cam.getViewPort();
203
204 attr_names = {
205 (None, 'id'): 'id',
206 (None, 'zoom'): 'zoom',
207 (None, 'tilt'): 'tile',
208 (None, 'rotation'): 'rotation',
209 (None, 'ref_layer_id'): 'ref_layer_id',
210 (None, 'ref_cell_width'): 'ref_cell_width',
211 (None, 'ref_cell_height'): 'ref_cell_height',
212 (None, 'viewport'): 'viewport',
213 }
214
215 attr_vals = {
216 (None, 'id'): cam.getId(),
217 (None, 'zoom'): str( cam.getZoom()),
218 (None, 'tilt'): str( cam.getTilt()),
219 (None, 'rotation'): str( cam.getRotation()),
220 (None, 'ref_layer_id'): cam.getLocation().getLayer().getId(),
221 (None, 'ref_cell_width'): str( celldimensions.x ),
222 (None, 'ref_cell_height'): str( celldimensions.y ),
223 (None, 'viewport'): '%d,%d,%d,%d' % (viewport.x, viewport.y, viewport.w, viewport.h),
224 }
225
226 attrs = AttributesNSImpl( attr_vals, attr_names )
227 self.startElement( 'camera', attrs );
228 self.endElement( 'camera' );
229
230 def flush(self):
231 self.xmlout.endDocument()
232 self.file.close()
233
234 def saveMapFile(path, engine, map, importList=[]):
235 map.setResourceFile(path)
236 writer = ModelSaver(path, engine)
237 writer.write_map(map, importList)