273 lines
6.0 KiB
Haxe
Executable File
273 lines
6.0 KiB
Haxe
Executable File
package haxework.gui;
|
|
|
|
import haxework.gui.skin.ISize;
|
|
import haxework.gui.core.SizeType;
|
|
import haxework.gui.core.HAlign;
|
|
import haxework.gui.core.VAlign;
|
|
import flash.events.Event;
|
|
import flash.display.Stage;
|
|
import haxework.gui.skin.FakeSkin;
|
|
import haxework.gui.skin.ISkin;
|
|
|
|
import flash.display.Sprite;
|
|
|
|
class View implements IView<Sprite> {
|
|
|
|
private static var counter:Int = 0;
|
|
public static var updater(default, null):Updater = new Updater();
|
|
|
|
public var id(default, null):String;
|
|
|
|
public var x(default, set):Float;
|
|
public var y(default, set):Float;
|
|
|
|
public var w(default, set):Float;
|
|
public var h(default, set):Float;
|
|
|
|
public var widthType(default, null):SizeType;
|
|
public var heightType(default, null):SizeType;
|
|
|
|
public var width(get, set):Float;
|
|
public var height(get, set):Float;
|
|
|
|
public var pWidth(default, set):Float;
|
|
public var pHeight(default, set):Float;
|
|
|
|
public var contentSize(default, set):Bool;
|
|
|
|
public var hAlign(default, set):HAlign;
|
|
public var vAlign(default, set):VAlign;
|
|
|
|
public var leftMargin(default, set):Float;
|
|
public var rightMargin(default, set):Float;
|
|
public var topMargin(default, set):Float;
|
|
public var bottomMargin(default, set):Float;
|
|
public var margins(null, set):Float;
|
|
|
|
public var content(default, null):Sprite;
|
|
public var skin(default, set):ISkin<Sprite, IView<Sprite>>;
|
|
|
|
public var parent(default, null):Null<IView<Sprite>>;
|
|
|
|
public function new() {
|
|
id = Type.getClassName(Type.getClass(this)) + counter++;
|
|
content = new Sprite();
|
|
skin = new FakeSkin();
|
|
x = 0;
|
|
y = 0;
|
|
width = 100;
|
|
height = 100;
|
|
margins = 0;
|
|
vAlign = VAlign.NONE;
|
|
hAlign = HAlign.NONE;
|
|
}
|
|
|
|
private function currentSkin():ISkin<Sprite, IView<Sprite>> {
|
|
return skin;
|
|
}
|
|
|
|
private function invalidate():Void {
|
|
updater.invalidate(this);
|
|
}
|
|
|
|
private function invalidateParent():Void {
|
|
if (parent != null)
|
|
updater.invalidate(parent);
|
|
}
|
|
|
|
public function update():Void {
|
|
content.x = x;
|
|
content.y = y;
|
|
if (contentSize && skin != null && Std.is(skin, ISize)) {
|
|
var size:ISize = cast(skin, ISize);
|
|
if (!Math.isNaN(size.width)) width = size.width;
|
|
if (!Math.isNaN(size.height)) height = size.height;
|
|
}
|
|
currentSkin().draw(this);
|
|
}
|
|
|
|
private function set_x(value:Float):Float {
|
|
if (x != value) {
|
|
x = value;
|
|
invalidate();
|
|
}
|
|
return x;
|
|
}
|
|
private function set_y(value:Float):Float {
|
|
if (y != value) {
|
|
y = value;
|
|
invalidate();
|
|
}
|
|
return y;
|
|
}
|
|
|
|
private function set_w(value:Float):Float {
|
|
if (w != value) {
|
|
w = value;
|
|
invalidate();
|
|
}
|
|
return w;
|
|
}
|
|
private function set_h(value:Float):Float {
|
|
if (h != value) {
|
|
h = value;
|
|
invalidate();
|
|
}
|
|
return h;
|
|
}
|
|
|
|
private function get_width():Float {
|
|
return w;
|
|
}
|
|
|
|
private function get_height():Float {
|
|
return h;
|
|
}
|
|
|
|
private function set_width(value:Float):Float {
|
|
if (w != value || widthType != SizeType.NORMAL) {
|
|
w = value;
|
|
widthType = SizeType.NORMAL;
|
|
invalidate();
|
|
invalidateParent();
|
|
}
|
|
return w;
|
|
}
|
|
|
|
private function set_height(value:Float):Float {
|
|
if (h != value || heightType != SizeType.NORMAL) {
|
|
h = value;
|
|
heightType = SizeType.NORMAL;
|
|
invalidate();
|
|
invalidateParent();
|
|
}
|
|
return h;
|
|
}
|
|
|
|
private function set_pWidth(value:Float):Float {
|
|
if (pWidth != value || widthType != SizeType.PERCENT) {
|
|
pWidth = value;
|
|
widthType = SizeType.PERCENT;
|
|
invalidate();
|
|
invalidateParent();
|
|
}
|
|
return pWidth;
|
|
}
|
|
|
|
private function set_pHeight(value:Float):Float {
|
|
if (pHeight != value || heightType != SizeType.PERCENT) {
|
|
pHeight = value;
|
|
heightType = SizeType.PERCENT;
|
|
invalidate();
|
|
invalidateParent();
|
|
}
|
|
return pHeight;
|
|
}
|
|
|
|
private function set_contentSize(value:Bool):Bool {
|
|
if (contentSize != value) {
|
|
contentSize = value;
|
|
invalidate();
|
|
invalidateParent();
|
|
}
|
|
return contentSize;
|
|
}
|
|
|
|
private function set_hAlign(value:HAlign):HAlign {
|
|
if (hAlign != value) {
|
|
hAlign = value;
|
|
invalidate();
|
|
invalidateParent();
|
|
}
|
|
return hAlign;
|
|
}
|
|
|
|
private function set_vAlign(value:VAlign):VAlign {
|
|
if (vAlign != value) {
|
|
vAlign = value;
|
|
invalidate();
|
|
invalidateParent();
|
|
}
|
|
return vAlign;
|
|
}
|
|
|
|
private function set_leftMargin(value:Float):Float {
|
|
if (leftMargin != value) {
|
|
leftMargin = value;
|
|
invalidate();
|
|
invalidateParent();
|
|
}
|
|
return leftMargin;
|
|
}
|
|
|
|
private function set_rightMargin(value:Float):Float {
|
|
if (rightMargin != value) {
|
|
rightMargin = value;
|
|
invalidate();
|
|
invalidateParent();
|
|
}
|
|
return rightMargin;
|
|
}
|
|
|
|
private function set_topMargin(value:Float):Float {
|
|
if (topMargin != value) {
|
|
topMargin = value;
|
|
invalidate();
|
|
invalidateParent();
|
|
}
|
|
return topMargin;
|
|
}
|
|
|
|
private function set_bottomMargin(value:Float):Float {
|
|
if (bottomMargin != value) {
|
|
bottomMargin = value;
|
|
invalidate();
|
|
invalidateParent();
|
|
}
|
|
return bottomMargin;
|
|
}
|
|
|
|
private function set_margins(value:Float):Float {
|
|
leftMargin = rightMargin = topMargin = bottomMargin = value;
|
|
invalidate();
|
|
invalidateParent();
|
|
return value;
|
|
}
|
|
|
|
private function set_skin(value:ISkin<Sprite, IView<Sprite>>):ISkin<Sprite, IView<Sprite>> {
|
|
skin = value;
|
|
invalidate();
|
|
return skin;
|
|
}
|
|
}
|
|
|
|
|
|
class Updater {
|
|
|
|
public var stage(null, set):Stage;
|
|
private var invalidated:Array<IView<Sprite>>;
|
|
|
|
public function new() {
|
|
invalidated = [];
|
|
}
|
|
|
|
private function set_stage(value:Stage):Stage {
|
|
value.addEventListener(Event.ENTER_FRAME, update);
|
|
return value;
|
|
}
|
|
|
|
public function invalidate(view:IView<Sprite>):Void {
|
|
invalidated.push(view);
|
|
}
|
|
|
|
public function update(?_):Void {
|
|
try {
|
|
while (invalidated.length > 0) {
|
|
invalidated.shift().update();
|
|
}
|
|
} catch (error:Dynamic) {
|
|
L.e("UPDATE", "", error);
|
|
}
|
|
}
|
|
|
|
} |