added dispatcher package
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
package examples;
|
package examples;
|
||||||
|
|
||||||
|
import haxework.gui.IGroupView;
|
||||||
|
import haxework.gui.ButtonView;
|
||||||
import haxework.gui.GuiBuilder;
|
import haxework.gui.GuiBuilder;
|
||||||
import haxework.asset.JsonAsset;
|
import haxework.asset.JsonAsset;
|
||||||
import haxework.gui.Root;
|
import haxework.gui.Root;
|
||||||
import haxework.gui.IView;
|
|
||||||
import flash.display.Sprite;
|
import flash.display.Sprite;
|
||||||
|
|
||||||
@:file("examples/form.json")
|
@:file("examples/form.json")
|
||||||
@@ -13,7 +14,12 @@ class ViewExample {
|
|||||||
|
|
||||||
public static function main() {
|
public static function main() {
|
||||||
var form:Dynamic = new Form().value;
|
var form:Dynamic = new Form().value;
|
||||||
var v:IView<Sprite> = GuiBuilder.build(form);
|
var listener = {
|
||||||
|
onPress:function(view:ButtonView):Void {
|
||||||
|
trace("onPress: " + view.id);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var v:IGroupView<Sprite> = GuiBuilder.build(form, {listener:listener});
|
||||||
new Root(v);
|
new Root(v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -24,6 +24,7 @@
|
|||||||
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x0000ff"}
|
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x0000ff"}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"id":"panel",
|
||||||
"type":"haxework.gui.HGroupView",
|
"type":"haxework.gui.HGroupView",
|
||||||
"layoutHAlign":"~haxework.gui.core.HAlign:LEFT",
|
"layoutHAlign":"~haxework.gui.core.HAlign:LEFT",
|
||||||
"layoutVAlign":"~haxework.gui.core.VAlign:MIDDLE",
|
"layoutVAlign":"~haxework.gui.core.VAlign:MIDDLE",
|
||||||
@@ -34,27 +35,33 @@
|
|||||||
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xffff00"},
|
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xffff00"},
|
||||||
"views":[
|
"views":[
|
||||||
{
|
{
|
||||||
"type":"haxework.gui.LabelView",
|
"id":"button1",
|
||||||
|
"type":"haxework.gui.ButtonView",
|
||||||
"width":100,
|
"width":100,
|
||||||
"pHeight":100,
|
"pHeight":100,
|
||||||
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x00ffff"},
|
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x00ffff"},
|
||||||
"text":"Text1"
|
"text":"Text1",
|
||||||
|
"onPress":"#listener"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type":"haxework.gui.LabelView",
|
"id":"button2",
|
||||||
|
"type":"haxework.gui.ButtonView",
|
||||||
"contentSize":true,
|
"contentSize":true,
|
||||||
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x00ffff"},
|
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x00ffff"},
|
||||||
"text":"Text2",
|
"text":"Text2",
|
||||||
"fontFamily":"Georgia",
|
"fontFamily":"Georgia",
|
||||||
"fontColor":"0xffffff"
|
"fontColor":"0xffffff",
|
||||||
|
"onPress":"#listener"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type":"haxework.gui.LabelView",
|
"id":"button3",
|
||||||
|
"type":"haxework.gui.ButtonView",
|
||||||
"contentSize":true,
|
"contentSize":true,
|
||||||
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x00ffff"},
|
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x00ffff"},
|
||||||
"text":"Text 3333333333 ddd",
|
"text":"Text 3333333333 ddd",
|
||||||
"fontFamily":"Tahoma",
|
"fontFamily":"Tahoma",
|
||||||
"fontColor":"0xff0000"
|
"fontColor":"0xff0000",
|
||||||
|
"onPress":"#listener"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
30
haxework/dispath/Dispatcher.hx
Executable file
30
haxework/dispath/Dispatcher.hx
Executable file
@@ -0,0 +1,30 @@
|
|||||||
|
package haxework.dispath;
|
||||||
|
|
||||||
|
import haxe.ds.ObjectMap;
|
||||||
|
|
||||||
|
class Dispatcher<L:{}> implements IDispatcher<L> {
|
||||||
|
|
||||||
|
private var listeners(null, null):ObjectMap<L, Bool>;
|
||||||
|
|
||||||
|
public function new() {
|
||||||
|
listeners = new ObjectMap<L, Bool>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addListener(listener:L):Void {
|
||||||
|
listeners.set(listener, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeListener(listener:L):Bool {
|
||||||
|
return listeners.remove(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeAllListeners():Void {
|
||||||
|
var i:Iterator<L> = listeners.keys();
|
||||||
|
while (i.hasNext()) listeners.remove(i.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dispatch(caller:L->Void):Void {
|
||||||
|
var i:Iterator<L> = listeners.keys();
|
||||||
|
while (i.hasNext()) caller(i.next());
|
||||||
|
}
|
||||||
|
}
|
||||||
13
haxework/dispath/IDispatcher.hx
Executable file
13
haxework/dispath/IDispatcher.hx
Executable file
@@ -0,0 +1,13 @@
|
|||||||
|
package haxework.dispath;
|
||||||
|
|
||||||
|
import haxe.ds.ObjectMap;
|
||||||
|
|
||||||
|
interface IDispatcher<L:{}> {
|
||||||
|
|
||||||
|
private var listeners(null, null):ObjectMap<L, Bool>;
|
||||||
|
|
||||||
|
public function addListener(listener:L):Void;
|
||||||
|
public function removeListener(listener:L):Bool;
|
||||||
|
public function removeAllListeners():Void;
|
||||||
|
public function dispatch(caller:L->Void):Void;
|
||||||
|
}
|
||||||
41
haxework/gui/ButtonView.hx
Executable file
41
haxework/gui/ButtonView.hx
Executable file
@@ -0,0 +1,41 @@
|
|||||||
|
package haxework.gui;
|
||||||
|
|
||||||
|
import flash.events.MouseEvent;
|
||||||
|
import haxework.dispath.Dispatcher;
|
||||||
|
import haxework.dispath.IDispatcher;
|
||||||
|
|
||||||
|
enum ButtonState {
|
||||||
|
UP;
|
||||||
|
OVER;
|
||||||
|
DOWN;
|
||||||
|
}
|
||||||
|
|
||||||
|
class ButtonView extends LabelView {
|
||||||
|
|
||||||
|
public var state(default, null):ButtonState;
|
||||||
|
public var dispatcher(default, null):IDispatcher<ButtonViewListener>;
|
||||||
|
public var onPress(null, set):ButtonViewListener;
|
||||||
|
|
||||||
|
public function new() {
|
||||||
|
super();
|
||||||
|
state = ButtonState.UP;
|
||||||
|
dispatcher = new Dispatcher<ButtonViewListener>();
|
||||||
|
content.addEventListener(MouseEvent.CLICK, onMouseClick);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function onMouseClick(event:MouseEvent):Void {
|
||||||
|
var self:ButtonView = this;
|
||||||
|
dispatcher.dispatch(function(listener:ButtonViewListener):Void {
|
||||||
|
listener.onPress(self);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private function set_onPress(value:ButtonViewListener):ButtonViewListener {
|
||||||
|
dispatcher.addListener(value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef ButtonViewListener = {
|
||||||
|
public function onPress(view:ButtonView):Void;
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package haxework.gui;
|
package haxework.gui;
|
||||||
|
|
||||||
import flash.errors.Error;
|
|
||||||
import haxework.gui.core.VAlign;
|
import haxework.gui.core.VAlign;
|
||||||
import haxework.gui.core.HAlign;
|
import haxework.gui.core.HAlign;
|
||||||
import haxework.gui.layout.DefaultLayout;
|
import haxework.gui.layout.DefaultLayout;
|
||||||
@@ -71,6 +70,25 @@ class GroupView extends View implements IGroupView<Sprite> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function findViewById<V:IView<Dynamic>>(id:String):Null<V> {
|
||||||
|
var idd:Array<String> = id.split(":");
|
||||||
|
if (idd.length > 1) {
|
||||||
|
var id0 = idd.shift();
|
||||||
|
if (viewsById.exists(id0)) {
|
||||||
|
var g:GroupView = findViewById(id0);
|
||||||
|
return g.findViewById(idd.join(":"));
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (viewsById.exists(id)) {
|
||||||
|
return cast viewsById.get(id);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function set_layoutVAlign(value:VAlign):VAlign {
|
private function set_layoutVAlign(value:VAlign):VAlign {
|
||||||
if (layoutVAlign != value) {
|
if (layoutVAlign != value) {
|
||||||
layoutVAlign = value;
|
layoutVAlign = value;
|
||||||
|
|||||||
@@ -8,31 +8,34 @@ import haxework.gui.HGroupView;
|
|||||||
import haxework.gui.VGroupView;
|
import haxework.gui.VGroupView;
|
||||||
import haxework.gui.TextView;
|
import haxework.gui.TextView;
|
||||||
import haxework.gui.LabelView;
|
import haxework.gui.LabelView;
|
||||||
|
import haxework.gui.ButtonView;
|
||||||
import haxework.gui.skin.ColorSkin;
|
import haxework.gui.skin.ColorSkin;
|
||||||
|
|
||||||
class GuiBuilder {
|
class GuiBuilder {
|
||||||
|
|
||||||
private function new() {}
|
private function new() {}
|
||||||
|
|
||||||
public static function build(data:Dynamic):Dynamic {
|
public static function build(data:Dynamic, links:Dynamic):Dynamic {
|
||||||
var type:String = data.type;
|
var type:String = data.type;
|
||||||
Reflect.deleteField(data, "type");
|
Reflect.deleteField(data, "type");
|
||||||
var object:Dynamic = instance(type);
|
var object:Dynamic = instance(type);
|
||||||
fill(object, data);
|
fill(object, data, links);
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function fill(object:Dynamic, data:Dynamic):Void {
|
private static function fill(object:Dynamic, data:Dynamic, links:Dynamic):Void {
|
||||||
var fields:Array<String> = Reflect.fields(data);
|
var fields:Array<String> = Reflect.fields(data);
|
||||||
for (field in fields) {
|
for (field in fields) {
|
||||||
var value:Dynamic = Reflect.field(data, field);
|
var value:Dynamic = Reflect.field(data, field);
|
||||||
if (Std.is(value, Array)) {
|
if (Std.is(value, Array)) {
|
||||||
var a:Array<Dynamic> = [];
|
var a:Array<Dynamic> = [];
|
||||||
for (o in cast(value, Array<Dynamic>)) a.push(build(o));
|
for (o in cast(value, Array<Dynamic>)) a.push(build(o, links));
|
||||||
value = a;
|
value = a;
|
||||||
} else if (Std.is(value, String)) {
|
} else if (Std.is(value, String)) {
|
||||||
var s:String = cast(value, String);
|
var s:String = cast(value, String);
|
||||||
if (s.charAt(0) == "~") {
|
if (s.charAt(0) == "#") {
|
||||||
|
value = Reflect.field(links, s.substr(1));
|
||||||
|
} else if (s.charAt(0) == "~") {
|
||||||
var a:Array<String> = s.substr(1).split(":");
|
var a:Array<String> = s.substr(1).split(":");
|
||||||
var e:Enum<Dynamic> = Type.resolveEnum(a[0]);
|
var e:Enum<Dynamic> = Type.resolveEnum(a[0]);
|
||||||
value = Type.createEnum(e, a[1]);
|
value = Type.createEnum(e, a[1]);
|
||||||
@@ -44,8 +47,8 @@ class GuiBuilder {
|
|||||||
} else if (Std.is(value, Bool)) {
|
} else if (Std.is(value, Bool)) {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
var o:Dynamic = build(value);
|
var o:Dynamic = build(value, links);
|
||||||
fill(o, value);
|
fill(o, value, links);
|
||||||
value = o;
|
value = o;
|
||||||
}
|
}
|
||||||
Reflect.setProperty(object, field, value);
|
Reflect.setProperty(object, field, value);
|
||||||
|
|||||||
@@ -22,4 +22,5 @@ interface IGroupView<C> extends IView<C> {
|
|||||||
public function addView(view:IView<C>):IView<C>;
|
public function addView(view:IView<C>):IView<C>;
|
||||||
public function removeView(view:IView<C>):IView<C>;
|
public function removeView(view:IView<C>):IView<C>;
|
||||||
public function removeViewById(id:String):IView<C>;
|
public function removeViewById(id:String):IView<C>;
|
||||||
|
public function findViewById<V:IView<Dynamic>>(id:String):Null<V>;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user