2013年2月5日火曜日

Feathers Example プロジェクトの作成2

ListExample.as
package
{
 import feathers.controls.List;
 import feathers.data.ListCollection;
 import feathers.themes.MetalWorksMobileTheme;

 import starling.display.Sprite;
 import starling.events.Event;

 public class ListExample extends Sprite
 {
  public function ListExample()
  {
   this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
  }

  protected var theme:MetalWorksMobileTheme;

  protected var list:List;

  protected function addedToStageHandler(event:Event):void
  {
   this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

   this.theme = new MetalWorksMobileTheme(this.stage);

   var list:List = new List();
   list.width = this.stage.stageWidth;
   list.height = this.stage.stageHeight;
   this.addChild(list);

   var listData:Object;
   var listTable:Array = [];
   for(var i:int=0;i<1000;i++)
   {
    listData = { text : i.toString()+":"+"アイテム" };
    listTable.push(listData);
   }
   var groceryList:ListCollection = new ListCollection(listTable);
   list.dataProvider = groceryList;
   list.itemRendererProperties.labelField = "text";
   list.itemRendererType = BasicItemRenderer;
  }
 }
}
BasicItemRenderer.as
package
{
 import feathers.controls.Label;
 import feathers.controls.List;
 import feathers.controls.Scroller;
 import feathers.controls.renderers.IListItemRenderer;
 import feathers.core.FeathersControl;

 import flash.geom.Point;
 import flash.text.TextFormat;

 import starling.display.Quad;
 import starling.events.Event;
 import starling.events.Touch;
 import starling.events.TouchEvent;
 import starling.events.TouchPhase;

 public class BasicItemRenderer extends FeathersControl implements IListItemRenderer
 {
  public function BasicItemRenderer()
  {
   this.addEventListener(TouchEvent.TOUCH,touchHandler);
   this.addEventListener(Event.REMOVED_FROM_STAGE,removedFromStageHandler);
  }

  protected var itemLabel:Label;

  protected var itemBG:Quad;

  protected var _index:int = -1;

  protected var touchPointID:Number;

  private static const HELPER_POINT:Point = new Point();

  protected var hasScrolled:Boolean = false;

  public function get index():int
  {
   return this._index;
  }

  public function set index(value:int):void
  {
   if(this._index == value) return;
   this._index = value;
   this.invalidate(INVALIDATION_FLAG_DATA);
  }

  protected var _owner:List;

  public function get owner():List
  {
   return List(this._owner);
  }

  public function set owner(value:List):void
  {
   if(this._owner == value) return;
   if(this._owner) this._owner.removeEventListener(Event.SCROLL, owner_scrollHandler);
   this._owner = value;
   if(this._owner) this._owner.addEventListener(Event.SCROLL, owner_scrollHandler);
   this.invalidate(INVALIDATION_FLAG_DATA);
  }

  protected var _data:Object;

  public function get data():Object
  {
   return this._data;
  }

  public function set data(value:Object):void
  {
   if(this._data == value)
   {
    return;
   }
   this._data = value;
   this.invalidate(INVALIDATION_FLAG_DATA);
  }

  protected var _isSelected:Boolean;

  public function get isSelected():Boolean
  {
   return this._isSelected;
  }

  public function set isSelected(value:Boolean):void
  {
   if(this._isSelected == value) return;
   this._isSelected = value;
   this.invalidate(INVALIDATION_FLAG_SELECTED);
   this.dispatchEventWith(Event.CHANGE);
  }

  override protected function initialize():void
  {
   if(!this.itemBG)
   {
    this.itemBG = new Quad(1,1,Math.floor(Math.random() * 0xffffff));
    this.addChild(this.itemBG);
   }

   if(!this.itemLabel)
   {
    this.itemLabel = new Label();
    this.addChild(this.itemLabel);
   }
  }

  override protected function draw():void
  {
   const dataInvalid:Boolean = this.isInvalid(INVALIDATION_FLAG_DATA);
   const selectionInvalid:Boolean = this.isInvalid(INVALIDATION_FLAG_SELECTED);
   var sizeInvalid:Boolean = this.isInvalid(INVALIDATION_FLAG_SIZE);

   if(dataInvalid) this.commitData();
   sizeInvalid = this.autoSizeIfNeeded() || sizeInvalid;
   if(dataInvalid || sizeInvalid) this.layout();
  }

  protected function autoSizeIfNeeded():Boolean
  {
   const needsWidth:Boolean = isNaN(this.explicitWidth);
   const needsHeight:Boolean = isNaN(this.explicitHeight);
   if(!needsWidth && !needsHeight) return false;
   this.itemLabel.width = NaN;
   this.itemLabel.height = NaN;
   this.itemLabel.validate();
   var newWidth:Number = this.explicitWidth;
   if(needsWidth) newWidth = this.itemLabel.width;
   this.explicitHeight = 150;
   var newHeight:Number = this.explicitHeight;
   if(needsHeight) newHeight = this.itemLabel.height;

   this.itemBG.width = newWidth;
   this.itemBG.height = newHeight;

   return this.setSizeInternal(newWidth, newHeight, false);
  }

  protected function commitData():void
  {
   if(this._data)
   {
    this.itemLabel.text = this._data.text.toString();
   } else {
    this.itemLabel.text = "";
   }
  }

  protected function layout():void
  {
   this.itemBG.width = this.actualWidth;
   this.itemBG.height = this.actualHeight;

   this.itemLabel.width = this.actualWidth;
   this.itemLabel.height = this.actualHeight;
   this.itemLabel.y = (this.actualHeight / 2) - (this.itemLabel.y / 2);
  }

  protected function touchHandler(event:TouchEvent):void
  {
   const touches:Vector.<touch> = event.getTouches(this);
   if(touches.length == 0)
   {
    //hover has ended
    return;
   }
   if(this.touchPointID >= 0)
   {
    var touch:Touch;
    for each(var currentTouch:Touch in touches)
    {
     if(currentTouch.id == this.touchPointID)
     {
      touch = currentTouch;
      break;
     }
    }
    if(!touch)
    {
     return;
    }
    if(touch.phase == TouchPhase.ENDED)
    {
     this.touchPointID = -1;
     this.itemBG.visible = true;
     if(!this.hasScrolled)
     {                    
      touch.getLocation(this, HELPER_POINT);
      //check if the touch is still over the target
      //also, only change it if we're not selected. we're not a toggle.
      if(this.hitTest(HELPER_POINT, true) != null && !this._isSelected)
      {
       this.isSelected = true;
       trace(_index);
      }
     }

     return;
    }
   }
   else
   {
    for each(touch in touches)
    {
     if(touch.phase == TouchPhase.BEGAN)
     {
      this.touchPointID = touch.id;
      this.hasScrolled = false;

      return;
     }
    }
   }
  }

  protected function removedFromStageHandler(event:Event):void
  {
   this.touchPointID = -1;
  }

  protected function owner_scrollHandler(event:Event):void
  {
   this.hasScrolled = true;
  }        

 }
}

0 件のコメント:

コメントを投稿