From cba776ec51823fd90ab159ca143ca60c640f31d7 Mon Sep 17 00:00:00 2001 From: shmyga Date: Fri, 22 Nov 2013 16:27:43 +0400 Subject: [PATCH] added resources & bitmap skin --- haxework/gui/GuiBuilder.hx | 12 ++++- haxework/gui/TextView.hx | 3 +- haxework/gui/ToggleButtonView.hx | 30 ++++++++++++ haxework/gui/View.hx | 12 ++++- haxework/gui/layout/HorizontalLayout.hx | 7 +++ haxework/gui/layout/VerticalLayout.hx | 7 +++ haxework/gui/skin/ButtonBitmapSkin.hx | 65 +++++++++++++++++++++++++ haxework/gui/skin/FakeSkin.hx | 10 +++- haxework/gui/skin/ISize.hx | 6 +++ haxework/resources/IResources.hx | 8 +++ haxework/resources/Resources.hx | 14 ++++++ project.xml | 8 +-- 12 files changed, 173 insertions(+), 9 deletions(-) create mode 100755 haxework/gui/ToggleButtonView.hx create mode 100755 haxework/gui/skin/ButtonBitmapSkin.hx create mode 100755 haxework/gui/skin/ISize.hx create mode 100755 haxework/resources/IResources.hx create mode 100755 haxework/resources/Resources.hx mode change 100644 => 100755 project.xml diff --git a/haxework/gui/GuiBuilder.hx b/haxework/gui/GuiBuilder.hx index a8f6cf4..f05deea 100755 --- a/haxework/gui/GuiBuilder.hx +++ b/haxework/gui/GuiBuilder.hx @@ -2,6 +2,8 @@ package haxework.gui; //ToDo: +import haxework.resources.IResources; +import haxework.provider.Provider; import haxework.gui.View; import haxework.gui.GroupView; import haxework.gui.HGroupView; @@ -9,8 +11,10 @@ import haxework.gui.VGroupView; import haxework.gui.TextView; import haxework.gui.LabelView; import haxework.gui.ButtonView; +import haxework.gui.ToggleButtonView; import haxework.gui.skin.ColorSkin; import haxework.gui.skin.ButtonColorSkin; +import haxework.gui.skin.ButtonBitmapSkin; import haxework.frame.FrameSwitcher; @@ -36,12 +40,16 @@ class GuiBuilder { value = a; } else if (Std.is(value, String)) { var s:String = cast(value, String); - if (s.charAt(0) == "#") { + var c:String = s.charAt(0); + if (c == "#") { value = Reflect.field(links, s.substr(1)); - } else if (s.charAt(0) == "~") { + } else if (c == "~") { var a:Array = s.substr(1).split(":"); var e:Enum = Type.resolveEnum(a[0]); value = Type.createEnum(e, a[1]); + } else if (c == "@") { + var a:Array = s.substr(1).split(":"); + value = Reflect.field(Provider.get(IResources), a[0]).get(a[1]); } else if (~/0x[A-Fa-f\d]{6}/.match(value)) { value = Std.parseInt(value); } diff --git a/haxework/gui/TextView.hx b/haxework/gui/TextView.hx index b3b4328..b88911c 100755 --- a/haxework/gui/TextView.hx +++ b/haxework/gui/TextView.hx @@ -1,5 +1,6 @@ package haxework.gui; +import haxework.gui.skin.ISize; import flash.text.TextFormat; import flash.display.Sprite; import flash.text.TextField; @@ -60,7 +61,7 @@ class TextView extends View implements ITextView { override public function update():Void { textField.setTextFormat(textFormat); - if (contentSize) { + if (contentSize && !Std.is(skin, ISize)) { width = textField.width; height = textField.height; } diff --git a/haxework/gui/ToggleButtonView.hx b/haxework/gui/ToggleButtonView.hx new file mode 100755 index 0000000..97ce5bb --- /dev/null +++ b/haxework/gui/ToggleButtonView.hx @@ -0,0 +1,30 @@ +package haxework.gui; + +import flash.display.Sprite; +import haxework.gui.skin.ISkin; + +class ToggleButtonView extends ButtonView { + + public var on(default, set):Bool; + public var onSkin(default, set):ISkin>; + + public function new() { + super(); + } + + private function set_on(value:Bool):Bool { + on = value; + invalidate(); + return on; + } + + private function set_onSkin(value:ISkin>):ISkin> { + onSkin = value; + invalidate(); + return onSkin; + } + + override private function currentSkin():ISkin> { + return on ? onSkin : skin; + } +} \ No newline at end of file diff --git a/haxework/gui/View.hx b/haxework/gui/View.hx index 64721d5..ac7233b 100755 --- a/haxework/gui/View.hx +++ b/haxework/gui/View.hx @@ -1,5 +1,6 @@ package haxework.gui; +import haxework.gui.skin.ISize; import haxework.gui.core.SizeType; import haxework.gui.core.HAlign; import haxework.gui.core.VAlign; @@ -61,6 +62,10 @@ class View implements IView { hAlign = HAlign.NONE; } + private function currentSkin():ISkin> { + return skin; + } + private function invalidate():Void { updater.invalidate(this); } @@ -73,7 +78,12 @@ class View implements IView { public function update():Void { content.x = x; content.y = y; - skin.draw(this); + if (contentSize && skin != null && Std.is(skin, ISize)) { + var size:ISize = cast(skin, ISize); + if (!Math.isNaN(size.width)) width = size.width; + if (!Math.isNaN(size.height)) height = size.height; + } + currentSkin().draw(this); } private function set_x(value:Float):Float { diff --git a/haxework/gui/layout/HorizontalLayout.hx b/haxework/gui/layout/HorizontalLayout.hx index e812d0e..b89762e 100755 --- a/haxework/gui/layout/HorizontalLayout.hx +++ b/haxework/gui/layout/HorizontalLayout.hx @@ -12,6 +12,7 @@ class HorizontalLayout extends DefaultLayout { override public function place(group:IGroupView, views:Array>):Void { var fixedSize:Float = group.layoutMargin * (views.length - 1); var leftSize:Float = group.width - group.leftPadding - group.rightPadding; + var maxHeight:Float = 0; for (view in views) { switch (view.widthType) { @@ -20,6 +21,12 @@ class HorizontalLayout extends DefaultLayout { } setViewHeight(group, view); placeViewVertical(group, view); + maxHeight = Math.max(maxHeight, view.height); + } + + if (group.contentSize) { + group.width = fixedSize; + group.height = maxHeight; } leftSize -= fixedSize; diff --git a/haxework/gui/layout/VerticalLayout.hx b/haxework/gui/layout/VerticalLayout.hx index 5cccee4..5c33586 100755 --- a/haxework/gui/layout/VerticalLayout.hx +++ b/haxework/gui/layout/VerticalLayout.hx @@ -12,6 +12,7 @@ class VerticalLayout extends DefaultLayout { override public function place(group:IGroupView, views:Array>):Void { var fixedSize:Float = group.layoutMargin * (views.length - 1); var leftSize:Float = group.height - group.topPadding - group.bottomPadding; + var maxWidth:Float = 0; for (view in views) { switch (view.heightType) { @@ -20,6 +21,12 @@ class VerticalLayout extends DefaultLayout { } setViewWidth(group, view); placeViewHorizontal(group, view); + maxWidth = Math.max(maxWidth, view.width); + } + + if (group.contentSize) { + group.width = maxWidth; + group.height = fixedSize; } leftSize -= fixedSize; diff --git a/haxework/gui/skin/ButtonBitmapSkin.hx b/haxework/gui/skin/ButtonBitmapSkin.hx new file mode 100755 index 0000000..0c406d8 --- /dev/null +++ b/haxework/gui/skin/ButtonBitmapSkin.hx @@ -0,0 +1,65 @@ +package haxework.gui.skin; + +import flash.display.BitmapData; +import haxework.gui.utils.ColorUtils; +import haxework.gui.ButtonView.ButtonState; +import flash.display.Graphics; +import flash.display.Sprite; + +class ButtonBitmapSkin implements ISkin implements ISize { + + public var width(default, null):Float; + public var height(default, null):Float; + + public var image(null, set):BitmapData; + public var upImage(null, set):BitmapData; + public var overImage(null, set):BitmapData; + public var downImage(null, set):BitmapData; + + private var images:Map; + + public function new(?image:BitmapData = null) { + images = new Map(); + if (image != null) { + this.image = image; + } + } + + private function set_image(value:BitmapData):BitmapData { + width = value.width; + height = value.height; + images.set(ButtonState.UP, value); + images.set(ButtonState.DOWN, value); + images.set(ButtonState.OVER, value); + //images.set(ButtonState.DISABLE, value); + return value; + } + + private function set_upImage(value:BitmapData):BitmapData { + width = value.width; + height = value.height; + images.set(ButtonState.UP, value); + return value; + } + + private function set_overImage(value:BitmapData):BitmapData { + images.set(ButtonState.OVER, value); + return value; + } + + private function set_downImage(value:BitmapData):BitmapData { + images.set(ButtonState.DOWN, value); + return value; + } + + public function draw(view:ButtonView):Void { + if (images == null) return; + var image:BitmapData = images.get(view.state); + var graphics:Graphics = view.content.graphics; + graphics.clear(); + graphics.beginBitmapFill(image, null, false, true); + graphics.drawRect(0, 0, view.width, view.height); + graphics.endFill(); + } + +} \ No newline at end of file diff --git a/haxework/gui/skin/FakeSkin.hx b/haxework/gui/skin/FakeSkin.hx index 00643ec..d56b551 100755 --- a/haxework/gui/skin/FakeSkin.hx +++ b/haxework/gui/skin/FakeSkin.hx @@ -1,11 +1,19 @@ package haxework.gui.skin; +import flash.display.Graphics; import flash.display.Sprite; class FakeSkin implements ISkin> { public function new() {} - public function draw(view:IView):Void {} + public function draw(view:IView):Void { + /*var g:Graphics = view.content.graphics; + g.clear(); + g.lineStyle(1, 0x00ff00); + g.drawRect(0, 0, view.width, view.height); + g.endFill(); + g.lineStyle();*/ + } } \ No newline at end of file diff --git a/haxework/gui/skin/ISize.hx b/haxework/gui/skin/ISize.hx new file mode 100755 index 0000000..afc2268 --- /dev/null +++ b/haxework/gui/skin/ISize.hx @@ -0,0 +1,6 @@ +package haxework.gui.skin; + +interface ISize { + public var width(default, null):Float; + public var height(default, null):Float; +} \ No newline at end of file diff --git a/haxework/resources/IResources.hx b/haxework/resources/IResources.hx new file mode 100755 index 0000000..ef4f630 --- /dev/null +++ b/haxework/resources/IResources.hx @@ -0,0 +1,8 @@ +package haxework.resources; + +import flash.display.BitmapData; + +interface IResources { + public var image(default, null):Map; + public var color(default, null):Map; +} \ No newline at end of file diff --git a/haxework/resources/Resources.hx b/haxework/resources/Resources.hx new file mode 100755 index 0000000..6f12f94 --- /dev/null +++ b/haxework/resources/Resources.hx @@ -0,0 +1,14 @@ +package haxework.resources; + +import flash.display.BitmapData; + +class Resources implements IResources { + + public var image(default, null):Map; + public var color(default, null):Map; + + public function new() { + image = new Map(); + color = new Map(); + } +} \ No newline at end of file diff --git a/project.xml b/project.xml old mode 100644 new mode 100755 index b8a6368..c8e5705 --- a/project.xml +++ b/project.xml @@ -1,10 +1,10 @@ - + - - + + - +