Object.hx 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. package h2d;
  2. import hxd.Math;
  3. /**
  4. A base 2D class that all scene tree elements inherit from.
  5. Serves as a virtual container that does not display anything but can contain other objects
  6. so the various transforms are inherited to its children.
  7. Private events `Object.onAdd`, `Object.onRemove` and `Object.onHierarchyChanged` can be used
  8. to capture when Object is added/removed from the currently active scene as well as being moved withing the object tree.
  9. Object exposes a number of properties to control position, scale and rotation of the Object relative to its parent,
  10. but they are used indirectly during rendering. Instead, they are being used to calculate the absolute matrix transform
  11. relative to the Scene. As optimization, it's not recalculated as soon as properties are modified and delayed until
  12. `Object.sync`. Absolute object position can be accessed through private variables `Object.matA`, `Object.matB`,
  13. `Object.matC`, `Object.matD`, `Object.absX` and `Object.absY`.
  14. But it should be noted that in order to ensure up-to-date values, it's advised to call `Object.syncPos` before accessing them.
  15. **/
  16. @:allow(h2d.Tools)
  17. class Object #if (domkit && !domkit_heaps) implements domkit.Model<h2d.Object> #end {
  18. static var nullDrawable : h2d.Drawable;
  19. var children : Array<Object>;
  20. /**
  21. The parent container of this object.
  22. See `Object.contentChanged` for more details.
  23. **/
  24. @:dox(show)
  25. var parentContainer : Object;
  26. /**
  27. The parent object in the scene tree.
  28. **/
  29. public var parent(default, null) : Object;
  30. /**
  31. How many immediate children this object has.
  32. **/
  33. public var numChildren(get, never) : Int;
  34. /**
  35. The name of the object. Can be used to retrieve an object within a tree by using `Object.getObjectByName`.
  36. **/
  37. public var name : String;
  38. /**
  39. The x position (in pixels) of the object relative to its parent.
  40. **/
  41. public var x(default,set) : Float = 0;
  42. /**
  43. The y position (in pixels) of the object relative to its parent.
  44. **/
  45. public var y(default, set) : Float = 0;
  46. /**
  47. The amount of horizontal scaling of this object.
  48. **/
  49. public var scaleX(default,set) : Float = 1;
  50. /**
  51. The amount of vertical scaling of this object.
  52. **/
  53. public var scaleY(default,set) : Float = 1;
  54. /**
  55. The rotation angle of this object, in radians.
  56. **/
  57. public var rotation(default, set) : Float = 0;
  58. /**
  59. Is the object and its children are displayed on screen.
  60. **/
  61. public var visible(default, set) : Bool = true;
  62. /**
  63. The amount of transparency of the Object.
  64. **/
  65. public var alpha : Float = 1.;
  66. /**
  67. The post process filter for this object.
  68. When set, `Object.alpha` value affects both filter and object transparency (use `Drawable.color.a` to set transparency only for the object).
  69. **/
  70. public var filter(default,set) : h2d.filter.Filter;
  71. /**
  72. The blending mode of the object.
  73. If there is no `Object.filter` active, only applies to the current object (not inherited by children).
  74. Otherwise tells how the filter is blended with background.
  75. **/
  76. public var blendMode : BlendMode = Alpha;
  77. #if domkit
  78. public var dom : domkit.Properties<h2d.Object>;
  79. @:dox(hide) @:noCompletion public inline function getChildren() return children;
  80. #end
  81. var matA : Float;
  82. var matB : Float;
  83. var matC : Float;
  84. var matD : Float;
  85. var absX : Float;
  86. var absY : Float;
  87. /**
  88. A flag that indicates that the object transform was modified and absolute position recalculation is required.
  89. Automatically cleared on `Object.sync` and can be manually synced with the `Object.syncPos`.
  90. **/
  91. @:dox(show)
  92. var posChanged : Bool;
  93. /**
  94. A flag that indicates whether the object was allocated or not.
  95. When adding children to allocated objects, `onAdd` is being called immediately,
  96. otherwise it's delayed until the whole tree is added to a currently active `Scene`.
  97. **/
  98. @:dox(show)
  99. var allocated : Bool;
  100. var lastFrame : Int;
  101. /**
  102. Create a new empty object.
  103. @param parent An optional parent `h2d.Object` instance to which Object adds itself if set.
  104. **/
  105. public function new( ?parent : Object ) {
  106. matA = 1; matB = 0; matC = 0; matD = 1; absX = 0; absY = 0;
  107. posChanged = parent != null;
  108. children = [];
  109. if( parent != null )
  110. parent.addChild(this);
  111. }
  112. /**
  113. Return the bounds of the object for its whole content, recursively.
  114. @param relativeTo An optional object relative to coordinates of which bounds are returned.
  115. Returns bounds in the absolute coordinates if not set.
  116. @param out An optional bounds instance to fill. Allocates new Bounds instance and returns it if not set.
  117. **/
  118. public function getBounds( ?relativeTo : Object, ?out : h2d.col.Bounds ) : h2d.col.Bounds {
  119. if( out == null ) out = new h2d.col.Bounds() else out.empty();
  120. if( relativeTo != null )
  121. relativeTo.syncPos();
  122. if( relativeTo != this )
  123. syncPos();
  124. getBoundsRec(relativeTo, out, false);
  125. if( out.isEmpty() ) {
  126. addBounds(relativeTo, out, -1, -1, 2, 2);
  127. out.xMax = out.xMin = (out.xMax + out.xMin) * 0.5;
  128. out.yMax = out.yMin = (out.yMax + out.yMin) * 0.5;
  129. }
  130. return out;
  131. }
  132. /**
  133. Similar to `getBounds(parent)`, but instead of the full content, it will return
  134. the size based on the alignment of the object. For instance for a text, `Object.getBounds` will return
  135. the full glyphs size whereas `getSize` will ignore the pixels under the baseline.
  136. @param out An optional bounds instance to fill. Allocates new Bounds instance and returns it if not set.
  137. **/
  138. public function getSize( ?out : h2d.col.Bounds ) : h2d.col.Bounds {
  139. if( out == null ) out = new h2d.col.Bounds() else out.empty();
  140. syncPos();
  141. getBoundsRec(parent, out, true);
  142. if( out.isEmpty() ) {
  143. addBounds(parent, out, -1, -1, 2, 2);
  144. out.xMax = out.xMin = (out.xMax + out.xMin) * 0.5;
  145. out.yMax = out.yMin = (out.yMax + out.yMin) * 0.5;
  146. }
  147. out.offset( -x, -y);
  148. return out;
  149. }
  150. /**
  151. Returns the updated absolute position matrix. See `Object.getMatrix` for current matrix values.
  152. **/
  153. public function getAbsPos() {
  154. syncPos();
  155. var m = new h2d.col.Matrix();
  156. m.a = matA;
  157. m.b = matB;
  158. m.c = matC;
  159. m.d = matD;
  160. m.x = absX;
  161. m.y = absY;
  162. return m;
  163. }
  164. /**
  165. Find a single object in the tree by calling `f` on each and returning the first not-null value returned, or null if not found.
  166. **/
  167. public function find<T>( f : Object -> Null<T> ) : Null<T> {
  168. var v = f(this);
  169. if( v != null )
  170. return v;
  171. for( o in children ) {
  172. var v = o.find(f);
  173. if( v != null ) return v;
  174. }
  175. return null;
  176. }
  177. /**
  178. Find several objects in the tree by calling `f` on each and returning all the not-null values returned.
  179. @param arr An optional array instance to fill results with. Allocates a new array if not set.
  180. **/
  181. public function findAll<T>( f : Object -> Null<T>, ?arr : Array<T> ) : Array<T> {
  182. if( arr == null ) arr = [];
  183. var v = f(this);
  184. if( v != null )
  185. arr.push(v);
  186. for( o in children )
  187. o.findAll(f,arr);
  188. return arr;
  189. }
  190. function set_filter(f : h2d.filter.Filter) {
  191. if( filter != null && allocated ) filter.unbind(this);
  192. filter = f;
  193. if( f != null && allocated ) f.bind(this);
  194. return f;
  195. }
  196. /**
  197. Override this method in order to expand the reported bounds of an object. `Object.addBounds` can be used to add bounds with respect to `relativeTo`.
  198. Do not remove the super call.
  199. @param relativeTo An object relative to which the Object bounds coordinates should be.
  200. @param out An output Bounds instance.
  201. @param forSize Whether it's being called for `Object.getSize` or `Object.getBounds`.
  202. **/
  203. @:dox(show)
  204. function getBoundsRec( relativeTo : Object, out : h2d.col.Bounds, forSize : Bool ) : Void {
  205. if( posChanged ) {
  206. calcAbsPos();
  207. for( c in children )
  208. c.posChanged = true;
  209. posChanged = false;
  210. }
  211. var n = children.length;
  212. if( n == 0 ) {
  213. out.empty();
  214. return;
  215. }
  216. if( n == 1 ) {
  217. var c = children[0];
  218. if( c.visible ) c.getBoundsRec(relativeTo, out,forSize) else out.empty();
  219. return;
  220. }
  221. var xmin = hxd.Math.POSITIVE_INFINITY, ymin = hxd.Math.POSITIVE_INFINITY;
  222. var xmax = hxd.Math.NEGATIVE_INFINITY, ymax = hxd.Math.NEGATIVE_INFINITY;
  223. for( c in children ) {
  224. if( !c.visible ) continue;
  225. c.getBoundsRec(relativeTo, out, forSize);
  226. if( out.xMin < xmin ) xmin = out.xMin;
  227. if( out.yMin < ymin ) ymin = out.yMin;
  228. if( out.xMax > xmax ) xmax = out.xMax;
  229. if( out.yMax > ymax ) ymax = out.yMax;
  230. }
  231. out.xMin = xmin;
  232. out.yMin = ymin;
  233. out.xMax = xmax;
  234. out.yMax = ymax;
  235. }
  236. /**
  237. Adds specified area in local coordinate space to the bounds. Expected to be used within `Object.getBoundsRec`.
  238. @param relativeTo An object relative to which the Object bounds coordinates should be. If not set, coordinates are in absolute coordinate space.
  239. @param out An output Bounds instance.
  240. @param dx The top-left X offset of the added bounds rectangle.
  241. @param dy The top-left Y offset of the added bounds rectangle.
  242. @param width The width of the added bounds rectangle.
  243. @param height The height of the added bounds rectangle.
  244. **/
  245. @:dox(show)
  246. function addBounds( relativeTo : Object, out : h2d.col.Bounds, dx : Float, dy : Float, width : Float, height : Float ) {
  247. if( width <= 0 || height <= 0 ) return;
  248. if( relativeTo == null ) {
  249. var x, y;
  250. out.addPos(dx * matA + dy * matC + absX, dx * matB + dy * matD + absY);
  251. out.addPos((dx + width) * matA + dy * matC + absX, (dx + width) * matB + dy * matD + absY);
  252. out.addPos(dx * matA + (dy + height) * matC + absX, dx * matB + (dy + height) * matD + absY);
  253. out.addPos((dx + width) * matA + (dy + height) * matC + absX, (dx + width) * matB + (dy + height) * matD + absY);
  254. return;
  255. }
  256. if( relativeTo == this ) {
  257. if( out.xMin > dx ) out.xMin = dx;
  258. if( out.yMin > dy ) out.yMin = dy;
  259. if( out.xMax < dx + width ) out.xMax = dx + width;
  260. if( out.yMax < dy + height ) out.yMax = dy + height;
  261. return;
  262. }
  263. var r = relativeTo.matA * relativeTo.matD - relativeTo.matB * relativeTo.matC;
  264. if( r == 0 )
  265. return;
  266. var det = 1 / r;
  267. var rA = relativeTo.matD * det;
  268. var rB = -relativeTo.matB * det;
  269. var rC = -relativeTo.matC * det;
  270. var rD = relativeTo.matA * det;
  271. var rX = absX - relativeTo.absX;
  272. var rY = absY - relativeTo.absY;
  273. var x, y;
  274. x = dx * matA + dy * matC + rX;
  275. y = dx * matB + dy * matD + rY;
  276. out.addPos(x * rA + y * rC, x * rB + y * rD);
  277. x = (dx + width) * matA + dy * matC + rX;
  278. y = (dx + width) * matB + dy * matD + rY;
  279. out.addPos(x * rA + y * rC, x * rB + y * rD);
  280. x = dx * matA + (dy + height) * matC + rX;
  281. y = dx * matB + (dy + height) * matD + rY;
  282. out.addPos(x * rA + y * rC, x * rB + y * rD);
  283. x = (dx + width) * matA + (dy + height) * matC + rX;
  284. y = (dx + width) * matB + (dy + height) * matD + rY;
  285. out.addPos(x * rA + y * rC, x * rB + y * rD);
  286. }
  287. /**
  288. Return the total number of children in the whole tree, recursively.
  289. **/
  290. public function getObjectsCount() : Int {
  291. var k = 0;
  292. for( c in children )
  293. k += c.getObjectsCount() + 1;
  294. return k;
  295. }
  296. /**
  297. Convert a local position (or `[0,0]` if `pt` is null) relative to the object origin into an absolute screen position, applying all the inherited transforms.
  298. @param pt An optional position to convert and return. Allocates new Point at 0,0 position if not set. Modifies the Point instance as is.
  299. **/
  300. public function localToGlobal( ?pt : h2d.col.Point ) : h2d.col.Point {
  301. syncPos();
  302. if( pt == null ) pt = new h2d.col.Point();
  303. var px = pt.x * matA + pt.y * matC + absX;
  304. var py = pt.x * matB + pt.y * matD + absY;
  305. pt.x = px;
  306. pt.y = py;
  307. return pt;
  308. }
  309. /**
  310. Convert an absolute screen position into a local position relative to the object origin, applying all the inherited transforms.
  311. @param pt A position to convert and return. Modifies the Point instance as is.
  312. **/
  313. public function globalToLocal( pt : h2d.col.Point ) : h2d.col.Point {
  314. syncPos();
  315. pt.x -= absX;
  316. pt.y -= absY;
  317. var invDet = 1 / (matA * matD - matB * matC);
  318. var px = (pt.x * matD - pt.y * matC) * invDet;
  319. var py = (-pt.x * matB + pt.y * matA) * invDet;
  320. pt.x = px;
  321. pt.y = py;
  322. return pt;
  323. }
  324. /**
  325. Returns an `h2d.Scene` down the hierarchy tree or `null` if object is not added to Scene.
  326. **/
  327. public function getScene() : Scene {
  328. var p = this;
  329. while( p.parent != null ) p = p.parent;
  330. return hxd.impl.Api.downcast(p, Scene);
  331. }
  332. function set_visible(b) {
  333. if( visible == b )
  334. return b;
  335. visible = b;
  336. onContentChanged();
  337. return b;
  338. }
  339. /**
  340. Add a child object at the end of the children list.
  341. **/
  342. public function addChild( s : Object ) : Void {
  343. addChildAt(s, children.length);
  344. }
  345. /**
  346. Insert a child object at the specified position of the children list.
  347. **/
  348. public function addChildAt( s : Object, pos : Int ) : Void {
  349. if( pos < 0 ) pos = 0;
  350. if( pos > children.length ) pos = children.length;
  351. var p = this;
  352. while( p != null ) {
  353. if( p == s ) throw "Recursive addChild";
  354. p = p.parent;
  355. }
  356. if( s.parent != null ) {
  357. // prevent calling onRemove
  358. var old = s.allocated;
  359. s.allocated = false;
  360. s.parent.removeChild(s);
  361. s.allocated = old;
  362. }
  363. children.insert(pos, s);
  364. if( !allocated && s.allocated )
  365. s.onRemove();
  366. s.parent = this;
  367. s.parentContainer = parentContainer;
  368. s.posChanged = true;
  369. // ensure that proper alloc/delete is done if we change parent
  370. if( allocated ) {
  371. if( !s.allocated )
  372. s.onAdd();
  373. else
  374. s.onHierarchyMoved(true);
  375. }
  376. onContentChanged();
  377. #if domkit
  378. if( s.dom != null ) s.dom.onParentChanged();
  379. #end
  380. }
  381. /**
  382. Should be called when Object content was changed in order to notify parent container. See `Object.contentChanged`.
  383. **/
  384. @:dox(show)
  385. inline function onContentChanged() {
  386. if( parentContainer != null ) parentContainer.contentChanged(this);
  387. }
  388. /**
  389. Sent when object was already allocated and moved within scene object tree hierarchy.
  390. Do not remove the super call when overriding.
  391. @param parentChanged Whether Object was moved withing same parent (through `Layers.ysort` for example) or relocated to a new one.
  392. **/
  393. @:dox(show)
  394. function onHierarchyMoved( parentChanged : Bool ) {
  395. for ( c in children )
  396. c.onHierarchyMoved(parentChanged);
  397. }
  398. /**
  399. Sent when object is being added to an allocated scene.
  400. Do not remove the super call when overriding.
  401. **/
  402. @:dox(show)
  403. function onAdd() {
  404. allocated = true;
  405. if( filter != null )
  406. filter.bind(this);
  407. for( c in children )
  408. c.onAdd();
  409. }
  410. /**
  411. Sent when object is removed from the allocated scene.
  412. Do not remove the super call when overriding.
  413. **/
  414. @:dox(show)
  415. function onRemove() {
  416. allocated = false;
  417. if( filter != null )
  418. filter.unbind(this);
  419. for( c in children )
  420. c.onRemove();
  421. }
  422. /**
  423. Populates Matrix with current absolute object transform values. See `Object.getAbsPos` for up-to-date values.
  424. **/
  425. @:dox(show)
  426. function getMatrix( m : h2d.col.Matrix ) {
  427. m.a = matA;
  428. m.b = matB;
  429. m.c = matC;
  430. m.d = matD;
  431. m.x = absX;
  432. m.y = absY;
  433. }
  434. /**
  435. Remove the given object from the immediate children list of the object if it's part of it.
  436. **/
  437. public function removeChild( s : Object ) {
  438. if( children.remove(s) ) {
  439. if( s.allocated ) s.onRemove();
  440. s.parent = null;
  441. if( s.parentContainer != null ) s.setParentContainer(null);
  442. s.posChanged = true;
  443. #if domkit
  444. if( s.dom != null ) s.dom.onParentChanged();
  445. #end
  446. onContentChanged();
  447. }
  448. }
  449. /**
  450. Sets the parent container for this Object and it's children.
  451. See `Object.contentChanged` for more details.
  452. **/
  453. @:dox(show)
  454. function setParentContainer( c : Object ) {
  455. parentContainer = c;
  456. for( s in children )
  457. s.setParentContainer(c);
  458. }
  459. /**
  460. Remove all children from the immediate children list.
  461. **/
  462. public function removeChildren() {
  463. while( numChildren>0 )
  464. removeChild( getChildAt(0) );
  465. }
  466. /**
  467. Same as `parent.removeChild(this)`, but does nothing if parent is null.
  468. **/
  469. public inline function remove() {
  470. if( this != null && parent != null ) parent.removeChild(this);
  471. }
  472. /**
  473. Draw the object and all its children into the given Texture.
  474. **/
  475. public function drawTo( t : h3d.mat.Texture ) {
  476. var s = getScene();
  477. var needDispose = s == null;
  478. if( s == null ) s = new h2d.Scene();
  479. @:privateAccess s.drawImplTo(this, [t]);
  480. if( needDispose ) {
  481. s.dispose();
  482. onRemove();
  483. }
  484. }
  485. /**
  486. Draw the object and all its children into the given Textures.
  487. **/
  488. public function drawToTextures( texs : Array<h3d.mat.Texture>, outputs : Array<hxsl.Output> ) {
  489. var s = getScene();
  490. var needDispose = s == null;
  491. if( s == null ) s = new h2d.Scene();
  492. @:privateAccess s.drawImplTo(this, texs, outputs);
  493. if( needDispose ) {
  494. s.dispose();
  495. onRemove();
  496. }
  497. }
  498. /**
  499. Override this method in order to add custom graphics rendering to your Object.
  500. `draw` is invoked before rendering of the object children.
  501. **/
  502. @:dox(show)
  503. function draw( ctx : RenderContext ) {
  504. }
  505. /**
  506. Performs a sync of data for rendering (such as absolute position recalculation).
  507. While this method can be used as a substitute to an update loop, it's primary purpose it to prepare the Object to be rendered.
  508. Do not remove the super call when overriding.
  509. **/
  510. @:dox(show)
  511. function sync( ctx : RenderContext ) {
  512. var changed = posChanged;
  513. if( changed ) {
  514. calcAbsPos();
  515. posChanged = false;
  516. }
  517. lastFrame = ctx.frame;
  518. var p = 0, len = children.length;
  519. while( p < len ) {
  520. var c = children[p];
  521. if( c == null )
  522. break;
  523. if( c.lastFrame != ctx.frame ) {
  524. if( changed ) c.posChanged = true;
  525. c.sync(ctx);
  526. }
  527. // if the object was removed, let's restart again.
  528. // our lastFrame ensure that no object will get synched twice
  529. if( children[p] != c ) {
  530. p = 0;
  531. len = children.length;
  532. } else
  533. p++;
  534. }
  535. }
  536. /**
  537. Ensures that object has an up-to-date position transform.
  538. **/
  539. @:dox(show)
  540. function syncPos() {
  541. if( parent != null ) parent.syncPos();
  542. if( posChanged ) {
  543. calcAbsPos();
  544. for( c in children )
  545. c.posChanged = true;
  546. posChanged = false;
  547. }
  548. }
  549. /**
  550. <span class="label">Internal usage</span>
  551. Calculates the absolute object position transform.
  552. See `Object.syncPos` for a safe position sync method.
  553. This method does not ensure that object parents also have up-to-date transform nor does it clear the `Object.posChanged` flag.
  554. **/
  555. @:dox(show)
  556. function calcAbsPos() {
  557. if( parent == null ) {
  558. var cr, sr;
  559. if( rotation == 0 ) {
  560. cr = 1.; sr = 0.;
  561. matA = scaleX;
  562. matB = 0;
  563. matC = 0;
  564. matD = scaleY;
  565. } else {
  566. cr = Math.cos(rotation);
  567. sr = Math.sin(rotation);
  568. matA = scaleX * cr;
  569. matB = scaleX * sr;
  570. matC = scaleY * -sr;
  571. matD = scaleY * cr;
  572. }
  573. absX = x;
  574. absY = y;
  575. } else {
  576. // M(rel) = S . R . T
  577. // M(abs) = M(rel) . P(abs)
  578. if( rotation == 0 ) {
  579. matA = scaleX * parent.matA;
  580. matB = scaleX * parent.matB;
  581. matC = scaleY * parent.matC;
  582. matD = scaleY * parent.matD;
  583. } else {
  584. var cr = Math.cos(rotation);
  585. var sr = Math.sin(rotation);
  586. var tmpA = scaleX * cr;
  587. var tmpB = scaleX * sr;
  588. var tmpC = scaleY * -sr;
  589. var tmpD = scaleY * cr;
  590. matA = tmpA * parent.matA + tmpB * parent.matC;
  591. matB = tmpA * parent.matB + tmpB * parent.matD;
  592. matC = tmpC * parent.matA + tmpD * parent.matC;
  593. matD = tmpC * parent.matB + tmpD * parent.matD;
  594. }
  595. absX = x * parent.matA + y * parent.matC + parent.absX;
  596. absY = x * parent.matB + y * parent.matD + parent.absY;
  597. }
  598. }
  599. /**
  600. Draws single Tile instance with this Object transform.
  601. **/
  602. @:dox(show)
  603. function emitTile( ctx : RenderContext, tile : h2d.Tile ) {
  604. if( nullDrawable == null )
  605. nullDrawable = @:privateAccess new h2d.Drawable(null);
  606. if( !ctx.hasBuffering() ) {
  607. nullDrawable.absX = absX;
  608. nullDrawable.absY = absY;
  609. nullDrawable.matA = matA;
  610. nullDrawable.matB = matB;
  611. nullDrawable.matC = matC;
  612. nullDrawable.matD = matD;
  613. ctx.drawTile(nullDrawable, tile);
  614. return;
  615. }
  616. if( !ctx.beginDrawBatch(nullDrawable, tile.getTexture()) ) return;
  617. var ax = absX + tile.dx * matA + tile.dy * matC;
  618. var ay = absY + tile.dx * matB + tile.dy * matD;
  619. var buf = ctx.buffer;
  620. var pos = ctx.bufPos;
  621. buf.grow(pos + 4 * 8);
  622. inline function emit(v:Float) buf[pos++] = v;
  623. emit(ax);
  624. emit(ay);
  625. emit(tile.u);
  626. emit(tile.v);
  627. emit(1.);
  628. emit(1.);
  629. emit(1.);
  630. emit(ctx.globalAlpha);
  631. var tw = tile.width;
  632. var th = tile.height;
  633. var dx1 = tw * matA;
  634. var dy1 = tw * matB;
  635. var dx2 = th * matC;
  636. var dy2 = th * matD;
  637. emit(ax + dx1);
  638. emit(ay + dy1);
  639. emit(tile.u2);
  640. emit(tile.v);
  641. emit(1.);
  642. emit(1.);
  643. emit(1.);
  644. emit(ctx.globalAlpha);
  645. emit(ax + dx2);
  646. emit(ay + dy2);
  647. emit(tile.u);
  648. emit(tile.v2);
  649. emit(1.);
  650. emit(1.);
  651. emit(1.);
  652. emit(ctx.globalAlpha);
  653. emit(ax + dx1 + dx2);
  654. emit(ay + dy1 + dy2);
  655. emit(tile.u2);
  656. emit(tile.v2);
  657. emit(1.);
  658. emit(1.);
  659. emit(1.);
  660. emit(ctx.globalAlpha);
  661. ctx.bufPos = pos;
  662. }
  663. /**
  664. <span class="label">Internal usage</span>
  665. Clip a local bounds with our global viewport.
  666. Used during filter rendering in order to clip out areas that are off-screen and should not be rendered.
  667. **/
  668. @:dox(show)
  669. function clipBounds( ctx : RenderContext, bounds : h2d.col.Bounds ) {
  670. var view = ctx.tmpBounds;
  671. var matA, matB, matC, matD, absX, absY;
  672. @:privateAccess if( ctx.inFilter != null ) {
  673. var f1 = ctx.baseShader.filterMatrixA;
  674. var f2 = ctx.baseShader.filterMatrixB;
  675. var tmpA = this.matA * f1.x + this.matB * f1.y;
  676. var tmpB = this.matA * f2.x + this.matB * f2.y;
  677. var tmpC = this.matC * f1.x + this.matD * f1.y;
  678. var tmpD = this.matC * f2.x + this.matD * f2.y;
  679. var tmpX = this.absX * f1.x + this.absY * f1.y + f1.z;
  680. var tmpY = this.absX * f2.x + this.absY * f2.y + f2.z;
  681. matA = tmpA * ctx.viewA + tmpB * ctx.viewC;
  682. matB = tmpA * ctx.viewB + tmpB * ctx.viewD;
  683. matC = tmpC * ctx.viewA + tmpD * ctx.viewC;
  684. matD = tmpC * ctx.viewB + tmpD * ctx.viewD;
  685. absX = tmpX * ctx.viewA + tmpY * ctx.viewC + ctx.viewX;
  686. absY = tmpX * ctx.viewB + tmpY * ctx.viewD + ctx.viewY;
  687. } else {
  688. matA = this.matA * ctx.viewA + this.matB * ctx.viewC;
  689. matB = this.matA * ctx.viewB + this.matB * ctx.viewD;
  690. matC = this.matC * ctx.viewA + this.matD * ctx.viewC;
  691. matD = this.matC * ctx.viewB + this.matD * ctx.viewD;
  692. absX = this.absX * ctx.viewA + this.absY * ctx.viewC + ctx.viewX;
  693. absY = this.absX * ctx.viewB + this.absY * ctx.viewD + ctx.viewY;
  694. }
  695. // intersect our transformed local view with our viewport in global space
  696. view.empty();
  697. inline function add(x:Float, y:Float) {
  698. view.addPos(x * matA + y * matC + absX, x * matB + y * matD + absY);
  699. }
  700. add(bounds.xMin, bounds.yMin);
  701. add(bounds.xMax, bounds.yMin);
  702. add(bounds.xMin, bounds.yMax);
  703. add(bounds.xMax, bounds.yMax);
  704. // clip with our scene
  705. @:privateAccess {
  706. if( view.xMin < -1 ) view.xMin = -1;
  707. if( view.yMin < -1 ) view.yMin = -1;
  708. if( view.xMax > 1 ) view.xMax = 1;
  709. if( view.yMax > 1 ) view.yMax = 1;
  710. }
  711. // inverse our matrix
  712. var invDet = 1 / (matA * matD - matB * matC);
  713. inline function add(x:Float, y:Float) {
  714. x -= absX;
  715. y -= absY;
  716. view.addPos((x * matD - y * matC) * invDet, ( -x * matB + y * matA) * invDet);
  717. }
  718. // intersect our resulting viewport with our calculated local space
  719. var sxMin = view.xMin;
  720. var syMin = view.yMin;
  721. var sxMax = view.xMax;
  722. var syMax = view.yMax;
  723. view.empty();
  724. add(sxMin, syMin);
  725. add(sxMax, syMin);
  726. add(sxMin, syMax);
  727. add(sxMax, syMax);
  728. // intersects
  729. bounds.doIntersect(view);
  730. }
  731. function drawFilters( ctx : RenderContext ) {
  732. if( !ctx.pushFilter(this) ) return;
  733. var bounds = ctx.tmpBounds;
  734. var total = new h2d.col.Bounds();
  735. var maxExtent = -1.;
  736. filter.sync(ctx, this);
  737. if( filter.autoBounds ) {
  738. maxExtent = filter.boundsExtend;
  739. } else {
  740. filter.getBounds(this, bounds);
  741. total.addBounds(bounds);
  742. }
  743. if( maxExtent >= 0 ) {
  744. getBounds(this, bounds);
  745. bounds.xMin -= maxExtent;
  746. bounds.yMin -= maxExtent;
  747. bounds.xMax += maxExtent;
  748. bounds.yMax += maxExtent;
  749. total.addBounds(bounds);
  750. }
  751. clipBounds(ctx, total);
  752. var xMin = Math.floor(total.xMin + 1e-10);
  753. var yMin = Math.floor(total.yMin + 1e-10);
  754. var width = Math.ceil(total.xMax - xMin - 1e-10);
  755. var height = Math.ceil(total.yMax - yMin - 1e-10);
  756. if( width <= 0 || height <= 0 || total.xMax < total.xMin ) {
  757. ctx.popFilter();
  758. return;
  759. }
  760. var t = ctx.textures.allocTarget("filterTemp", width, height, false);
  761. ctx.pushTarget(t, xMin, yMin, width, height);
  762. ctx.engine.clear(0);
  763. // reset transform and update children
  764. var oldAlpha = ctx.globalAlpha;
  765. var shader = @:privateAccess ctx.baseShader;
  766. var oldA = shader.filterMatrixA.clone();
  767. var oldB = shader.filterMatrixB.clone();
  768. // 2x3 inverse matrix
  769. var invDet = 1 / (matA * matD - matB * matC);
  770. var invA = matD * invDet;
  771. var invB = -matB * invDet;
  772. var invC = -matC * invDet;
  773. var invD = matA * invDet;
  774. var invX = -(absX * invA + absY * invC);
  775. var invY = -(absX * invB + absY * invD);
  776. shader.filterMatrixA.set(invA, invC, invX);
  777. shader.filterMatrixB.set(invB, invD, invY);
  778. ctx.globalAlpha = 1;
  779. drawContent(ctx);
  780. ctx.flush();
  781. var finalTile = h2d.Tile.fromTexture(t);
  782. finalTile.dx = xMin;
  783. finalTile.dy = yMin;
  784. var prev = finalTile;
  785. finalTile = filter.draw(ctx, finalTile);
  786. if( finalTile != prev && finalTile != null ) {
  787. finalTile.dx += xMin;
  788. finalTile.dy += yMin;
  789. }
  790. shader.filterMatrixA.load(oldA);
  791. shader.filterMatrixB.load(oldB);
  792. ctx.popTarget();
  793. ctx.popFilter();
  794. ctx.globalAlpha = oldAlpha;
  795. if( finalTile == null )
  796. return;
  797. drawFiltered(ctx, finalTile);
  798. }
  799. function drawFiltered( ctx : RenderContext, tile : h2d.Tile ) {
  800. @:privateAccess {
  801. var oldAlpha = ctx.globalAlpha;
  802. ctx.currentBlend = null;
  803. ctx.inFilterBlend = blendMode;
  804. ctx.globalAlpha *= alpha;
  805. emitTile(ctx, tile);
  806. ctx.globalAlpha = oldAlpha;
  807. ctx.flush();
  808. ctx.inFilterBlend = null;
  809. ctx.currentBlend = null;
  810. }
  811. }
  812. function drawRec( ctx : RenderContext ) {
  813. if( !visible ) return;
  814. // fallback in case the object was added during a sync() event and we somehow didn't update it
  815. if( posChanged ) {
  816. // only sync anim, don't update() (prevent any event from occuring during draw())
  817. // if( currentAnimation != null ) currentAnimation.sync();
  818. calcAbsPos();
  819. for( c in children )
  820. c.posChanged = true;
  821. posChanged = false;
  822. }
  823. if( filter != null && filter.enable ) {
  824. drawFilters(ctx);
  825. } else {
  826. var old = ctx.globalAlpha;
  827. ctx.globalAlpha *= alpha;
  828. drawContent(ctx);
  829. ctx.globalAlpha = old;
  830. }
  831. }
  832. function drawContent( ctx : RenderContext ) {
  833. if ( ctx.front2back ) {
  834. var i = children.length;
  835. while ( i-- > 0 ) children[i].drawRec(ctx);
  836. draw(ctx);
  837. } else {
  838. draw(ctx);
  839. for ( c in children ) c.drawRec(ctx);
  840. }
  841. }
  842. inline function set_x(v) {
  843. posChanged = true;
  844. return x = v;
  845. }
  846. inline function set_y(v) {
  847. posChanged = true;
  848. return y = v;
  849. }
  850. inline function set_scaleX(v) {
  851. posChanged = true;
  852. return scaleX = v;
  853. }
  854. inline function set_scaleY(v) {
  855. posChanged = true;
  856. return scaleY = v;
  857. }
  858. inline function set_rotation(v) {
  859. posChanged = true;
  860. return rotation = v;
  861. }
  862. /**
  863. Move the object by the specified amount along its current direction (`Object.rotation` angle).
  864. **/
  865. public function move( dx : Float, dy : Float ) {
  866. x += dx * Math.cos(rotation);
  867. y += dy * Math.sin(rotation);
  868. }
  869. /**
  870. Set the position of the object relative to its parent.
  871. **/
  872. public inline function setPosition( x : Float, y : Float ) {
  873. this.x = x;
  874. this.y = y;
  875. }
  876. /**
  877. Rotate the object by the given angle (in radians)
  878. **/
  879. public inline function rotate( v : Float ) {
  880. rotation += v;
  881. }
  882. /**
  883. Scale uniformly the object by the given factor.
  884. **/
  885. public inline function scale( v : Float ) {
  886. scaleX *= v;
  887. scaleY *= v;
  888. }
  889. /**
  890. Set the uniform scale for the object.
  891. **/
  892. public inline function setScale( v : Float ) {
  893. scaleX = v;
  894. scaleY = v;
  895. }
  896. /**
  897. Return the `n`th element among the immediate children list of this object, or `null` if there is no Object at this position.
  898. **/
  899. public inline function getChildAt( n ) {
  900. return children[n];
  901. }
  902. /**
  903. Return the index of the object `o` within the immediate children list of this object, or `-1` if it is not part of the children list.
  904. **/
  905. public function getChildIndex( o ) {
  906. for( i in 0...children.length )
  907. if( children[i] == o )
  908. return i;
  909. return -1;
  910. }
  911. /**
  912. Search for an object recursively by name, return `null` if not found.
  913. **/
  914. public function getObjectByName( name : String ) {
  915. if( this.name == name )
  916. return this;
  917. for( c in children ) {
  918. var o = c.getObjectByName(name);
  919. if( o != null ) return o;
  920. }
  921. return null;
  922. }
  923. inline function get_numChildren() {
  924. return children.length;
  925. }
  926. /**
  927. Return an iterator over this object immediate children
  928. **/
  929. public inline function iterator() {
  930. return new hxd.impl.ArrayIterator(children);
  931. }
  932. @:dox(hide)
  933. function toString() {
  934. var c = Type.getClassName(Type.getClass(this));
  935. return name == null ? c : name + "(" + c + ")";
  936. }
  937. // ---- additional methods for containers (h2d.Flow)
  938. /**
  939. <span class="label">Advanced usage</span>
  940. Called by the children of a container object if they have `parentContainer` defined in them.
  941. Primary use-case is when the child size got changed, requiring content to reevaluate positioning such as `Flow` layouts,
  942. but also can be used for other purposes.
  943. **/
  944. @:dox(show)
  945. function contentChanged( s : Object ) {
  946. }
  947. /**
  948. <span class="label">Advanced usage</span>
  949. This can be called by a parent container to constraint the size of its children.
  950. Negative value mean that constraint is to be disabled.
  951. For example, `Text` constraints it's maximum width, causing word-wrap to occur within constrained area.
  952. @see `FlowProperties.constraint`
  953. **/
  954. @:dox(show)
  955. function constraintSize( maxWidth : Float, maxHeight : Float ) {
  956. }
  957. }