Scale9 BitmapFill
とりあえずFP10の新描画APIでBitmapDataのScale9塗り。
FP自体が画像のScale9に対応してくれていれば楽なんですねどね
/**
* Copyright zahir ( http://wonderfl.net/user/zahir )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/4Iyb
*/
/*
FP10の新描画APIを使ってBitmapのScale9
部分リピートなんて出来ないので修正。
最適化してくださるなら是非
*/
package{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.system.LoaderContext;
[SWF(width="465", height ="465", backgroundColor="0xcccccc")]
public class ScalingBitmapTest extends Sprite{
private var l:Loader;
private var fill:ScalingBitmapFill;
private var s:Sprite;
public function ScalingBitmapTest(){
l = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void{
var bmp:Bitmap = l.content as Bitmap;
fill = new ScalingBitmapFill( bmp.bitmapData , 0,0, 40, 40, 40, 40 );
s = new Sprite();
addChild( s );
fill.width = 350;
fill.height = 250;
fill.smooth = true;
fill.draw( s.graphics );
stage.addEventListener( MouseEvent.MOUSE_MOVE, onMove );
});
l.load( new URLRequest("http://zahir.coresv.com/wonderfl/img/scalingBitmapTest.png"), new LoaderContext(true) );
}
private function onMove(e:MouseEvent):void{
e.updateAfterEvent();
fill.width = lim(mouseX + 25);
fill.height = lim(mouseY + 25);
s.graphics.clear();
fill.draw( s.graphics );
}
private function lim( num:Number ):Number{
return (num > 465) ? 465 : num;
}
}
}
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.GraphicsBitmapFill;
import flash.display.GraphicsPath;
import flash.display.IGraphicsData;
import flash.geom.Matrix;
import flash.display.GraphicsEndFill;
import flash.display.GraphicsSolidFill;
class ScalingBitmapFill
{
private var _bitmapData:BitmapData;
private var _smooth:Boolean;
private var _left:Number;
private var _top:Number;
private var _right:Number;
private var _bottom:Number;
private var _width:Number;
private var _height:Number;
private var _innerWidth:Number;
private var _innerHeight:Number;
private var v:Vector.<IGraphicsData>;
private var flg:Boolean = false;
public function ScalingBitmapFill(
bitmapData:BitmapData,
width:int, height:int,
left:int = 0, top:int = 0, right:int = 0, bottom:int = 0,
smooth:Boolean = false) {
v = new Vector.<IGraphicsData>;
setData( bitmapData, width, height, left, top, right, bottom, smooth);
}
public function setData(
bitmapData:BitmapData,
width:int, height:int,
left:int = 0, top:int = 0, right:int = 0, bottom:int = 0,
smooth:Boolean = false):void{
this.bitmapData = bitmapData;
this.width = width;
this.height = height;
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
this.smooth = smooth;
}
public function draw(graphics:Graphics):Graphics
{
if (flg == false) calc();
graphics.drawGraphicsData( v );
return graphics;
}
private function calc():void
{
var minWidth:Number = left + right;
var minHeight:Number = top + bottom;
var innerWidth:Number = this.innerWidth, innerHeight:Number = this.innerHeight;
if (minWidth >= width) width = minWidth, innerWidth = 0;
if (minHeight >= height) height = minHeight, innerHeight = 0;
var r:int = width - right;
var b:int = height - bottom;
var sx:Number = innerWidth / (_bitmapData.width - minWidth);
var sy:Number = innerHeight / (_bitmapData.height - minHeight);
var offsetX1:Number = left - left * sx;
var offsetX2:Number = width % _bitmapData.width;
var offsetY1:Number = top - top * sy;
var offsetY2:Number = height % _bitmapData.height;
v.length = 0;
// LT
v.push( new GraphicsBitmapFill( _bitmapData, null , true, smooth), drawRect(0, 0, left, top));
// T
v.push( new GraphicsBitmapFill( _bitmapData, box( offsetX1,0,sx) , true, smooth), drawRect(left,0, innerWidth,top));
// RT
v.push( new GraphicsBitmapFill( _bitmapData, box(offsetX2) , true, smooth), drawRect(r,0, right,top));
// L
v.push( new GraphicsBitmapFill( _bitmapData, box( 0,offsetY1, 1,sy ), true, smooth), drawRect(0,top, left, innerHeight));
// Center
v.push( new GraphicsBitmapFill( _bitmapData, box( offsetX1, offsetY1, sx,sy ), true, smooth), drawRect(left,top, innerWidth, innerHeight));
// right
v.push( new GraphicsBitmapFill( _bitmapData, box( offsetX2,offsetY1, 1,sy ), true, smooth), drawRect(r,top, right, innerHeight));
// LB
v.push( new GraphicsBitmapFill( _bitmapData, box( 0, offsetY2 ), true, smooth), drawRect(0, b, left,top) );
// B
v.push( new GraphicsBitmapFill( _bitmapData, box( offsetX1, offsetY2, sx ), true, smooth), drawRect(left,b, innerWidth,top) );
// RB
v.push( new GraphicsBitmapFill( _bitmapData, box( offsetX2, offsetY2 ), true, smooth), drawRect(r,b, right,top) );
flg = true;
}
public function set bitmapData(value:BitmapData):void {
_bitmapData = value.clone();
flg = false;
}
public function get left():Number { return _left; }
public function set left(value:Number):void
{
if (value == _left) return;
value = (value < 0) ? 0 : value;
_left = value;
flg = false;
}
public function get top():Number { return _top; }
public function set top(value:Number):void
{
if (value == _top) return;
value = (value < 0) ? 0 : value;
_top = value;
flg = false;
}
public function get right():Number { return _right; }
public function set right(value:Number):void
{
if (value == _right) return;
value = (value < 0) ? 0 : value;
_right = value;
flg = false;
}
public function get bottom():Number { return _bottom; }
public function set bottom(value:Number):void
{
if (value == _bottom) return;
value = (value < 0) ? 0 : value;
_bottom = value;
flg = false;
}
public function get width():Number { return _width; }
public function set width(value:Number):void
{
if (value == _width) return;
value = (value < 0) ? 0 : value;
_width = value;
flg = false;
}
public function get height():Number { return _height; }
public function set height(value:Number):void
{
if (value == _height) return;
value = (value < 0) ? 0 : value;
_height = value;
flg = false;
}
public function get smooth():Boolean { return _smooth; }
public function set smooth(value:Boolean):void
{
if (value == _smooth) return;
_smooth = value;
flg = false;
}
public function get innerWidth():Number { return width - (left + top); }
public function get innerHeight():Number { return height - (top + bottom); }
private function drawRect(x:Number, y:Number, width:Number, height:Number):GraphicsPath {
var path:GraphicsPath = new GraphicsPath();
path.commands = Vector.<int>([1, 2, 2, 2, 2]);
path.data = Vector.<Number>([x,y, x+width,y, x+width,y+height, x,y+height, x,y]);
return path;
}
private function box( dx:int = 0, dy:int = 0, scaleX:Number = 1, scaleY:Number = 1):Matrix{
var m:Matrix = new Matrix();
m.scale( scaleX, scaleY );
m.translate( dx, dy );
return m;
}
}