forked from scratchfoundation/scratch-flash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStretchyBitmap.as
More file actions
64 lines (55 loc) · 2.18 KB
/
StretchyBitmap.as
File metadata and controls
64 lines (55 loc) · 2.18 KB
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
/*
* Scratch Project Editor and Player
* Copyright (C) 2014 Massachusetts Institute of Technology
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package uiwidgets {
import flash.display.*;
import flash.geom.*;
public class StretchyBitmap extends Sprite {
private var srcBM:BitmapData;
private var cachedBM:Bitmap;
public function StretchyBitmap(bm:BitmapData = null, w:int = 100, h:int = 75) {
srcBM = bm;
if (srcBM == null) srcBM = new BitmapData(1, 1, false, 0x808080);
cachedBM = new Bitmap(srcBM);
addChild(cachedBM);
setWidthHeight(w, h);
}
public function setWidthHeight(w:int, h:int):void {
var srcW:int = srcBM.width;
var srcH:int = srcBM.height;
w = Math.max(w, srcW);
h = Math.max(h, srcH);
var halfSrc:int;
// adjust width
var newBM:BitmapData = new BitmapData(w, h, true, 0xFF000000);
halfSrc = srcW / 2;
newBM.copyPixels(srcBM, new Rectangle(0, 0, halfSrc, srcH), new Point(0, 0));
newBM.copyPixels(srcBM, new Rectangle(srcW - halfSrc, 0, halfSrc, srcH), new Point(w - halfSrc, 0));
for (var dstX:int = halfSrc; dstX < (w - halfSrc); dstX++) {
newBM.copyPixels(srcBM, new Rectangle(halfSrc, 0, 1, srcH), new Point(dstX, 0));
}
// adjust height
halfSrc = srcH / 2;
newBM.copyPixels(newBM, new Rectangle(0, (srcH - halfSrc), w, halfSrc), new Point(0, h - halfSrc));
for (var dstY:int = halfSrc + 1; dstY < (h - halfSrc); dstY++) {
newBM.copyPixels(newBM, new Rectangle(0, halfSrc, w, 1), new Point(0, dstY));
}
// install new bitmap
cachedBM.bitmapData = newBM;
}
}}