智慧 + 毅力 = 无所不能

正确性、健壮性、可靠性、效率、易用性、可读性、可复用性、兼容性、可移植性...

导航

Feathers UI 扩展实例 For Starling Framework

Posted on 2013-01-22 15:37  Bill Yuan  阅读(1406)  评论(0编辑  收藏  举报
package com.renaun.controls
{
import feathers.controls.Label;
import feathers.core.FeathersControl;
import starling.display.DisplayObject;

public class Group extends FeathersControl
{
    public function Group()
    {
        super();
    
    }
    
    protected var children:Vector.<FeathersControl> = new Vector.<FeathersControl>();
    protected var childrenPercentValues:Vector.<int> = new Vector.<int>();

    protected var explicitTotal:Number;

    protected var percentTotalValue:Number;
    
    protected var isValid:Boolean = false;

    protected var isFirstTime:Boolean = false;

    /**
     *  Currently you have to add items in order, there is not addLayoutItemAt.
     * 
     *  @param item The target item you want to add to this group, add in order
     *      @param percentHeight The percent value of the VGroup's height. 100 = 100%, and if the total percent is> 100 it will be scaled by the value/totalPrecents.
     */
    public function addLayoutItem(item:FeathersControl, percentValue:int = -1):void
    {
        addChild(item);
        children.push(item);
        item.onResize.add(resizeHandler);
        childrenPercentValues.push(percentValue);
        measure();
        invalidate();
    }
    
    override protected function initialize():void
    {
        //trace("init");
        super.initialize();
        isFirstTime = true;
    }
    
    protected function measure():void
    {
    }
    
    protected function resizeHandler(target:FeathersControl, oldWidth:Number, oldHeight:Number):void
    {
        /*
        if (target is Label)
            trace("resizeHandler2["+(target as Label).text+"]: " + oldWidth + " - " + width);
        else
        trace("resizeHandler2["+target+"]: " + oldWidth + " - " + width);
        */
        isValid = false;
        invalidate();
    }
    
}
}
package com.renaun.controls
{
import feathers.core.FeathersControl;
import starling.display.DisplayObject;

public class VGroup extends Group
{
    public function VGroup()
    {
        super();
    
    }

    public var gap:int = 8;
    public var paddingLeft:int = 8;
    public var paddingRight:int = 8;
    public var paddingTop:int = 8;
    public var paddingBottom:int = 8;
    
    override protected function measure():void
    {
        var item:DisplayObject;
        explicitTotal = -gap;
        percentTotalValue = 0;
        var i:int;
        for (i = 0; i <children.length; i++)
        {
            item = children[i];
            if (childrenPercentValues[i]> 0)
                percentTotalValue += childrenPercentValues[i];
            else
            {
            
                if (item is FeathersControl)
                {
                    //item.height = (item as BitmapFontTextRenderer).measureText().y;
                    (item as FeathersControl).validate();
                }
                explicitTotal = explicitTotal + item.height;
            }      
            explicitTotal += gap;
        }
        isValid = true;
    }
    
    override protected function draw():void
    {
        // Delay items width/height are still not valid inside initalize method so have to wait for draw
        if (isFirstTime)
        {
            measure();
            isFirstTime = false;
        }
        var availableHeightForPercents:Number = explicitHeight - explicitTotal - paddingTop - paddingBottom;
        //trace("availableHeightForPercents: " + availableHeightForPercents + " - " + explicitTotal + " - " + explicitHeight);
        // Make smaller if the percents add up more then 100
        if (percentTotalValue> 100)
            availableHeightForPercents = availableHeightForPercents / (percentTotalValue / 100);
        
        //trace("percentTotalValue: " + percentTotalValue + " aHFP: " + availableHeightForPercents + " - " + explicitTotal + " - " + explicitHeight);
        var percentHeightValue:int = 0;
        var lastHeight:int = 0;
        var i:int;
        var item:DisplayObject;
        for (i = 0; i <children.length; i++)
        {
            item = children[i];
            // Set Y
            item.y = lastHeight + paddingTop;
            if (childrenPercentValues[i]> 0)
            {
                percentHeightValue = childrenPercentValues[i];
                // Set HEIGHT
                item.height = int(availableHeightForPercents * percentHeightValue / 100);
            }
            lastHeight += item.height + gap;
            
            // Set X
            item.x = paddingLeft;
            // Set WIDTH
            item.width = explicitWidth - paddingLeft - paddingRight;
            //trace("lastHeight : " + lastHeight);
        }
    }
}
}
var vgroup:VGroup = new VGroup();
vgroup.addLayoutItem(renderer);
vgroup.addLayoutItem(list, 100);
vgroup.addLayoutItem(appCountLabel);

addChild(vgroup);

转自:http://renaun.com/blog/2012/10/playing-with-feathers-ui-components-for-starling-framework/