forked from scratchfoundation/scratch-flash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChildRender.as
More file actions
151 lines (131 loc) · 4.68 KB
/
ChildRender.as
File metadata and controls
151 lines (131 loc) · 4.68 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
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
/*
* 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 {
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.geom.Point;
import flash.geom.Rectangle;
public class ChildRender extends BitmapData {
private const allowPartial:Boolean = true;
private const maxSize:uint = 1022;
private const halfSize:uint = maxSize >> 1;
private var orig_width:Number;
private var orig_height:Number;
private var orig_bounds:Rectangle;
public var inner_x:Number;
public var inner_y:Number;
public var inner_w:Number;
public var inner_h:Number;
public var scale:Number;
public function ChildRender(w:Number, h:Number, dispObj:DisplayObject, penLayer:DisplayObject, b:Rectangle) {
orig_width = w;
orig_height = h;
orig_bounds = b;
scale = 1;
reset(dispObj, penLayer);
super(Math.ceil(Math.min(w, maxSize)), Math.ceil(Math.min(h, maxSize)), true, 0);
}
public function reset(dispObj:DisplayObject, penLayer:DisplayObject):void {
inner_x = inner_y = 0;
inner_w = inner_h = 1;
if(!allowPartial) {
if(orig_width > maxSize || orig_height > maxSize)
scale = maxSize / Math.max(orig_width, orig_height);
return;
}
// Is it too large and needs to be partially rendered?
var bounds:Rectangle;
if(orig_width > maxSize || orig_height > maxSize) {
bounds = getVisibleBounds(dispObj, penLayer);
bounds.inflate(halfSize - bounds.width / 2, 0);
if(bounds.x < 0) {
bounds.width += bounds.x;
bounds.x = 0;
}
if(bounds.right > orig_width)
bounds.width += orig_width - bounds.right;
inner_x = bounds.x / orig_width;
inner_w = maxSize / orig_width;
}
if(orig_height > maxSize) {
if(!bounds) bounds = getVisibleBounds(dispObj, penLayer);
bounds.inflate(0, halfSize - bounds.height / 2);
if(bounds.y < 0) {
bounds.height += bounds.y;
bounds.y = 0;
}
if(bounds.bottom > orig_height)
bounds.height += orig_height - bounds.bottom;
inner_y = bounds.y / orig_height;
inner_h = maxSize / orig_height;
}
}
public function isPartial():Boolean {
return allowPartial && (width < orig_width || height < orig_height);
}
public function get renderWidth():Number {
return (width > orig_width ? orig_width : width);
}
public function get renderHeight():Number {
return (height > orig_height ? orig_height : height);
}
public function needsResize(w:Number, h:Number):Boolean {
if(width > orig_width && Math.ceil(w) > width) {
return true;
}
if(height > orig_height && Math.ceil(h) > height) {
return true;
}
return false;
}
public function needsRender(dispObj:DisplayObject, w:Number, h:Number, penLayer:DisplayObject):Boolean {
if(inner_x == 0 && inner_y == 0 && inner_w == 1 && inner_h == 1) return false;
var renderRect:Rectangle = new Rectangle(inner_x*w, inner_y*h, inner_w*w, inner_h*h);
renderRect.width += 0.001;
renderRect.height += 0.001;
var stageRect:Rectangle = getVisibleBounds(dispObj, penLayer);
var containsStage:Boolean = renderRect.containsRect(stageRect);
return !containsStage;
}
private function getVisibleBounds(dispObj:DisplayObject, penLayer:DisplayObject):Rectangle {
var visibleBounds:Rectangle = penLayer.getBounds(dispObj);
var tl:Point = orig_bounds.topLeft;
visibleBounds.offset(-tl.x, -tl.y);
if(visibleBounds.x < 0) {
visibleBounds.width += visibleBounds.x;
visibleBounds.x = 0;
}
if(visibleBounds.y < 0) {
visibleBounds.height += visibleBounds.y;
visibleBounds.y = 0;
}
if(visibleBounds.right > orig_width) {
visibleBounds.width += orig_width - visibleBounds.right;
}
if(visibleBounds.bottom > orig_height) {
visibleBounds.height += orig_height - visibleBounds.bottom;
}
visibleBounds.x *= dispObj.scaleX;
visibleBounds.y *= dispObj.scaleY;
visibleBounds.width *= dispObj.scaleX;
visibleBounds.height *= dispObj.scaleY;
return visibleBounds;
}
}
}