OSDN Git Service

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