comparison engine/python/fife/extensions/serializers/xmlobject.py @ 661:e3140f01749d

* Merged the light branch back into trunk. * Modified the demos so they work with the new loaders and setting.
author helios2000@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 05 Nov 2010 15:21:10 +0000
parents 64738befdf3b
children
comparison
equal deleted inserted replaced
660:b0733d998d0f 661:e3140f01749d
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2
3 # #################################################################### 2 # ####################################################################
4 # Copyright (C) 2005-2009 by the FIFE team 3 # Copyright (C) 2005-2010 by the FIFE team
5 # http://www.fifengine.de 4 # http://www.fifengine.de
6 # This file is part of FIFE. 5 # This file is part of FIFE.
7 # 6 #
8 # FIFE is free software; you can redistribute it and/or 7 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public 8 # modify it under the terms of the GNU Lesser General Public
19 # License along with this library; if not, write to the 18 # License along with this library; if not, write to the
20 # Free Software Foundation, Inc., 19 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # #################################################################### 21 # ####################################################################
23 22
23 """ submodule for xml map parsing """
24
24 from fife import fife 25 from fife import fife
25 from fife.extensions.serializers import * 26
26 from fife.extensions.fife_utils import * 27 from fife.extensions.serializers import ET
28 from fife.extensions.serializers import SerializerError, InvalidFormat
29 from fife.extensions.serializers import NameClash, NotFound, WrongFileType
27 30
28 class ObjectLocation(fife.ResourceLocation): 31 class ObjectLocation(fife.ResourceLocation):
32 """
33
34
35 """
29 def __init__(self, file, node=None): 36 def __init__(self, file, node=None):
37 """
38
39 """
30 fife.ResourceLocation.__init__(self, file) 40 fife.ResourceLocation.__init__(self, file)
31 self.node = node 41 self.node = node
32 42
33 class XMLObjectLoader(fife.ResourceLoader): 43 class XMLObjectLoader(fife.ResourceLoader):
44 """
45
46 """
34 def __init__(self, image_pool, anim_pool, model, vfs=None): 47 def __init__(self, image_pool, anim_pool, model, vfs=None):
48 """
49
50 """
35 self.image_pool = image_pool 51 self.image_pool = image_pool
36 self.anim_pool = anim_pool 52 self.anim_pool = anim_pool
37 self.model = model 53 self.model = model
38 self.vfs = vfs 54 self.vfs = vfs
39 self.source = None 55 self.source = None
40 self.filename = '' 56 self.filename = ''
41 57
42 def loadResource(self, location): 58 def loadResource(self, location):
59 """
60
61 """
43 self.source = location 62 self.source = location
44 self.filename = self.source.getFilename() 63 self.filename = self.source.getFilename()
45 self.node = None 64 self.node = None
46 self.file = None 65 self.file = None
47 if hasattr(location, 'node'): 66 if hasattr(location, 'node'):
59 78
60 if isobjectfile and s != obj_identifier: 79 if isobjectfile and s != obj_identifier:
61 isobjectfile = False 80 isobjectfile = False
62 81
63 if not isobjectfile: 82 if not isobjectfile:
83 return
64 raise WrongFileType('Tried to open non-object file %s with XMLObjectLoader.' % self.filename) 84 raise WrongFileType('Tried to open non-object file %s with XMLObjectLoader.' % self.filename)
85
65 self.do_load_resource(f) 86 self.do_load_resource(f)
66 87
67 def do_load_resource(self, file): 88 def do_load_resource(self, file):
89 """
90
91 """
68 if file: 92 if file:
69 tree = ET.parse(file) 93 tree = ET.parse(file)
70 self.node = tree.getroot() 94 self.node = tree.getroot()
71 self.parse_object(self.node) 95 self.parse_object(self.node)
72 96
73 def parse_object(self, object): 97 def parse_object(self, object):
98 """
99
100 """
74 if self.node.tag != 'object': 101 if self.node.tag != 'object':
75 raise InvalidFormat('Expected <object> tag, but found <%s>.' % self.node.tag) 102 raise InvalidFormat('Expected <object> tag, but found <%s>.' % self.node.tag)
76 103
77 id = object.get('id') 104 _id = object.get('id')
78 if not id: 105 if not _id:
79 raise InvalidFormat('<object> declared without an id attribute.') 106 raise InvalidFormat('<object> declared without an id attribute.')
107 _id = str(_id)
80 108
81 nspace = object.get('namespace') 109 nspace = object.get('namespace')
82 if not nspace: 110 if not nspace:
83 raise InvalidFormat('<object> %s declared without a namespace attribute.' % str(id)) 111 raise InvalidFormat('<object> %s declared without a namespace attribute.' % str(_id))
112 nspace = str(nspace)
84 113
85 obj = None 114 obj = None
86 parent = object.get('parent', None) 115 parent = object.get('parent', None)
87 if parent: 116 if parent:
88 query = self.metamodel.getObjects('id', str(parent)) 117 query = self.metamodel.getObjects('id', str(parent))
90 raise NotFound('No objects found with identifier %s.' % str(parent)) 119 raise NotFound('No objects found with identifier %s.' % str(parent))
91 elif len(query) > 1: 120 elif len(query) > 1:
92 raise NameClash('%d objects found with identifier %s.' % (len(query), str(parent))) 121 raise NameClash('%d objects found with identifier %s.' % (len(query), str(parent)))
93 parent = query[0] 122 parent = query[0]
94 123
95 try: 124 # check if model already has this object
96 obj = self.model.createObject(str(id), str(nspace), parent) 125 if not bool(self.model.getObject(_id, nspace)):
97 except RuntimeError, e: 126 obj = self.model.createObject(_id, nspace, parent)
98 if is_fife_exc(fife.NameClash, e): 127 else:
99 raise NameClash('Tried to create already existing object, ignoring') 128 print NameClash('Tried to create already existing object \n\t...ignoring: %s, %s' % (_id, nspace))
100 raise 129 return
101 130
102 obj.setResourceLocation(self.source) 131 obj.setResourceLocation(self.source)
103 fife.ObjectVisual.create(obj) 132 fife.ObjectVisual.create(obj)
104 obj.setBlocking(bool( int(object.get('blocking', False)) )) 133 obj.setBlocking(bool( int(object.get('blocking', False)) ))
105 obj.setStatic(bool( int(object.get('static', False)) )) 134 obj.setStatic(bool( int(object.get('static', False)) ))
109 138
110 self.parse_images(object, obj) 139 self.parse_images(object, obj)
111 self.parse_actions(object, obj) 140 self.parse_actions(object, obj)
112 141
113 def parse_images(self, objelt, object): 142 def parse_images(self, objelt, object):
143 """
144
145 """
114 for image in objelt.findall('image'): 146 for image in objelt.findall('image'):
115 source = image.get('source') 147 source = image.get('source')
116 if not source: 148 if not source:
117 raise InvalidFormat('<image> declared without a source attribute.') 149 raise InvalidFormat('<image> declared without a source attribute.')
118 150
124 image_location = fife.ImageLocation('/'.join(path)) 156 image_location = fife.ImageLocation('/'.join(path))
125 image_location .setXShift(int( image.get('x_offset', 0) )) 157 image_location .setXShift(int( image.get('x_offset', 0) ))
126 image_location .setYShift(int( image.get('y_offset', 0) )) 158 image_location .setYShift(int( image.get('y_offset', 0) ))
127 id = self.image_pool.addResourceFromLocation(image_location) 159 id = self.image_pool.addResourceFromLocation(image_location)
128 object.get2dGfxVisual().addStaticImage(int( image.get('direction', 0) ), id) 160 object.get2dGfxVisual().addStaticImage(int( image.get('direction', 0) ), id)
129 #img = self.image_pool.getImage(id)
130 161
131 def parse_actions(self, objelt, object): 162 def parse_actions(self, objelt, object):
163 """
164
165 """
132 for action in objelt.findall('action'): 166 for action in objelt.findall('action'):
133 id = action.get('id') 167 id = action.get('id')
134 if not id: 168 if not id:
135 raise InvalidFormat('<action> declared without an id attribute.') 169 raise InvalidFormat('<action> declared without an id attribute.')
136 170
137 act_obj = object.createAction(str(id)) 171 act_obj = object.createAction(str(id))
138 fife.ActionVisual.create(act_obj) 172 fife.ActionVisual.create(act_obj)
139 self.parse_animations(action, act_obj) 173 self.parse_animations(action, act_obj)
140 174
141 def parse_animations(self, actelt, action): 175 def parse_animations(self, actelt, action):
176 """
177
178 """
142 for anim in actelt.findall('animation'): 179 for anim in actelt.findall('animation'):
143 source = anim.get('source') 180 source = anim.get('source')
144 if not source: 181 if not source:
145 raise InvalidFormat('Animation declared with no source location.') 182 raise InvalidFormat('Animation declared with no source location.')
146 183