~extremepopcorn/dhlib/dhlib_ep

« back to all changes in this revision

Viewing changes to lib/flashx/Eraser.hx

  • Committer: edA-qa mort-ora-y
  • Date: 2010-02-16 05:36:32 UTC
  • Revision ID: eda-qa@disemia.com-20100216053632-60lt7fndfi3fgblw
first

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* <license>
 
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.
 
4
 * </license> 
 
5
 */
 
6
package flashx;
 
7
 
 
8
import flash.display.DisplayObject;
 
9
import flash.display.BitmapData;
 
10
 
 
11
/**
 
12
 * This provides a way to erase the form of one sprite from another one...
 
13
 *
 
14
 * This is just an attempt to workaround the circus of shitty Flash APIs, a true carneval
 
15
 * of nonsense:
 
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
 
19
 */
 
20
class Eraser
 
21
{
 
22
        var alpha : Float;      //any alpha above this is considered to be part of eraser in provided sprite
 
23
        
 
24
        var objx : RenderCache;
 
25
        
 
26
        public function new( alpha : Null<Float> )
 
27
        {
 
28
                this.alpha = (alpha == null) ? 0.5 : alpha;
 
29
                
 
30
                objx = new RenderCache();
 
31
                objx.useFilters = false;
 
32
        }
 
33
        
 
34
        /**
 
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)
 
37
         *
 
38
         * @param       what    [in] the stencil form to erase
 
39
         * @param       from    [in] from where to erase "what"
 
40
         */
 
41
        public function fullErase( what : DisplayObject, from : BitmapData )
 
42
        {
 
43
                //This is what doesn't work correclt (see FlashDefectErase)
 
44
                //from.draw( what, null, null, flash.display.BlendMode.ERASE );
 
45
                
 
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 );
 
51
                
 
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 );
 
56
                
 
57
                //we need these a lot
 
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 !
 
61
                
 
62
                //create the rendering form of "what"
 
63
                var clip = objx.renderBitmap( what, rect, false, true );
 
64
                
 
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) )
 
69
                        {
 
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...
 
73
                        }
 
74
#else
 
75
                //use Flash threshhold function (should be faster, but no option to preserve color)
 
76
                from.threshold( clip, 
 
77
                        new flash.geom.Rectangle( 0, 0, rect.width, rect.height ),
 
78
                        new flash.geom.Point( rect.left, rect.top ),
 
79
                        ">=",   alphaInt << 24,
 
80
                        0x00000000,     //new color
 
81
                        0xFF000000,     //mask
 
82
                        false );
 
83
#end                    
 
84
        }
 
85
}