Package fife :: Package extensions :: Package serializers :: Module xmlanimation
[hide private]
[frames] | no frames]

Source Code for Module fife.extensions.serializers.xmlanimation

  1  # -*- coding: utf-8 -*- 
  2   
  3  # #################################################################### 
  4  #  Copyright (C) 2005-2019 by the FIFE team 
  5  #  http://www.fifengine.net 
  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 builtins import str 
 25  from fife import fife 
 26  from fife.extensions.serializers import ET 
 27   
28 -def loadXMLAnimation(engine, filename):
29 f = engine.getVFS().open(filename) 30 f.thisown = 1 31 32 imgMgr = engine.getImageManager() 33 aniMgr = engine.getAnimationManager() 34 35 tree = ET.parse(f) 36 node = tree.getroot() 37 38 ani_id = node.get('id') 39 if not ani_id: 40 ani_id = filename 41 42 if aniMgr.exists(ani_id): 43 animation = aniMgr.getPtr(str(ani_id)) 44 else: 45 animation = aniMgr.create(str(ani_id)) 46 47 common_width = int(node.get('width', 0)) 48 common_height = int(node.get('height', 0)) 49 common_frame_delay = int(node.get('delay', 0)) 50 common_x_offset = int(node.get('x_offset', 0)) 51 common_y_offset = int(node.get('y_offset', 0)) 52 animation.setActionFrame(int(node.get('action_frame', 0))) 53 54 frames = node.findall('frame') 55 if not frames: 56 raise InvalidFormat('animation without <frame>s') 57 58 atlas = node.get('atlas') 59 60 if atlas: 61 path = filename.split('/') 62 path.pop() 63 path.append(str(atlas)) 64 atlas_file = '/'.join(path) 65 if imgMgr.exists(atlas_file): 66 atlas_img = imgMgr.get(str(atlas_file)) 67 else: 68 atlas_img = imgMgr.create(str(atlas_file)) 69 # parse atlas animation format 2 (e.g. cursor) 70 for frame in frames: 71 source = frame.get('source') 72 if not source: 73 raise InvalidFormat('animation without <frame>s') 74 75 frame_x_pos = int(frame.get('xpos', 0)) 76 frame_y_pos = int(frame.get('ypos', 0)) 77 frame_x_offset = int(frame.get('x_offset', common_x_offset)) 78 frame_y_offset = int(frame.get('y_offset', common_y_offset)) 79 frame_delay = int(frame.get('delay', common_frame_delay)) 80 frame_width = int(frame.get('width', common_width)) 81 frame_height = int(frame.get('height', common_height)) 82 83 # xml paths are relative to the directory of the file they're used in. 84 path = filename.split('/') 85 path.pop() 86 path.append(str(source)) 87 88 frame_file = '/'.join(path) 89 frame_file = atlas,":",frame_file 90 91 if imgMgr.exists(str(frame_file)): 92 frame_img = imgMgr.get(str(frame_file)) 93 else: 94 frame_img = imgMgr.create(str(frame_file)) 95 region = fife.Rect(frame_x_pos, frame_y_pos, frame_width, frame_height) 96 frame_img.useSharedImage(atlas_img, region) 97 frame_img.setXShift(frame_x_offset) 98 frame_img.setYShift(frame_y_offset) 99 100 animation.addFrame(frame_img, frame_delay) 101 else: 102 # parse single images 103 for frame in frames: 104 source = frame.get('source') 105 if not source: 106 raise InvalidFormat('animation without <frame>s') 107 108 frame_x_offset = int(frame.get('x_offset', common_x_offset)) 109 frame_y_offset = int(frame.get('y_offset', common_y_offset)) 110 frame_delay = int(frame.get('delay', common_frame_delay)) 111 112 # xml paths are relative to the directory of the file they're used in. 113 path = filename.split('/') 114 path.pop() 115 path.append(str(source)) 116 117 image_file = '/'.join(path) 118 119 img = imgMgr.create(image_file) 120 img.setXShift(frame_x_offset) 121 img.setYShift(frame_y_offset) 122 123 animation.addFrame(img, frame_delay) 124 125 # animation.thisown = 0 126 return animation
127