~eda-qa/dhlib/main

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
/* <license>
 * This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y
 * For full copyright and license information please refer to doc/license.txt.
 * </license> 
 */
package flashx;

import mathx.Point2;
import flash.display.BitmapData;

typedef RenderReturn =
{
	//the rendered content
	bitmapData : BitmapData,
	//the origin of the bitmap in relation to the provided object (in parent coords)
	origin : Point2
}

class SpriteUtil
{
	/**
	 * Renders the display object to a bitmap.  The size of the resulting
	 * bitmap will be the size of the provided object as rendered on the
	 * current stage.
	 *
	 * @return	[out] the set of data
	 */
	static public function render( dobj : flash.display.DisplayObject ) : Dynamic
	{
		ASSERT_NOTNULL( dobj.parent )
		ASSERT_NOTNULL( dobj.stage )
		
		var sq = dobj.getBounds( dobj.stage );
		var lq = dobj.getBounds( null );
		
		//extend the bounds to include any partial pixels
		var x0 = Math.floor( sq.left );
		var x1 = Math.ceil( sq.right );
		var y0 = Math.floor( sq.top );
		var y1 = Math.ceil( sq.bottom );
		
		ASSERT_FALSE( x1 == x0 )
		ASSERT_FALSE( y1 == y0 )
		
		//position to upperleft of bitmap		
		var mat = new flash.geom.Matrix();
		mat.translate( -lq.x, -lq.y );
		
		//and draw it
		var bd = new BitmapData(  x1 - x0 + 1, y1 - y0 + 1, true, 0 );
		bd.draw( dobj, mat );
		
		return {
			bitmapData: bd,
			origin: Point2.from( dobj.parent.globalToLocal( new flash.geom.Point( x0, y0 ) ) )
			};
	}
}