changeset 141:5e85737281bc

- some modification to loaders and serializers/xmlmap, now you can set a callback which informs you about finished parts of the maploading process - this can be used to prepare other parts of your client (e. g. load ingame gui) or to show a maploading progress bar USAGE (example): - map = loadMapFile(mapfile, engine, my_callback) - def mycallback(msg, percent): print msg; print percent
author chewie@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 03 Oct 2008 12:14:39 +0000
parents 3fddb45c0304
children 816b17db2bec
files engine/extensions/loaders.py engine/extensions/serializers/xmlmap.py
diffstat 2 files changed, 72 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/engine/extensions/loaders.py	Sun Sep 28 11:12:36 2008 +0000
+++ b/engine/extensions/loaders.py	Fri Oct 03 12:14:39 2008 +0000
@@ -6,14 +6,24 @@
 from serializers.xmlmap import XMLMapLoader
 from serializers import WrongFileType, NameClash
 
+from serializers.xmlobject import XMLObjectLoader
+
 fileExtensions = ('xml',)
 
-def loadMapFile(path, engine, content = ''):
-	map_loader = XMLMapLoader(engine)
+def loadMapFile(path, engine, callback=None):
+	""" load map file and get (an optional) callback if major stuff is done:
+	- map creation
+	- parsed imports
+	- parsed layers 
+	- parsed cameras
+	the callback will send both a string and a float (which shows
+	the overall process), callback(string, float)
+		
+	@return	map	: map object
+	"""
+	map_loader = XMLMapLoader(engine, callback)
 	return map_loader.loadResource(fife.ResourceLocation(path))
 
-from serializers.xmlobject import XMLObjectLoader
-
 def loadImportFile(path, engine):
 	object_loader = XMLObjectLoader(engine.getImagePool(), engine.getAnimationPool(), engine.getModel(), engine.getVFS())
 	res = None
--- a/engine/extensions/serializers/xmlmap.py	Sun Sep 28 11:12:36 2008 +0000
+++ b/engine/extensions/serializers/xmlmap.py	Fri Oct 03 12:14:39 2008 +0000
@@ -10,9 +10,21 @@
 FORMAT = '1.0'
 
 class XMLMapLoader(fife.ResourceLoader):
-	def __init__(self, engine):
+	def __init__(self, engine, callback):
+		""" The XMLMapLoader parses the xml map using several section. 
+		Each section fires a callback (if given) which can e. g. be
+		used to show a progress bar.
+		
+		The callback sends two values, a string and a float (which shows
+		the overall process): callback(string, float)
+			
+		@param	engine	:	a pointer to fife.engine
+		@param	callback:	a callback with two arguments, optional
+		"""
 		fife.ResourceLoader.__init__(self)
 		self.thisown = 0
+		
+		self.callback = callback
 
 		self.engine = engine
 		self.vfs = self.engine.getVFS()
@@ -33,7 +45,7 @@
 		f.thisown = 1
 		tree = ET.parse(f)
 		root = tree.getroot()
-
+			
 		map = self.parse_map(root)
 		return map
 
@@ -57,13 +69,22 @@
 		# xml-specific directory imports. This is used by xml savers.
 		self.map.importDirs = []
 
+		if self.callback is not None:
+			self.callback('created map', float(0.25) )
+
 		self.parse_imports(mapelt, self.map)
+		
 		self.parse_layers(mapelt, self.map)	
+		
 		self.parse_cameras(mapelt, self.map)
 
 		return self.map
 
 	def parse_imports(self, mapelt, map):
+		if self.callback is not None:		
+			tmplist = mapelt.findall('import')
+			i = float(0)
+		
 		for item in mapelt.findall('import'):
 			file = item.get('file')
 			if file:
@@ -81,8 +102,21 @@
 				map.importDirs.append(dir)
 			else:
 				print 'Empty import statement?'
+				
+			if self.callback is not None:
+				i += 1				
+				self.callback('loaded imports', float( i / float(len(tmplist)) * 0.25 + 0.25 ) )
+				
+		# cleanup
+		if self.callback is not None:
+			del tmplist
+			del i
 
 	def parse_layers(self, mapelt, map):
+		if self.callback is not None:		
+			tmplist = mapelt.findall('layer')
+			i = float(0)
+
 		for layer in mapelt.findall('layer'):
 			id = layer.get('id')
 			grid_type = layer.get('grid_type')
@@ -130,6 +164,15 @@
 
 			self.parse_instances(layer, layer_obj)
 
+			if self.callback is not None:
+				i += 1
+				self.callback('loaded layer :' + str(id), float( i / float(len(tmplist)) * 0.25 + 0.5 ) )
+
+		# cleanup
+		if self.callback is not None:
+			del tmplist
+			del i
+
 	def parse_instances(self, layerelt, layer):
 		instelt = layerelt.find('instances')
 
@@ -214,6 +257,10 @@
 				inst.act('default', target, True)
 
 	def parse_cameras(self, mapelt, map):
+		if self.callback is not None:		
+			tmplist = mapelt.findall('camera')
+			i = float(0)
+
 		for camera in mapelt.findall('camera'):
 			id = camera.get('id')
 			zoom = camera.get('zoom')
@@ -245,3 +292,12 @@
 				cam.setZoom(float(zoom))
 			except fife.Exception, e:
 				print e.getMessage()
+				
+			if self.callback is not None:
+				i += 1
+				self.callback('loaded camera: ' +  str(id), float( i / len(tmplist) * 0.25 + 0.75 ) )	
+			
+		# cleanup
+		if self.callback is not None:
+			del tmplist
+			del i