Object.hx 32 KB

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