My favorites
▼
|
Sign in
hexagon
hexagonlib moved!
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
hexagon
/
src
/
hexagonlib
/
flash
/
com
/
hexagonstar
/
display
/
bitmaps
/
AnimatedBitmap.as
‹r355
r376
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
/*
* hexagonlib - Multi-Purpose ActionScript 3 Library.
* __ __
* __/ \__/ \__ __
* / \__/HEXAGON \__/ \
* \__/ \__/ LIBRARY _/
* \__/ \__/
*
* Licensed under the MIT License
*
* Copyright (c) 2007-2010 Sascha Balkau / Hexagon Star Softworks
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.hexagonstar.display.bitmaps
{
import com.hexagonstar.core.IDisposable;
import com.hexagonstar.display.IAnimatedDisplayObject;
import com.hexagonstar.display.PlayMode;
import com.hexagonstar.event.FrameEvent;
import com.hexagonstar.time.FrameRateInterval;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.TimerEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.setTimeout;
/**
* Dispatched everytime a frame is entered.
* @eventType com.hexagonstar.event.FrameEvent
*/
[Event(name="enterFrame", type="com.hexagonstar.event.FrameEvent")]
/**
* Represents an animateable bitmap. When creating the AnimatedBitmap a bitmapData
* object has to be specified that consists of horizonally layed out, same-sized
* single frames that the AnimatedBitmap uses as frames for it's animation.
*
* @author Sascha Balkau
* @additional backwards play and custom loops Nilsen Filc
* @version 1.2.0
*/
public class AnimatedBitmap extends Bitmap implements IAnimatedDisplayObject,
IDisposable
{
////////////////////////////////////////////////////////////////////////////////////////
// Properties //
////////////////////////////////////////////////////////////////////////////////////////
/**
* The global interval is used for all animated bitmaps that do not use their
* own dedicated interval. The framerate of this interval can be changed with
* the globalFrameRate property.
* @private
*/
protected static var _globalInterval:FrameRateInterval;
/**
* The framrate used with the global interval.
* @private
*/
protected static var _globalFrameRate:int = 12;
/**
* The buffer that contains all single frames of the animation.
* @private
*/
protected var _buffer:BitmapData;
/**
* Interval that is used to make the animation 'run'.
* @private
*/
protected var _interval:FrameRateInterval;
/**
* Point used to position frame window on the buffer.
* @private
*/
protected var _point:Point;
/**
* Rectangle used as the frame window.
* @private
*/
protected var _rect:Rectangle;
/**
* Storage object for frame coords on the buffer image.
* @private
*/
protected var _frameCoords:Vector.<Point>;
/**
* Storage object for defined animation sequences.
* @private
*/
protected var _sequences:Object;
/**
* The currently playing animation sequence.
* @private
*/
protected var _sequence:Sequence;
/**
* Frame number from which to start playing.
* @private
*/
protected var _startFrame:int;
/**
* Frame number at which playback ends.
* @private
*/
protected var _endFrame:int;
/**
* The width of a single animation frame.
* @private
*/
protected var _frameWidth:int;
/**
* The height of a single animation frame.
* @private
*/
protected var _frameHeight:int;
/**
* The number of total frames of the animated bitmap.
* @private
*/
protected var _totalFrames:int;
/**
* The frame number on that the playhead currently is.
* @private
*/
protected var _currentFrame:int;
/**
* Frame count property used for ping pong mode playback.
* @private
*/
protected var _addFrame:int = 1;
/**
* @private
*/
protected var _loopCount:int = 0;
/**
* Determines if the animated bitmap is currently playing.
* @private
*/
protected var _isPlaying:Boolean = false;
/**
* Determines if the animated bitmap is flipped in x direction.
* @private
*/
protected var _isFlipX:Boolean = false;
/**
* Determines if the animated bitmap is flipped in y direction.
* @private
*/
protected var _isFlipY:Boolean = false;
/**
* Determines if the animated bitmap uses the global interval.
* @private
*/
protected var _useGlobalInterval:Boolean = false;
/**
* Determines if the animated bitmap has been disposed.
* @private
*/
protected var _isDisposed:Boolean = false;
protected var _isStopAtNextLoop:Boolean = false;
private var _frameRate:int;
////////////////////////////////////////////////////////////////////////////////////////
// Public Methods //
////////////////////////////////////////////////////////////////////////////////////////
/**
* Creates a new AnimatedBitmap instance. Required parameters are bitmap,
* frameWidth and frameHeight.
*
* @param bitmapData
* @param frameWidth
* @param frameHeight
* @param totalFrames
* @param playMode
* @param interval
* @param transparent
* @param pixelSnapping
* @param smoothing
*/
public function AnimatedBitmap(bitmapData:BitmapData,
frameWidth:int,
frameHeight:int,
totalFrames:int = 0,
playMode:int = 0,
interval:FrameRateInterval = null,
transparent:Boolean = true,
pixelSnapping:String = "auto",
smoothing:Boolean = false)
{
super(new BitmapData(frameWidth, frameHeight, transparent, 0x00000000),
pixelSnapping, smoothing);
_buffer = bitmapData.clone();
_point = new Point(0, 0);
_sequences = {};
_frameWidth = frameWidth;
_frameHeight = frameHeight;
_startFrame = _currentFrame = 1;
_endFrame = _totalFrames = (totalFrames > 0)
? totalFrames
: (_buffer.width / _frameWidth) * (_buffer.height / _frameHeight);
if (!interval)
{
if (!_globalInterval)
{
_globalInterval = new FrameRateInterval(_globalFrameRate);
}
_interval = _globalInterval;
_useGlobalInterval = true;
}
else
{
_interval = interval;
}
_frameRate = _interval.frameRate;
calculateFrameCoords();
defineSequence("all", _startFrame, _endFrame, playMode);
sequence = "all";
draw();
}
/**
* Starts the playback of the animated bitmap. If the animated bitmap is already
* playing while calling this method, it calls stop() and then play again instantly
* to allow for framerate changes during playback.
*/
public function play():void
{
_isStopAtNextLoop = false;
if (!_isPlaying)
{
if (_loopCount == _sequence.loops)
{
// TODO This is bad! Needs to be fixed differently!
/* Temp fix! needs testing! Causes anims with only one loop to play
* again after they are stopped and play() is called again. */
//_currentFrame = 1;
_loopCount = 0;
}
_isPlaying = true;
_interval.addEventListener(TimerEvent.TIMER, onInterval, false, 0, true);
_interval.start();
}
else
{
stop();
play();
}
}
/**
* Stops the playback of the animated bitmap.
*/
public function stop():void
{
if (_isPlaying)
{
_interval.removeEventListener(TimerEvent.TIMER, onInterval);
_isPlaying = false;
}
}
/**
* stopAtNextLoop
*/
public function stopAtNextLoop():void
{
_isStopAtNextLoop = true;
}
/**
* Jumps to the specified frame nr. and plays the animated bitmap from that
* position. Note that the frames of an animated bitmap start at 1 just like
* a MovieClip.
*
* @param frame an Integer that designates the frame from which to start play.
* @param scene unused in animated bitmaps.
*/
public function gotoAndPlay(frameOrSequence:Object, scene:String = null):void
{
_currentFrame = resolveFrame(frameOrSequence) - 1;
play();
}
/**
* Jumps to the specified frame nr. and stops the animated bitmap at that position.
* Note that the frames of an animated bitmap start at 1 just like a MovieClip.
*
* @param frame an Integer that designates the frame to which to jump.
* @param scene unused in animated bitmaps.
*/
public function gotoAndStop(frameOrSequence:Object, scene:String = null):void
{
var f:int = resolveFrame(frameOrSequence);
if (f >= _currentFrame)
{
_currentFrame = f - 1;
nextFrame();
}
else
{
_currentFrame = f + 1;
prevFrame();
}
}
/**
* Moves the animation to the next of the current frame. If the animated bitmap is
* playing, the playback is stopped by this operation.
*/
public function nextFrame():void
{
if (_isPlaying) stop();
_currentFrame++;
if (_currentFrame > _totalFrames) _currentFrame = _totalFrames;
draw();
}
/**
* Moves the animation to the previous of the current frame. If the animated bitmap
* is playing, the playback is stopped by this operation.
*/
public function prevFrame():void
{
if (_isPlaying) stop();
_currentFrame--;
if (_currentFrame < 1) _currentFrame = 1;
draw();
}
/**
* Defines a new animation sequence to the animated bitmap.
*
* @param name The name of the sequence.
* @param startFrame Starting frame number of the range.
* @param endFrame Ending frame number of the range.
* @param loops Nr of loops that the sequence should play.
* @param playMode
* @param followSequence Optional sequence name that follows after this sequence
* reached its end.
* @param followDelay Delay in ms after that the followSequence should play.
*/
public function defineSequence(name:String,
startFrame:int,
endFrame:int,
loops:int = 0,
playMode:int = 0,
followSequence:String = null,
followDelay:int = 0
):void
{
_sequences[name] = new Sequence(name, startFrame, endFrame, playMode, loops,
followSequence, followDelay);
}
/**
* removeRange
*/
public function removeSequence(name:String):void
{
delete(_sequences[name]);
}
/**
* Disposes the animated bitmap.
*/
public function dispose():void
{
stop();
_isDisposed = true;
}
////////////////////////////////////////////////////////////////////////////////////////
// Getters & Setters //
////////////////////////////////////////////////////////////////////////////////////////
/**
* Sets the frame rate timer object used for the animated bitmap. This method is
* useful when it is desired to change the framerate at a later timer.
*
* @param timer The frame rate timer used for the animated bitmap.
*/
public function set frameRateInterval(v:FrameRateInterval):void
{
if (_isPlaying)
{
stop();
_interval = v;
play();
}
else
{
_interval = v;
}
}
/**
* Indicates the framerate of the global framerate Interval that can be used
* as a default interval for all animated objects. The valid range is
* between 1 and 1000.
*/
public static function get globalFrameRate():int
{
return _globalFrameRate;
}
public static function set globalFrameRate(v:int):void
{
if (v < 1) v = 1;
else if (v > 1000) v = 1000;
_globalFrameRate = v;
if (_globalInterval) _globalInterval.frameRate = _globalFrameRate;
}
/**
* The framerate with that the animated bitmap is playing.
*/
public function get frameRate():int
{
if (_useGlobalInterval) return AnimatedBitmap.globalFrameRate;
return _frameRate;
}
public function set frameRate(v:int):void
{
if (v < 1) v = 1;
else if (v > 1000) v = 1000;
_frameRate = v;
if (_useGlobalInterval)
{
AnimatedBitmap.globalFrameRate = _frameRate;
}
else
{
_interval.frameRate = _frameRate;
}
}
/**
* The current frame position of the animated bitmap.
*/
public function get currentFrame():int
{
return _currentFrame;
}
/**
* The total amount of frames that the animated bitmap has.
*/
public function get totalFrames():int
{
return _totalFrames;
}
/**
* Returns the play mode of the currently played sequence.
*/
public function get playMode():int
{
return _sequence.playMode;
}
/**
* Sets the name of the animation sequence that should be played or returns the name
* of the animation sequence that the play header is currently in.
*
* @see defineSequence
* @see removeSequence
*/
public function get sequence():String
{
if (!_sequence) return null;
return _sequence.name;
}
public function set sequence(v:String):void
{
var s:Sequence = _sequences[v];
if (s)
{
_sequence = s;
_startFrame = _currentFrame = _sequence.start;
_endFrame = _sequence.end;
_loopCount = 0;
// TODO Temp Off! Not needed?
// This is indeed required!
draw();
}
}
/**
* Shortcut property to set horizontal and vertical scaling equally.
*/
public function get scale():Number
{
return scaleX;
}
public function set scale(v:Number):void
{
scaleX = scaleY = v;
}
/**
* A 'workaround' property that always returns the top-left x position of the
* animated bitmap, regardless if it has been flipped in x direction. If an animated
* bitmap is flipped with flipX it's x coordinate changes to the opposite side of
* it's original position. For example an animated bitmap with a width of 200 and
* whose x coordinate is at 200 will change it's x coordinate to 400 if it is
* flipped on the x axis. To circumvent the changed x position use xPos which would
* still return 200 in the above example, no matter if it is flipped or not.
*/
public function get xPos():int
{
if (_isFlipX) return x - width;
return x;
}
public function set xPos(v:int):void
{
x = v;
}
/**
* A 'workaround' property that always returns the top-left y position of the
* animated bitmap, regardless if it has been flipped in y direction. If an animated
* bitmap is flipped with flipY it's y coordinate changes to the opposite side of
* it's original position. For example an animated bitmap with a height of 200 and
* whose y coordinate is at 200 will change it's y coordinate to 400 if it is
* flipped on the y axis. To circumvent the changed y position use yPos which would
* still return 200 in the above example, no matter if it is flipped or not.
*/
public function get yPos():int
{
if (_isFlipY) return y - height;
return y;
}
public function set yPos(v:int):void
{
y = v;
}
/**
* Indicates if the animated bitmap is flipped in the x direction.
*/
public function get flipX():Boolean
{
return _isFlipX;
}
public function set flipX(v:Boolean):void
{
if (v == _isFlipX) return;
_isFlipX = v;
var m:Matrix = transform.matrix;
m.transformPoint(new Point(width / 2, height / 2));
m.a = -1 * m.a;
if (_isFlipX) m.tx = width + x;
else m.tx = x - width;
transform.matrix = m;
}
/**
* Indicates if the animated bitmap is flipped in the y direction.
*/
public function get flipY():Boolean
{
return _isFlipY;
}
public function set flipY(v:Boolean):void
{
if (v == _isFlipY) return;
_isFlipY = v;
var m:Matrix = transform.matrix;
m.transformPoint(new Point(width / 2, height / 2));
m.d = -1 * m.d;
if (_isFlipY) m.ty = y + height;
else m.ty = y - height;
transform.matrix = m;
}
/**
* Returns whether the animated bitmap is playing or not.
*/
public function get isPlaying():Boolean
{
return _isPlaying;
}
/**
* Determines if the object has been disposed (true), or is still available
* for use (false).
*/
public function get isDisposed():Boolean
{
return _isDisposed;
}
////////////////////////////////////////////////////////////////////////////////////////
// Event Handlers //
////////////////////////////////////////////////////////////////////////////////////////
/**
* @private
*/
protected function onInterval(e:TimerEvent):void
{
if (_sequence.playMode == PlayMode.FORWARD)
{
_currentFrame++;
if (_currentFrame > _endFrame)
{
_loopCount++;
if (_isStopAtNextLoop || _loopCount == _sequence.loops)
{
_isStopAtNextLoop = false;
stop();
_currentFrame--;
checkFollowSequence();
return;
}
_currentFrame = _startFrame;
}
}
else if (_sequence.playMode == PlayMode.BACKWARD)
{
_currentFrame--;
if (_currentFrame < _startFrame)
{
_loopCount++;
if (_isStopAtNextLoop || _loopCount == _sequence.loops)
{
_isStopAtNextLoop = false;
stop();
_currentFrame++;
checkFollowSequence();
return;
}
_currentFrame = _endFrame;
}
}
else if (_sequence.playMode == PlayMode.PINGPONG)
{
_currentFrame += _addFrame;
if (_currentFrame == _endFrame)
{
_addFrame = -_addFrame;
}
else if (_currentFrame == _startFrame)
{
_loopCount++;
_addFrame = -_addFrame;
if (_isStopAtNextLoop || _loopCount == _sequence.loops)
{
_isStopAtNextLoop = false;
stop();
draw();
checkFollowSequence();
return;
}
}
}
draw();
}
////////////////////////////////////////////////////////////////////////////////////////
// Private Methods //
////////////////////////////////////////////////////////////////////////////////////////
/**
* Draws the next bitmap frame from the buffer to the visible area.
*
* @private
*/
protected function draw():void
{
dispatchEvent(new FrameEvent(FrameEvent.ENTER_FRAME, _currentFrame));
var p:Point = _frameCoords[_currentFrame - 1];
_rect = new Rectangle(p.x, p.y, _frameWidth, _frameHeight);
bitmapData.copyPixels(_buffer, _rect, _point);
}
/**
* Prepares coordinates for frames on the buffer.
*
* @private
*/
protected function calculateFrameCoords():void
{
_frameCoords = new Vector.<Point>(_totalFrames, true);
var x:int = 0;
var y:int = 0;
/* Iterate through frames on the buffer */
for (var i:int = 0; i < _totalFrames; i++)
{
_frameCoords[i] = new Point(x, y);
/* Increase x position */
x += _frameWidth;
/* Reset x and increase y after reaching the last frame per row */
if (x > (_buffer.width - _frameWidth))
{
x = 0;
y += _frameHeight;
}
}
}
/**
* Resolves the frame number of the specified value. If v is a number it is returned
* directly. If v is not a number it is seen as the name of a sequence and the start
* frame number of the sequence is tried to be returned.
*
* @private
*/
protected function resolveFrame(v:*):int
{
if (isNaN(v))
{
var s:Sequence = _sequences[String(v)];
if (s) return s.start;
}
return v;
}
/**
* @private
*/
protected function checkFollowSequence():void
{
if (_sequence.followSeq)
{
if (_sequence.followDelay < 1)
{
sequence = _sequence.followSeq;
play();
}
else
{
setTimeout(function():void
{
sequence = _sequence.followSeq;
play();
}, _sequence.followDelay);
}
}
}
}
}
/**
* Sequence Data Object
* @private
*/
class Sequence
{
public var name:String;
public var start:int;
public var end:int;
public var playMode:int;
public var loops:int;
public var followSeq:String;
public var followDelay:int;
public function Sequence(n:String, s:int, e:int, pm:int, l:int, fs:String, fd:int)
{
name = n;
start = s;
end = e;
playMode = pm;
loops = l;
followSeq = fs;
followDelay = fd;
}
}
Show details
Hide details
Change log
r357
by sbalkau on May 19, 2010
Diff
fixes in the animated bitmap class.
Go to:
/trunk/hexagon/bin/app.swf
/trunk/hexagon/bld/build.number
...agon/bld/hexagonlib_build.number
...sh/com/hexagonstar/HexagonLib.as
...isplay/bitmaps/AnimatedBitmap.as
...xagon/src/rhombus/app/AppInfo.as
...hexagon/swc/hexagonlib_flash.swc
Project members,
sign in
to write a code review
Older revisions
r355
by sbalkau on May 15, 2010
Diff
[No log message]
r353
by sbalkau on Apr 12, 2010
Diff
[No log message]
r352
by sbalkau on Apr 4, 2010
Diff
[No log message]
All revisions of this file
File info
Size: 22177 bytes, 863 lines
View raw file
Powered by
Google Project Hosting