2
* This file is part of the dis-Emi-A HaXe Library. Copyright (c) edA-qa mort-ora-y
3
* For full copyright and license information please refer to doc/license.txt.
8
import flash.display.DisplayObject;
11
private class FreeItemHolder implements Sizeable, implements BaseItemHolder
13
public var item : Widget;
14
public var sizeTypes : Array<SizeType>; //left,top,width,height
15
public var sizes : Array<Float>;
16
public var visible : Bool;
17
public var zIndex : Int;
18
public var addOrder : Int; //used to track original insertion order for zOrder calculations
20
public var layout : FreeLayout;
22
public function getWidget() : Widget { return item; }
23
public function setVisible( vis : Bool ) : Void { visible = vis; }
27
sizeTypes = new Array<SizeType>();
28
sizes = new Array<Float>();
31
addOrder = 0; //though default should not be used
34
public function getSizeType() : SizeType
36
return sizeTypes[layout.itemIndex];
39
public function setPlacement( offset : Float, size : Float ) : Void
42
sizes[layout.itemIndex] = size;
47
* Creates a layout for arbitrary placement.
49
* For relative sizing behaves as a vertical layout for primary/secondary sizing, though
50
* beware the Fill sizes do *NOT* work!
53
* spacing - this is applied only around outside edges and does not impact individual items
54
* collapseMargin - if true, equivalent to 0 spacing
56
* Requires manual update after modifications
58
class FreeLayout extends Layout<FreeItemHolder>
61
public var itemIndex : Int;
66
public var offset : mathx.Point2; //how much to offset each item
68
private function new()
73
spacing = padding = 0; //better defaults for free layout
74
offset = Point2.at( 0, 0 );
78
* Creates a simple FreeLayout where all the items are placed
79
* individually and behave normally.
81
static public function simple() : FreeLayout
83
return new FreeLayout();
86
override public function _resize( w : Float, h : Float )
90
var horzSizer = new Sizer( 0, true, false );
91
var vertSizer = new Sizer( 0, true, true );
101
horzSizer.size( w, h, items );
104
vertSizer.size( h, w, items );
107
horzSizer.size( w, h, items );
110
vertSizer.size( h, w, items );
114
var left = i.sizes[0] + padding + spacing + offset.x;
115
var top = i.sizes[1] + padding + spacing + offset.y;
116
var width = i.sizes[2] - 2 * padding;
117
var height = i.sizes[3] - 2 * padding;
119
//hide items not partially shown
122
if( (showPartial && (left >= w || top >= h || (left+width) <= 0 || (top+height) <= 0))
123
|| (!showPartial && (left < 0 || top < 0 || (left+width) > w || (top+height) > h) ) )
125
i.item.setVisible( false );
130
i.item.move( left, top );
131
i.item.resize( width, height );
132
i.item.setVisible( i.visible );
139
//FEATURE: zOrder could be moved to Layout base (though only makes sense for overlapping items)
140
function resetZOrder()
142
var sorted = items.copy(); //shallow copy
143
sorted.sort( function( a, b ) {
144
var ret = a.zIndex - b.zIndex;
147
//when z is equal, resort to original add order
148
return a.addOrder - b.addOrder;
151
//FLASH: This can produce error 2006: The Supplied index is out of bounds
152
//This happens if the same widget has been added twice -- removed won't see this since
153
//flash won't consider the child removed. (TODO: fix/prevent this)
154
for( i in 0...sorted.length )
155
setChildIndex( sorted[i].item.getNative(), i );
157
//finally restore background position
158
if( background != null )
159
setChildIndex( background.getNative(), 0 );
163
* Adds an item to the layout.
165
* @param item [in,keep] which item to add
166
* @param zIndex [in] in which layer to add it (default 0). The layers will decide
167
* the order in which these are placed in the FreeLayout -- lower numbers
168
* are the lower layers.
169
* To remain simple, and retain natural flash use, the order in which items are
170
* added will be tracked. If the zOrder of two items are the same, the one which
171
* was added first will be lower in the final zOrder.
173
public function add( item : Widget,
174
left : SizeType, top : SizeType,
175
width : SizeType, height : SizeType,
178
Assert.notNull( item );
180
var fih = new FreeItemHolder();
182
fih.sizeTypes[0] = left;
183
fih.sizeTypes[1] = top;
184
fih.sizeTypes[2] = width;
185
fih.sizeTypes[3] = height;
189
fih.addOrder = addOrder++;
192
item.setVisible( false );
193
insertToItems( fih );
197
* This is a specialized form where all sizes are SizeType.Primary and is used quite
198
* often for FreeLayout's more often than other sizing types in our current uses
200
public function addPS( item : Widget, left : Float, top : Float, width : Float, height : Float, zIndex : Int = 0 )
202
add( item, SizeType.Primary( left ), SizeType.Primary( top ), SizeType.Primary( width ), SizeType.Primary( height ), zIndex );
205
public function addPSCtr( item : Widget, x : Float, y : Float, width : Float, height : Float, zIndex : Int = 0 )
207
var ctr = item.getCenter();
208
add( item, SizeType.Primary( x-width*ctr.x ), SizeType.Primary( y-height*ctr.y ), SizeType.Primary( width ), SizeType.Primary( height ), zIndex );
211
public function addPSRect( item : Widget, rect : mathx.Rect2, zIndex : Int = 0 )
213
add( item, SizeType.Primary( rect.left ), SizeType.Primary( rect.top ), SizeType.Primary( rect.width ), SizeType.Primary( rect.height ), zIndex );
216
override public function getPrefWidth() : ui.SizeType
218
return SizeType.Fill( 1 );
221
override public function getPrefHeight() : ui.SizeType
223
return SizeType.Fill( 1 );