OSDN Git Service

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