view engine/extensions/serializers/xmlanimation.py @ 324:e249fa887259

* Split EventListener into several internal subclasses. This fixes issue with multiple inheritance where each BaseClass.__init__() call would overwrite the previous one. This meant that you could only add EventListener to one type of event managers. * MapView now adds maps and layers to EventListener * LayerEditor should now fetch only NameClash exceptions when creating or editing layers
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 21 Aug 2009 14:39:54 +0000
parents 679ed3e15513
children 48c99636453e
line wrap: on
line source

import fife
from serializers import *

class XMLAnimationLoader(fife.ResourceLoader):
	def __init__(self, imagepool, vfs):
		fife.ResourceLoader.__init__(self)
		self.imagepool = imagepool
		self.vfs = vfs
		self.thisown = 0
		self.filename = ''
		self.node = None

	def loadResource(self, location):
		self.filename = location.getFilename()
		return self.do_load_resource()

	def do_load_resource(self):
		f = self.vfs.open(self.filename)
		f.thisown = 1
		tree = ET.parse(f)
		self.node = tree.getroot()

		animation = fife.Animation()
		common_frame_delay = int(self.node.get('delay', 0))
		x_offset = int(self.node.get('x_offset', 0))
		y_offset = int(self.node.get('y_offset', 0))
		animation.setActionFrame(int(self.node.get('action', 0)))

		frames = self.node.findall('frame')
		if not frames:
			raise InvalidFormat('animation without <frame>s')

		for frame in frames:
			source = frame.get('source')
			if not source:
				raise InvalidFormat('animation without <frame>s')

			frame_x_offset = int(frame.get('x_offset', x_offset))
			frame_y_offset = int(frame.get('y_offset', y_offset))
			frame_delay = int(frame.get('delay', common_frame_delay))

			# xml paths are relative to the directory of the file they're used in.
			path = self.filename.split('/')
			path.pop()
			path.append(str(source))

			image_location = fife.ImageLocation('/'.join(path))
			image_location.setXShift(frame_x_offset)
			image_location.setYShift(frame_y_offset)

			image_index = self.imagepool.addResourceFromLocation(image_location)
			animation.addFrame(fife.ResourcePtr(self.imagepool,image_index), frame_delay)

		animation.thisown = 0
		return animation