OSDN Git Service

194c8f703a5f6419ece6c8b392b5135e97342976
[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 26: // PlaceObject2
116                 // set display List;
117                 var obj = null;
118                 if (tag.move && (tag.id === null)) {
119                     this.displayList.move(tag.depth, tag);
120                 } else {
121                     if (tag.move) {
122                         obj = this.displayList.get(tag.depth);
123                         if (obj === null) {
124                             console.error("Can't get DisplayObject by depth:"+tag.depth);
125                         }
126                         if (obj.code === 39) { // DefineSprite
127                             this.deleteChildMovieClip(obj.name);
128                         }
129                         this.displayList.del(tag.depth);
130                     }
131                     defineTag = dict.get(tag.id);
132                     if (defineTag === null) {
133                         console.error("defineTag === undefined");
134                         return ;
135                     }
136                     var name = tag.name;
137                     if ((name === null) && (defineTag.code === 39)) {
138                         name = "instance"+this.instanceSeqno;
139                         tag.name = name;
140                         this.instanceSeqno++;
141                     }
142                     obj = FlappObject.factory(this, name, defineTag, tag,
143                                               dict);
144 //                    console.debug(obj);
145                     if (defineTag.code === 39) { // DefineSprite
146                         obj.totalframes = defineTag.count;
147                         obj.control(dict);
148                         this.addChildMovieClip(name, obj);
149                     }
150                     this.displayList.set(tag.depth, obj, tag);
151                     break;
152                 }
153             }
154         }
155     },
156     action: function() {
157         for (var mc in this.childMovieClips) {
158             this.childMovieClips[mc].action();
159         }
160         if (this.playing) {
161             this.actionThis();
162         }
163
164     },
165     actionThis: function() {
166         var actionTags = this.actionTagsList[this.currentFrame];
167         var l = actionTags.length;
168         console.debug("FlappMovieClip::actionThis: actionTags.length:"+l);
169         for (var i = 0 ; i < l ; i++) {
170             var tag = actionTags[i];
171             var movieClip = this;
172             FlappAction.exec(tag, movieClip, this.rootMovieClip);
173         }
174     },
175     increment: function() {
176         for (var mc in this.childMovieClips) {
177             this.childMovieClips[mc].increment();
178         }
179         if (this.playing) {
180             this.incrementThis();
181         }
182     },
183     incrementThis: function() {
184         console.debug("FlappMovieClip::incrementThis: "+this.currentFrame);
185         this.currentFrame++;
186         if (this.totalframes <= this.currentFrame) {
187             if (this.loop) {
188                 this.currentFrame = 0; // play
189             } else {
190                 this.playing = false;
191             }
192         }
193         if (this.totalframes === 1) {
194             this.playing = false;
195         }
196     },
197     render: function(canvas, dict) {
198         console.debug("FlappMovieClip::render: name:"+this.name);
199         var depthList = this.displayList.descSortedDepth();
200         for (var i = 0, l = depthList.length  ; i < l ; i++) {
201             var depth = depthList[i];
202             var obj = this.displayList.get(depth);
203             console.debug(obj);
204             //      obj.render(this.canvas);
205             obj.render(canvas, dict);
206         }
207     },
208     setVariable: function(key, value) {
209         var lcKey = key.toLowerCase();
210         this.actionVarriableTable[lcKey] = value;
211         this.actionVarriablOrigKeys[lcKey] = key;
212     },
213     getVariable: function(key) {
214         var lcKey = key.toLowerCase();
215         if (lcKey in this.actionVarriableTable) {
216             return this.actionVarriableTable[lcKey];
217         }
218         return null;
219     },
220     gotoFrame: function(frameNum) {
221         console.debug("FlappMovieClip::gotoFrame"+frameNum);
222         this.currentFrame = frameNum;
223     },
224     gotoLabel: function(frameLabel) {
225         var frameNum = this.labelMap[frameLabel];
226         console.debug("FlappMovieClip::gotoFrame"+frameLabel+"=>"+frameNum);
227         this.currentFrame = frameNum;
228     },
229     play: function() {
230         this.playing = true;
231     },
232     stop: function() {
233         this.playing = false;
234     },
235     destroy: function() { // destructor
236         for (var name in this.childMovieClips) {
237             this.childMovieClips[name].destroy();
238         }
239         this.parentMovieClip = null;
240         this.rootMovieClip = null;
241         this.controlTagsList = null;
242         this.actionTagsList = null;
243         this.labelMap = null;
244         this.canvas = null;
245         this.displayList = null;
246     }
247 };
248
249 });