//////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2008 Gabriel Montagné Láscaris-Comneno and Alberto // Brealey-Guzmán. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. // //////////////////////////////////////////////////////////////////////////////// package com.rojored.view.controls.carouselClasses.positionManagers { import com.rojored.view.controls.carouselClasses.IPosition; import com.rojored.view.controls.carouselClasses.IPositionManager; import com.rojored.view.controls.carouselClasses.positions.SimplePosition; import com.rojored.view.controls.carouselClasses.positions.Base3DPosition; import flash.events.Event; import flash.events.EventDispatcher; import mx.core.IMXMLObject; //-------------------------------------- // Events //-------------------------------------- /** * Dispatched when a change in the position manager or on any of its positions * warrants a carousel update. */ [Event("change")] /** * Controls the positions of a Grid carousel * * @author Gabriel Montagné Láscaris-Comneno gabriel@rojored.com * @version $Id$ */ public class GridPositionManager extends EventDispatcher implements IPositionManager, IMXMLObject { //-------------------------------------------------------------------------- // // Constructor // //-------------------------------------------------------------------------- /** * Constructor */ public function GridPositionManager() { super(); } //-------------------------------------------------------------------------- // // Variables // //-------------------------------------------------------------------------- /** * @private */ private var explicitOffscreenPositionPrevious:IPosition; /** * @private */ private var explicitOffscreenPositionNext:IPosition; /** * @private */ private var explicitOriginPosition:IPosition; /** * @private */ protected var positionsCache:Array = []; //-------------------------------------------------------------------------- // // Properties // //-------------------------------------------------------------------------- //-------------------------------------- // use3DPositions //-------------------------------------- /** * @private * Storage for the use3DPositions property */ private var _use3DPositions:Boolean; /** * If true positions will be of type * Base3DPosition instead of SimplePosition. * *

This has performance and rendering implications because when you * set the fp10 3D display object properties, even if you're not going to * use them, a matrix is generated, etc.

*/ public function get use3DPositions():Boolean { return _use3DPositions; } public function set use3DPositions(value:Boolean):void { _use3DPositions = value; } //---------------------------------- // rows //---------------------------------- /** * @private * Storage for the rows property */ private var _rows:Number = 5; [Bindable("change")] /** * The ammount of rows for the grid carousel. */ public function get rows():Number { return _rows; } public function set rows(value:Number):void { if (_rows == value) return; _rows = value; invalidatePositions(); } //---------------------------------- // columns //---------------------------------- /** * @private * Storage for the columns property */ private var _columns:Number = 5; [Bindable("change")] /** * The ammount of columns for the grid carousel. */ public function get columns():Number { return _columns; } public function set columns(value:Number):void { if (_columns == value) return; _columns = value; invalidatePositions(); } //---------------------------------- // rowSpacing //---------------------------------- /** * @private * Storage for the rowSpacing property */ private var _rowSpacing:Number = 50; [Bindable("change")] /** * Vertical spacing to which the positions get attached */ public function get rowSpacing():Number { return _rowSpacing; } public function set rowSpacing(value:Number):void { if (_rowSpacing == value) return; _rowSpacing = value; invalidatePositions(); } //---------------------------------- // columnSpacing //---------------------------------- /** * @private * Storage for the columnSpacing property */ private var _columnSpacing:Number = 50; [Bindable("change")] /** * Horizontal spacing to which the positions get attached */ public function get columnSpacing():Number { return _columnSpacing; } public function set columnSpacing(value:Number):void { if (_columnSpacing == value) return; _columnSpacing = value; invalidatePositions(); } //-------------------------------------- // xOffset //-------------------------------------- /** * @private * Storage for the xOffset propertx */ private var _xOffset:Number = 0; [Bindable("change")] /** * offset for all the positions on the X axis */ public function get xOffset():Number { return _xOffset; } public function set xOffset(value:Number):void { if (_xOffset == value) return; _xOffset = value; invalidatePositions(); } //-------------------------------------- // yOffset //-------------------------------------- /** * @private * Storage for the yOffset property */ private var _yOffset:Number = 0; [Bindable("change")] /** * offset for all the positions on the Y axis */ public function get yOffset():Number { return _yOffset; } public function set yOffset(value:Number):void { if (_yOffset == value) return; _yOffset = value; invalidatePositions(); } //---------------------------------- // offscreenPositionPrevious //---------------------------------- /** * @private * storage for the offscreenPositionPrevious property */ private var _offscreenPositionPrevious:IPosition; /** * @copy com.rojored.view.controls.carouselClasses.IPositionManager#offscreenPositionPrevious */ public function get offscreenPositionPrevious():IPosition { if (explicitOffscreenPositionPrevious) return explicitOffscreenPositionPrevious; if (!_offscreenPositionPrevious) offscreenPositionPrevious = use3DPositions ? new Base3DPosition() : new SimplePosition() ; return _offscreenPositionPrevious; } public function set offscreenPositionPrevious(value:IPosition):void { if (explicitOffscreenPositionPrevious == value) return; explicitOffscreenPositionPrevious = _offscreenPositionPrevious = value; // FIXME: now i'm not sure about invalidating for these offscreen // positions. we should probably just change them silently. dispatchEvent(new Event(Event.CHANGE)); } //---------------------------------- // offscreenPositionNext //---------------------------------- /** * @private * storage for the offscreenPositionNext property */ private var _offscreenPositionNext:IPosition; /** * @copy com.rojored.view.controls.carouselClasses.IPositionManager#offscreenPositionNext */ public function get offscreenPositionNext():IPosition { if (explicitOffscreenPositionNext) return explicitOffscreenPositionNext; if (!_offscreenPositionNext) offscreenPositionNext = use3DPositions ? new Base3DPosition() : new SimplePosition() ; return _offscreenPositionNext; } public function set offscreenPositionNext(value:IPosition):void { if (explicitOffscreenPositionNext == value) return; explicitOffscreenPositionNext = _offscreenPositionNext = value; // FIXME: now i'm not sure about invalidating for these offscreen // positions. we should probably just change them silently. dispatchEvent(new Event(Event.CHANGE)); } //---------------------------------- // originPosition //---------------------------------- /** * @property * Storage for the originPosition property */ private var _originPosition:IPosition; /** * @copy com.rojored.view.controls.carouselClasses.IPositionManager#originPosition */ public function get originPosition():IPosition { if (explicitOriginPosition) return explicitOriginPosition; if (!_originPosition) _originPosition = use3DPositions ? new Base3DPosition() : new SimplePosition() ; return _originPosition; } public function set originPosition(value:IPosition):void { explicitOriginPosition = value; if (_originPosition == value) return; _originPosition = value; // FIXME: now i'm not sure about invalidating for these offscreen // positions. we should probably just change them silently. invalidatePositions(); } //---------------------------------- // totalPositionCount //---------------------------------- [Bindable("change")] /** * @copy com.rojored.view.controls.carouselClasses.IPositionManager#totalPositionCount */ public function get totalPositionCount():int { return rows * columns; } //---------------------------------- // id //---------------------------------- /** * id of the StaticPositionManager instance. */ public var id:String; //-------------------------------------------------------------------------- // // Methods // //-------------------------------------------------------------------------- /** * Cleans the position cache and the generated (not the explicit) * previous and next positions. */ protected function invalidatePositions():void { _offscreenPositionPrevious = null; _offscreenPositionNext = null; _originPosition = null; positionsCache = []; dispatchEvent(new Event(Event.CHANGE)); } /** * @copy com.rojored.view.controls.carouselClasses.IPositionManager#getPositionAtIndex */ public function getPositionAtIndex(index:int):IPosition { if (positionsCache[index]) return positionsCache[index]; var column:Number = index % _columns; var row:Number = int(index / _columns); var position:IPosition = use3DPositions ? new Base3DPosition( (column * columnSpacing) + xOffset, (row * rowSpacing) + yOffset) : new SimplePosition( (column * columnSpacing) + xOffset, (row * rowSpacing) + yOffset) ; positionsCache[index] = position; return position; } /** * @private */ public function initialized(document:Object, id:String):void { this.id = id; } } }