1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 from builtins import str
25 from fife import fife
26 from fife.extensions.serializers import ET
27
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
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
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
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
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
126 return animation
127