babylon.prim2dBase.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  7. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  8. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  9. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  10. return c > 3 && r && Object.defineProperty(target, key, r), r;
  11. };
  12. var BABYLON;
  13. (function (BABYLON) {
  14. var Render2DContext = (function () {
  15. function Render2DContext() {
  16. }
  17. return Render2DContext;
  18. })();
  19. BABYLON.Render2DContext = Render2DContext;
  20. /**
  21. * This class store information for the pointerEventObservable Observable.
  22. * The Observable is divided into many sub events (using the Mask feature of the Observable pattern): PointerOver, PointerEnter, PointerDown, PointerMouseWheel, PointerMove, PointerUp, PointerDown, PointerLeave, PointerGotCapture and PointerLostCapture.
  23. */
  24. var PrimitivePointerInfo = (function () {
  25. function PrimitivePointerInfo() {
  26. this.primitivePointerPos = BABYLON.Vector2.Zero();
  27. this.tilt = BABYLON.Vector2.Zero();
  28. this.cancelBubble = false;
  29. }
  30. Object.defineProperty(PrimitivePointerInfo, "PointerOver", {
  31. // The behavior is based on the HTML specifications of the Pointer Events (https://www.w3.org/TR/pointerevents/#list-of-pointer-events). This is not 100% compliant and not meant to be, but still, it's based on these specs for most use cases to be programmed the same way (as closest as possible) as it would have been in HTML.
  32. /**
  33. * This event type is raised when a pointing device is moved into the hit test boundaries of a primitive.
  34. * Bubbles: yes
  35. */
  36. get: function () {
  37. return PrimitivePointerInfo._pointerOver;
  38. },
  39. enumerable: true,
  40. configurable: true
  41. });
  42. Object.defineProperty(PrimitivePointerInfo, "PointerEnter", {
  43. /**
  44. * This event type is raised when a pointing device is moved into the hit test boundaries of a primitive or one of its descendants.
  45. * Bubbles: no
  46. */
  47. get: function () {
  48. return PrimitivePointerInfo._pointerEnter;
  49. },
  50. enumerable: true,
  51. configurable: true
  52. });
  53. Object.defineProperty(PrimitivePointerInfo, "PointerDown", {
  54. /**
  55. * This event type is raised when a pointer enters the active button state (non-zero value in the buttons property). For mouse it's when the device transitions from no buttons depressed to at least one button depressed. For touch/pen this is when a physical contact is made.
  56. * Bubbles: yes
  57. */
  58. get: function () {
  59. return PrimitivePointerInfo._pointerDown;
  60. },
  61. enumerable: true,
  62. configurable: true
  63. });
  64. Object.defineProperty(PrimitivePointerInfo, "PointerMouseWheel", {
  65. /**
  66. * This event type is raised when the pointer is a mouse and it's wheel is rolling
  67. * Bubbles: yes
  68. */
  69. get: function () {
  70. return PrimitivePointerInfo._pointerMouseWheel;
  71. },
  72. enumerable: true,
  73. configurable: true
  74. });
  75. Object.defineProperty(PrimitivePointerInfo, "PointerMove", {
  76. /**
  77. * This event type is raised when a pointer change coordinates or when a pointer changes button state, pressure, tilt, or contact geometry and the circumstances produce no other pointers events.
  78. * Bubbles: yes
  79. */
  80. get: function () {
  81. return PrimitivePointerInfo._pointerMove;
  82. },
  83. enumerable: true,
  84. configurable: true
  85. });
  86. Object.defineProperty(PrimitivePointerInfo, "PointerUp", {
  87. /**
  88. * This event type is raised when the pointer leaves the active buttons states (zero value in the buttons property). For mouse, this is when the device transitions from at least one button depressed to no buttons depressed. For touch/pen, this is when physical contact is removed.
  89. * Bubbles: yes
  90. */
  91. get: function () {
  92. return PrimitivePointerInfo._pointerUp;
  93. },
  94. enumerable: true,
  95. configurable: true
  96. });
  97. Object.defineProperty(PrimitivePointerInfo, "PointerOut", {
  98. /**
  99. * This event type is raised when a pointing device is moved out of the hit test the boundaries of a primitive.
  100. * Bubbles: yes
  101. */
  102. get: function () {
  103. return PrimitivePointerInfo._pointerOut;
  104. },
  105. enumerable: true,
  106. configurable: true
  107. });
  108. Object.defineProperty(PrimitivePointerInfo, "PointerLeave", {
  109. /**
  110. * This event type is raised when a pointing device is moved out of the hit test boundaries of a primitive and all its descendants.
  111. * Bubbles: no
  112. */
  113. get: function () {
  114. return PrimitivePointerInfo._pointerLeave;
  115. },
  116. enumerable: true,
  117. configurable: true
  118. });
  119. Object.defineProperty(PrimitivePointerInfo, "PointerGotCapture", {
  120. /**
  121. * This event type is raised when a primitive receives the pointer capture. This event is fired at the element that is receiving pointer capture. Subsequent events for that pointer will be fired at this element.
  122. * Bubbles: yes
  123. */
  124. get: function () {
  125. return PrimitivePointerInfo._pointerGotCapture;
  126. },
  127. enumerable: true,
  128. configurable: true
  129. });
  130. Object.defineProperty(PrimitivePointerInfo, "PointerLostCapture", {
  131. /**
  132. * This event type is raised after pointer capture is released for a pointer.
  133. * Bubbles: yes
  134. */
  135. get: function () {
  136. return PrimitivePointerInfo._pointerLostCapture;
  137. },
  138. enumerable: true,
  139. configurable: true
  140. });
  141. Object.defineProperty(PrimitivePointerInfo, "MouseWheelPrecision", {
  142. get: function () {
  143. return PrimitivePointerInfo._mouseWheelPrecision;
  144. },
  145. enumerable: true,
  146. configurable: true
  147. });
  148. PrimitivePointerInfo.prototype.updateRelatedTarget = function (prim, primPointerPos) {
  149. this.relatedTarget = prim;
  150. this.relatedTargetPointerPos = primPointerPos;
  151. };
  152. PrimitivePointerInfo.getEventTypeName = function (mask) {
  153. switch (mask) {
  154. case PrimitivePointerInfo.PointerOver: return "PointerOver";
  155. case PrimitivePointerInfo.PointerEnter: return "PointerEnter";
  156. case PrimitivePointerInfo.PointerDown: return "PointerDown";
  157. case PrimitivePointerInfo.PointerMouseWheel: return "PointerMouseWheel";
  158. case PrimitivePointerInfo.PointerMove: return "PointerMove";
  159. case PrimitivePointerInfo.PointerUp: return "PointerUp";
  160. case PrimitivePointerInfo.PointerOut: return "PointerOut";
  161. case PrimitivePointerInfo.PointerLeave: return "PointerLeave";
  162. case PrimitivePointerInfo.PointerGotCapture: return "PointerGotCapture";
  163. case PrimitivePointerInfo.PointerLostCapture: return "PointerLostCapture";
  164. }
  165. };
  166. PrimitivePointerInfo._pointerOver = 0x0001;
  167. PrimitivePointerInfo._pointerEnter = 0x0002;
  168. PrimitivePointerInfo._pointerDown = 0x0004;
  169. PrimitivePointerInfo._pointerMouseWheel = 0x0008;
  170. PrimitivePointerInfo._pointerMove = 0x0010;
  171. PrimitivePointerInfo._pointerUp = 0x0020;
  172. PrimitivePointerInfo._pointerOut = 0x0040;
  173. PrimitivePointerInfo._pointerLeave = 0x0080;
  174. PrimitivePointerInfo._pointerGotCapture = 0x0100;
  175. PrimitivePointerInfo._pointerLostCapture = 0x0200;
  176. PrimitivePointerInfo._mouseWheelPrecision = 3.0;
  177. return PrimitivePointerInfo;
  178. })();
  179. BABYLON.PrimitivePointerInfo = PrimitivePointerInfo;
  180. /**
  181. * Stores information about a Primitive that was intersected
  182. */
  183. var PrimitiveIntersectedInfo = (function () {
  184. function PrimitiveIntersectedInfo(prim, intersectionLocation) {
  185. this.prim = prim;
  186. this.intersectionLocation = intersectionLocation;
  187. }
  188. return PrimitiveIntersectedInfo;
  189. })();
  190. BABYLON.PrimitiveIntersectedInfo = PrimitiveIntersectedInfo;
  191. /**
  192. * Main class used for the Primitive Intersection API
  193. */
  194. var IntersectInfo2D = (function () {
  195. function IntersectInfo2D() {
  196. this.findFirstOnly = false;
  197. this.intersectHidden = false;
  198. this.pickPosition = BABYLON.Vector2.Zero();
  199. }
  200. Object.defineProperty(IntersectInfo2D.prototype, "isIntersected", {
  201. /**
  202. * true if at least one primitive intersected during the test
  203. */
  204. get: function () {
  205. return this.intersectedPrimitives && this.intersectedPrimitives.length > 0;
  206. },
  207. enumerable: true,
  208. configurable: true
  209. });
  210. IntersectInfo2D.prototype.isPrimIntersected = function (prim) {
  211. for (var _i = 0, _a = this.intersectedPrimitives; _i < _a.length; _i++) {
  212. var cur = _a[_i];
  213. if (cur.prim === prim) {
  214. return cur.intersectionLocation;
  215. }
  216. }
  217. return null;
  218. };
  219. // Internals, don't use
  220. IntersectInfo2D.prototype._exit = function (firstLevel) {
  221. if (firstLevel) {
  222. this._globalPickPosition = null;
  223. }
  224. };
  225. return IntersectInfo2D;
  226. })();
  227. BABYLON.IntersectInfo2D = IntersectInfo2D;
  228. var Prim2DBase = (function (_super) {
  229. __extends(Prim2DBase, _super);
  230. function Prim2DBase() {
  231. _super.apply(this, arguments);
  232. }
  233. Prim2DBase.prototype.setupPrim2DBase = function (owner, parent, id, position, origin, isVisible) {
  234. if (isVisible === void 0) { isVisible = true; }
  235. if (!(this instanceof BABYLON.Group2D) && !(this instanceof BABYLON.Sprite2D && id !== null && id.indexOf("__cachedSpriteOfGroup__") === 0) && (owner.cachingStrategy === BABYLON.Canvas2D.CACHESTRATEGY_TOPLEVELGROUPS) && (parent === owner)) {
  236. throw new Error("Can't create a primitive with the canvas as direct parent when the caching strategy is TOPLEVELGROUPS. You need to create a Group below the canvas and use it as the parent for the primitive");
  237. }
  238. this.setupSmartPropertyPrim();
  239. this._pointerEventObservable = new BABYLON.Observable();
  240. this._isPickable = true;
  241. this._siblingDepthOffset = this._hierarchyDepthOffset = 0;
  242. this._boundingInfoDirty = true;
  243. this._boundingInfo = new BABYLON.BoundingInfo2D();
  244. this._owner = owner;
  245. this._parent = parent;
  246. if (parent != null) {
  247. this._hierarchyDepth = parent._hierarchyDepth + 1;
  248. this._renderGroup = this.parent.traverseUp(function (p) { return p instanceof BABYLON.Group2D && p.isRenderableGroup; });
  249. parent.addChild(this);
  250. }
  251. else {
  252. this._hierarchyDepth = 0;
  253. this._renderGroup = null;
  254. }
  255. this._id = id;
  256. this.propertyChanged = new BABYLON.Observable();
  257. this._children = new Array();
  258. this._globalTransformProcessStep = 0;
  259. this._globalTransformStep = 0;
  260. if (this instanceof BABYLON.Group2D) {
  261. var group = this;
  262. group.detectGroupStates();
  263. }
  264. this.position = position;
  265. this.rotation = 0;
  266. this.scale = 1;
  267. this.levelVisible = isVisible;
  268. this.origin = origin || new BABYLON.Vector2(0.5, 0.5);
  269. };
  270. Object.defineProperty(Prim2DBase.prototype, "actionManager", {
  271. get: function () {
  272. if (!this._actionManager) {
  273. this._actionManager = new BABYLON.ActionManager(this.owner.scene);
  274. }
  275. return this._actionManager;
  276. },
  277. enumerable: true,
  278. configurable: true
  279. });
  280. /**
  281. * From 'this' primitive, traverse up (from parent to parent) until the given predicate is true
  282. * @param predicate the predicate to test on each parent
  283. * @return the first primitive where the predicate was successful
  284. */
  285. Prim2DBase.prototype.traverseUp = function (predicate) {
  286. var p = this;
  287. while (p != null) {
  288. if (predicate(p)) {
  289. return p;
  290. }
  291. p = p._parent;
  292. }
  293. return null;
  294. };
  295. Object.defineProperty(Prim2DBase.prototype, "owner", {
  296. /**
  297. * Retrieve the owner Canvas2D
  298. */
  299. get: function () {
  300. return this._owner;
  301. },
  302. enumerable: true,
  303. configurable: true
  304. });
  305. Object.defineProperty(Prim2DBase.prototype, "parent", {
  306. /**
  307. * Get the parent primitive (can be the Canvas, only the Canvas has no parent)
  308. */
  309. get: function () {
  310. return this._parent;
  311. },
  312. enumerable: true,
  313. configurable: true
  314. });
  315. Object.defineProperty(Prim2DBase.prototype, "children", {
  316. /**
  317. * The array of direct children primitives
  318. */
  319. get: function () {
  320. return this._children;
  321. },
  322. enumerable: true,
  323. configurable: true
  324. });
  325. Object.defineProperty(Prim2DBase.prototype, "id", {
  326. /**
  327. * The identifier of this primitive, may not be unique, it's for information purpose only
  328. */
  329. get: function () {
  330. return this._id;
  331. },
  332. enumerable: true,
  333. configurable: true
  334. });
  335. Object.defineProperty(Prim2DBase.prototype, "position", {
  336. get: function () {
  337. return this._position;
  338. },
  339. set: function (value) {
  340. this._position = value;
  341. },
  342. enumerable: true,
  343. configurable: true
  344. });
  345. Object.defineProperty(Prim2DBase.prototype, "rotation", {
  346. get: function () {
  347. return this._rotation;
  348. },
  349. set: function (value) {
  350. this._rotation = value;
  351. },
  352. enumerable: true,
  353. configurable: true
  354. });
  355. Object.defineProperty(Prim2DBase.prototype, "scale", {
  356. get: function () {
  357. return this._scale;
  358. },
  359. set: function (value) {
  360. this._scale = value;
  361. },
  362. enumerable: true,
  363. configurable: true
  364. });
  365. Object.defineProperty(Prim2DBase.prototype, "actualSize", {
  366. /**
  367. * this method must be implemented by the primitive type to return its size
  368. * @returns The size of the primitive
  369. */
  370. get: function () {
  371. return undefined;
  372. },
  373. enumerable: true,
  374. configurable: true
  375. });
  376. Object.defineProperty(Prim2DBase.prototype, "origin", {
  377. /**
  378. * The origin defines the normalized coordinate of the center of the primitive, from the top/left corner.
  379. * The origin is used only to compute transformation of the primitive, it has no meaning in the primitive local frame of reference
  380. * For instance:
  381. * 0,0 means the center is bottom/left. Which is the default for Canvas2D instances
  382. * 0.5,0.5 means the center is at the center of the primitive, which is default of all types of Primitives
  383. * 0,1 means the center is top/left
  384. * @returns The normalized center.
  385. */
  386. get: function () {
  387. return this._origin;
  388. },
  389. set: function (value) {
  390. this._origin = value;
  391. },
  392. enumerable: true,
  393. configurable: true
  394. });
  395. Object.defineProperty(Prim2DBase.prototype, "levelVisible", {
  396. get: function () {
  397. return this._levelVisible;
  398. },
  399. set: function (value) {
  400. this._levelVisible = value;
  401. },
  402. enumerable: true,
  403. configurable: true
  404. });
  405. Object.defineProperty(Prim2DBase.prototype, "isVisible", {
  406. get: function () {
  407. return this._isVisible;
  408. },
  409. set: function (value) {
  410. this._isVisible = value;
  411. },
  412. enumerable: true,
  413. configurable: true
  414. });
  415. Object.defineProperty(Prim2DBase.prototype, "zOrder", {
  416. get: function () {
  417. return this._zOrder;
  418. },
  419. set: function (value) {
  420. this._zOrder = value;
  421. },
  422. enumerable: true,
  423. configurable: true
  424. });
  425. Object.defineProperty(Prim2DBase.prototype, "isPickable", {
  426. /**
  427. * Define if the Primitive can be subject to intersection test or not (default is true)
  428. */
  429. get: function () {
  430. return this._isPickable;
  431. },
  432. set: function (value) {
  433. this._isPickable = value;
  434. },
  435. enumerable: true,
  436. configurable: true
  437. });
  438. Object.defineProperty(Prim2DBase.prototype, "hierarchyDepth", {
  439. /**
  440. * Return the depth level of the Primitive into the Canvas' Graph. A Canvas will be 0, its direct children 1, and so on.
  441. * @returns {}
  442. */
  443. get: function () {
  444. return this._hierarchyDepth;
  445. },
  446. enumerable: true,
  447. configurable: true
  448. });
  449. Object.defineProperty(Prim2DBase.prototype, "renderGroup", {
  450. /**
  451. * Retrieve the Group that is responsible to render this primitive
  452. * @returns {}
  453. */
  454. get: function () {
  455. return this._renderGroup;
  456. },
  457. enumerable: true,
  458. configurable: true
  459. });
  460. Object.defineProperty(Prim2DBase.prototype, "globalTransform", {
  461. /**
  462. * Get the global transformation matrix of the primitive
  463. */
  464. get: function () {
  465. return this._globalTransform;
  466. },
  467. enumerable: true,
  468. configurable: true
  469. });
  470. Object.defineProperty(Prim2DBase.prototype, "invGlobalTransform", {
  471. /**
  472. * Get invert of the global transformation matrix of the primitive
  473. * @returns {}
  474. */
  475. get: function () {
  476. return this._invGlobalTransform;
  477. },
  478. enumerable: true,
  479. configurable: true
  480. });
  481. Object.defineProperty(Prim2DBase.prototype, "localTransform", {
  482. /**
  483. * Get the local transformation of the primitive
  484. */
  485. get: function () {
  486. this._updateLocalTransform();
  487. return this._localTransform;
  488. },
  489. enumerable: true,
  490. configurable: true
  491. });
  492. Object.defineProperty(Prim2DBase.prototype, "boundingInfo", {
  493. /**
  494. * Get the boundingInfo associated to the primitive.
  495. * The value is supposed to be always up to date
  496. */
  497. get: function () {
  498. if (this._boundingInfoDirty) {
  499. this._boundingInfo = this.levelBoundingInfo.clone();
  500. var bi = this._boundingInfo;
  501. var tps = new BABYLON.BoundingInfo2D();
  502. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  503. var curChild = _a[_i];
  504. curChild.boundingInfo.transformToRef(curChild.localTransform, tps);
  505. bi.unionToRef(tps, bi);
  506. }
  507. this._boundingInfoDirty = false;
  508. }
  509. return this._boundingInfo;
  510. },
  511. enumerable: true,
  512. configurable: true
  513. });
  514. Object.defineProperty(Prim2DBase.prototype, "pointerEventObservable", {
  515. /**
  516. * Interaction with the primitive can be create using this Observable. See the PrimitivePointerInfo class for more information
  517. */
  518. get: function () {
  519. return this._pointerEventObservable;
  520. },
  521. enumerable: true,
  522. configurable: true
  523. });
  524. Prim2DBase.prototype.levelIntersect = function (intersectInfo) {
  525. return false;
  526. };
  527. /**
  528. * Capture all the Events of the given PointerId for this primitive.
  529. * Don't forget to call releasePointerEventsCapture when done.
  530. * @param pointerId the Id of the pointer to capture the events from.
  531. */
  532. Prim2DBase.prototype.setPointerEventCapture = function (pointerId) {
  533. return this.owner._setPointerCapture(pointerId, this);
  534. };
  535. /**
  536. * Release a captured pointer made with setPointerEventCapture.
  537. * @param pointerId the Id of the pointer to release the capture from.
  538. */
  539. Prim2DBase.prototype.releasePointerEventsCapture = function (pointerId) {
  540. return this.owner._releasePointerCapture(pointerId, this);
  541. };
  542. /**
  543. * Make an intersection test with the primitive, all inputs/outputs are stored in the IntersectInfo2D class, see its documentation for more information.
  544. * @param intersectInfo contains the settings of the intersection to perform, to setup before calling this method as well as the result, available after a call to this method.
  545. */
  546. Prim2DBase.prototype.intersect = function (intersectInfo) {
  547. if (!intersectInfo) {
  548. return false;
  549. }
  550. // If this is null it means this method is call for the first level, initialize stuffs
  551. var firstLevel = !intersectInfo._globalPickPosition;
  552. if (firstLevel) {
  553. // Compute the pickPosition in global space and use it to find the local position for each level down, always relative from the world to get the maximum accuracy (and speed). The other way would have been to compute in local every level down relative to its parent's local, which wouldn't be as accurate (even if javascript number is 80bits accurate).
  554. intersectInfo._globalPickPosition = BABYLON.Vector2.Zero();
  555. BABYLON.Vector2.TransformToRef(intersectInfo.pickPosition, this.globalTransform, intersectInfo._globalPickPosition);
  556. intersectInfo._localPickPosition = intersectInfo.pickPosition.clone();
  557. intersectInfo.intersectedPrimitives = new Array();
  558. intersectInfo.topMostIntersectedPrimitive = null;
  559. }
  560. if (!intersectInfo.intersectHidden && !this.isVisible) {
  561. return false;
  562. }
  563. // Fast rejection test with boundingInfo
  564. if (!this.boundingInfo.doesIntersect(intersectInfo._localPickPosition)) {
  565. // Important to call this before each return to allow a good recursion next time this intersectInfo is reused
  566. intersectInfo._exit(firstLevel);
  567. return false;
  568. }
  569. // We hit the boundingInfo that bounds this primitive and its children, now we have to test on the primitive of this level
  570. var levelIntersectRes = this.levelIntersect(intersectInfo);
  571. if (levelIntersectRes) {
  572. var pii = new PrimitiveIntersectedInfo(this, intersectInfo._localPickPosition.clone());
  573. intersectInfo.intersectedPrimitives.push(pii);
  574. if (!intersectInfo.topMostIntersectedPrimitive || (intersectInfo.topMostIntersectedPrimitive.prim.getActualZOffset() > pii.prim.getActualZOffset())) {
  575. intersectInfo.topMostIntersectedPrimitive = pii;
  576. }
  577. // If we must stop at the first intersection, we're done, quit!
  578. if (intersectInfo.findFirstOnly) {
  579. intersectInfo._exit(firstLevel);
  580. return true;
  581. }
  582. }
  583. // Recurse to children if needed
  584. if (!levelIntersectRes || !intersectInfo.findFirstOnly) {
  585. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  586. var curChild = _a[_i];
  587. // Don't test primitive not pick able or if it's hidden and we don't test hidden ones
  588. if (!curChild.isPickable || (!intersectInfo.intersectHidden && !curChild.isVisible)) {
  589. continue;
  590. }
  591. // Must compute the localPickLocation for the children level
  592. BABYLON.Vector2.TransformToRef(intersectInfo._globalPickPosition, curChild.invGlobalTransform, intersectInfo._localPickPosition);
  593. // If we got an intersection with the child and we only need to find the first one, quit!
  594. if (curChild.intersect(intersectInfo) && intersectInfo.findFirstOnly) {
  595. intersectInfo._exit(firstLevel);
  596. return true;
  597. }
  598. }
  599. }
  600. intersectInfo._exit(firstLevel);
  601. return intersectInfo.isIntersected;
  602. };
  603. Prim2DBase.prototype.moveChild = function (child, previous) {
  604. if (child.parent !== this) {
  605. return false;
  606. }
  607. var prevOffset, nextOffset;
  608. var childIndex = this._children.indexOf(child);
  609. var prevIndex = previous ? this._children.indexOf(previous) : -1;
  610. // Move to first position
  611. if (!previous) {
  612. prevOffset = 1;
  613. nextOffset = this._children[1]._siblingDepthOffset;
  614. }
  615. else {
  616. prevOffset = this._children[prevIndex]._siblingDepthOffset;
  617. nextOffset = this._children[prevIndex + 1]._siblingDepthOffset;
  618. }
  619. child._siblingDepthOffset = (nextOffset - prevOffset) / 2;
  620. this._children.splice(prevIndex + 1, 0, this._children.splice(childIndex, 1)[0]);
  621. };
  622. Prim2DBase.prototype.addChild = function (child) {
  623. child._hierarchyDepthOffset = this._hierarchyDepthOffset + ((this._children.length + 1) * this._siblingDepthOffset);
  624. child._siblingDepthOffset = this._siblingDepthOffset / this.owner.hierarchyLevelMaxSiblingCount;
  625. this._children.push(child);
  626. };
  627. Prim2DBase.prototype.dispose = function () {
  628. if (!_super.prototype.dispose.call(this)) {
  629. return false;
  630. }
  631. if (this._actionManager) {
  632. this._actionManager.dispose();
  633. this._actionManager = null;
  634. }
  635. // If there's a parent, remove this object from its parent list
  636. if (this._parent) {
  637. var i = this._parent._children.indexOf(this);
  638. if (i !== undefined) {
  639. this._parent._children.splice(i, 1);
  640. }
  641. this._parent = null;
  642. }
  643. // Recurse dispose to children
  644. if (this._children) {
  645. while (this._children.length > 0) {
  646. this._children[this._children.length - 1].dispose();
  647. }
  648. }
  649. return true;
  650. };
  651. Prim2DBase.prototype.getActualZOffset = function () {
  652. return this._zOrder || (1 - this._hierarchyDepthOffset);
  653. };
  654. Prim2DBase.prototype.onPrimBecomesDirty = function () {
  655. if (this._renderGroup) {
  656. this._renderGroup._addPrimToDirtyList(this);
  657. }
  658. };
  659. Prim2DBase.prototype._needPrepare = function () {
  660. return this._visibilityChanged || this._modelDirty || (this._instanceDirtyFlags !== 0) || (this._globalTransformProcessStep !== this._globalTransformStep);
  661. };
  662. Prim2DBase.prototype._prepareRender = function (context) {
  663. this._prepareRenderPre(context);
  664. this._prepareRenderPost(context);
  665. };
  666. Prim2DBase.prototype._prepareRenderPre = function (context) {
  667. };
  668. Prim2DBase.prototype._prepareRenderPost = function (context) {
  669. // Don't recurse if it's a renderable group, the content will be processed by the group itself
  670. if (this instanceof BABYLON.Group2D) {
  671. var self = this;
  672. if (self.isRenderableGroup) {
  673. return;
  674. }
  675. }
  676. // Check if we need to recurse the prepare to children primitives
  677. // - must have children
  678. // - the global transform of this level have changed, or
  679. // - the visible state of primitive has changed
  680. if (this._children.length > 0 && ((this._globalTransformProcessStep !== this._globalTransformStep) ||
  681. this.checkPropertiesDirty(Prim2DBase.isVisibleProperty.flagId))) {
  682. this._children.forEach(function (c) {
  683. // As usual stop the recursion if we meet a renderable group
  684. if (!(c instanceof BABYLON.Group2D && c.isRenderableGroup)) {
  685. c._prepareRender(context);
  686. }
  687. });
  688. }
  689. // Finally reset the dirty flags as we've processed everything
  690. this._modelDirty = false;
  691. this._instanceDirtyFlags = 0;
  692. };
  693. Prim2DBase.CheckParent = function (parent) {
  694. if (!parent) {
  695. throw new Error("A Primitive needs a valid Parent, it can be any kind of Primitives based types, even the Canvas (with the exception that only Group2D can be direct child of a Canvas if the cache strategy used is TOPLEVELGROUPS)");
  696. }
  697. };
  698. Prim2DBase.prototype.updateGlobalTransVisOf = function (list, recurse) {
  699. for (var _i = 0; _i < list.length; _i++) {
  700. var cur = list[_i];
  701. cur.updateGlobalTransVis(recurse);
  702. }
  703. };
  704. Prim2DBase.prototype._updateLocalTransform = function () {
  705. var tflags = Prim2DBase.positionProperty.flagId | Prim2DBase.rotationProperty.flagId | Prim2DBase.scaleProperty.flagId;
  706. if (this.checkPropertiesDirty(tflags)) {
  707. var rot = BABYLON.Quaternion.RotationAxis(new BABYLON.Vector3(0, 0, 1), this._rotation);
  708. var local = BABYLON.Matrix.Compose(new BABYLON.Vector3(this._scale, this._scale, this._scale), rot, new BABYLON.Vector3(this._position.x, this._position.y, 0));
  709. this._localTransform = local;
  710. this.clearPropertiesDirty(tflags);
  711. // this is important to access actualSize AFTER fetching a first version of the local transform and reset the dirty flag, because accessing actualSize on a Group2D which actualSize is built from its content will trigger a call to this very method on this very object. We won't mind about the origin offset not being computed, as long as we return a local transform based on the position/rotation/scale
  712. //var actualSize = this.actualSize;
  713. //if (!actualSize) {
  714. // throw new Error(`The primitive type: ${Tools.getClassName(this)} must implement the actualSize get property!`);
  715. //}
  716. //local.m[12] -= (actualSize.width * this.origin.x) * local.m[0] + (actualSize.height * this.origin.y) * local.m[4];
  717. //local.m[13] -= (actualSize.width * this.origin.x) * local.m[1] + (actualSize.height * this.origin.y) * local.m[5];
  718. return true;
  719. }
  720. return false;
  721. };
  722. Prim2DBase.prototype.updateGlobalTransVis = function (recurse) {
  723. if (this.isDisposed) {
  724. return;
  725. }
  726. // Check if the parent is synced
  727. if (this._parent && this._parent._globalTransformProcessStep !== this.owner._globalTransformProcessStep) {
  728. this._parent.updateGlobalTransVis(false);
  729. }
  730. // Check if we must update this prim
  731. if (this === this.owner || this._globalTransformProcessStep !== this.owner._globalTransformProcessStep) {
  732. var curVisibleState = this.isVisible;
  733. this.isVisible = (!this._parent || this._parent.isVisible) && this.levelVisible;
  734. // Detect a change of visibility
  735. this._visibilityChanged = curVisibleState !== this.isVisible;
  736. // Get/compute the localTransform
  737. var localDirty = this._updateLocalTransform();
  738. // Check if we have to update the globalTransform
  739. if (!this._globalTransform || localDirty || (this._parent && this._parent._globalTransformStep !== this._parentTransformStep)) {
  740. this._globalTransform = this._parent ? this._localTransform.multiply(this._parent._globalTransform) : this._localTransform;
  741. this._invGlobalTransform = BABYLON.Matrix.Invert(this._globalTransform);
  742. this._globalTransformStep = this.owner._globalTransformProcessStep + 1;
  743. this._parentTransformStep = this._parent ? this._parent._globalTransformStep : 0;
  744. }
  745. this._globalTransformProcessStep = this.owner._globalTransformProcessStep;
  746. }
  747. if (recurse) {
  748. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  749. var child = _a[_i];
  750. // Stop the recursion if we meet a renderable group
  751. child.updateGlobalTransVis(!(child instanceof BABYLON.Group2D && child.isRenderableGroup));
  752. }
  753. }
  754. };
  755. Prim2DBase.PRIM2DBASE_PROPCOUNT = 10;
  756. __decorate([
  757. BABYLON.instanceLevelProperty(1, function (pi) { return Prim2DBase.positionProperty = pi; }, false, true)
  758. ], Prim2DBase.prototype, "position", null);
  759. __decorate([
  760. BABYLON.instanceLevelProperty(2, function (pi) { return Prim2DBase.rotationProperty = pi; }, false, true)
  761. ], Prim2DBase.prototype, "rotation", null);
  762. __decorate([
  763. BABYLON.instanceLevelProperty(3, function (pi) { return Prim2DBase.scaleProperty = pi; }, false, true)
  764. ], Prim2DBase.prototype, "scale", null);
  765. __decorate([
  766. BABYLON.instanceLevelProperty(4, function (pi) { return Prim2DBase.originProperty = pi; }, false, true)
  767. ], Prim2DBase.prototype, "origin", null);
  768. __decorate([
  769. BABYLON.dynamicLevelProperty(5, function (pi) { return Prim2DBase.levelVisibleProperty = pi; })
  770. ], Prim2DBase.prototype, "levelVisible", null);
  771. __decorate([
  772. BABYLON.instanceLevelProperty(6, function (pi) { return Prim2DBase.isVisibleProperty = pi; })
  773. ], Prim2DBase.prototype, "isVisible", null);
  774. __decorate([
  775. BABYLON.instanceLevelProperty(7, function (pi) { return Prim2DBase.zOrderProperty = pi; })
  776. ], Prim2DBase.prototype, "zOrder", null);
  777. Prim2DBase = __decorate([
  778. BABYLON.className("Prim2DBase")
  779. ], Prim2DBase);
  780. return Prim2DBase;
  781. })(BABYLON.SmartPropertyPrim);
  782. BABYLON.Prim2DBase = Prim2DBase;
  783. })(BABYLON || (BABYLON = {}));