Scale9Bitmap

by zahir
♥2 | Line 234 | Modified 2011-04-01 08:35:17 | MIT License
play

ActionScript3 source code

/**
 * Copyright zahir ( http://wonderfl.net/user/zahir )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/8nlx
 */

/*
 drawTrianglesを使ったScale9Bitmap
 でもなんか微妙にズレてる
 */
package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Graphics;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.geom.Point;
    
    public class Scale9Bitmap_Triangles extends Sprite
    {
        private var g:Graphics;
        private var s:Shape;
        private var s9b:Scale9Bitmap;
        
        public function Scale9Bitmap_Triangles()
        {
            addChild( s = new Shape() );
            g = s.graphics;
            s9b = new Scale9Bitmap( createBitmap(), 16,16,16,16 );
            s9b.width = 96;
            s9b.height = 96;
            draw();
        }
        private function draw():void
        {
            g.clear();
            s9b.draw( g );
            
            /*
            g.lineStyle( 0, 0xFFFF00 );
            g.moveTo(16,0);
            g.lineTo(16,96);
            g.lineStyle( 0, 0x00FFFF );
            g.moveTo(80,0);
            g.lineTo(80,96);
            g.moveTo(96,0);
            g.lineTo(96,96);
            
            g.lineStyle( 0, 0xFF0000 );
            g.moveTo(0,80);
            g.lineTo(96,80);
            g.moveTo(0, 96);
            g.lineTo(96,96);
            //*/
        }
        private function createBitmap():BitmapData
        {
            var bd:BitmapData = new BitmapData(48,48,false,0);
            var f:BitmapData = new BitmapData(16,16, false, 0xFFFFFF);
            var p:Point = new Point();
            bd.copyPixels( f, f.rect, p );
            p.x = 16;
            f.fillRect(f.rect, 0xFF0000);
            bd.copyPixels( f, f.rect, p );
            p.x = 32;
            f.fillRect(f.rect, 0xFFFF00);
            bd.copyPixels( f, f.rect, p );
            
            p.y = 16;
            p.x = 0;
            f.fillRect(f.rect, 0xFF00);
            bd.copyPixels( f, f.rect, p );
            p.x = 16;
            f.fillRect(f.rect, 0x0);
            bd.copyPixels( f, f.rect, p );
            p.x = 32;
            f.fillRect(f.rect, 0xFF);
            bd.copyPixels( f, f.rect, p );
            
            p.y = 32;
            p.x = 0;
            f.fillRect(f.rect, 0xFF00FF);
            bd.copyPixels( f, f.rect, p );
            p.x = 16;
            f.fillRect(f.rect, 0xFFFF);
            bd.copyPixels( f, f.rect, p );
            p.x = 32;
            f.fillRect(f.rect, 0xFFFFFF);
            bd.copyPixels( f, f.rect, p );
            return bd;
        }
    }
}
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.GraphicsBitmapFill;
import flash.display.GraphicsEndFill;
import flash.display.GraphicsTrianglePath;
import flash.display.IGraphicsData;


class Scale9Bitmap
{
    private static const LT:int = 0;
    private static const T:int = 12;
    private static const RT:int = 24;
    private static const L:int = 36;
    private static const R:int = 48;
    private static const LB:int = 60;
    private static const B:int = 72;
    private static const RB:int = 84;
    private static const C:int = 96;
    
    protected var data:Vector.<IGraphicsData>;
    
    protected var v:Vector.<Number>;
    protected var uv:Vector.<Number>;
    protected var index:Vector.<int>;
    
    protected var culculated:Boolean = false;
    
    private var _bitmapData:BitmapData;
    
    private var _l:Number = 0;
    private var _t:Number = 0;
    private var _r:Number = 0;
    private var _b:Number = 0;
    
    private var _w:int;
    private var _h:int;
    
    public function Scale9Bitmap( bitmapData:BitmapData, left:Number = 0, top:Number = 0, right:Number = 0, bottom:Number = 0)
    {
        _bitmapData = bitmapData;
        _w = bitmapData.width;
        _h = bitmapData.height;
        
        v = new Vector.<Number>( 108, false); // ( ( 2 * 3 ) * 2 ) * 9 = 108
        uv = new Vector.<Number>( 108, false );
        index = new Vector.<int>( 54 , false );
        for(var i:int = 0; i<54; i++) index[i] = i;
        
        data = new Vector.<IGraphicsData>( 3, false );
        data[0] = new GraphicsBitmapFill( bitmapData );
        data[1] = new GraphicsTrianglePath( v, index, uv);
        data[2] = new GraphicsEndFill();
        
        setGrid( left, top, right, bottom );
    }
    
    protected function culc():void
    {
        culculated = true;
        
        var iw:Number = bitmapData.width - ( left + right);
        var ih:Number= bitmapData.height - ( top + bottom );
        var r:Number = width - right;
        var b:Number = height - bottom;
        
        
        setRect( LT, 0,0, left, top);
        setRect( T, left, 0, innerWidth, top);
        setRect( RT, r,0, right, top);
        
        setRect( L, 0,top, left, innerHeight );
        setRect( C, left,top, innerWidth, innerHeight );
        setRect( R, r,top, right, innerHeight );
        
        setRect( LB, 0, b, left, bottom );
        setRect( B, left, b, innerWidth, bottom );
        setRect( RB, r,b, right, bottom );
        
        
        setUV( LT, 0,0, left, top);
        setUV( T, left, 0, iw, top);
        setUV( RT, r,0, right, top);
        
        setUV( L, 0,top, left, ih );
        setUV( C, left,top, iw, ih );
        setUV( R, r,top, right, ih );
        
        b = bitmapData.height - bottom;
        setUV( LB, 0, b, left, bottom );
        setUV( B, left,b, iw, bottom );
        setUV( RB, r,b, right, bottom );
        
    }
    protected function setRect( offset:int, x:Number, y:Number, width:Number, height:Number ):void
    {
        v[offset] = v[offset + 6] = v[offset+10] = x;
        v[offset + 1] = v[offset + 3] = v[offset + 7] = y;
        v[offset + 2] = v[offset + 4] = v[offset + 8] = x + width;
        v[offset + 5] = v[offset + 9] = v[offset + 11] = y + height;
    }
    protected function setUV( offset:int, x:Number, y:Number, width:Number, height:Number ):void
    {
        var w:int = bitmapData.width;
        var h:int = bitmapData.height;
        uv[offset] = uv[offset + 6] = uv[offset+10] = x / w;
        uv[offset + 1] = uv[offset + 3] = uv[offset + 7] = y / h;
        uv[offset + 2] = uv[offset + 4] = uv[offset + 8] = (x + width) / w;
        uv[offset + 5] = uv[offset + 9] = uv[offset + 11] = (y + height) / h;
    }
    
    public function setGrid( left:Number = 0, top:Number = 0, right:Number = 0, bottom:Number = 0 ):void
    {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }
    
    public function get bitmapData():BitmapData
    {
        return _bitmapData;
    }
    public function set bitmapData( value:BitmapData ):void
    {
        _bitmapData = value;
        (data[0] as GraphicsBitmapFill).bitmapData = value;
    }
    
    public function get left():Number
    {
        return _l;
    }
    public function set left( value:Number ):void
    {
        _l = value < 0 ? 0 : value;
        culculated = false;
    }
    
    public function get right():Number
    {
        return _r;
    }
    public function set right( value:Number ):void
    {
        _r = value < 0 ? 0 : value;
        culculated = false;
    }
    
    public function get top():Number
    {
        return _t;
    }
    public function set top( value:Number ):void
    {
        _t = value < 0 ? 0 : value;
        culculated = false;
    }
    
    public function get bottom():Number
    {
        return _b;
    }
    public function set bottom( value:Number ):void
    {
        _b = value < 0 ? 0 : value;
        culculated = false;
    }
    
    public function get innerWidth():Number
    {
        return width - ( left + right );
    }
    
    public function get innerHeight():Number
    {
        return height - ( top + bottom );
    }
    
    public function get width():int
    {
        return _w;
    }
    public function set width( value:int ):void
    {
        _w = value < (left + right) ? (left+right) : value;  
        culculated = false;
    }
    
    public function get height():int
    {
        return _h;
    }
    public function set height( value:int ):void
    {
        _h = value < (left + right) ? (left+right) : value;
        culculated = false;
    }
    
    public function draw( graphics:Graphics ):void
    {
        if( culculated == false ) culc();
        graphics.drawGraphicsData( data );
    }
}