Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.paperlesspost.components.coverflow
{
import com.paperlesspost.components.coverflow.renderers.IItemRenderer;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
// import flash.geom.PerspectiveProjection;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.text.TextField;
import mx.collections.ArrayCollection;
import com.paperlesspost.components.coverflow.events.CoverflowEvent;
public class Coverflow extends Sprite
{
public var fontFace:String = "TributeRoman";
private var _data:Array;
private var _items:Sprite;
private var _itemsArray:Array;
private var _scroll:ScrollBar;
private var _renderer:Class;
private var _selected:uint = 1;
private var _orientation:String = "horizontal";
private var _itemSize:uint = 100;
private var _width:uint = 100;
private var _height:uint = 100;
private var _rendererSizeOffset:int = 15;
private var _label:TextField;
public function Coverflow()
{
super();
init();
}
public function get label():TextField
{
if( _label == null ) {
_label = new TextField();
_label.selectable = false;
_label.embedFonts = true;
_label.y = _height - 55;
_label.width = _width;
addChild( _label );
}
return _label;
}
public function set text( value:String ):void
{
label.htmlText = "<p align='center'><font face='"+fontFace+"' size='16'>"+ value +"</font></p>";
}
public function get orientation():String
{
return _orientation;
}
public function set orientation(value:String):void
{
if (value !== _orientation)
{
_orientation = value;
}
}
public function get itemSize():uint
{
return _itemSize;
}
public function set itemSize(value:uint):void
{
if (value !== _itemSize)
{
_itemSize = value;
}
}
public function get selected():uint
{
return _selected;
}
public function set selected(value:uint):void
{
//if (value !== _selected)
//{
_selected = value;
render();
_scroll.setTimedPosition( ((_width/2)+(_itemSize*_selected)-(_itemSize)-(_rendererSizeOffset)), 0 ); // -(_itemSize/2)
//}
}
public function get renderer():Class
{
return _renderer;
}
public function set renderer(value:Class):void
{
_renderer = value;
}
public function get data():Array
{
return _data;
}
public function set data(value:Array):void
{
if (value !== _data) {
while(_items.numChildren > 0) {
var child:* = _items.removeChildAt(_items.numChildren-1);
child.destroy();
child = null;
}
_data = value;
draw();
render();
if( _scroll != null ) {
_selected = 1;
_scroll.setPosition(((_width/2)+(_itemSize*_selected)-(_itemSize)-(_rendererSizeOffset)),0);
_scroll.updateScroller();
selected = 1;
}
}
}
public function setSize( width:uint, height:uint ):void
{
_width = width;
_height = height;
if( _scroll ) {
_scroll.setSize( _width, _height );
}
if( _data != null ) {
render();
}
}
public function next( event:Event=null ):void
{
selected += 1;
}
public function last( event:Event=null ):void
{
selected -= 1;
}
private function init():void
{
_items = new Sprite();
addChild( _items );
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
_scroll = new ScrollBar( this, _items );
_scroll.addEventListener(Event.CHANGE, onScrollChange);
_scroll.addEventListener("scrollUp", onScrollUp);
_scroll.addEventListener("scrollDown", onScrollDown);
_scroll.addEventListener("update", onScrollUpdate);
_scroll.setSize( _width, _height );
addChild( _scroll );
_scroll.updateScroller();
selected = 1;
}
private function onScrollUp(event:Event):void
{
last( event );
if( _itemsArray ) {
var item:IItemRenderer = _itemsArray[_selected];
dispatchEvent(new CoverflowEvent(CoverflowEvent.COVERFLOW_MOVED, item.data, item.item ));
}
}
private function onScrollDown(event:Event):void
{
next( event );
var item:IItemRenderer = _itemsArray[_selected];
dispatchEvent(new CoverflowEvent(CoverflowEvent.COVERFLOW_MOVED, item.data, item.item ));
}
private function onScrollChange(event:Event):void
{
var length:uint = _itemsArray.length;
var pos:int = _scroll.positionX + _width/2;
_selected = int(((_scroll.positionX+(_width/2)-(_rendererSizeOffset))/(_items.width-(_width/2)-(_rendererSizeOffset)))*length)-1;
render();
_scroll.setTimedPosition( ((_width/2)+(_itemSize*_selected)-(_itemSize)-(_rendererSizeOffset)), 0 );
var item:IItemRenderer = _itemsArray[_selected];
dispatchEvent(new CoverflowEvent(CoverflowEvent.COVERFLOW_MOVED, item.data, item.item ));
}
private function onScrollUpdate(event:Event):void
{
var length:uint = _itemsArray.length;
var pos:int = _scroll.positionX + _width/2;
_selected = int(((_scroll.positionX+(_width/2)-(_rendererSizeOffset))/(_items.width-(_width/2)-(_rendererSizeOffset)))*length)-1;
render();
}
private function draw():void
{
var count:uint = 0;
_itemsArray = [];
for each( var data:* in _data ) {
var item:IItemRenderer = new _renderer( this, count, data );
if( _orientation == "horizontal" ) {
item.x = (_width/2)+_itemSize * count;
item.y = 15;
}else{
item.y = (_height/2)+_itemSize * count;
}
_itemsArray.push( item );
_items.addChild( item as DisplayObject );
count++;
}
_items.graphics.clear();
_items.graphics.beginFill(0xFFFFFF,0);
_items.graphics.drawRect(0,0,(_width/2)+_itemSize * _itemsArray.length, _height);
}
private function render():void
{
/*
var pp:PerspectiveProjection=new PerspectiveProjection();
pp.fieldOfView = 30;
pp.projectionCenter=new Point(_itemsArray[_selected].x,_itemSize/2);
_items.transform.perspectiveProjection=pp;
*/
var length:uint = _itemsArray.length;
var item:IItemRenderer;
var index:uint;
for( var i:uint = 0; i < length; i += 1 ) {
item = _itemsArray[i];
index = item.index;
if( index < _selected ) {
item.beforeSelected();
}else if( index > _selected ) {
item.afterSelected();
}else if( index == _selected ){
item.selected();
}
if( i == _selected || // should write this a little nicer
i == _selected + 1 || i == _selected - 1 ||
i == _selected + 2 || i == _selected - 2 ||
i == _selected + 3 || i == _selected - 3 ) {
item.display = true;
}else if( index != 0 && index != _itemsArray.length ){
// item.display = false;
}
}
if( _scroll != null ) {
_scroll.updateScrollee();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.