Revisão | a1799f773946097545bdc7374519e226f5236952 (tree) |
---|---|
Hora | 2022-04-05 04:52:32 |
Autor | Jaime Marquínez Ferrándiz <jaime.marquinez.ferrandiz@fast...> |
Commiter | Jaime Marquínez Ferrándiz |
Use creators defined in nativexml
@@ -11,6 +11,7 @@ | ||
11 | 11 | --library haxeui-core |
12 | 12 | --library haxe-YUI |
13 | 13 | |
14 | +--dce full | |
14 | 15 | --cpp bin |
15 | 16 | |
16 | 17 | --macro haxe.macro.Compiler.include("haxe.ui", ["haxe.ui.macros"]) |
\ No newline at end of file |
@@ -4,21 +4,11 @@ | ||
4 | 4 | |
5 | 5 | package haxe.ui.backend; |
6 | 6 | |
7 | -import haxe.ui.backend.yui.DialogWrapper; | |
7 | +import haxe.ui.backend.yui.creators.Creator; | |
8 | 8 | import haxe.ui.backend.yui.WidgetWrapper; |
9 | -import haxe.ui.components.Button; | |
10 | -import haxe.ui.components.Label; | |
11 | -import haxe.ui.containers.HBox; | |
12 | -import haxe.ui.containers.VBox; | |
13 | -import haxe.ui.core.TextInput; | |
14 | 9 | import haxe.ui.core.Component; |
15 | -import haxe.ui.events.UIEvent; | |
16 | 10 | import haxe.ui.events.MouseEvent; |
17 | -import yui.hxHelpers.AutoRawPointer; | |
18 | 11 | import yui.YEvent; |
19 | -import yui.YUI; | |
20 | -import yui.YWidget; | |
21 | -import yui.YWidgetFactory; | |
22 | 12 | |
23 | 13 | using cpp.Pointer; |
24 | 14 | using yui.hxHelpers.YWidgetHelper; |
@@ -49,34 +39,38 @@ | ||
49 | 39 | parent = Toolkit.screen.mainWindow; |
50 | 40 | } |
51 | 41 | |
52 | - var factory = YUI.widgetFactory().fromRaw().ref; | |
53 | - var rPointer : AutoRawPointer<YWidget> = null; | |
54 | - // TODO: use creators defined at native.xml | |
55 | - if (this is VBox) { | |
56 | - rPointer = factory.createVBox(parent.widget); | |
57 | - } | |
58 | - else if (this is HBox) { | |
59 | - rPointer = factory.createHBox(parent.widget); | |
42 | + var creatorClass:String = this.getCreator(); | |
43 | + if (creatorClass != null) { | |
44 | + var creator : Creator = Type.createInstance(Type.resolveClass(creatorClass), [this]); | |
45 | + this.wrapper = creator.createWrapper(parent); | |
60 | 46 | } |
61 | - else if (this is Button) { | |
62 | - var b = cast(this, Button); | |
63 | - rPointer = factory.createPushButton(parent.widget, b.text); | |
64 | - } | |
65 | - else if (this is Label) { | |
66 | - var l = cast(this, Label); | |
67 | - rPointer = factory.createLabel(parent.widget, l.text); | |
68 | - } | |
69 | - | |
70 | - if (rPointer != null) { | |
71 | - var pointer : cpp.RawPointer<YWidget> = rPointer; | |
72 | - this.wrapper = new WidgetWrapper(pointer.fromRaw()); | |
73 | - } else { | |
74 | - trace('pointer not created'); | |
47 | + else { | |
48 | + trace('There is not a creator for ${className}'); | |
75 | 49 | } |
76 | 50 | |
77 | 51 | parent.children.push(cast(this, Component)); |
78 | 52 | } |
79 | 53 | |
54 | + private function getCreator() { | |
55 | + var valueType = Type.typeof(this); | |
56 | + switch (valueType) { | |
57 | + case TClass(cls): | |
58 | + return this.getCreatorClassname(cls); | |
59 | + default: | |
60 | + return null; | |
61 | + } | |
62 | + } | |
63 | + | |
64 | + private function getCreatorClassname(cls : Class<Dynamic>) { | |
65 | + var clsName = Type.getClassName(cls); | |
66 | + var creatorClass = Toolkit.nativeConfig.query('component[id=${clsName}].@creator', null, this); | |
67 | + if (creatorClass != null) { | |
68 | + return creatorClass; | |
69 | + } else { | |
70 | + return this.getCreatorClassname(Type.getSuperClass(cls)); | |
71 | + } | |
72 | + } | |
73 | + | |
80 | 74 | public function handleYEvent(eventPointer : Pointer<YEvent>) : Bool { |
81 | 75 | var event = eventPointer.ref; |
82 | 76 | var widgetPointer = event.widget(); |
@@ -5,8 +5,12 @@ | ||
5 | 5 | --> |
6 | 6 | <?xml version="1.0" encoding="utf-8" ?> |
7 | 7 | <native> |
8 | - <component id="haxe.ui.components.Button" allowChildren="false"> | |
8 | + <component id="haxe.ui.components.Button" allowChildren="false" creator="haxe.ui.backend.yui.creators.ButtonCreator"> | |
9 | 9 | <behaviour id="text" class="haxe.ui.backend.yui.behaviours.WidgetLabel" /> |
10 | 10 | <behaviour id="value" class="haxe.ui.backend.yui.behaviours.WidgetLabel" /> |
11 | 11 | </component> |
12 | + <component id="haxe.ui.components.Label" allowChildren="false" creator="haxe.ui.backend.yui.creators.LabelCreator" /> | |
13 | + | |
14 | + <component id="haxe.ui.containers.HBox" allowChildren="true" creator="haxe.ui.backend.yui.creators.HBoxCreator" /> | |
15 | + <component id="haxe.ui.containers.VBox" allowChildren="true" creator="haxe.ui.backend.yui.creators.VBoxCreator" /> | |
12 | 16 | </native> |
\ No newline at end of file |
@@ -10,7 +10,6 @@ | ||
10 | 10 | import yui.YWidget; |
11 | 11 | |
12 | 12 | using cpp.Pointer; |
13 | -using cpp.RawPointer; | |
14 | 13 | |
15 | 14 | /** |
16 | 15 | * Wrapper for `YWidget`. |
@@ -21,6 +20,12 @@ | ||
21 | 20 | this.widget = w; |
22 | 21 | this.children = []; |
23 | 22 | } |
23 | + | |
24 | + public static inline function fromPointer<TWidget:YWidget>(widgetPointer : cpp.Pointer<TWidget>) { | |
25 | + var ref : cpp.Reference<YWidget> = widgetPointer.ref; | |
26 | + return new WidgetWrapper(ref.addressOf()); | |
27 | + } | |
28 | + | |
24 | 29 | public var widget : cpp.Pointer<YWidget>; |
25 | 30 | |
26 | 31 | public var children : Array<Component>; |
@@ -9,6 +9,7 @@ | ||
9 | 9 | import yui.YProperty; |
10 | 10 | import yui.YPropertyValue; |
11 | 11 | |
12 | +@:keep | |
12 | 13 | class WidgetLabel extends DataBehaviour { |
13 | 14 | static final LABEL_PROPERTY = "Label"; |
14 | 15 |
@@ -0,0 +1,21 @@ | ||
1 | +// SPDX-FileCopyrightText: 2022 Jaime Marquínez Ferrándiz <jaime.marquinez.ferrandiz@fastmail.net> | |
2 | +// | |
3 | +// SPDX-License-Identifier: Unlicense | |
4 | + | |
5 | +package haxe.ui.backend.yui.creators; | |
6 | + | |
7 | +import yui.YWidget; | |
8 | + | |
9 | +@:keep | |
10 | +class ButtonCreator extends Creator { | |
11 | + private var _button : haxe.ui.components.Button; | |
12 | + function new(button : haxe.ui.components.Button) { | |
13 | + super(button); | |
14 | + this._button = button; | |
15 | + } | |
16 | + | |
17 | + function createWidget(parentWidget : cpp.Pointer<YWidget>) : cpp.Pointer<YWidget> { | |
18 | + var p = this.factory.ref.createPushButton(parentWidget, this._button.text); | |
19 | + return this.convertPointer(p); | |
20 | + } | |
21 | +} | |
\ No newline at end of file |
@@ -0,0 +1,41 @@ | ||
1 | +// SPDX-FileCopyrightText: 2022 Jaime Marquínez Ferrándiz <jaime.marquinez.ferrandiz@fastmail.net> | |
2 | +// | |
3 | +// SPDX-License-Identifier: Unlicense | |
4 | + | |
5 | +package haxe.ui.backend.yui.creators; | |
6 | + | |
7 | +import haxe.ui.core.Component; | |
8 | +import yui.YUI; | |
9 | +import yui.YWidget; | |
10 | +import yui.YWidgetFactory; | |
11 | + | |
12 | +using cpp.Pointer; | |
13 | + | |
14 | +abstract class Creator { | |
15 | + private var component : Component; | |
16 | + | |
17 | + public function new(comp : Component) { | |
18 | + this.component = comp; | |
19 | + } | |
20 | + | |
21 | + private var factory (get, never) : cpp.Pointer<YWidgetFactory>; | |
22 | + function get_factory() { | |
23 | + return YUI.widgetFactory().fromRaw(); | |
24 | + } | |
25 | + | |
26 | + /** | |
27 | + * Create the widget associated to the component | |
28 | + * @return The widget | |
29 | + */ | |
30 | + private abstract function createWidget(parentWidget : cpp.Pointer<YWidget>) : cpp.Pointer<YWidget>; | |
31 | + | |
32 | + private inline function convertPointer<T:YWidget>(p : cpp.RawPointer<T>) { | |
33 | + var r : cpp.RawPointer<YWidget> = cast p; | |
34 | + return r.fromRaw(); | |
35 | + } | |
36 | + | |
37 | + public function createWrapper(parent : WidgetWrapper) : WidgetWrapper { | |
38 | + var widget = this.createWidget(parent.widget); | |
39 | + return WidgetWrapper.fromPointer(widget); | |
40 | + } | |
41 | +} | |
\ No newline at end of file |
@@ -0,0 +1,21 @@ | ||
1 | +// SPDX-FileCopyrightText: 2022 Jaime Marquínez Ferrándiz <jaime.marquinez.ferrandiz@fastmail.net> | |
2 | +// | |
3 | +// SPDX-License-Identifier: Unlicense | |
4 | + | |
5 | +package haxe.ui.backend.yui.creators; | |
6 | + | |
7 | +import yui.YWidget; | |
8 | + | |
9 | +@:keep | |
10 | +class HBoxCreator extends Creator { | |
11 | + private var _hbox : haxe.ui.containers.HBox; | |
12 | + function new(hbox : haxe.ui.containers.HBox) { | |
13 | + super(hbox); | |
14 | + this._hbox = hbox; | |
15 | + } | |
16 | + | |
17 | + function createWidget(parentWidget : cpp.Pointer<YWidget>) : cpp.Pointer<YWidget> { | |
18 | + var p = factory.ref.createHBox(parentWidget); | |
19 | + return this.convertPointer(p); | |
20 | + } | |
21 | +} | |
\ No newline at end of file |
@@ -0,0 +1,21 @@ | ||
1 | +// SPDX-FileCopyrightText: 2022 Jaime Marquínez Ferrándiz <jaime.marquinez.ferrandiz@fastmail.net> | |
2 | +// | |
3 | +// SPDX-License-Identifier: Unlicense | |
4 | + | |
5 | +package haxe.ui.backend.yui.creators; | |
6 | + | |
7 | +import yui.YWidget; | |
8 | + | |
9 | +@:keep | |
10 | +class LabelCreator extends Creator { | |
11 | + private var _label : haxe.ui.components.Label; | |
12 | + function new(label : haxe.ui.components.Label) { | |
13 | + super(label); | |
14 | + this._label = label; | |
15 | + } | |
16 | + | |
17 | + function createWidget(parentWidget : cpp.Pointer<YWidget>) : cpp.Pointer<YWidget> { | |
18 | + var p = this.factory.ref.createLabel(parentWidget, this._label.text); | |
19 | + return this.convertPointer(p); | |
20 | + } | |
21 | +} | |
\ No newline at end of file |
@@ -0,0 +1,21 @@ | ||
1 | +// SPDX-FileCopyrightText: 2022 Jaime Marquínez Ferrándiz <jaime.marquinez.ferrandiz@fastmail.net> | |
2 | +// | |
3 | +// SPDX-License-Identifier: Unlicense | |
4 | + | |
5 | +package haxe.ui.backend.yui.creators; | |
6 | + | |
7 | +import yui.YWidget; | |
8 | + | |
9 | +@:keep | |
10 | +class VBoxCreator extends Creator { | |
11 | + private var _vbox : haxe.ui.containers.VBox; | |
12 | + function new(vbox : haxe.ui.containers.VBox) { | |
13 | + super(vbox); | |
14 | + this._vbox = vbox; | |
15 | + } | |
16 | + | |
17 | + function createWidget(parentWidget : cpp.Pointer<YWidget>) : cpp.Pointer<YWidget> { | |
18 | + var p = factory.ref.createVBox(parentWidget); | |
19 | + return this.convertPointer(p); | |
20 | + } | |
21 | +} | |
\ No newline at end of file |