package // com.example.programmingas3.spritearranger { //import com.example.programmingas3.geometricshapes.IGeometricShape; import flash.display.Shape; import flash.display.Sprite; import flash.events.MouseEvent; import flash.geom.Rectangle; public class actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite extends Sprite { public var size:Number; public var lineColor:Number = 0x000000; public var fillColor:Number = 0xDDDDEE; public var shapeType:String = "GeometricSprite"; /** * An instance of a purely geometric shape, that is, one that defines * a shape mathematically but not visually. */ public var geometricShape:actionscript_book_SpriteArranger_com_example_programmingas3_geometricshapes_IGeometricShape; /** * Keeps track of the currently selected shape. * This is a static property, so there can only be one actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite * selected at any given time. */ public static var selectedSprite:actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite; /** * Holds a border rectangle that is shown when this actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite instance is selected. */ public var selectionIndicator:Shape; public function actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite(size:Number = 100, lColor:Number = 0x000000, fColor:Number = 0xDDDDEE) { this.size = size; this.lineColor = lColor; this.fillColor = fColor; this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); } public function drawShape():void { // to be overridden in subclasses } private function onMouseDown(evt:MouseEvent):void { this.showSelected(); // limits dragging to the area inside the canvas var boundsRect:Rectangle = this.parent.getRect(this.parent); boundsRect.width -= this.size; boundsRect.height -= this.size; this.startDrag(false, boundsRect); } public function onMouseUp(evt:MouseEvent):void { this.stopDrag(); } private function showSelected():void { if (this.selectionIndicator == null) { // draws a red rectangle around the selected shape this.selectionIndicator = new Shape(); this.selectionIndicator.graphics.lineStyle(1.0, 0xFF0000, 1.0); this.selectionIndicator.graphics.drawRect(-1, -1, this.size + 1, this.size + 1); this.addChild(this.selectionIndicator); } else { this.selectionIndicator.visible = true; } if (actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite.selectedSprite != this) { if (actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite.selectedSprite != null) { actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite.selectedSprite.hideSelected(); } actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite.selectedSprite = this; } } private function hideSelected():void { if (this.selectionIndicator != null) { this.selectionIndicator.visible = false; } } /** * Returns true if this shape's selection rectangle is currently showing. */ public function isSelected():Boolean { return !(this.selectionIndicator == null || this.selectionIndicator.visible == false); } public override function toString():String { return this.shapeType + " of size " + this.size + " at " + this.x + ", " + this.y; } } }