view engine/extensions/serializers/xmlanimation.py @ 151:afcd83f7fac8

Backwards compatibility module fife_compat. Import and you'll get the old interface. This is just a start to make small API changes smoother.
author phoku@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 11 Oct 2008 12:25:53 +0000
parents 6e7d228def30
children 679ed3e15513
line wrap: on
line source

import fife
import fife_compat
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.addFrame(self.imagepool.get(image_index), frame_delay)
			#print "...",image_index,image_location.getFilename()

		animation.thisown = 0
		return animation