package basement_core.display { import flash.display.DisplayObject; import flash.display.Sprite; import flash.geom.Rectangle; public class Scale9Mask extends Sprite { // Constants private static const ALPHA:Number = 1; // Display Objects private var topLeftCorner:Sprite; private var topRightCorner:Sprite; private var bottomLeftCorner:Sprite; private var bottomRightCorner:Sprite; private var topRectangle:Sprite; private var leftRectangle:Sprite; private var rightRectangle:Sprite; private var bottomRectangle:Sprite; private var mainRectangle:Sprite; // Properties private var topLeftRadius:Number; private var topRightRadius:Number; private var bottomRightRadius:Number; private var bottomLeftRadius:Number; private var leftMargin:Number; private var bottomMargin:Number; private var topMargin:Number; private var rightMargin:Number; private var innerWidth:Number; private var innerHeight:Number; public var color:uint; public function Scale9Mask(width:Number, height:Number, topLeftRadius:Number, topRightRadius:Number, bottomRightRadius:Number, bottomLeftRadius:Number, color:uint=0, alpha:Number=1) { this.topLeftRadius = topLeftRadius; this.topRightRadius = topRightRadius; this.bottomLeftRadius = bottomLeftRadius; this.bottomRightRadius = bottomRightRadius; initMargins(); this.color = color; this.innerWidth = width - this.leftMargin - this.rightMargin; this.innerHeight = height - this.topMargin - this.bottomMargin; initDisplayObjects(); } private function initMargins():void { if(this.topLeftRadius >this.topRightRadius){ this.topMargin = this.topLeftRadius; }else { this.topMargin = this.topRightRadius; } if(this.bottomLeftRadius >this.topLeftRadius){ this.leftMargin = this.bottomLeftRadius; }else { this.leftMargin = this.topLeftRadius; } if(this.bottomLeftRadius >this.bottomRightRadius){ this.bottomMargin = this.bottomLeftRadius; }else { this.bottomMargin = this.bottomRightRadius; } if(this.topRightRadius >this.bottomRightRadius){ this.rightMargin = this.topRightRadius; }else { this.rightMargin = this.bottomRightRadius; } } private function initDisplayObjects():void { this.topRectangle = createRect(this.leftMargin, 0, this.innerWidth, this.topMargin); this.rightRectangle = createRect(this.leftMargin+this.innerWidth, this.topMargin, this.rightMargin, this.innerHeight); this.bottomRectangle = createRect(this.leftMargin, this.topMargin+this.innerHeight, this.innerWidth, this.bottomMargin); this.leftRectangle = createRect(0, this.topMargin, this.leftMargin, this.innerHeight); this.mainRectangle = createRect(this.leftMargin, this.topMargin, this.innerWidth, this.innerHeight); if(this.topRightRadius>0){ this.topRightCorner = createCorner(this.rightRectangle.x, 0, this.rightMargin, this.topMargin, 0); }else{ this.topRightCorner = createRect(this.rightRectangle.x, 0, this.rightMargin, this.topMargin); } if(this.bottomRightRadius>0){ this.bottomRightCorner = createCorner(this.rightRectangle.x+this.rightMargin, this.bottomRectangle.y, this.rightMargin, this.bottomMargin, 90); }else{ this.bottomRightCorner = createRect(this.rightRectangle.x, this.bottomRectangle.y, this.rightMargin, this.bottomMargin); } if(this.topLeftRadius>0){ this.topLeftCorner = createCorner(0, this.topMargin, this.leftMargin, this.topMargin, 270); }else{ this.topLeftCorner = createRect(0, 0, this.leftMargin, this.topMargin); } if(this.bottomLeftRadius>0){ this.bottomLeftCorner = createCorner(this.leftMargin, this.bottomRectangle.y+this.bottomMargin, this.leftMargin, this.bottomMargin, 180); }else{ this.bottomLeftCorner = createRect(0, this.bottomRectangle.y, this.leftMargin, this.bottomMargin); } } // Getters / Setters override public function set width(width : Number) : void { innerWidth = width - leftMargin - rightMargin; topRectangle.width = mainRectangle.width = bottomRectangle.width = innerWidth; var newLeft : Number = mainRectangle.x + mainRectangle.width; topRightCorner.x = newLeft; bottomRightCorner.x = newLeft; if(bottomRightRadius > 0) { bottomRightCorner.x += rightMargin; } rightRectangle.x = newLeft; } override public function set height(height : Number) : void { innerHeight = height - topMargin - bottomMargin; leftRectangle.height = mainRectangle.height = rightRectangle.height = innerHeight; var newTop : Number = mainRectangle.y + mainRectangle.height; bottomLeftCorner.y = newTop; if(bottomLeftRadius > 0) { bottomLeftCorner.y += leftMargin; } bottomRightCorner.y = newTop; bottomRectangle.y = newTop; } override public function set scaleX(scale:Number):void { this.topRectangle.scaleX = this.mainRectangle.scaleX = this.bottomRectangle.scaleX = scale; var newLeft:Number = this.mainRectangle.x + this.mainRectangle.width; this.topRightCorner.x = newLeft; this.bottomRightCorner.x = newLeft; if(this.bottomRightRadius>0){ this.bottomRightCorner.x+=this.rightMargin; } this.rightRectangle.x = newLeft; } override public function get scaleX():Number { return this.mainRectangle.scaleX; } override public function set scaleY(scale:Number):void { this.leftRectangle.scaleY = this.mainRectangle.scaleY = this.rightRectangle.scaleY = scale; var newTop:Number = this.mainRectangle.y + this.mainRectangle.height; this.bottomLeftCorner.y = newTop; if(this.bottomLeftRadius>0){ this.bottomLeftCorner.y+=+leftMargin; } this.bottomRightCorner.y = newTop; this.bottomRectangle.y = newTop; } override public function get scaleY():Number { return this.mainRectangle.scaleY; } // Features // Helpers private function createRect(xPos:Number, yPos:Number, w:Number, h:Number):Sprite { var sp:Sprite = new Sprite(); sp.graphics.beginFill(this.color, ALPHA); sp.graphics.drawRect(0, 0, w, h); sp.graphics.endFill(); sp.x = xPos; sp.y = yPos; addChild(sp); return sp; } private function createCorner(xPos:Number, yPos:Number, w:Number, h:Number, rotation:Number):Sprite { var sp:Sprite = new Sprite(); sp.graphics.beginFill(this.color, ALPHA); sp.graphics.moveTo(0, 0); sp.graphics.curveTo(w, 0, w, h); sp.graphics.lineTo(0, h); sp.graphics.lineTo(0, 0); sp.graphics.endFill(); sp.rotation = rotation; sp.x = xPos; sp.y = yPos; addChild(sp); return sp; } // Logger private function log(msg:String):void { trace("[Scale9Mask] "+msg); } // Handlers } }