2
* This file is part of the dis-Emi-A HaXe Library. Copyright (c) edA-qa mort-ora-y
3
* For full copyright and license information please refer to doc/license.txt.
8
import flash.display.DisplayObject;
9
import flash.display.BitmapData;
12
* This provides a way to erase the form of one sprite from another one...
14
* This is just an attempt to workaround the circus of shitty Flash APIs, a true carneval
16
* 1. erase mode doesn't erase everything (see FlashDefectErase)
17
* 2. an object without a parent has an odd transform (scaled 5 on my test)
18
* 3. the stage's children during startup/resize have an incorrect (likely previous) transform
22
var alpha : Float; //any alpha above this is considered to be part of eraser in provided sprite
24
var objx : RenderCache;
26
public function new( alpha : Null<Float> )
28
this.alpha = (alpha == null) ? 0.5 : alpha;
30
objx = new RenderCache();
31
objx.useFilters = false;
35
* For any pixel in "what" which has an alpha >= to ctor alpha the corresponding
36
* pixel in from is set to 0 (color is not preserved in this version)
38
* @param what [in] the stencil form to erase
39
* @param from [in] from where to erase "what"
41
public function fullErase( what : DisplayObject, from : BitmapData )
43
//This is what doesn't work correclt (see FlashDefectErase)
44
//from.draw( what, null, null, flash.display.BlendMode.ERASE );
46
//if parent is null then the transform space is undefined (and unusual in Flash, not simply neutral)
47
//changing mind, if there is a translation space it is likely wrong, espeically on startup or
48
//during a resize. We will rather here draw without a transform matrix, and thus not support
49
//sprites in the display list (we'll assume they are already confined to the bitmap)
50
Assert.isNull( what.parent );
52
//translate to a null space gives the native coords
53
var bound = what.getBounds( null );
54
//only do the parts which intersect
55
var rect = from.rect.intersection( bound );
58
var top = Math.floor( rect.top );
59
var left = Math.floor( rect.left );
60
var alphaInt : Int = Math.floor( alpha * 255 ); //Note: *NOT* 256, since max is 0xFF !
62
//create the rendering form of "what"
63
var clip = objx.renderBitmap( what, rect, false, true );
65
#if false // Inefficient but clear method //also the form needed if color is to be preserved...
66
//iterate through "what" space
67
for( y in 0...Math.floor(rect.height) )
68
for( x in 0...Math.floor(rect.width) )
70
var cc = clip.getPixel32( x, y );
71
if( cc >> 24 & 0xFF >= alphaInt ) //we can't optimize this to avoid shifting, since haXe Int is signed, and the high-alpha bit changes the sign :(
72
from.setPixel32( x + left, y + top, 0 ); //ignore color for now...
75
//use Flash threshhold function (should be faster, but no option to preserve color)
77
new flash.geom.Rectangle( 0, 0, rect.width, rect.height ),
78
new flash.geom.Point( rect.left, rect.top ),
80
0x00000000, //new color