diff --git a/demo/src/demo/Demo.yaml b/demo/src/demo/Demo.yaml index 9cb02e0..7821278 100644 --- a/demo/src/demo/Demo.yaml +++ b/demo/src/demo/Demo.yaml @@ -8,8 +8,8 @@ views: - id: tabs $type: haxework.gui.HGroupView layout.margin: 5 - layout.hAlign: LEFT - geometry.size.percent.width: 100 + layout.hAlign: left + geometry.size.width: 100% geometry.padding.left: 5 views: - id: list_form diff --git a/demo/src/demo/form/ListForm.yaml b/demo/src/demo/form/ListForm.yaml index 7b0dca0..a101be1 100644 --- a/demo/src/demo/form/ListForm.yaml +++ b/demo/src/demo/form/ListForm.yaml @@ -8,5 +8,5 @@ views: scroll: $type: haxework.gui.list.VScrollBarView skin: $r:skin:scroll - geometry.size.percent.height: 100 - geometry.size.fixed.width: 10 + geometry.size.height: 100% + geometry.size.width: 10 diff --git a/demo/src/demo/form/TailForm.yaml b/demo/src/demo/form/TailForm.yaml index a5dbdb2..d7816af 100644 --- a/demo/src/demo/form/TailForm.yaml +++ b/demo/src/demo/form/TailForm.yaml @@ -5,12 +5,12 @@ views: scroll: $type: haxework.gui.list.VScrollBarView skin: $r:skin:scroll - geometry.size.percent.height: 100 - geometry.size.fixed.width: 10 + geometry.size.height: 100% + geometry.size.width: 10 view: id: group $type: haxework.gui.GroupView - geometry.size.percent.width: 100 + geometry.size.width: 100% layout.margin: 5 layout: $type: haxework.gui.layout.TailLayout diff --git a/src/main/haxework/gui/ImageView.hx b/src/main/haxework/gui/ImageView.hx index 88515ea..c2aef53 100755 --- a/src/main/haxework/gui/ImageView.hx +++ b/src/main/haxework/gui/ImageView.hx @@ -21,8 +21,10 @@ class ImageView extends SpriteView { if (image != value) { image = value; skin = [new BitmapSkin(image, FillType.DEFAULT)]; - toUpdate(); - toRedraw(); + //geometry.size.content.width = value.width; + //geometry.size.content.height = value.height; + //toUpdate(); + //toRedraw(); } return image; } diff --git a/src/main/haxework/gui/ScrollView.hx b/src/main/haxework/gui/ScrollView.hx index a902197..55fc7a0 100644 --- a/src/main/haxework/gui/ScrollView.hx +++ b/src/main/haxework/gui/ScrollView.hx @@ -1,5 +1,6 @@ package haxework.gui; +import haxework.gui.core.Geometry.Position; import flash.display.DisplayObject; import flash.display.Sprite; import flash.events.MouseEvent; @@ -44,7 +45,7 @@ class ScrollView extends HGroupView { if (scroll.ratio == ratio) return; scroll.ratio = ratio; scroll.visible = ratio < 1; - scroll.geometry.skipLayout = ratio >= 1; + scroll.geometry.position = ratio < 1 ? LAYOUT : ABSOLUTE; }); scroll.onScroll.connect(function(position) this.position = position); return scroll; diff --git a/src/main/haxework/gui/SpriteView.hx b/src/main/haxework/gui/SpriteView.hx index e24064d..bf47b37 100755 --- a/src/main/haxework/gui/SpriteView.hx +++ b/src/main/haxework/gui/SpriteView.hx @@ -11,15 +11,11 @@ class SpriteView extends View { override public function redraw():Void { this.content.graphics.clear(); super.redraw(); + #if dev_layout + var graphics = content.graphics; + graphics.lineStyle(1, 0x00ff00); + graphics.drawRect(0, 0, width, height); + graphics.lineStyle(); + #end } - - #if dev_layout - override public function update():Void { - super.update(); - var g:Graphics = content.graphics; - g.lineStyle(1, 0x00ff00); - g.drawRect(0, 0, width, height); - g.lineStyle(); - } - #end } diff --git a/src/main/haxework/gui/TextView.hx b/src/main/haxework/gui/TextView.hx index 797c1f1..8303a86 100755 --- a/src/main/haxework/gui/TextView.hx +++ b/src/main/haxework/gui/TextView.hx @@ -1,11 +1,11 @@ package haxework.gui; -import haxework.gui.core.VAlign; -import haxework.gui.core.HAlign; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; import flash.text.TextFormatAlign; +import haxework.gui.core.HAlign; +import haxework.gui.core.VAlign; import haxework.text.BitmapTextField; import haxework.text.TextUtil; @@ -172,15 +172,15 @@ class TextView extends SpriteView implements ITextView { textField.height = geometry.size.content.height; textField.x = switch (layout.hAlign) { - case HAlign.LEFT | HAlign.NONE: geometry.padding.left; - case HAlign.CENTER: (width - textField.width) / 2 + geometry.padding.left - geometry.padding.right; - case HAlign.RIGHT: width - textField.width - geometry.padding.right; + case LEFT | NONE: geometry.padding.left; + case CENTER: (width - textField.width) / 2 + geometry.padding.left - geometry.padding.right; + case RIGHT: width - textField.width - geometry.padding.right; default: 0; } textField.y = switch (layout.vAlign) { - case VAlign.TOP | VAlign.NONE: geometry.padding.top; - case VAlign.MIDDLE: (height - geometry.size.content.height) / 2 + geometry.padding.top - geometry.padding.bottom; - case VAlign.BOTTOM: height - geometry.size.content.height - geometry.padding.bottom; + case TOP | NONE: geometry.padding.top; + case MIDDLE: (height - geometry.size.content.height) / 2 + geometry.padding.top - geometry.padding.bottom; + case BOTTOM: height - geometry.size.content.height - geometry.padding.bottom; default: 0; } } diff --git a/src/main/haxework/gui/View.hx b/src/main/haxework/gui/View.hx index 0cefc11..e2e8ece 100755 --- a/src/main/haxework/gui/View.hx +++ b/src/main/haxework/gui/View.hx @@ -1,16 +1,15 @@ package haxework.gui; +import haxework.gui.skin.ISkin.ISizeSkin; import flash.display.DisplayObject; import flash.display.InteractiveObject; -import flash.display.Stage; -import flash.events.Event; import haxework.gui.core.Geometry; import haxework.gui.skin.ISkin.SkinSet; class View implements IView { private static var counter:Int = 0; - public static var updater(default, null):Updater = new Updater(); + public static var updater(default, null):ViewUpdater = new ViewUpdater(); public var id(default, default):String; @@ -61,6 +60,13 @@ class View implements IView { public function redraw():Void { for (skin in this.skin) { + if (Std.is(skin, ISizeSkin)) { + var sizeSkin:ISizeSkin = cast skin; + if (sizeSkin.width != geometry.size.content.width || sizeSkin.height != geometry.size.content.width) { + geometry.size.content = [sizeSkin.width, sizeSkin.height]; + toUpdateParent(); + } + } skin.draw(this); } } @@ -131,46 +137,3 @@ class View implements IView { return mouseEnabled; } } - - -class Updater { - - public var stage(null, set):Stage; - private var updateViews:Array>; - private var redrawViews:Array>; - - public function new() { - updateViews = []; - redrawViews = []; - } - - private function set_stage(value:Stage):Stage { - value.addEventListener(Event.ENTER_FRAME, update); - value.addEventListener(Event.ENTER_FRAME, redraw); - return value; - } - - public function toUpdate(view:IView):Void { - if (updateViews.indexOf(view) == -1) updateViews.push(view); - } - - public function toRedraw(view:IView):Void { - if (redrawViews.indexOf(view) == -1) redrawViews.push(view); - } - - public function update(?_):Void { - while (updateViews.length > 0) { - var v = null; - v = updateViews.shift(); - v.update(); - } - } - - public function redraw(?_):Void { - while (redrawViews.length > 0) { - var v = null; - v = redrawViews.shift(); - v.redraw(); - } - } -} diff --git a/src/main/haxework/gui/ViewUpdater.hx b/src/main/haxework/gui/ViewUpdater.hx new file mode 100644 index 0000000..4a8d44c --- /dev/null +++ b/src/main/haxework/gui/ViewUpdater.hx @@ -0,0 +1,53 @@ +package haxework.gui; + +import flash.events.Event; +import flash.display.Stage; + +class ViewUpdater { + public var stage(null, set):Stage; + private var updateViews:Array>; + private var redrawViews:Array>; + + public function new() { + updateViews = []; + redrawViews = []; + } + + private function set_stage(value:Stage):Stage { + value.addEventListener(Event.ENTER_FRAME, onEnterFrame); + return value; + } + + public function toUpdate(view:IView):Void { + if (updateViews.indexOf(view) == -1) updateViews.push(view); + } + + public function toRedraw(view:IView):Void { + if (redrawViews.indexOf(view) == -1) redrawViews.push(view); + } + + private function onEnterFrame(_):Void { + update(); + redraw(); + if (updateViews.length > 0) { + update(); + redraw(); + } + } + + public function update():Void { + while (updateViews.length > 0) { + var v = null; + v = updateViews.shift(); + v.update(); + } + } + + public function redraw():Void { + while (redrawViews.length > 0) { + var v = null; + v = redrawViews.shift(); + v.redraw(); + } + } +} diff --git a/src/main/haxework/gui/core/Box.hx b/src/main/haxework/gui/core/Box.hx new file mode 100644 index 0000000..3731b6e --- /dev/null +++ b/src/main/haxework/gui/core/Box.hx @@ -0,0 +1,68 @@ +package haxework.gui.core; + +abstract Box(Array) { + public var left(get, set):Float; + public var right(get, set):Float; + public var top(get, set):Float; + public var bottom(get, set):Float; + public var vertical(get, never):Float; + public var horizontal(get, never):Float; + + inline public function new(value:Array) { + this = switch(value) { + case []: [0, 0, 0, 0]; + case [a]: [a, a, a, a]; + case [a, b]: [a, a, b, b]; + case [a, b, c, d]: [a, b, c, b]; + case x: x; + } + } + + inline private function get_left():Float { + return this[0]; + } + + inline private function set_left(value:Float):Float { + return this[0] = value; + } + + inline private function get_right():Float { + return this[1]; + } + + inline private function set_right(value:Float):Float { + return this[1] = value; + } + + inline private function get_top():Float { + return this[2]; + } + + inline private function set_top(value:Float):Float { + return this[2] = value; + } + + inline private function get_bottom():Float { + return this[3]; + } + + inline private function set_bottom(value:Float):Float { + return this[3] = value; + } + + inline private function get_vertical():Float { + return top + bottom; + } + + inline private function get_horizontal():Float { + return left + right; + } + + @:from static public inline function fromArray(value:Array):Box { + return new Box(value); + } + + @:from static public inline function fromFloat(value:Float):Box { + return new Box([value]); + } +} diff --git a/src/main/haxework/gui/core/Direction.hx b/src/main/haxework/gui/core/Direction.hx deleted file mode 100755 index a7c7722..0000000 --- a/src/main/haxework/gui/core/Direction.hx +++ /dev/null @@ -1,6 +0,0 @@ -package haxework.gui.core; - -enum Direction { - HORIZONTAL; - VERTICAL; -} \ No newline at end of file diff --git a/src/main/haxework/gui/core/Geometry.hx b/src/main/haxework/gui/core/Geometry.hx index 27252d9..57b85ef 100644 --- a/src/main/haxework/gui/core/Geometry.hx +++ b/src/main/haxework/gui/core/Geometry.hx @@ -1,59 +1,20 @@ package haxework.gui.core; -abstract Size(Array) { - public var width(get, set):Float; - public var height(get, set):Float; - - inline public function new(value:Array) { - this = switch(value) { - case []: [-1, -1]; - case [a]: [a, a]; - case [a, b]: [a, b]; - case x: x; - } - } - - inline private function get_width():Float { - return this[0]; - } - - inline private function set_width(value:Float):Float { - return this[0] = value; - } - - inline private function get_height():Float { - return this[1]; - } - - inline private function set_height(value:Float):Float { - return this[1] = value; - } - - @:from static public inline function fromArray(value:Array):Size { - return new Size(value); - } - - @:from static public inline function fromFloat(value:Float):Size { - return new Size([value]); - } -} - enum SizeValue { FIXED(value:Float); PERCENT(value:Float); } class SizeSet { - public var min(default, default):Size; - public var max(default, default):Size; public var content(default, default):Size; public var fixed(default, default):Size; public var percent(default, default):Size; public var stretch(null, set):Bool; + public var width(null, set):Dynamic; + public var height(null, set):Dynamic; + public function new() { - this.min = []; - this.max = []; this.content = []; this.fixed = []; this.percent = []; @@ -63,73 +24,27 @@ class SizeSet { this.percent = value ? [100] : []; return value; } -} -abstract Box(Array) { - public var left(get, set):Float; - public var right(get, set):Float; - public var top(get, set):Float; - public var bottom(get, set):Float; - public var vertical(get, never):Float; - public var horizontal(get, never):Float; - - inline public function new(value:Array) { - this = switch(value) { - case []: [0, 0, 0, 0]; - case [a]: [a, a, a, a]; - case [a, b]: [a, a, b, b]; - case [a, b, c, d]: [a, b, c, b]; - case x: x; + inline private function set_width(value:Dynamic):Dynamic { + if (Std.is(value, Float)) { + return fixed.width = value; + } else { + return percent.width = Std.parseFloat(value); } } - inline private function get_left():Float { - return this[0]; + inline private function set_height(value:Dynamic):Dynamic { + if (Std.is(value, Float)) { + return fixed.height = value; + } else { + return percent.height = Std.parseFloat(value); + } } +} - inline private function set_left(value:Float):Float { - return this[0] = value; - } - - inline private function get_right():Float { - return this[1]; - } - - inline private function set_right(value:Float):Float { - return this[1] = value; - } - - inline private function get_top():Float { - return this[2]; - } - - inline private function set_top(value:Float):Float { - return this[2] = value; - } - - inline private function get_bottom():Float { - return this[3]; - } - - inline private function set_bottom(value:Float):Float { - return this[3] = value; - } - - inline private function get_vertical():Float { - return top + bottom; - } - - inline private function get_horizontal():Float { - return left + right; - } - - @:from static public inline function fromArray(value:Array):Box { - return new Box(value); - } - - @:from static public inline function fromFloat(value:Float):Box { - return new Box([value]); - } +@:enum abstract Position(String) from String to String { + var LAYOUT = "layout"; + var ABSOLUTE = "absolute"; } class Geometry { @@ -138,7 +53,7 @@ class Geometry { public var size(default, default):SizeSet; public var hAlign(default, default):HAlign; public var vAlign(default, default):VAlign; - public var skipLayout(default, default):Bool; + public var position(default, default):Position; public var width(get, never):SizeValue; public var height(get, never):SizeValue; @@ -149,7 +64,7 @@ class Geometry { this.size = new SizeSet(); this.hAlign = HAlign.NONE; this.vAlign = VAlign.NONE; - this.skipLayout = false; + this.position = Position.LAYOUT; } public function get_width():SizeValue { diff --git a/src/main/haxework/gui/core/HAlign.hx b/src/main/haxework/gui/core/HAlign.hx index 7d5b1dd..e063b16 100755 --- a/src/main/haxework/gui/core/HAlign.hx +++ b/src/main/haxework/gui/core/HAlign.hx @@ -1,8 +1,8 @@ package haxework.gui.core; @:enum abstract HAlign(String) from String to String { - var NONE = "NONE"; - var LEFT = "LEFT"; - var CENTER = "CENTER"; - var RIGHT = "RIGHT"; + var NONE = "none"; + var LEFT = "left"; + var CENTER = "center"; + var RIGHT = "right"; } diff --git a/src/main/haxework/gui/core/Size.hx b/src/main/haxework/gui/core/Size.hx new file mode 100644 index 0000000..9402281 --- /dev/null +++ b/src/main/haxework/gui/core/Size.hx @@ -0,0 +1,39 @@ +package haxework.gui.core; + +abstract Size(Array) { + public var width(get, set):Float; + public var height(get, set):Float; + + inline public function new(value:Array) { + this = switch(value) { + case []: [-1, -1]; + case [a]: [a, a]; + case [a, b]: [a, b]; + case x: x; + } + } + + inline private function get_width():Float { + return this[0]; + } + + inline private function set_width(value:Float):Float { + return this[0] = value; + } + + inline private function get_height():Float { + return this[1]; + } + + inline private function set_height(value:Float):Float { + return this[1] = value; + } + + @:from static public inline function fromArray(value:Array):Size { + return new Size(value); + } + + @:from static public inline function fromFloat(value:Float):Size { + return new Size([value]); + } +} diff --git a/src/main/haxework/gui/core/VAlign.hx b/src/main/haxework/gui/core/VAlign.hx index f1ae5e4..142990e 100755 --- a/src/main/haxework/gui/core/VAlign.hx +++ b/src/main/haxework/gui/core/VAlign.hx @@ -1,8 +1,8 @@ package haxework.gui.core; @:enum abstract VAlign(String) from String to String { - var NONE = "NONE"; - var TOP = "TOP"; - var MIDDLE = "MIDDLE"; - var BOTTOM = "BOTTOM"; + var NONE = "none"; + var TOP = "top"; + var MIDDLE = "middle"; + var BOTTOM = "bottom"; } diff --git a/src/main/haxework/gui/layout/DefaultLayout.hx b/src/main/haxework/gui/layout/DefaultLayout.hx index 5f937a6..58392cc 100755 --- a/src/main/haxework/gui/layout/DefaultLayout.hx +++ b/src/main/haxework/gui/layout/DefaultLayout.hx @@ -1,5 +1,6 @@ package haxework.gui.layout; +import haxework.gui.core.Geometry.Position; import haxework.gui.core.Geometry.SizeValue; import haxework.gui.core.VAlign; import haxework.gui.core.HAlign; @@ -27,14 +28,15 @@ class DefaultLayout implements ILayout { private function filterViews(group:IGroupView, views:Array>):Array> { return Lambda.array(Lambda.filter(views, function(view:IView):Bool { - return if (!view.geometry.skipLayout) { - true; - } else { - setViewWidth(group, view); - setViewHeight(group, view); - placeViewHorizontal(group, view); - placeViewVertical(group, view); - false; + return switch (view.geometry.position) { + case ABSOLUTE: + setViewWidth(group, view); + setViewHeight(group, view); + placeViewHorizontal(group, view); + placeViewVertical(group, view); + false; + case LAYOUT: + true; } })); } diff --git a/src/main/haxework/gui/list/ListView.hx b/src/main/haxework/gui/list/ListView.hx index d40dafe..09c3590 100755 --- a/src/main/haxework/gui/list/ListView.hx +++ b/src/main/haxework/gui/list/ListView.hx @@ -1,12 +1,13 @@ package haxework.gui.list; -import haxework.gui.skin.Skin; -import haxework.signal.Signal; +import flash.events.MouseEvent; +import haxework.gui.core.Geometry.Position; +import haxework.gui.core.HAlign; import haxework.gui.core.VAlign; import haxework.gui.layout.ILayout; +import haxework.gui.skin.Skin; +import haxework.signal.Signal; import haxework.utils.NumberUtil; -import flash.events.MouseEvent; -import haxework.gui.core.HAlign; class ListView extends GroupView { @@ -41,8 +42,8 @@ class ListView extends GroupView { public function new(layout:ILayout, otherLayout:ILayout) { super(otherLayout); main = new GroupView(layout); - main.layout.hAlign = HAlign.CENTER; - main.layout.vAlign = VAlign.MIDDLE; + main.layout.hAlign = CENTER; + main.layout.vAlign = MIDDLE; main.geometry.size.stretch = true; addView(main); box = new GroupView(layout); @@ -50,7 +51,7 @@ class ListView extends GroupView { main.addView(box); mask = new SpriteView(); mask.geometry.size.stretch = true; - mask.geometry.skipLayout = true; + mask.geometry.position = ABSOLUTE; mask.skin.push(Skin.color(0xffffff)); box.content.mask = mask.content; box.addView(mask); diff --git a/src/main/haxework/gui/skin/BitmapSkin.hx b/src/main/haxework/gui/skin/BitmapSkin.hx index e81d0ce..f3b6115 100755 --- a/src/main/haxework/gui/skin/BitmapSkin.hx +++ b/src/main/haxework/gui/skin/BitmapSkin.hx @@ -2,10 +2,10 @@ package haxework.gui.skin; import flash.display.BitmapData; import flash.geom.Rectangle; +import haxework.gui.skin.ISkin.ISizeSkin; import haxework.gui.utils.DrawUtil; -class BitmapSkin implements ISkin { - +class BitmapSkin implements ISkin implements ISizeSkin { public var width(default, null):Float; public var height(default, null):Float; @@ -33,7 +33,5 @@ class BitmapSkin implements ISkin { public function draw(view:SpriteView):Void { if (image == null) return; DrawUtil.draw(view.content.graphics, image, new Rectangle(0, 0, view.width, view.height), fillType, color); - view.geometry.size.content.width = image.width; - view.geometry.size.content.height = image.height; } } diff --git a/src/main/haxework/gui/skin/ButtonBitmapSkin.hx b/src/main/haxework/gui/skin/ButtonBitmapSkin.hx index fc6fec4..69195fc 100755 --- a/src/main/haxework/gui/skin/ButtonBitmapSkin.hx +++ b/src/main/haxework/gui/skin/ButtonBitmapSkin.hx @@ -3,10 +3,11 @@ package haxework.gui.skin; import flash.display.BitmapData; import flash.geom.Rectangle; import haxework.gui.ButtonView.ButtonState; +import haxework.gui.skin.ISkin.ISizeSkin; import haxework.gui.utils.BitmapUtil; import haxework.gui.utils.DrawUtil; -class ButtonBitmapSkin implements ISkin { +class ButtonBitmapSkin implements ISkin implements ISizeSkin { public var width(default, null):Float; public var height(default, null):Float; @@ -62,8 +63,5 @@ class ButtonBitmapSkin implements ISkin { if (images == null) return; var image:BitmapData = view.disabled ? disableImage == null ? disable : disableImage : images.get(view.state); DrawUtil.draw(view.content.graphics, image, new Rectangle(0, 0, view.width, view.height), fillType, color); - view.geometry.size.content.width = image.width; - view.geometry.size.content.height = image.height; } - } diff --git a/src/main/haxework/gui/skin/ISkin.hx b/src/main/haxework/gui/skin/ISkin.hx index 72e0035..6bf25b0 100644 --- a/src/main/haxework/gui/skin/ISkin.hx +++ b/src/main/haxework/gui/skin/ISkin.hx @@ -4,4 +4,9 @@ interface ISkin> { public function draw(view: V): Void; } +interface ISizeSkin { + public var width(default, null):Float; + public var height(default, null):Float; +} + typedef SkinSet = Array>; diff --git a/src/main/haxework/gui/skin/SizeSkin.hx b/src/main/haxework/gui/skin/SizeSkin.hx new file mode 100644 index 0000000..e2be774 --- /dev/null +++ b/src/main/haxework/gui/skin/SizeSkin.hx @@ -0,0 +1,18 @@ +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.toUpdate(); + } + } +} diff --git a/src/main/haxework/gui/skin/Skin.hx b/src/main/haxework/gui/skin/Skin.hx index 62fff3b..b30d8b2 100644 --- a/src/main/haxework/gui/skin/Skin.hx +++ b/src/main/haxework/gui/skin/Skin.hx @@ -4,6 +4,10 @@ import flash.display.BitmapData; class Skin { + public static function size(width:Float, height:Float): ISkin { + return new SizeSkin(width, height); + } + public static function bitmap(image:BitmapData): ISkin { return new BitmapSkin(image); }