diff --git a/src/main/haxework/gui/core/Box.hx b/src/main/haxework/gui/core/Box.hx index 3731b6e..2578142 100644 --- a/src/main/haxework/gui/core/Box.hx +++ b/src/main/haxework/gui/core/Box.hx @@ -7,6 +7,7 @@ abstract Box(Array) { public var bottom(get, set):Float; public var vertical(get, never):Float; public var horizontal(get, never):Float; + public var empty(get, never):Bool; inline public function new(value:Array) { this = switch(value) { @@ -58,6 +59,13 @@ abstract Box(Array) { return left + right; } + inline private function get_empty():Bool { + return switch this { + case [0, 0, 0, 0]: true; + case _: false; + } + } + @:from static public inline function fromArray(value:Array):Box { return new Box(value); } diff --git a/src/main/haxework/gui/core/Geometry.hx b/src/main/haxework/gui/core/Geometry.hx index 1fd5bbd..34a5c57 100644 --- a/src/main/haxework/gui/core/Geometry.hx +++ b/src/main/haxework/gui/core/Geometry.hx @@ -33,6 +33,7 @@ class SizeSet { public var fixed(default, default):Size; public var percent(default, default):Size; public var stretch(null, set):Bool; + public var empty(get, null):Bool; public var width(null, set):ASizeValue; public var height(null, set):ASizeValue; @@ -43,11 +44,15 @@ class SizeSet { this.percent = []; } - private function set_stretch(value:Bool):Bool { + inline private function set_stretch(value:Bool):Bool { this.percent = value ? [100] : []; return value; } + inline private function get_empty():Bool { + return fixed.empty && percent.empty; + } + inline private function set_width(value:ASizeValue):ASizeValue { switch cast(value, SizeValue) { case PERCENT(v): percent.width = v; @@ -117,4 +122,31 @@ class Geometry { result += padding.vertical; return FIXED(result); } + + public function setMargin(margin:Box):Geometry { + this.margin = margin; + return this; + } + + public function setPadding(padding:Box):Geometry { + this.padding = padding; + return this; + } + + public function setAlign(hAlign:HAlign, vAlign:VAlign):Geometry { + this.hAlign = hAlign; + this.vAlign = vAlign; + return this; + } + + public function setPosition(position:Position):Geometry { + this.position = position; + return this; + } + + public function setSize(width:ASizeValue, height:ASizeValue):Geometry { + this.size.width = width; + this.size.height = height; + return this; + } } diff --git a/src/main/haxework/gui/core/Size.hx b/src/main/haxework/gui/core/Size.hx index 9402281..f6ff0e7 100644 --- a/src/main/haxework/gui/core/Size.hx +++ b/src/main/haxework/gui/core/Size.hx @@ -3,6 +3,7 @@ package haxework.gui.core; abstract Size(Array) { public var width(get, set):Float; public var height(get, set):Float; + public var empty(get, never):Bool; inline public function new(value:Array) { this = switch(value) { @@ -29,6 +30,13 @@ abstract Size(Array) { return this[1] = value; } + inline private function get_empty():Bool { + return switch this { + case [-1, -1]: true; + case _: false; + } + } + @:from static public inline function fromArray(value:Array):Size { return new Size(value); } diff --git a/src/main/haxework/gui/skin/GeometrySkin.hx b/src/main/haxework/gui/skin/GeometrySkin.hx new file mode 100644 index 0000000..f770431 --- /dev/null +++ b/src/main/haxework/gui/skin/GeometrySkin.hx @@ -0,0 +1,49 @@ +package haxework.gui.skin; + +import haxework.gui.core.HAlign; +import haxework.gui.core.VAlign; +import haxework.gui.core.Geometry; + +class GeometrySkin implements ISkin> { + + private var geometry(default, default):Geometry; + + public function new(geometry:Geometry) { + this.geometry = geometry; + } + + public function draw(view:IView):Void { + var updated = false; + if (!geometry.padding.empty && view.geometry.padding != geometry.padding) { + view.geometry.padding = geometry.padding; + updated = true; + } + if (!geometry.margin.empty && view.geometry.margin != geometry.margin) { + view.geometry.margin = geometry.margin; + updated = true; + } + if (geometry.position != view.geometry.position) { + view.geometry.position = geometry.position; + updated = true; + } + if (geometry.hAlign != NONE && geometry.hAlign != view.geometry.hAlign) { + view.geometry.hAlign = geometry.hAlign; + updated = true; + } + if (geometry.vAlign != NONE && geometry.vAlign != view.geometry.vAlign) { + view.geometry.vAlign = geometry.vAlign; + updated = true; + } + if (!geometry.size.fixed.empty && geometry.size.fixed != view.geometry.size.fixed) { + view.geometry.size.fixed = geometry.size.fixed; + updated = true; + } + if (!geometry.size.percent.empty && geometry.size.percent != view.geometry.size.percent) { + view.geometry.size.percent = geometry.size.percent; + updated = true; + } + if (updated) { + view.toUpdate(); + } + } +} diff --git a/src/main/haxework/gui/skin/SizeSkin.hx b/src/main/haxework/gui/skin/SizeSkin.hx deleted file mode 100644 index a383773..0000000 --- a/src/main/haxework/gui/skin/SizeSkin.hx +++ /dev/null @@ -1,18 +0,0 @@ -package haxework.gui.skin; - -class SizeSkin implements ISkin> { - public var width(default, null):Float; - public var height(default, null):Float; - - public function new(width:Float, height:Float) { - this.width = width; - this.height = height; - } - - public function draw(view:IView):Void { - if (view.geometry.size.fixed.width != width || view.geometry.size.fixed.height != height) { - view.geometry.size.fixed = [width, height]; - view.toUpdateParent(); - } - } -} diff --git a/src/main/haxework/gui/skin/Skin.hx b/src/main/haxework/gui/skin/Skin.hx index 596db6a..5d80a5e 100644 --- a/src/main/haxework/gui/skin/Skin.hx +++ b/src/main/haxework/gui/skin/Skin.hx @@ -1,11 +1,12 @@ package haxework.gui.skin; +import haxework.gui.core.Geometry; import flash.display.BitmapData; class Skin { public static function size(width:Float, height:Float):ISkin { - return new SizeSkin(width, height); + return new GeometrySkin(new Geometry().setSize(width, height)); } public static function bitmap(image:BitmapData):ISkin { @@ -43,4 +44,8 @@ class Skin { public static function scrollVertical(foreColor:Int, backColor:Int) { return new VScrollBarSkin(foreColor, backColor); } + + public static function geometry(geometry:Geometry):GeometrySkin { + return new GeometrySkin(geometry); + } }