package com.zehfernando.ui { import flash.display.DisplayObject; import flash.display.MovieClip; import flash.display.Sprite; import flash.geom.Rectangle; /** * @author Zeh Fernando - z at zeh.com.br */ public class GridContainer extends MovieClip { // Constants public const ORDER_VERTICAL:String = "v"; public const ORDER_HORIZONTAL:String = "h"; // Properties protected var items:Array; protected var drawOrder:String; protected var _minimumWidth:Number; protected var _maximumWidth:Number; protected var _minimumHeight:Number; protected var _maximumHeight:Number; protected var _marginY:Number; protected var _marginX:Number; protected var _height:Number; protected var _width:Number; protected var _scrollX:Number = 0; protected var _scrollY:Number = 0; // Instances protected var container:Sprite; // ================================================================================================================ // CONSTRUCTOR ---------------------------------------------------------------------------------------------------- public function GridContainer() { items = []; _minimumWidth = 0; _maximumWidth = 600; _minimumHeight = 0; _maximumHeight = 600; _marginY = 1; _marginX = 0; _scrollX = 0; _scrollY = 0; drawOrder = ORDER_VERTICAL; container = new Sprite(); super.addChild(container); redrawAllChildren(); redrawContainerPosition(); } // ================================================================================================================ // INSTANCE functions --------------------------------------------------------------------------------------------- public function redrawContainerPosition(): void { container.scrollRect = new Rectangle(_scrollX, _scrollY, _maximumWidth, _maximumHeight); } public function redrawAllChildren(): void { // Redraw the position of all items var i:int; _width = 0; _height = 0; var ni:DisplayObject; for (i = 0; i < items.length; i++) { ni = items[i]; redrawChild(ni); } } public function setDrawOrder(__order:String): void { drawOrder = __order; redrawAllChildren(); } public function redrawChild(__child:DisplayObject): void { if (!isNaN(minimumWidth)) __child.width = minimumWidth; switch (drawOrder) { case ORDER_HORIZONTAL: if (_width > 0) _width += _marginX; __child.x = _width; _width += __child.width; break; default: if (_height > 0) _height += _marginY; __child.y = _height; _height += __child.height; } } public function redraw(): void { redrawContainerPosition(); redrawAllChildren(); } // ================================================================================================================ // SUPER functions ------------------------------------------------------------------------------------------------ override public function addChild(__child:DisplayObject): DisplayObject { items.push(__child); container.addChild(__child); redrawChild(__child); return __child; } override public function removeChild(__child:DisplayObject): DisplayObject { container.removeChild(__child); var ii:int = getChildIndex(__child); items.splice(ii, 1); redraw(); return __child; } override public function getChildIndex(__child:DisplayObject): int { return items.indexOf(__child); } override public function getChildAt(__index:int): DisplayObject { return items[__index]; } public function removeAllChildren(): void { for (var i:uint = 0; i < items.length; i++) { container.removeChild(items[i]); } items = []; redraw(); } override public function get numChildren(): int { return items.length; } // ================================================================================================================ // ACCESSOR functions --------------------------------------------------------------------------------------------- public function get scrollY(): Number { return _scrollY; } public function set scrollY(__value:Number): void { _scrollY = __value; redrawContainerPosition(); } public function get minimumWidth(): Number { return _minimumWidth; } public function set minimumWidth(__value:Number): void { _minimumWidth = __value; if (_maximumWidth < _minimumWidth) _maximumWidth = _minimumWidth; redrawAllChildren(); } public function get maximumWidth(): Number { return _maximumWidth; } public function set maximumWidth(__value:Number): void { _maximumWidth = __value; redrawContainerPosition(); //redrawAllChildren(); } public function get minimumHeight(): Number { return _minimumHeight; } public function set minimumHeight(__value:Number): void { _minimumHeight = __value; redrawAllChildren(); } public function get maximumHeight(): Number { return _maximumHeight; } public function set maximumHeight(__value:Number): void { _maximumHeight = __value; redrawContainerPosition(); //redrawAllChildren(); } // Overrides override public function get width(): Number { return _width; } override public function get height(): Number { return _height; } } }