1 | "use strict";
|
2 |
|
3 | var buffer = require("./buffer");
|
4 | var Animation = require("./animation");
|
5 |
|
6 | function makeFrame(img, frameWidth, f) {
|
7 | return buffer.makeBuffer(frameWidth, img.height, function(ctx) {
|
8 | var sx = f * frameWidth;
|
9 | ctx.drawImage(img, sx, 0, frameWidth, img.height, 0, 0, frameWidth, img.height);
|
10 | });
|
11 | }
|
12 |
|
13 | function makeAnimation(img, numFrames, time) {
|
14 | var a = new Animation();
|
15 | var frameWidth = img.width / numFrames |0;
|
16 | for (var f = 0; f < numFrames; f++) {
|
17 | a.add(makeFrame(img, frameWidth, f), time);
|
18 | }
|
19 | return a;
|
20 | }
|
21 |
|
22 | function loadImageFromManifest(imageLoader, name, info) {
|
23 | if (info.strip !== undefined) {
|
24 | imageLoader.load(name, info.strip);
|
25 | } else if (info.prefix !== undefined) {
|
26 | for (var i = 1; i <= info.frames; i++) {
|
27 | var number = "" + i;
|
28 | if (info.padNumberTo > 1) {
|
29 | while (number.length < info.padNumberTo) {
|
30 | number = "0" + number;
|
31 | }
|
32 | }
|
33 | name = info.prefix + number + info.suffix;
|
34 | imageLoader.load(name + i, name);
|
35 | }
|
36 | }
|
37 | }
|
38 |
|
39 | function loadImagesFromManifest(imageLoader, manifest) {
|
40 | for (var key in manifest) {
|
41 | if (manifest.hasOwnProperty(key)) {
|
42 | var info = manifest[key];
|
43 | loadImageFromManifest(imageLoader, key, info);
|
44 | }
|
45 | }
|
46 | }
|
47 |
|
48 | function makeAnimationFromManifest(images, key, manifestEntry) {
|
49 | var animation;
|
50 | if (manifestEntry.strip !== undefined) {
|
51 | var strip = images.get(key);
|
52 | animation = makeAnimation(strip, manifestEntry.frames, manifestEntry.msPerFrame);
|
53 | } else if (manifestEntry.prefix !== undefined) {
|
54 | animation = new Animation();
|
55 | for (var i = 1; i <= manifestEntry.frames; i++) {
|
56 | var frame = images.get(key + i);
|
57 | animation.add(frame, manifestEntry.msPerFrame);
|
58 | }
|
59 | }
|
60 | if (manifestEntry.repeatAt !== undefined) {
|
61 | animation.repeatAt = manifestEntry.repeatAt;
|
62 | }
|
63 | if (manifestEntry.flip === "horizontal") {
|
64 | animation.flipHorizontally();
|
65 | }
|
66 | if (manifestEntry.flip === "vertical") {
|
67 | animation.flipVertically();
|
68 | }
|
69 | if (manifestEntry.rotate === "cw") {
|
70 | animation.rotateClockwise();
|
71 | }
|
72 | if (manifestEntry.rotate === "180") {
|
73 | animation.rotateClockwise().rotateClockwise();
|
74 | }
|
75 | if (manifestEntry.rotate === "ccw") {
|
76 | animation.rotateCounterclockwise();
|
77 | }
|
78 | animation.name = key;
|
79 | return animation;
|
80 | }
|
81 |
|
82 | function generateAnimationsFromManifest(images, manifest) {
|
83 | var animations = {};
|
84 | for (var key in manifest) {
|
85 | if (manifest.hasOwnProperty(key)) {
|
86 | var info = manifest[key];
|
87 | animations[key] = makeAnimationFromManifest(images, key, info);
|
88 | }
|
89 | }
|
90 | return animations;
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | function AnimationLoader(imageLoader, manifest) {
|
125 | |
126 |
|
127 |
|
128 |
|
129 | this.imageLoader = imageLoader;
|
130 | |
131 |
|
132 |
|
133 |
|
134 | this.manifest = manifest;
|
135 | loadImagesFromManifest(imageLoader, manifest);
|
136 | }
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | AnimationLoader.prototype.allLoaded = function() {
|
142 | if (this.loaded) {
|
143 | return true;
|
144 | }
|
145 | var loaded = this.imageLoader.allLoaded();
|
146 | if (loaded) {
|
147 | this.animations = generateAnimationsFromManifest(this.imageLoader, this.manifest);
|
148 | this.loaded = true;
|
149 | }
|
150 | return loaded;
|
151 | };
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | AnimationLoader.prototype.load = function(name, info) {
|
158 | this.manifest[name] = info;
|
159 | this.loaded = false;
|
160 | loadImageFromManifest(this.imageLoader, name, info);
|
161 | };
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | AnimationLoader.prototype.get = function(name) {
|
168 | var anim = this.animations[name];
|
169 | if (anim === undefined) {
|
170 | console.error("Unknown animation: " + name);
|
171 | }
|
172 | return anim;
|
173 | };
|
174 |
|
175 | module.exports = AnimationLoader;
|