Skip to content

Commit 2c61382

Browse files
committed
ComponentBase extends Sprite!!
1 parent 84c67a7 commit 2c61382

File tree

8 files changed

+99
-71
lines changed

8 files changed

+99
-71
lines changed

extraParams.hxml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--macro patchTypes("patchHeaps.txt")
2+
--macro patchTypes("patchHaxeUI.txt")

haxe/ui/backend/ComponentBase.hx

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,41 @@ import haxe.ui.styles.Style;
1515
import haxe.ui.util.Rectangle;
1616
import hxd.Cursor;
1717

18-
class ComponentBase {
19-
public var sprite(default, null):UISprite;
18+
class ComponentBase extends UISprite {
2019
private var _eventMap:Map<String, UIEvent->Void>;
2120

2221
public function new() {
22+
super(null);
2323
_eventMap = new Map<String, UIEvent->Void>();
2424
}
2525

2626
public function handleCreate(native:Bool) {
27-
sprite = new UISprite(null);
2827
}
2928

3029
private function handlePosition(left:Null<Float>, top:Null<Float>, style:Style) {
3130
if (left != null) {
32-
sprite.x = left;
31+
x = left;
3332
}
3433

3534
if (top != null) {
36-
sprite.y = top;
35+
y = top;
3736
}
3837
}
3938

40-
private function handleSize(width:Null<Float>, height:Null<Float>, style:Style) {
41-
if (width == null || height == null || width <= 0 || height <= 0) {
39+
private function handleSize(w:Null<Float>, h:Null<Float>, style:Style) {
40+
if (h == null || w == null || w <= 0 || h <= 0) {
4241
return;
4342
}
4443

45-
sprite.width = width;
46-
sprite.height = height;
47-
48-
StyleHelper.apply(sprite, style, sprite.x, sprite.y, width, height);
44+
setSize(w, h);
45+
StyleHelper.apply(this, style, x, y, __width, __height);
4946
}
5047

5148
private function handleReady() {
5249
}
5350

5451
private function handleClipRect(value:Rectangle) {
55-
sprite.clipRect = value;
52+
clipRect = value;
5653
}
5754

5855
public function handlePreReposition() {
@@ -62,7 +59,7 @@ class ComponentBase {
6259
}
6360

6461
private function handleVisibility(show:Bool) {
65-
sprite.visible = show;
62+
visible = show;
6663
}
6764

6865
//***********************************************************************************************************
@@ -73,7 +70,7 @@ class ComponentBase {
7370
if (_textDisplay == null) {
7471
_textDisplay = new TextDisplay();
7572
_textDisplay.parentComponent = cast(this, Component);
76-
sprite.addChild(_textDisplay.sprite);
73+
addChild(_textDisplay.sprite);
7774
}
7875
if (text != null) {
7976
_textDisplay.text = text;
@@ -94,7 +91,7 @@ class ComponentBase {
9491
if (_textInput == null) {
9592
_textInput = new TextInput();
9693
_textInput.parentComponent = cast(this, Component);
97-
sprite.addChild(_textInput.sprite);
94+
addChild(_textInput.sprite);
9895
}
9996
if (text != null) {
10097
_textInput.text = text;
@@ -117,7 +114,7 @@ class ComponentBase {
117114
public function createImageDisplay():ImageDisplay {
118115
if (_imageDisplay == null) {
119116
_imageDisplay = new ImageDisplay();
120-
sprite.addChild(_imageDisplay.sprite);
117+
addChild(_imageDisplay.sprite);
121118
}
122119
return _imageDisplay;
123120
}
@@ -140,29 +137,29 @@ class ComponentBase {
140137
// Display tree
141138
//***********************************************************************************************************
142139
private function handleSetComponentIndex(child:Component, index:Int) {
143-
sprite.addChildAt(child.sprite, index);
140+
addChildAt(child, index);
144141
}
145142

146143
private function handleAddComponent(child:Component):Component {
147-
sprite.addChild(child.sprite);
144+
addChild(child);
148145
return child;
149146
}
150147

151148
private function handleAddComponentAt(child:Component, index:Int):Component {
152-
sprite.addChildAt(child.sprite, index);
149+
addChildAt(child, index);
153150
return child;
154151
}
155152

156153
private function handleRemoveComponent(child:Component, dispose:Bool = true):Component {
157-
sprite.removeChild(child.sprite);
154+
removeChild(child);
158155
//TODO - dispose
159156
return child;
160157
}
161158

162159
private function handleRemoveComponentAt(index:Int, dispose:Bool = true):Component {
163160
var child = cast(this, Component)._children[index];
164161
if (child != null) {
165-
sprite.removeChild(child.sprite);
162+
removeChild(child);
166163

167164
//TODO - dispose
168165
}
@@ -171,23 +168,23 @@ class ComponentBase {
171168

172169
private function applyStyle(style:Style) {
173170
if (style.cursor != null && style.cursor == "pointer") {
174-
sprite.cursor = Cursor.Button;
175-
} else if (sprite.cursor != hxd.Cursor.Default) {
176-
sprite.cursor = Cursor.Default;
171+
cursor = Cursor.Button;
172+
} else if (cursor != hxd.Cursor.Default) {
173+
cursor = Cursor.Default;
177174
}
178175

179176
if (style.filter != null) {
180177
//TODO
181178
} else {
182-
sprite.filter = null;
179+
filter = null;
183180
}
184181

185182
if (style.hidden != null) {
186-
sprite.visible = !style.hidden;
183+
visible = !style.hidden;
187184
}
188185

189186
if (style.opacity != null) {
190-
sprite.alpha = style.opacity;
187+
alpha = style.opacity;
191188
}
192189
}
193190

@@ -200,9 +197,9 @@ class ComponentBase {
200197
| MouseEvent.MOUSE_DOWN | MouseEvent.MOUSE_UP | MouseEvent.MOUSE_WHEEL
201198
| MouseEvent.CLICK:
202199
if (!_eventMap.exists(type)) {
203-
sprite.interactive = true;
200+
interactive = true;
204201
_eventMap.set(type, listener);
205-
Reflect.setProperty(sprite.interactiveObj, EventMapper.HAXEUI_TO_HEAPS.get(type), __onMouseEvent.bind(_, type));
202+
Reflect.setProperty(interactiveObj, EventMapper.HAXEUI_TO_HEAPS.get(type), __onMouseEvent.bind(_, type));
206203
}
207204
}
208205
}
@@ -213,11 +210,11 @@ class ComponentBase {
213210
| MouseEvent.MOUSE_DOWN | MouseEvent.MOUSE_UP | MouseEvent.MOUSE_WHEEL
214211
| MouseEvent.CLICK:
215212
_eventMap.remove(type);
216-
Reflect.setProperty(sprite.interactiveObj, EventMapper.HAXEUI_TO_HEAPS.get(type), null);
213+
Reflect.setProperty(interactiveObj, EventMapper.HAXEUI_TO_HEAPS.get(type), null);
217214
}
218215

219216
if (Lambda.empty(_eventMap)) {
220-
sprite.interactive = false;
217+
interactive = false;
221218
}
222219
}
223220

haxe/ui/backend/ScreenBase.hx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ScreenBase {
4545
if (value.hasTextInput()) {
4646
value.getTextInput().focus();
4747
} else {
48-
value.sprite.focus();
48+
value.setFocus();
4949
}
5050
}
5151

@@ -78,15 +78,15 @@ class ScreenBase {
7878
}
7979

8080
public function addComponent(component:Component) {
81-
app.s2d.addChildAt(component.sprite, 0);//TODO
81+
app.s2d.addChildAt(component, 0);//TODO
8282
}
8383

8484
public function removeComponent(component:Component) {
85-
app.s2d.removeChild(component.sprite);
85+
app.s2d.removeChild(component);
8686
}
8787

8888
private function handleSetComponentIndex(child:Component, index:Int) {
89-
app.s2d.addChildAt(child.sprite, index);
89+
app.s2d.addChildAt(child, index);
9090
}
9191

9292
//***********************************************************************************************************

haxe/ui/backend/heaps/UISprite.hx

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,15 @@ import hxd.Cursor;
88

99
class UISprite extends Graphics
1010
{
11-
public var width(get, set):Float;
12-
public var height(get, set):Float;
1311
public var interactive(default, set):Bool = false;
1412
public var interactiveObj(default, null):Interactive;
1513
public var clipRect:Rectangle;
1614
public var cursor(default, set):Cursor = Cursor.Default;
1715

1816
private var _backgrounds:Map<String, IBackground>;
1917

20-
private var _width:Float = 0;
21-
private function set_width(value:Float):Float {
22-
if (_width != value) {
23-
_width = value;
24-
25-
if (interactiveObj != null) {
26-
interactiveObj.width = value;
27-
}
28-
}
29-
30-
return value;
31-
}
32-
private function get_width():Float {
33-
return _width;
34-
}
35-
36-
private var _height:Float = 0;
37-
private function set_height(value:Float):Float {
38-
if (_height != value) {
39-
_height = value;
40-
41-
if (interactiveObj != null) {
42-
interactiveObj.height = value;
43-
}
44-
}
45-
46-
return value;
47-
}
48-
private function get_height():Float {
49-
return _height;
50-
}
18+
private var __width:Float = 0;
19+
private var __height:Float = 0;
5120

5221
private function set_interactive(value:Bool):Bool {
5322
if (interactive != value) {
@@ -72,11 +41,20 @@ class UISprite extends Graphics
7241
super(parent);
7342
}
7443

44+
public function setSize(w:Float, h:Float) {
45+
__width = w;
46+
__height = h;
47+
if (interactiveObj != null) {
48+
interactiveObj.width = w;
49+
interactiveObj.height = h;
50+
}
51+
}
52+
7553
public function hasFocus():Bool {
7654
return interactiveObj != null && interactiveObj.hasFocus();
7755
}
7856

79-
public function focus() {
57+
public function setFocus() {
8058
if (interactiveObj != null) {
8159
interactiveObj.focus();
8260
}
@@ -133,7 +111,7 @@ class UISprite extends Graphics
133111
else {
134112
super.getBoundsRec(relativeTo, out, forSize);
135113
if (forSize) {
136-
addBounds(relativeTo, out, 0, 0, width, height);
114+
addBounds(relativeTo, out, 0, 0, __width, __height);
137115
}
138116
}
139117
}
@@ -195,7 +173,7 @@ class UISprite extends Graphics
195173
private function checkInteraction() {
196174
if (interactive || cursor != Cursor.Default) {
197175
if (interactiveObj == null) {
198-
interactiveObj = new Interactive(width, height, this);
176+
interactiveObj = new Interactive(__width, __height, this);
199177
interactiveObj.propagateEvents = !interactive;
200178
}
201179
} else if (interactiveObj == null) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package haxe.ui.backend.macros;
2+
3+
import haxe.macro.Expr;
4+
5+
class HaxeUIMacros {
6+
macro public static function buildComponent():Array<Field> {
7+
var pos = haxe.macro.Context.currentPos();
8+
var fields:Array<Field> = haxe.macro.Context.getBuildFields();
9+
10+
var i:Int = fields.length;
11+
while(--i >= 0) {
12+
var f:Field = fields[i];
13+
switch(f.name) {
14+
case "color":
15+
fields.remove(f);
16+
}
17+
}
18+
19+
return fields;
20+
}
21+
22+
/*static private function replaceWithCode(f:Field, code:String) {
23+
var e:Expr = haxe.macro.Context.parseInlineString(code, f.pos);
24+
var fn = switch (e).expr {
25+
case EFunction(_, f): f;
26+
case _: throw "false";
27+
}
28+
f.kind = FFun(fn);
29+
}*/
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package haxe.ui.backend.macros;
2+
3+
import haxe.macro.Expr;
4+
5+
class HeapsMacros {
6+
macro public static function removeInline(propNames:Array<String>):Array<Field> {
7+
var pos = haxe.macro.Context.currentPos();
8+
var fields:Array<Field> = haxe.macro.Context.getBuildFields();
9+
10+
for(f in fields)
11+
{
12+
if(propNames.indexOf(f.name) != -1){
13+
f.access = [APrivate];
14+
}
15+
}
16+
17+
return fields;
18+
}
19+
}

patchHaxeUI.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@:build(haxe.ui.backend.macros.HaxeUIMacros.buildComponent()) haxe.ui.core.Component

patchHeaps.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@:build(haxe.ui.backend.macros.HeapsMacros.removeInline(["set_x", "set_y"])) h2d.Sprite

0 commit comments

Comments
 (0)