
Scaling in Flash using partitioning of DisplayObject to 9 pieces is well known to developers and designers. The problems of using it goes from not affecting children of DisplayObject, proper scaling only when same(equal) scaling to X and Y, no scaling support of the Bitmaps and so on...
The idea is to make bitmapData of the DisplayObject, partition and drawing 9 parts of the inital bitmapData according to supplied scale9Grid rectangle and scaling requested.

I wanted to have OO approach to the problem YourSprite inherts ScaleBitmapSprite inherts ->Spite and not helper static object SpriteHelper.createScale9GridAvailableSprite(YourSprite) returning new Scale9GridAvailableSprite or Scale9GridAvailableSprite Class Scale9GridAvailableSprite(sourceSprite) that takes source in constructor.
First I took the Troy Gilbert version and made it 00. But I wasn't satisfied with making 9 Bitmap children and add to original Spite (ScaleComponentsSprite).
Then I wrote ScaleBitmapSprite based on Didier Brun's ScaleBitmapSprite which uses one BitmapData instead of nine incapsulated in Bitmaps as in above example and have advance options like outer Rectangle for effects and filters.
So you can use my version like:
var s:ScaleBitmapSprite=new ScaleBitmapSprite();
addChild(s);
s.scale9Grid=new Rectangle(5, 5, 90, 90 );
s.width=500;
or inherits to as much SkinClasses in your library or run-time as you like.

package
{
/**
* ...
* @author alex winx (www.winx.ws)
*/
public class MySkinClass extends ScaleBitmapSprite
{
}
}
Using OO way you can extend functionality in different or scenario that suit you.
Class code:
package {
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Rectangle;
public class ScaleBitmapSprite extends Sprite {
// ------------------------------------------------
//
// ---o properties
//
// ------------------------------------------------
private var _width : Number;
private var _height : Number;
private var _inner : Rectangle;
private var _outer : Rectangle;
private var _bitmapData : BitmapData;
private var _outerWidth : Number;
private var _outerHeight : Number;
private var _minWidth : Number;
private var _minHeight : Number;
private var _originalWidth:Number;
private var _originalHeight:Number;
// ------------------------------------------------
//
// ---o constructor
//
// ------------------------------------------------
public function ScaleBitmapSprite():void
{
_width = super.width;
_height = super.height;
_bitmapData = new BitmapData(_width, _height, true, 0);
_bitmapData.draw(this, new Matrix(), null, null, null, true);
}
override public function get scale9Grid():Rectangle { return super.scale9Grid; }
override public function set scale9Grid(value:Rectangle):void
{
//super.scale9Grid = value;
_inner = value;
if (!_inner) return;
if (_outer!=null){
_width = _outer.width;
_height = _outer.height;
_outerWidth = _bitmapData.width - _outer.width;
_outerHeight = _bitmapData.height - _outer.height;
} else {
_width = _inner.width;
_height = _inner.height;
_outerWidth = 0;
_outerHeight = 0;
}
_minWidth = _bitmapData.width - _inner.width - _outerWidth;
_minHeight = _bitmapData.height - _inner.height - _outerHeight;
draw();
}
public function get scale9OuterGrid():Rectangle { return _outer }
public function set scale9OuterGrid(value:Rectangle):void
{
_outer = value;
if (!_outer) return;
_width = _outer.width;
_height = _outer.height;
_outerWidth = _bitmapData.width - _outer.width;
_outerHeight = _bitmapData.height - _outer.height;
_minWidth = _bitmapData.width - _inner.width - _outerWidth;
_minHeight = _bitmapData.height - _inner.height - _outerHeight;
draw();
}
// ------------------------------------------------
//
// ---o public methods
//
// ------------------------------------------------
/**
* draw
*/
public function draw():void {
graphics.clear();
ScaleBitmap.draw( _bitmapData,
graphics,
Math.floor(_width + _outerWidth),
Math.floor(_height + _outerHeight),
_inner,
_outer);
}
// ------------------------------------------------
//
// ---o public methods
//
// ------------------------------------------------
/**
* get width
*/
override public function get width():Number{
return _width;
}
/**
* set width
*/
override public function set width(w:Number):void
{
if (_inner)
{
removeAll();
//reset
this.scaleX = 1;
this.scaleY = 1;
_width=Math.max(w,_minWidth);
draw();
}
else
{
_width = w;
super.width = w;
}
}
/**
* get height
*/
override public function get height():Number{
return _height;
}
/**
* set height
*/
override public function set height(h:Number):void
{
if (_inner)
{
removeAll();
//reset
this.scaleX = 1;
this.scaleY = 1;
_height=Math.max(h, _minHeight);
draw();
}
else
{
_height = h;
super.height = h;
}
}
/**
* get bitmapData
*/
public function get bitmapData() : BitmapData{
return _bitmapData;
}
/**
* set bitmapData
*/
public function set bitmapData(b : BitmapData) : void {
_bitmapData=b;
draw();
}
/**
*
*/
public function removeAll():void
{
var i:int = 0
var num:int = this.numChildren;
for (; i < num; i++)
removeChildAt(0);
}
}
}
Source:download
No comments:
Post a Comment