From 752d0dd413e151a07549bfaefa25fee9959a792b Mon Sep 17 00:00:00 2001 From: shmyga Date: Tue, 27 Aug 2013 09:25:41 +0200 Subject: [PATCH] added root --- .gitignore | 3 +- build.hxml | 3 ++ examples/ViewExample.hx | 15 +++++---- haxework/gui/GroupView.hx | 4 +++ haxework/gui/IView.hx | 7 ++-- haxework/gui/Root.hx | 39 ++++++++++++++++++++++ haxework/gui/View.hx | 50 ++++++++++++++++++++-------- haxework/gui/core/Direction.hx | 6 ++++ haxework/gui/core/HAlign.hx | 7 ++-- haxework/gui/core/VAlign.hx | 7 ++-- haxework/gui/layout/DefaultLayout.hx | 45 +++++++++++++++++++++++++ 11 files changed, 157 insertions(+), 29 deletions(-) create mode 100755 build.hxml create mode 100755 haxework/gui/Root.hx create mode 100755 haxework/gui/core/Direction.hx diff --git a/.gitignore b/.gitignore index bd81735..1b9b3b3 100755 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.iml *.ipr *.iws -out/ \ No newline at end of file +out/ +target/ \ No newline at end of file diff --git a/build.hxml b/build.hxml new file mode 100755 index 0000000..86f6fab --- /dev/null +++ b/build.hxml @@ -0,0 +1,3 @@ +-main examples.ViewExample +-swf target/ViewExample.swf +-debug \ No newline at end of file diff --git a/examples/ViewExample.hx b/examples/ViewExample.hx index d805489..9bb6837 100755 --- a/examples/ViewExample.hx +++ b/examples/ViewExample.hx @@ -1,5 +1,8 @@ package examples; +import haxework.gui.Root; +import haxework.gui.core.HAlign; +import haxework.gui.core.VAlign; import flash.Lib; import haxework.gui.skin.ColorSkin; import haxework.gui.View; @@ -11,15 +14,13 @@ import haxework.gui.IGroupView; class ViewExample { public static function main() { - View.updater.stage = Lib.current.stage; - var group:IGroupView = new GroupView(); - group.width = 400; - group.height = 400; + group.layoutVAlign = VAlign.MIDDLE; + group.layoutHAlign = HAlign.CENTER; group.skin = new ColorSkin(0xffff00); var view:IView = new View(); - view.width = 200; - view.height = 200; + view.pWidth = 80; + view.pHeight = 80; view.skin = new ColorSkin(0xff0000); group.addView(view); view = new View(); @@ -33,5 +34,7 @@ class ViewExample { view.skin = new ColorSkin(0x0000ff); group.addView(view); Lib.current.addChild(group.content); + + new Root(group); } } \ No newline at end of file diff --git a/haxework/gui/GroupView.hx b/haxework/gui/GroupView.hx index 799c4f7..b2f7f48 100755 --- a/haxework/gui/GroupView.hx +++ b/haxework/gui/GroupView.hx @@ -26,6 +26,10 @@ class GroupView extends View implements IGroupView { public function new(?layout:ILayout) { super(); this.layout = layout == null ? new DefaultLayout() : layout; + paddings = 0; + layoutMargin = 0; + layoutHAlign = HAlign.NONE; + layoutVAlign = VAlign.NONE; views = []; viewsById = new Map>(); } diff --git a/haxework/gui/IView.hx b/haxework/gui/IView.hx index 6ac61e5..dcb1264 100755 --- a/haxework/gui/IView.hx +++ b/haxework/gui/IView.hx @@ -11,11 +11,14 @@ interface IView { 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(default, set):Float; - public var height(default, set):Float; + public var width(get, set):Float; + public var height(get, set):Float; public var pWidth(default, set):Float; public var pHeight(default, set):Float; diff --git a/haxework/gui/Root.hx b/haxework/gui/Root.hx new file mode 100755 index 0000000..5966de5 --- /dev/null +++ b/haxework/gui/Root.hx @@ -0,0 +1,39 @@ +package haxework.gui; + +import flash.display.StageAlign; +import flash.display.StageScaleMode; +import flash.display.DisplayObject; +import flash.events.Event; +import flash.display.Sprite; + +class Root { + + private var view:IView; + + public function new(view:IView) { + this.view = view; + var content:DisplayObject = view.content; + if (content.stage == null) { + content.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); + } else { + onAddedToStage(); + } + } + + private function onAddedToStage(?_):Void { + var content:DisplayObject = view.content; + content.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage); + content.stage.scaleMode = StageScaleMode.NO_SCALE; + content.stage.align = StageAlign.TOP_LEFT; + View.updater.stage = content.stage; + + content.stage.addEventListener(Event.RESIZE, onResize); + onResize(); + } + + private function onResize(?_):Void { + var content:DisplayObject = view.content; + view.width = content.stage.stageWidth; + view.height = content.stage.stageHeight; + } +} diff --git a/haxework/gui/View.hx b/haxework/gui/View.hx index ee3360c..b293870 100755 --- a/haxework/gui/View.hx +++ b/haxework/gui/View.hx @@ -1,7 +1,5 @@ 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; @@ -22,11 +20,14 @@ class View implements IView { 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(default, set):Float; - public var height(default, set):Float; + public var width(get, set):Float; + public var height(get, set):Float; public var pWidth(default, set):Float; public var pHeight(default, set):Float; @@ -54,8 +55,8 @@ class View implements IView { width = 100; height = 100; margins = 0; - vAlign = VAlign.CENTER; - hAlign = HAlign.MIDDLE; + vAlign = VAlign.NONE; + hAlign = HAlign.NONE; } private function invalidate():Void { @@ -88,24 +89,47 @@ class View implements IView { 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 (width != value || widthType != SizeType.NORMAL) { - width = value; + if (w != value || widthType != SizeType.NORMAL) { + w = value; widthType = SizeType.NORMAL; invalidate(); invalidateParent(); } - return width; + return w; } private function set_height(value:Float):Float { - if (height != value || heightType != SizeType.NORMAL) { - height = value; + if (h != value || heightType != SizeType.NORMAL) { + h = value; heightType = SizeType.NORMAL; invalidate(); invalidateParent(); } - return height; + return h; } private function set_pWidth(value:Float):Float { @@ -207,8 +231,6 @@ class Updater { } 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; } diff --git a/haxework/gui/core/Direction.hx b/haxework/gui/core/Direction.hx new file mode 100755 index 0000000..a7c7722 --- /dev/null +++ b/haxework/gui/core/Direction.hx @@ -0,0 +1,6 @@ +package haxework.gui.core; + +enum Direction { + HORIZONTAL; + VERTICAL; +} \ No newline at end of file diff --git a/haxework/gui/core/HAlign.hx b/haxework/gui/core/HAlign.hx index 1ed9a2d..de5a45d 100755 --- a/haxework/gui/core/HAlign.hx +++ b/haxework/gui/core/HAlign.hx @@ -1,7 +1,8 @@ package haxework.gui.core; enum HAlign { - TOP; - MIDDLE; - BOTTOM; + NONE; + LEFT; + CENTER; + RIGHT; } \ No newline at end of file diff --git a/haxework/gui/core/VAlign.hx b/haxework/gui/core/VAlign.hx index 64d6056..5026ad9 100755 --- a/haxework/gui/core/VAlign.hx +++ b/haxework/gui/core/VAlign.hx @@ -1,7 +1,8 @@ package haxework.gui.core; enum VAlign { - LEFT; - CENTER; - RIGHT; + NONE; + TOP; + MIDDLE; + BOTTOM; } \ No newline at end of file diff --git a/haxework/gui/layout/DefaultLayout.hx b/haxework/gui/layout/DefaultLayout.hx index 38cb382..06538a4 100755 --- a/haxework/gui/layout/DefaultLayout.hx +++ b/haxework/gui/layout/DefaultLayout.hx @@ -1,5 +1,9 @@ package haxework.gui.layout; +import haxework.gui.core.SizeType; +import haxework.gui.core.VAlign; +import haxework.gui.core.HAlign; + class DefaultLayout implements ILayout { public function new() { @@ -7,6 +11,47 @@ class DefaultLayout implements ILayout { } public function place(group:IGroupView, views:Array>):Void { + for (view in views) { + setViewSize(group, view); + placeViewHorizontal(group, view); + placeViewVertical(group, view); + } + } + private function setViewSize(group:IGroupView, view:IView):Void { + if (view.widthType == SizeType.PERCENT) { + view.w = view.pWidth / 100 * group.width; + } + if (view.heightType == SizeType.PERCENT) { + view.h = view.pHeight / 100 * group.height; + } + } + + private function placeViewHorizontal(group:IGroupView, view:IView):Void { + var align:HAlign = view.hAlign; + if (align == HAlign.NONE) align = group.layoutHAlign; + switch (align) { + case HAlign.LEFT: + view.x = group.leftPadding + view.leftMargin; + case HAlign.CENTER: + view.x = (group.width - view.width) / 2 + (group.leftPadding - group.rightPadding) + (view.leftMargin - view.rightMargin); + case HAlign.RIGHT: + view.x = group.width - view.width - group.rightPadding - view.rightMargin; + case HAlign.NONE: + } + } + + private function placeViewVertical(group:IGroupView, view:IView):Void { + var align:VAlign = view.vAlign; + if (align == VAlign.NONE) align = group.layoutVAlign; + switch (align) { + case VAlign.TOP: + view.y = group.topPadding + view.topMargin; + case VAlign.MIDDLE: + view.y = (group.height - view.height) / 2 + (group.topPadding - group.topPadding) + (view.bottomMargin - view.bottomMargin); + case VAlign.BOTTOM: + view.y = group.height - view.height - group.bottomPadding - view.bottomMargin; + case VAlign.NONE: + } } } \ No newline at end of file