[gui] add GeometrySkin

This commit is contained in:
2019-03-19 22:47:02 +03:00
parent 0771e89873
commit 7a1a98daf3
6 changed files with 104 additions and 20 deletions

View File

@@ -7,6 +7,7 @@ abstract Box(Array<Float>) {
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<Float>) {
this = switch(value) {
@@ -58,6 +59,13 @@ abstract Box(Array<Float>) {
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<Float>):Box {
return new Box(value);
}

View File

@@ -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;
}
}

View File

@@ -3,6 +3,7 @@ package haxework.gui.core;
abstract Size(Array<Float>) {
public var width(get, set):Float;
public var height(get, set):Float;
public var empty(get, never):Bool;
inline public function new(value:Array<Float>) {
this = switch(value) {
@@ -29,6 +30,13 @@ abstract Size(Array<Float>) {
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<Float>):Size {
return new Size(value);
}

View File

@@ -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<IView<Dynamic>> {
private var geometry(default, default):Geometry;
public function new(geometry:Geometry) {
this.geometry = geometry;
}
public function draw(view:IView<Dynamic>):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();
}
}
}

View File

@@ -1,18 +0,0 @@
package haxework.gui.skin;
class SizeSkin implements ISkin<IView<Dynamic>> {
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<Dynamic>):Void {
if (view.geometry.size.fixed.width != width || view.geometry.size.fixed.height != height) {
view.geometry.size.fixed = [width, height];
view.toUpdateParent();
}
}
}

View File

@@ -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<Dynamic> {
return new SizeSkin(width, height);
return new GeometrySkin(new Geometry().setSize(width, height));
}
public static function bitmap(image:BitmapData):ISkin<SpriteView> {
@@ -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);
}
}