OSDN Git Service

0084a28649f9e8b27b1c75827a9128f14f1b6c79
[flapp/flapp.git] / src / movieclip.js
1 goog.provide('FlappMovieClip');
2 goog.require('FlappObject');
3 goog.require('FlappDisplay');
4 goog.require('FlappShape');
5 goog.require('FlappAction');
6 goog.require('FlappCanvas');
7
8 goog.scope(function() {
9
10 /**
11  * @constructor
12  */
13 FlappMovieClip = function(parentMovieClip, matrix, colorTransform) {
14     this.OBJECT_TYPE = 2; // 1:Shape, 2:MovieClip
15     this.parentMovieClip = parentMovieClip?parentMovieClip:null;
16     this.rootMovieClip = parentMovieClip?parentMovieClip.rootMovieClip:this;
17     this.name = null;
18     this.setMatrix(matrix);
19     this.colorTransform = colorTransform;
20     this.childMovieClips = {}; // name => movieClip
21     this.instanceSeqno = 1; // for name object (movieClip, etc...)
22     this.clearControlTags();
23     //
24     this.prevShowFramePos = 0;
25     //
26     var canvas = document.createElement('canvas');
27     canvas.width = 240; // XXX
28     canvas.height = 240; // XXX
29     this.canvas = new FlappCanvas(canvas);
30     this.canvasDirty = false; // dirtyFlag
31     //
32     this.displayList = new FlappDisplay();
33     this.displayObjectLifeSpan = {}; // for gotoFrame
34     //
35     this.totalframes = 0;
36     this.currentFrame = 0;
37     this.playing = true;
38     this.loop = true;
39 //    this.loop = false;
40     this.actionVarriableTable = {};
41     this.actionVarriablOrigKeys = {};
42 };
43
44 FlappMovieClip.prototype = {
45     setMatrix: function(matrix) {
46         this.matrix = matrix;
47         this.absoluteMatrix = this.parentMovieClip?(matrix?FlappSWFMatrix.multiply(this.parentMovieClip.absoluteMatrix, matrix):this.parentMovieClip.absoluteMatrix):matrix;
48     },
49     clearControlTags: function() {
50         this.controlTagsList = [[]]; //
51         this.actionTagsList = [[]];
52         this.labelMap = {}; // label => frameNum
53         this.framesLoaded = 0;
54     },
55     appendControlTag: function(controlTag) {
56         // console.debug("FlappMovieClip::appendControlTag");
57         if (controlTag.code === 12) { // DoAction
58             this.actionTagsList[this.framesLoaded].push(controlTag);
59             return ;
60         }
61         this.controlTagsList[this.framesLoaded].push(controlTag);
62         if (controlTag.code === 1) { // ShowFrame
63             this.controlTagsList.push([]);
64             this.actionTagsList.push([]);
65             this.framesLoaded++;
66         } else if (controlTag.code === 43) { // FrameLabel
67             this.labelMap[controlTag.name] = this.framesLoaded;
68         }
69     },
70     setControlTags: function(controlTags) {
71         this.clearControlTags();
72         for (var i = 0, l = controlTags.length ; i < l ; i++) {
73             this.appendControlTag(controlTags[i]);
74         }
75     },
76     addChildMovieClip: function(name, movieClip) {
77         this.childMovieClips[name] = movieClip;
78     },
79     deleteChildMovieClip: function(name) {
80         delete this.childMovieClips[name];
81     },
82     control: function(dict) {
83         if (this.totalframes === 0) { // imcomplete
84             return false;
85         }
86 //        console.debug("FlappMovieClip::control: name:"+this.name+" playing:"+this.playing);
87         if (this.framesLoaded < this.totalframes) { // imcomplete
88             if (this.currentFrame < this.framesLoaded) {
89                 console.debug("control:imcomplete (currentFrame:"+this.currentFrame+" < framesLoaded:"+this.framesLoaded+")");
90                 return false; // idle
91             }
92         }
93         for (var mc in this.childMovieClips) {
94             this.childMovieClips[mc].control(dict);
95         }
96         if (this.playing) {
97             this.controlThis(dict);
98         }
99         return true;
100     },
101     controlThis: function(dict) {
102 //        console.debug("FlappMovieClip::controlThis: name:"+this.name);
103         var tag, i, l;
104         var defineTag;
105         if ((this.currentFrame < 0 ) || (this.totalframes <= this.currentFrame)) {
106             this.currentFrame = 0;
107         }
108         var controlTags = this.controlTagsList[this.currentFrame];
109
110         for (i = 0, l = controlTags.length ; i < l ; i++) {
111             tag = controlTags[i];
112             switch (tag.code) {
113             case 1: // ShowFrame
114                 break;
115             case 4:  // PlaceObject
116             case 26: // PlaceObject2
117                 // set display List;
118                 var obj = null;
119                 if (tag.move && (tag.id === null)) {
120                     this.displayList.move(tag.depth, tag);
121                 } else {
122                     if (tag.move) {
123                         obj = this.displayList.get(tag.depth);
124                         if (obj === null) {
125                             console.error("Can't get DisplayObject by depth:"+tag.depth);
126                         }
127                         if (obj.code === 39) { // DefineSprite
128                             this.deleteChildMovieClip(obj.name);
129                         }
130                         this.displayList.del(tag.depth);
131                     }
132                     defineTag = dict.get(tag.id);
133                     if (defineTag === null) {
134                         console.error("defineTag === undefined");
135                         return ;
136                     }
137                     var name = tag.name;
138                     if ((name === null) && (defineTag.code === 39)) {
139                         name = "instance"+this.instanceSeqno;
140                         tag.name = name;
141                         this.instanceSeqno++;
142                     }
143                     obj = FlappObject.factory(this, name, defineTag, tag,
144                                               dict);
145 //                    console.debug(obj);
146                      if (defineTag.code === 39) { // DefineSprite
147                         obj.totalframes = defineTag.count;
148                         obj.control(dict);
149                         this.addChildMovieClip(name, obj);
150                     }
151                     this.displayList.set(tag.depth, obj, tag);
152                     break;
153                 }
154                 break;
155             case 5:  // RemoveObject XXX
156             case 28: // RemoveObject2
157                 this.displayList.del(tag.depth);
158                 break;
159             }
160         }
161     },
162     action: function() {
163         for (var mc in this.childMovieClips) {
164             this.childMovieClips[mc].action();
165         }
166         if (this.playing) {
167             this.actionThis();
168         }
169
170     },
171     actionThis: function() {
172         var actionTags = this.actionTagsList[this.currentFrame];
173         var l = actionTags.length;
174 //        console.debug("FlappMovieClip::actionThis: actionTags.length:"+l);
175         for (var i = 0 ; i < l ; i++) {
176             var tag = actionTags[i];
177             var movieClip = this;
178             FlappAction.exec(tag, movieClip, this.rootMovieClip);
179         }
180     },
181     increment: function() {
182         for (var mc in this.childMovieClips) {
183             this.childMovieClips[mc].increment();
184         }
185         if (this.playing) {
186             this.incrementThis();
187         }
188     },
189     incrementThis: function() {
190 //        console.debug("FlappMovieClip::incrementThis: "+this.currentFrame);
191         this.currentFrame++;
192         if (this.totalframes <= this.currentFrame) {
193             if (this.loop) {
194                 this.currentFrame = 0; // play
195             } else {
196                 this.playing = false;
197             }
198         }
199         if (this.totalframes === 1) {
200             this.playing = false;
201         }
202     },
203     render: function(canvas, dict) {
204 //        console.debug("FlappMovieClip::render: name:"+this.name);
205         var depthList = this.displayList.ascSortedDepth();
206         for (var i = 0, l = depthList.length  ; i < l ; i++) {
207             var depth = depthList[i];
208             var obj = this.displayList.get(depth);
209 //            console.debug(obj);
210             obj.render(canvas, dict);
211         }
212     },
213     setVariable: function(key, value) {
214         var lcKey = key.toLowerCase();
215         this.actionVarriableTable[lcKey] = value;
216         this.actionVarriablOrigKeys[lcKey] = key;
217     },
218     getVariable: function(key) {
219         var lcKey = key.toLowerCase();
220         if (lcKey in this.actionVarriableTable) {
221             return this.actionVarriableTable[lcKey];
222         }
223         return null;
224     },
225     gotoFrame: function(frameNum) {
226         console.debug("FlappMovieClip::gotoFrame"+frameNum);
227         this.currentFrame = frameNum;
228     },
229     gotoLabel: function(frameLabel) {
230         var frameNum = this.labelMap[frameLabel];
231         console.debug("FlappMovieClip::gotoFrame"+frameLabel+"=>"+frameNum);
232         this.currentFrame = frameNum;
233     },
234     play: function() {
235         this.playing = true;
236     },
237     stop: function() {
238         this.playing = false;
239     },
240     destroy: function() { // destructor
241         for (var name in this.childMovieClips) {
242             this.childMovieClips[name].destroy();
243         }
244         this.parentMovieClip = null;
245         this.rootMovieClip = null;
246         this.controlTagsList = null;
247         this.actionTagsList = null;
248         this.labelMap = null;
249         this.canvas = null;
250         this.displayList = null;
251     }
252 };
253
254 });