Created
March 5, 2012 09:52
-
-
Save leeprobert/1977677 to your computer and use it in GitHub Desktop.
A layout class for automatically positioning and resizing child elements of a container to a grid.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cc.eurostarlive.layouts | |
{ | |
import mx.core.ILayoutElement; | |
import mx.core.IVisualElement; | |
import spark.layouts.supportClasses.LayoutBase; | |
public class GridConstraintBasicLayout extends LayoutBase | |
{ | |
/* | |
The Layout class has size properties for the grid cells | |
the X and Y positions of objects within this layout will then | |
snap to the grid. | |
There is also a flag that can be set to true to convert pixels to columns or rows | |
*/ | |
protected var _cellWidth:Number = 1; | |
[Bindable] | |
public function set cellWidth(v:Number):void { _cellWidth = v; }; | |
public function get cellWidth():Number { return _cellWidth; }; | |
protected var _cellHeight:Number = 1; | |
[Bindable] | |
public function set cellHeight(v:Number):void { _cellHeight = v; }; | |
public function get cellHeight():Number { return _cellHeight; }; | |
protected var _horizontalGap:Number = 1; | |
[Bindable] | |
public function set horizontalGap(v:Number):void { _horizontalGap = v; }; | |
public function get horizontalGap():Number { return _horizontalGap; }; | |
protected var _verticalGap:Number = 1; | |
[Bindable] | |
public function set verticalGap(v:Number):void { _verticalGap = v; }; | |
public function get verticalGap():Number { return _verticalGap; }; | |
protected var _convertPixelsToColumnsAndRows:Boolean = true; | |
[Bindable] | |
public function set convertPixelsToColumnsAndRows(v:Boolean):void { _convertPixelsToColumnsAndRows = v; }; | |
public function get convertPixelsToColumnsAndRows():Boolean { return _convertPixelsToColumnsAndRows; }; | |
protected var _resizeElementsToGrid:Boolean = true; | |
[Bindable] | |
public function set resizeElementsToGrid(v:Boolean):void { _resizeElementsToGrid = v; }; | |
public function get resizeElementsToGrid():Boolean { return _resizeElementsToGrid; }; | |
protected var _isForceRoundUpResize:Boolean = true; | |
[Bindable] | |
public function set isForceRoundUpResize(v:Boolean):void { _isForceRoundUpResize = v; }; | |
public function get isForceRoundUpResize():Boolean { return _isForceRoundUpResize; }; | |
//-------------------------------------------- | |
/* | |
Getters used only by containers using this class to do calculations based on rows and columns created | |
*/ | |
protected var _numberOfRows:int; | |
[Bindable] | |
public function get numberOfRows():int { return _numberOfRows; }; | |
protected var _numberOfColumns:int; | |
[Bindable] | |
public function get numberOfColumns():int { return _numberOfColumns; }; | |
//-------------------------------------------- | |
public function GridConstraintBasicLayout() | |
{ | |
super(); | |
} | |
//-------------------------------------------------------------------------- | |
override public function updateDisplayList(width:Number, height:Number):void | |
{ | |
super.updateDisplayList(width,height); | |
var layoutElement:ILayoutElement; | |
var count:uint = target.numElements; | |
for(var i:int = 0; i<count; i++) | |
{ | |
layoutElement = useVirtualLayout? target.getVirtualElementAt(i) : | |
target.getElementAt(i); | |
var xPos:Number; | |
var yPos:Number; | |
var w:Number; | |
var h:Number; | |
var sourceX:Number = IVisualElement(layoutElement).x; | |
var sourceY:Number = IVisualElement(layoutElement).y; | |
var sourceW:Number = IVisualElement(layoutElement).width? IVisualElement(layoutElement).width : IVisualElement(layoutElement).getPreferredBoundsWidth(); | |
var sourceH:Number = IVisualElement(layoutElement).height? IVisualElement(layoutElement).height : IVisualElement(layoutElement).getPreferredBoundsHeight(); | |
if(convertPixelsToColumnsAndRows) | |
{ | |
xPos = sourceX==0? 0 : (sourceX*cellWidth)-cellWidth; | |
yPos = sourceY==0? 0 : (sourceY*cellHeight)-cellHeight; | |
} | |
else | |
{ | |
if(sourceX<cellWidth) | |
{ | |
xPos = sourceX<cellWidth/2? 0 : cellWidth; | |
} | |
else | |
{ | |
xPos = sourceX%cellWidth<cellWidth/2? cellWidth*(Math.floor(sourceX/cellWidth)) : | |
cellWidth*(Math.ceil(sourceX/cellWidth)); | |
} | |
if(sourceY<cellHeight) | |
{ | |
yPos = sourceY<cellHeight/2? 0 : cellHeight; | |
} | |
else | |
{ | |
yPos = sourceY%cellHeight<cellHeight/2? cellHeight*(Math.floor(sourceY/cellHeight)) : | |
cellHeight*(Math.ceil(sourceY/cellHeight)); | |
} | |
} | |
if(resizeElementsToGrid) | |
{ | |
if(convertPixelsToColumnsAndRows) | |
{ | |
w = sourceW*cellWidth; | |
h = sourceH*cellHeight; | |
} | |
else | |
{ | |
if(sourceW<cellWidth) | |
{ | |
w = cellWidth; | |
} | |
else | |
{ | |
w = sourceW%cellWidth<cellWidth/2? isForceRoundUpResize? cellWidth*(Math.ceil(sourceW/cellWidth)) : cellWidth*(Math.floor(sourceW/cellWidth)) : | |
cellWidth*(Math.ceil(sourceW/cellWidth)); | |
} | |
if(sourceH<cellHeight) | |
{ | |
h = cellHeight; | |
} | |
else | |
{ | |
h = sourceH%cellHeight<cellHeight/2? isForceRoundUpResize? cellHeight*(Math.ceil(sourceH/cellHeight)) : cellHeight*(Math.floor(sourceH/cellHeight)) : | |
cellHeight*(Math.ceil(sourceH/cellHeight)); | |
} | |
} | |
} | |
else | |
{ | |
w = sourceW; | |
h = sourceH; | |
} | |
_numberOfColumns = Math.round(w/cellWidth); | |
_numberOfRows = Math.round(h/cellHeight); | |
layoutElement.setLayoutBoundsPosition(xPos,yPos); | |
layoutElement.setLayoutBoundsSize(w,h); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment