This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
/
Sprite.hx
228 lines (150 loc) · 5.21 KB
/
Sprite.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package luxe;
import luxe.Log.*;
import luxe.Visual;
import luxe.Vector;
import luxe.Rectangle;
import luxe.Resources;
import phoenix.geometry.Geometry;
import phoenix.geometry.QuadGeometry;
import luxe.options.SpriteOptions;
class Sprite extends Visual {
@:isVar public var centered (default, set) : Bool = true;
@:isVar public var flipx (default, set) : Bool = false;
@:isVar public var flipy (default, set) : Bool = false;
@:isVar public var uv (default, set) : Rectangle;
public var geometry_quad : QuadGeometry;
public function new( options:SpriteOptions ) {
uv = new Rectangle();
assertnull(options, 'Sprite requires non-null options');
//centered
if(options.centered != null) {
centered = options.centered;
}
//flipx
if(options.flipx != null) {
flipx = options.flipx;
}
//flipy
if(options.flipy != null) {
flipy = options.flipy;
}
//create visual
super( options );
}
override function on_geometry_created() {
super.on_geometry_created();
if(texture != null) {
//because the default is 0,0,1,1 uv for the quad, we don't want that when
//textures are padded (like on web)
uv = def(options.uv, new Rectangle(0,0,texture.width,texture.height));
//if texture is render target, flipy
if(texture.resource_type == ResourceType.render_texture) {
flipy = true;
}
} //texture !null
//set the origin and centered once created
centered = !!centered;
//and re assign the flip values
flipx = !!flipx;
flipy = !!flipy;
} //on_geometry_created
override function set_geometry( _g:Geometry ) {
geometry_quad = cast _g;
return super.set_geometry(_g);
} //set_geometry
override public function ondestroy() {
uv = null;
geometry_quad = null;
super.ondestroy();
}
//Helper functions
/** Returns true if a point is inside the sprite, takes into account the sprite transform,
which includes more cost than simple AABB like `point_inside_AABB` */
public function point_inside( _p:Vector ) : Bool {
if(geometry == null) return false;
return Luxe.utils.geometry.point_in_geometry(_p, geometry);
} //point_inside
/** Returns true if a point is inside the AABB unrotated */
public function point_inside_AABB(_p:Vector) : Bool {
if(pos == null) return false;
if(size == null) return false;
//scaled size
var _s_x = size.x * scale.x;
var _s_y = size.y * scale.y;
if(centered) {
var _hx = _s_x / 2;
var _hy = _s_y / 2;
if(_p.x < pos.x - _hx) return false;
if(_p.y < pos.y - _hy) return false;
if(_p.x > pos.x+_s_x - _hx) return false;
if(_p.y > pos.y+_s_y - _hy) return false;
} else {
if(_p.x < pos.x) return false;
if(_p.y < pos.y) return false;
if(_p.x > pos.x+_s_x) return false;
if(_p.y > pos.y+_s_y) return false;
}
return true;
} //point_inside_AABB
//Properties
//UV / source rect
function set_uv(_uv:Rectangle) : Rectangle {
if(_uv == null) return uv = _uv;
if(geometry_quad != null) {
geometry_quad.uv(_uv);
}
uv = _uv;
Rectangle.listen( uv, _uv_change );
return uv;
}
//Flipping
function set_flipy(_v:Bool) {
if(_v == flipy) {
return flipy;
}
if(geometry_quad != null) {
geometry_quad.flipy = _v;
}
return flipy = _v;
} //set_flipy
function set_flipx(_v:Bool) {
if(_v == flipx) {
return flipx;
}
if(geometry_quad != null) {
geometry_quad.flipx = _v;
}
return flipx = _v;
} //set_flipv
//Size
override function set_size( _v:Vector ) : Vector {
//resize the mesh vertices themselves, as scale is relative to this size
//if explicitly set
if(geometry_quad != null) {
geometry_quad.resize( new Vector( _v.x, _v.y ) );
//If the user doesn't specify a custom origin, we try and work with the center
if(!_has_custom_origin) {
if(centered) {
//half of the new size
origin = _v.clone().divideScalar(2);
}
}
}
//done
return super.set_size(_v);
} //set_size
//Centered
function set_centered(_c:Bool) : Bool {
//centered geometry affects the origin directly
if(size != null) {
if(_c) {
origin = new Vector(size.x/2, size.y/2);
} else {
origin = new Vector();
}
} //size != null
return centered = _c;
} //set_centered
//An internal callback for when x y or w or h on a transform changes
function _uv_change(_v:Float) { this.set_uv(uv); }
} //Sprite