Skip to content

Instantly share code, notes, and snippets.

@nitrobin
Created November 17, 2014 08:10
Show Gist options
  • Save nitrobin/59bb8493a238f822a67b to your computer and use it in GitHub Desktop.
Save nitrobin/59bb8493a238f822a67b to your computer and use it in GitHub Desktop.
StablexUI ScaleBox Widget
package;
import flash.display.DisplayObject;
import ru.stablex.ui.widgets.Widget;
enum VPolicy{
Top;
Bottom;
Middle;
Justify;
}
enum HPolicy{
Left;
Right;
Center;
Justify;
}
class ScaleBoxExt extends Widget {
public var paddingLeft : Float = 0;
public var paddingRight : Float = 0;
public var paddingTop : Float = 0;
public var paddingBottom : Float = 0;
public var fitContent:Bool = true;
public var vPolicy:VPolicy = VPolicy.Middle;
public var hPolicy:HPolicy = HPolicy.Center;
public function new() {
super();
}
override public function applyLayout() : Void {
var w = this.w - (paddingLeft + paddingRight);
var h = this.h - (paddingTop + paddingBottom);
for (i in 0...this.numChildren) {
var child:DisplayObject = this.getChildAt(i);
if (Std.is(child, Widget)) {
var widget:Widget = cast(child, Widget);
if (vPolicy == VPolicy.Justify && hPolicy == HPolicy.Justify) {
widget.scaleX = 1.0;
widget.scaleY = 1.0;
widget.resize(w, h);
} else if(vPolicy == VPolicy.Justify) {
if(widget.w == 0 || w == 0) {
continue;
}
var sX = w / widget.w;
widget.scaleX = sX;
widget.scaleY = sX;
widget.h = h / sX;
} else if(hPolicy == HPolicy.Justify) {
if(widget.h == 0 || h == 0) {
continue;
}
var sY = h / widget.h;
widget.scaleX = sY;
widget.scaleY = sY;
widget.w = w / sY;
} else {
if(widget.w == 0 || widget.h == 0) {
continue;
}
var sX = w / widget.w;
var sY = h / widget.h;
var sS = fitContent ? Math.min(sX, sY) : Math.max(sX, sY);
widget.scaleX = sS;
widget.scaleY = sS;
}
if(vPolicy == VPolicy.Top || vPolicy == VPolicy.Justify) {
widget.y = paddingTop;
} else if(vPolicy == VPolicy.Middle) {
widget.y = (h - widget.h * widget.scaleY) / 2.0 + paddingTop;
} else if(vPolicy == VPolicy.Bottom) {
widget.y = h - widget.h * widget.scaleY - paddingBottom + paddingTop;
} else {
widget.y = paddingTop;
}
if(hPolicy == HPolicy.Left || hPolicy == HPolicy.Justify) {
widget.x = paddingLeft;
} else if(hPolicy == HPolicy.Center) {
widget.x = (w - widget.w * widget.scaleX) / 2.0 + paddingLeft;
} else if(hPolicy == HPolicy.Right) {
widget.x = w - widget.w * widget.scaleX - paddingRight + paddingLeft;
}
} else {
trace("Unsupported!");
}
}//for()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment