Mercurial > fife-parpg
comparison engine/python/fife/extensions/serializers/xmlanimation.py @ 378:64738befdf3b
bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author | vtchill@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Mon, 11 Jan 2010 23:34:52 +0000 |
parents | |
children | e3140f01749d |
comparison
equal
deleted
inserted
replaced
377:fe6fb0e0ed23 | 378:64738befdf3b |
---|---|
1 # -*- coding: utf-8 -*- | |
2 | |
3 # #################################################################### | |
4 # Copyright (C) 2005-2009 by the FIFE team | |
5 # http://www.fifengine.de | |
6 # This file is part of FIFE. | |
7 # | |
8 # FIFE is free software; you can redistribute it and/or | |
9 # modify it under the terms of the GNU Lesser General Public | |
10 # License as published by the Free Software Foundation; either | |
11 # version 2.1 of the License, or (at your option) any later version. | |
12 # | |
13 # This library is distributed in the hope that it will be useful, | |
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 # Lesser General Public License for more details. | |
17 # | |
18 # You should have received a copy of the GNU Lesser General Public | |
19 # License along with this library; if not, write to the | |
20 # Free Software Foundation, Inc., | |
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
22 # #################################################################### | |
23 | |
24 from fife import fife | |
25 from fife.extensions.serializers import * | |
26 | |
27 class XMLAnimationLoader(fife.ResourceLoader): | |
28 def __init__(self, imagepool, vfs): | |
29 fife.ResourceLoader.__init__(self) | |
30 self.imagepool = imagepool | |
31 self.vfs = vfs | |
32 self.thisown = 0 | |
33 self.filename = '' | |
34 self.node = None | |
35 | |
36 def loadResource(self, location): | |
37 self.filename = location.getFilename() | |
38 return self.do_load_resource() | |
39 | |
40 def do_load_resource(self): | |
41 f = self.vfs.open(self.filename) | |
42 f.thisown = 1 | |
43 tree = ET.parse(f) | |
44 self.node = tree.getroot() | |
45 | |
46 animation = fife.Animation() | |
47 common_frame_delay = int(self.node.get('delay', 0)) | |
48 x_offset = int(self.node.get('x_offset', 0)) | |
49 y_offset = int(self.node.get('y_offset', 0)) | |
50 animation.setActionFrame(int(self.node.get('action', 0))) | |
51 | |
52 frames = self.node.findall('frame') | |
53 if not frames: | |
54 raise InvalidFormat('animation without <frame>s') | |
55 | |
56 for frame in frames: | |
57 source = frame.get('source') | |
58 if not source: | |
59 raise InvalidFormat('animation without <frame>s') | |
60 | |
61 frame_x_offset = int(frame.get('x_offset', x_offset)) | |
62 frame_y_offset = int(frame.get('y_offset', y_offset)) | |
63 frame_delay = int(frame.get('delay', common_frame_delay)) | |
64 | |
65 # xml paths are relative to the directory of the file they're used in. | |
66 path = self.filename.split('/') | |
67 path.pop() | |
68 path.append(str(source)) | |
69 | |
70 image_location = fife.ImageLocation('/'.join(path)) | |
71 image_location.setXShift(frame_x_offset) | |
72 image_location.setYShift(frame_y_offset) | |
73 | |
74 image_index = self.imagepool.addResourceFromLocation(image_location) | |
75 animation.addFrame(fife.ResourcePtr(self.imagepool,image_index), frame_delay) | |
76 | |
77 animation.thisown = 0 | |
78 return animation | |
79 |