Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 185 lines (170 sloc) 5.306 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
package com.renaun.controls
{
import flash.utils.Dictionary;

import feathers.core.DisplayListWatcher;
import feathers.core.FeathersControl;
import feathers.core.IFeathersControl;

import starling.display.DisplayObject;
import starling.display.Stage;
import starling.events.Event;
import starling.events.ResizeEvent;

/*
Add display objects here
For each one do a onResize signal of the parent FeathersControl
Some might say listen for stage resize, these you do the measure() and invalidate, which should bubble the children if they are affected
Global Singleton Manager or instance it?
instance it for now.
*/
public class RelativePositionLayoutManager
{
public static const LEFT:String = "left";
public static const RIGHT:String = "right";
public static const TOP:String = "top";
public static const BOTTOM:String = "bottom";
public static const HORIZONTAL_CENTER:String = "horizontalCenter";
public static const VERTICAL_CENTER:String = "verticalCenter";

public function RelativePositionLayoutManager(root:FeathersControl, resizeWithStage:Boolean = true)
{
this.root = root;
stage = root.stage
root.width = stage.stageWidth;
root.height = stage.stageHeight;
if (resizeWithStage)
stage.addEventListener(Event.RESIZE, resizeHandler);
}

private var stage:Stage;
private var root:FeathersControl;
private var controls:Dictionary = new Dictionary();
private var controlsByParent:Dictionary = new Dictionary();

protected function resizeHandler(event:Event):void
{
root.width = stage.stageWidth;
root.height = stage.stageHeight;
}

public function setPosition(control:DisplayObject, positionType:String, value:Number):void
{
if (value != value) // isNaN check
return;
if (!controls[control])
controls[control] = new Object();

switch (positionType)
{
case LEFT:
case RIGHT:
case TOP:
case BOTTOM:
case HORIZONTAL_CENTER:
case VERTICAL_CENTER:
controls[control][positionType] = value;
break;
default:
return;
break;
}
// Extra step for convience
if (control.width == 0 && control.height == 0 && control is FeathersControl)
(control as FeathersControl).validate();
// valid property
if (control.parent is FeathersControl)
{
var featherControl:FeathersControl = control.parent as FeathersControl;
//featherControl.onResize.add(parentResizeHandler);
featherControl.addEventListener(ResizeEvent.RESIZE, parentResizeHandler);
if (!controlsByParent[featherControl])
controlsByParent[featherControl] = new Vector.<DisplayObject>();
if ((controlsByParent[featherControl] as Vector.<DisplayObject>).indexOf(control) == -1)
{
(controlsByParent[featherControl] as Vector.<DisplayObject>).push(control);
}
}
layout(control, positionType, value);
}

protected function layoutAll(control:DisplayObject):void
{
var props:Object = controls[control];
for (var positionType:String in props)
{
layout(control, positionType, props[positionType]);
}
}
protected function layout(control:DisplayObject, positionType:String, value:Number):void
{
switch (positionType)
{
case LEFT:
control.x = control.parent.x + value;
if (controls[control][RIGHT] != undefined)
control.width = control.parent.width - value - controls[control][RIGHT];
break;
case RIGHT:
if (controls[control][LEFT] != undefined)
control.width = control.parent.width - value - controls[control][LEFT];
else
control.x = control.parent.width - value - control.width;
break;
case TOP:
control.y = control.parent.y + value;
if (controls[control][BOTTOM] != undefined)
{
control.height = (control.parent.height - controls[control][BOTTOM]) - value;
}
break;
case BOTTOM:
if (controls[control][TOP] != undefined)
{
control.height = (control.parent.height - controls[control][TOP]) - value;
}
else
control.y = control.parent.height - value - control.height;
break;
case HORIZONTAL_CENTER:
control.x = (control.parent.width/2 - control.width/2) + value;
break;
case VERTICAL_CENTER:
control.y = (control.parent.height/2 - control.height/2) + value;
break;
default:
return;
break;
}
}

/**
* {fill:true} - makes left,right,top,bottom = 0
*
* Use the format {left: 2, right: 3} etc...
*
*/
public function setPositionValues(control:DisplayObject, positions:Object):void
{
if (positions.fill)
{
setPositionValues(control, {left:0, right:0, top:0, bottom:0});
}
else
{
for (var key:String in positions)
setPosition(control, key, positions[key]);
}
}

//protected function parentResizeHandler(target:FeathersControl, oldWidth:Number, oldHeight:Number):void
protected function parentResizeHandler(event:Event):void
{
if (controlsByParent[event.target])
{
var items:Vector.<DisplayObject> = (controlsByParent[event.target] as Vector.<DisplayObject>);
for (var i:int = 0; i < items.length; i++)
{
layoutAll(items[i]);
if (items[i] is FeathersControl)
(items[i] as FeathersControl).invalidate();
}

}
}

public function invalidate(control:DisplayObject):void
{
layoutAll(control);
if (control is FeathersControl)
(control as FeathersControl).invalidate();
}

}
}
Something went wrong with that request. Please try again.