/
BitmapScroller.as
321 lines (261 loc) · 9.12 KB
/
BitmapScroller.as
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
package com.flashartofwar
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.geom.Rectangle;
public class BitmapScroller extends Bitmap
{
private const INVALID_SIZE:String = "size";
private const INVALID_SCROLL:String = "scroll";
private const INVALID_SIZE_SCROLL:String = "all";
protected var _bitmapDataCollection:Vector.<BitmapData>;
protected var collectionRects:Vector.<Rectangle>;
protected var _totalWidth:int = 0;
protected var _maxHeight:Number = 0;
protected var collectionTotal:int = 0;
protected var copyPixelOffset:Point = new Point();
protected var internalSampleArea:Rectangle = new Rectangle(0,0, 0, 0);
protected var collectionID:int;
protected var sourceRect:Rectangle;
protected var sourceBitmapData:BitmapData;
protected var leftOver:Number;
protected var point:Point;
protected var calculationPoint:Point = new Point();
protected var difference:Number;
protected var sampleArea:Rectangle;
protected var samplePositionPoint:Point = new Point();
protected var _invalid:Boolean;
protected var invalidSize:Boolean;
protected var invalidScroll:Boolean;
public function BitmapScroller(bitmapData:BitmapData = null, pixelSnapping:String = "auto", smoothing:Boolean = false)
{
super(null, pixelSnapping ,smoothing);
}
override public function get width():Number
{
return internalSampleArea.width;
}
override public function set width(value:Number):void
{
if( value == internalSampleArea.width)
{
return;
}
else
{
internalSampleArea.width = value;
invalidate(INVALID_SIZE);
}
}
override public function get height():Number
{
return internalSampleArea.height;
}
override public function set height(value:Number):void
{
if( value == internalSampleArea.height)
{
return;
}
else
{
internalSampleArea.height = value;
invalidate(INVALID_SIZE);
}
}
public function get scrollX():Number
{
return internalSampleArea.x;
}
public function set scrollX(value:Number):void
{
if(value == internalSampleArea.x)
{
return;
}
else
{
internalSampleArea.x = value;
invalidate(INVALID_SCROLL);
}
}
public function get totalWidth():int
{
return _totalWidth;
}
public function get maxHeight():Number
{
return _maxHeight;
}
public function render():void
{
if (_invalid)
{
// We check to see if the size has changed. If it has we create a new bitmap. If not we clear it with fillRect
if(invalidSize)
{
bitmapData = new BitmapData(internalSampleArea.width, internalSampleArea.height, true, 0x000000);
invalidSize = false;
}
else
{
bitmapData.fillRect(internalSampleArea, 0);
}
// Call sample to get the party started
draw(internalSampleArea.clone());
// Clear any invalidation
_invalid = false;
}
}
protected function indexCollection():void
{
var bmd:BitmapData;
var lastX:Number = 0;
var i:int;
collectionTotal = _bitmapDataCollection.length;
var rect:Rectangle;
collectionRects = new Vector.<Rectangle>(collectionTotal);
var lastWidth:Number = 0;
_totalWidth = 0;
_maxHeight = 0;
for (i = 0; i < collectionTotal; ++ i)
{
bmd = _bitmapDataCollection[i] as BitmapData;
// create a rect to represent the BitmapData
rect = new Rectangle(lastX, 0, bmd.width, bmd.height);
collectionRects[i] = rect;
lastX += bmd.width + 1;
// Save out width information
lastWidth = bmd.width;
_totalWidth += lastWidth;
if (bmd.height > _maxHeight)
{
_maxHeight = bmd.height;
}
}
}
/**
*
* @param sampleCoord
* @return
*/
protected function calculateCollectionStartIndex(sampleCoord:Point):int
{
if (sampleCoord.x < 0)
return -1;
var i:int;
for (i = 0; i < collectionTotal; ++ i)
{
if (collectionRects[i].containsPoint(sampleCoord))
{
return i;
}
}
return -1;
}
protected function draw(sampleArea:Rectangle,offset:Point = null):void
{
var output:BitmapData = this.bitmapData;
calculationPoint.x = sampleArea.x;
calculationPoint.y = sampleArea.y;
collectionID = calculateCollectionStartIndex(calculationPoint);
if (collectionID != -1)
{
sourceRect = collectionRects[collectionID];
sourceBitmapData = _bitmapDataCollection[collectionID];
leftOver = Math.round(calculateLeftOverValue(sampleArea.x, sampleArea.width, sourceRect));
if (!offset)
offset = copyPixelOffset;
point = calculateSamplePosition(sampleArea, sourceRect);
sampleArea.x = point.x;
sampleArea.y = point.y;
output.copyPixels(sourceBitmapData, sampleArea, offset);
if (leftOver > 0)
{
offset = new Point(output.width - leftOver, 0);
var leftOverSampleArea:Rectangle = calculateLeftOverSampleArea(sampleArea, leftOver, sourceRect);
draw(leftOverSampleArea, offset);
}
}
}
protected function calculateLeftOverValue(offset:Number, sampleWidth:Number, sourceRect:Rectangle):Number
{
difference = (offset + sampleWidth) - (sourceRect.x + sourceRect.width);
return (difference < 0) ? 0 : difference;
}
protected function calculateLeftoverOffset(sampleArea:Rectangle, leftOver:Number):Point
{
return new Point(sampleArea.width - leftOver, 0);
}
protected function calculateLeftOverSampleArea(sampleAreaSRC:Rectangle, leftOver:Number, sourceRect:Rectangle):Rectangle
{
sampleArea = sampleAreaSRC.clone();
sampleArea.width = leftOver + 1;
sampleArea.x = sourceRect.x + sourceRect.width + 1;
return sampleArea;
}
protected function calculateSamplePosition(sampleRect:Rectangle, sourceArea:Rectangle):Point
{
samplePositionPoint.x = sampleRect.x - sourceArea.x;
return samplePositionPoint;
}
public function clear():void
{
}
/** Invalidation Logic **/
protected function invalidate(type:String = "all"):void
{
if (!_invalid)
{
if(stage)
{
stage.invalidate();
_invalid = true;
switch(type)
{
case INVALID_SIZE:
invalidSize = true;
break;
case INVALID_SCROLL:
invalidScroll = true;
break;
case INVALID_SIZE_SCROLL:
invalidScroll = true;
invalidSize = true;
break;
}
}
else
{
_invalid = false;
}
}
}
/**
*
* @param event
*/
protected function onAddedToStage(event:Event):void
{
render();
}
/**
*
* @param event
*/
protected function onRemovedFromStage(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
public function set bitmapDataCollection(value:Vector.<BitmapData>):void
{
_bitmapDataCollection = value;
indexCollection();
}
}
}