refactored package
This commit is contained in:
226
haxework/gui/View.hx
Executable file
226
haxework/gui/View.hx
Executable file
@@ -0,0 +1,226 @@
|
||||
package haxework.gui;
|
||||
|
||||
import flash.display.StageAlign;
|
||||
import flash.display.StageScaleMode;
|
||||
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 widthType(default, null):SizeType;
|
||||
public var heightType(default, null):SizeType;
|
||||
|
||||
public var width(default, set):Float;
|
||||
public var height(default, set):Float;
|
||||
|
||||
public var pWidth(default, set):Float;
|
||||
public var pHeight(default, set):Float;
|
||||
|
||||
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.CENTER;
|
||||
hAlign = HAlign.MIDDLE;
|
||||
}
|
||||
|
||||
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;
|
||||
skin.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_width(value:Float):Float {
|
||||
if (width != value || widthType != SizeType.NORMAL) {
|
||||
width = value;
|
||||
widthType = SizeType.NORMAL;
|
||||
invalidate();
|
||||
invalidateParent();
|
||||
}
|
||||
return width;
|
||||
}
|
||||
|
||||
private function set_height(value:Float):Float {
|
||||
if (height != value || heightType != SizeType.NORMAL) {
|
||||
height = value;
|
||||
heightType = SizeType.NORMAL;
|
||||
invalidate();
|
||||
invalidateParent();
|
||||
}
|
||||
return height;
|
||||
}
|
||||
|
||||
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_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.scaleMode = StageScaleMode.NO_SCALE;
|
||||
value.align = StageAlign.TOP_LEFT;
|
||||
value.addEventListener(Event.ENTER_FRAME, update);
|
||||
return value;
|
||||
}
|
||||
|
||||
public function invalidate(view:IView<Sprite>):Void {
|
||||
invalidated.push(view);
|
||||
}
|
||||
|
||||
public function update(?_):Void {
|
||||
while (invalidated.length > 0) {
|
||||
invalidated.shift().update();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user