////////////////////////////////////////////////////////////////////////////////
//
// 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.Base2DPosition;
import com.rojored.view.controls.carouselClasses.positions.Base3DPosition;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
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")]
//--------------------------------------
// Other metadata
//--------------------------------------
[DefaultProperty("positions")]
/**
* Position manager which stores a set of predefined positions.
*
* @author Gabriel Montagné Láscaris-Comneno gabriel@rojored.com
* @version $Id$
*/
public class StaticPositionManager extends EventDispatcher
implements IPositionManager, IMXMLObject
{
//--------------------------------------------------------------------------
//
// Constructor
//
//--------------------------------------------------------------------------
/**
* Constructor
*/
public function StaticPositionManager()
{
super();
}
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
//--------------------------------------
// defaultTo2DPositions
//--------------------------------------
/**
* If true, undefined offscreen positions
* (origin, offscreenPositionNext,
* offscreenPositionPrevious) will be of type
* Base3DPosition instead of Base2DPosition.
*
*
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 var use3DPositions:Boolean; //-------------------------------------- // 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 (!_offscreenPositionNext) _offscreenPositionNext = use3DPositions ? new Base3DPosition() : new Base2DPosition() ; return _offscreenPositionNext; } public function set offscreenPositionNext(value:IPosition):void { if (_offscreenPositionNext == value) return; if (_offscreenPositionNext) unsubscribeToPositionsChange([_offscreenPositionNext]); _offscreenPositionNext = value; suscribeToPositionsChange([_offscreenPositionNext]); dispatchEvent(new Event(Event.CHANGE)); } //-------------------------------------- // 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 (!_offscreenPositionPrevious) _offscreenPositionPrevious = use3DPositions ? new Base3DPosition() : new Base2DPosition() ; return _offscreenPositionPrevious; } public function set offscreenPositionPrevious(value:IPosition):void { if (_offscreenPositionPrevious == value) return; if (_offscreenPositionPrevious) unsubscribeToPositionsChange([_offscreenPositionPrevious]); _offscreenPositionPrevious = value; suscribeToPositionsChange([_offscreenPositionPrevious]); dispatchEvent(new Event(Event.CHANGE)); } //-------------------------------------- // originPosition //-------------------------------------- /** * @private * Storage for the originPosition property */ private var _originPosition:IPosition; /** * @copy com.rojored.view.controls.carouselClasses.IPositionManager#originPosition */ public function get originPosition():IPosition { if (!_originPosition) _originPosition = use3DPositions ? new Base3DPosition() : new Base2DPosition() ; return _originPosition; } public function set originPosition(value:IPosition):void { if (_originPosition == value) return; if (_originPosition) unsubscribeToPositionsChange([_originPosition]); _originPosition = value; suscribeToPositionsChange([_originPosition]) dispatchEvent(new Event(Event.CHANGE)); } //-------------------------------------- // totalPositionCount //-------------------------------------- [Bindable("totalPositionCountChanged")] /** * @copy com.rojored.view.controls.carouselClasses.IPositionManager#totalPositionCount */ public function get totalPositionCount():int { return positions.length } //---------------------------------- // positions //---------------------------------- /** * @private * Storage for the positions property */ private var _positions:Array = []; /** * The positions this position manager tends. */ public function get positions():Array { return _positions; } public function set positions(value:Array):void { if (_positions == value) return; var positionCountChanged:Boolean; if (_positions) unsubscribeToPositionsChange(_positions); // TODO: double check that this is working correctly. I'm playing it // by ear here, to prevent binding warnings. positionCountChanged = ( (value && value.length) != (_positions && _positions.length) ); _positions = value; suscribeToPositionsChange(_positions); dispatchEvent(new Event(Event.CHANGE)); if (positionCountChanged) dispatchEvent(new Event("totalPositionCountChanged")); } //---------------------------------- // id //---------------------------------- /** * @private */ private var _id:String; /** * ID of the StaticPositionManager instance. */ public function get id():String { return _id; } /** * @private */ public function set id(value:String):void { _id = value; } //-------------------------------------------------------------------------- // // Methods // //-------------------------------------------------------------------------- /** * @private */ public function initialized(document:Object, id:String):void { this.id = id; } /** * @copy com.rojored.view.controls.carouselClasses.IPositionManager#getPositionAtIndex */ public function getPositionAtIndex(index:int):IPosition { return IPosition(positions[index]); } /** * @private */ private function suscribeToPositionsChange(positions:Array):void { for each (var position:IPosition in positions) { if (position is IEventDispatcher) { IEventDispatcher(position).addEventListener( Event.CHANGE, position_changeHandler, false, 0, true ); } } } /** * @private */ private function unsubscribeToPositionsChange(positions:Array):void { for each (var position:IPosition in positions) { if (position is IEventDispatcher) { IEventDispatcher(position).removeEventListener( Event.CHANGE, position_changeHandler ); } } } //-------------------------------------------------------------------------- // // Event handlers // //-------------------------------------------------------------------------- /** * @private */ private function position_changeHandler(event:Event):void { dispatchEvent(new Event(Event.CHANGE)); } } }