babylon.prim2dBase.js 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526
  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 PrepareRender2DContext = (function () {
  15. function PrepareRender2DContext() {
  16. this.forceRefreshPrimitive = false;
  17. }
  18. return PrepareRender2DContext;
  19. })();
  20. BABYLON.PrepareRender2DContext = PrepareRender2DContext;
  21. var Render2DContext = (function () {
  22. function Render2DContext(renderMode) {
  23. this._renderMode = renderMode;
  24. this.useInstancing = false;
  25. this.groupInfoPartData = null;
  26. this.partDataStartIndex = this.partDataEndIndex = null;
  27. }
  28. Object.defineProperty(Render2DContext.prototype, "renderMode", {
  29. /**
  30. * Define which render Mode should be used to render the primitive: one of Render2DContext.RenderModeXxxx property
  31. */
  32. get: function () {
  33. return this._renderMode;
  34. },
  35. enumerable: true,
  36. configurable: true
  37. });
  38. Object.defineProperty(Render2DContext, "RenderModeOpaque", {
  39. /**
  40. * The set of primitives to render is opaque.
  41. * This is the first rendering pass. All Opaque primitives are rendered. Depth Compare and Write are both enabled.
  42. */
  43. get: function () {
  44. return Render2DContext._renderModeOpaque;
  45. },
  46. enumerable: true,
  47. configurable: true
  48. });
  49. Object.defineProperty(Render2DContext, "RenderModeAlphaTest", {
  50. /**
  51. * The set of primitives to render is using Alpha Test (aka masking).
  52. * Alpha Blend is enabled, the AlphaMode must be manually set, the render occurs after the RenderModeOpaque and is depth independent (i.e. primitives are not sorted by depth). Depth Compare and Write are both enabled.
  53. */
  54. get: function () {
  55. return Render2DContext._renderModeAlphaTest;
  56. },
  57. enumerable: true,
  58. configurable: true
  59. });
  60. Object.defineProperty(Render2DContext, "RenderModeTransparent", {
  61. /**
  62. * The set of primitives to render is transparent.
  63. * Alpha Blend is enabled, the AlphaMode must be manually set, the render occurs after the RenderModeAlphaTest and is depth dependent (i.e. primitives are stored by depth and rendered back to front). Depth Compare is on, but Depth write is Off.
  64. */
  65. get: function () {
  66. return Render2DContext._renderModeTransparent;
  67. },
  68. enumerable: true,
  69. configurable: true
  70. });
  71. Render2DContext._renderModeOpaque = 1;
  72. Render2DContext._renderModeAlphaTest = 2;
  73. Render2DContext._renderModeTransparent = 3;
  74. return Render2DContext;
  75. })();
  76. BABYLON.Render2DContext = Render2DContext;
  77. /**
  78. * This class store information for the pointerEventObservable Observable.
  79. * 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.
  80. */
  81. var PrimitivePointerInfo = (function () {
  82. function PrimitivePointerInfo() {
  83. this.primitivePointerPos = BABYLON.Vector2.Zero();
  84. this.tilt = BABYLON.Vector2.Zero();
  85. this.cancelBubble = false;
  86. }
  87. Object.defineProperty(PrimitivePointerInfo, "PointerOver", {
  88. // 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.
  89. /**
  90. * This event type is raised when a pointing device is moved into the hit test boundaries of a primitive.
  91. * Bubbles: yes
  92. */
  93. get: function () {
  94. return PrimitivePointerInfo._pointerOver;
  95. },
  96. enumerable: true,
  97. configurable: true
  98. });
  99. Object.defineProperty(PrimitivePointerInfo, "PointerEnter", {
  100. /**
  101. * This event type is raised when a pointing device is moved into the hit test boundaries of a primitive or one of its descendants.
  102. * Bubbles: no
  103. */
  104. get: function () {
  105. return PrimitivePointerInfo._pointerEnter;
  106. },
  107. enumerable: true,
  108. configurable: true
  109. });
  110. Object.defineProperty(PrimitivePointerInfo, "PointerDown", {
  111. /**
  112. * 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.
  113. * Bubbles: yes
  114. */
  115. get: function () {
  116. return PrimitivePointerInfo._pointerDown;
  117. },
  118. enumerable: true,
  119. configurable: true
  120. });
  121. Object.defineProperty(PrimitivePointerInfo, "PointerMouseWheel", {
  122. /**
  123. * This event type is raised when the pointer is a mouse and it's wheel is rolling
  124. * Bubbles: yes
  125. */
  126. get: function () {
  127. return PrimitivePointerInfo._pointerMouseWheel;
  128. },
  129. enumerable: true,
  130. configurable: true
  131. });
  132. Object.defineProperty(PrimitivePointerInfo, "PointerMove", {
  133. /**
  134. * 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.
  135. * Bubbles: yes
  136. */
  137. get: function () {
  138. return PrimitivePointerInfo._pointerMove;
  139. },
  140. enumerable: true,
  141. configurable: true
  142. });
  143. Object.defineProperty(PrimitivePointerInfo, "PointerUp", {
  144. /**
  145. * 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.
  146. * Bubbles: yes
  147. */
  148. get: function () {
  149. return PrimitivePointerInfo._pointerUp;
  150. },
  151. enumerable: true,
  152. configurable: true
  153. });
  154. Object.defineProperty(PrimitivePointerInfo, "PointerOut", {
  155. /**
  156. * This event type is raised when a pointing device is moved out of the hit test the boundaries of a primitive.
  157. * Bubbles: yes
  158. */
  159. get: function () {
  160. return PrimitivePointerInfo._pointerOut;
  161. },
  162. enumerable: true,
  163. configurable: true
  164. });
  165. Object.defineProperty(PrimitivePointerInfo, "PointerLeave", {
  166. /**
  167. * This event type is raised when a pointing device is moved out of the hit test boundaries of a primitive and all its descendants.
  168. * Bubbles: no
  169. */
  170. get: function () {
  171. return PrimitivePointerInfo._pointerLeave;
  172. },
  173. enumerable: true,
  174. configurable: true
  175. });
  176. Object.defineProperty(PrimitivePointerInfo, "PointerGotCapture", {
  177. /**
  178. * 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.
  179. * Bubbles: yes
  180. */
  181. get: function () {
  182. return PrimitivePointerInfo._pointerGotCapture;
  183. },
  184. enumerable: true,
  185. configurable: true
  186. });
  187. Object.defineProperty(PrimitivePointerInfo, "PointerLostCapture", {
  188. /**
  189. * This event type is raised after pointer capture is released for a pointer.
  190. * Bubbles: yes
  191. */
  192. get: function () {
  193. return PrimitivePointerInfo._pointerLostCapture;
  194. },
  195. enumerable: true,
  196. configurable: true
  197. });
  198. Object.defineProperty(PrimitivePointerInfo, "MouseWheelPrecision", {
  199. get: function () {
  200. return PrimitivePointerInfo._mouseWheelPrecision;
  201. },
  202. enumerable: true,
  203. configurable: true
  204. });
  205. PrimitivePointerInfo.prototype.updateRelatedTarget = function (prim, primPointerPos) {
  206. this.relatedTarget = prim;
  207. this.relatedTargetPointerPos = primPointerPos;
  208. };
  209. PrimitivePointerInfo.getEventTypeName = function (mask) {
  210. switch (mask) {
  211. case PrimitivePointerInfo.PointerOver: return "PointerOver";
  212. case PrimitivePointerInfo.PointerEnter: return "PointerEnter";
  213. case PrimitivePointerInfo.PointerDown: return "PointerDown";
  214. case PrimitivePointerInfo.PointerMouseWheel: return "PointerMouseWheel";
  215. case PrimitivePointerInfo.PointerMove: return "PointerMove";
  216. case PrimitivePointerInfo.PointerUp: return "PointerUp";
  217. case PrimitivePointerInfo.PointerOut: return "PointerOut";
  218. case PrimitivePointerInfo.PointerLeave: return "PointerLeave";
  219. case PrimitivePointerInfo.PointerGotCapture: return "PointerGotCapture";
  220. case PrimitivePointerInfo.PointerLostCapture: return "PointerLostCapture";
  221. }
  222. };
  223. PrimitivePointerInfo._pointerOver = 0x0001;
  224. PrimitivePointerInfo._pointerEnter = 0x0002;
  225. PrimitivePointerInfo._pointerDown = 0x0004;
  226. PrimitivePointerInfo._pointerMouseWheel = 0x0008;
  227. PrimitivePointerInfo._pointerMove = 0x0010;
  228. PrimitivePointerInfo._pointerUp = 0x0020;
  229. PrimitivePointerInfo._pointerOut = 0x0040;
  230. PrimitivePointerInfo._pointerLeave = 0x0080;
  231. PrimitivePointerInfo._pointerGotCapture = 0x0100;
  232. PrimitivePointerInfo._pointerLostCapture = 0x0200;
  233. PrimitivePointerInfo._mouseWheelPrecision = 3.0;
  234. return PrimitivePointerInfo;
  235. })();
  236. BABYLON.PrimitivePointerInfo = PrimitivePointerInfo;
  237. /**
  238. * Defines the horizontal and vertical alignment information for a Primitive.
  239. */
  240. var PrimitiveAlignment = (function () {
  241. function PrimitiveAlignment(changeCallback) {
  242. this._changedCallback = changeCallback;
  243. this._horizontal = PrimitiveAlignment.AlignLeft;
  244. this._vertical = PrimitiveAlignment.AlignBottom;
  245. }
  246. Object.defineProperty(PrimitiveAlignment, "AlignLeft", {
  247. /**
  248. * Alignment is made relative to the left edge of the Primitive. Valid for horizontal alignment only.
  249. */
  250. get: function () { return PrimitiveAlignment._AlignLeft; },
  251. enumerable: true,
  252. configurable: true
  253. });
  254. Object.defineProperty(PrimitiveAlignment, "AlignTop", {
  255. /**
  256. * Alignment is made relative to the top edge of the Primitive. Valid for vertical alignment only.
  257. */
  258. get: function () { return PrimitiveAlignment._AlignTop; },
  259. enumerable: true,
  260. configurable: true
  261. });
  262. Object.defineProperty(PrimitiveAlignment, "AlignRight", {
  263. /**
  264. * Alignment is made relative to the right edge of the Primitive. Valid for horizontal alignment only.
  265. */
  266. get: function () { return PrimitiveAlignment._AlignRight; },
  267. enumerable: true,
  268. configurable: true
  269. });
  270. Object.defineProperty(PrimitiveAlignment, "AlignBottom", {
  271. /**
  272. * Alignment is made relative to the bottom edge of the Primitive. Valid for vertical alignment only.
  273. */
  274. get: function () { return PrimitiveAlignment._AlignBottom; },
  275. enumerable: true,
  276. configurable: true
  277. });
  278. Object.defineProperty(PrimitiveAlignment, "AlignCenter", {
  279. /**
  280. * Alignment is made to center the content from equal distance to the opposite edges of the Primitive
  281. */
  282. get: function () { return PrimitiveAlignment._AlignCenter; },
  283. enumerable: true,
  284. configurable: true
  285. });
  286. Object.defineProperty(PrimitiveAlignment, "AlignStretch", {
  287. /**
  288. * The content is stretched toward the opposite edges of the Primitive
  289. */
  290. get: function () { return PrimitiveAlignment._AlignStretch; },
  291. enumerable: true,
  292. configurable: true
  293. });
  294. Object.defineProperty(PrimitiveAlignment.prototype, "horizontal", {
  295. /**
  296. * Get/set the horizontal alignment. Use one of the AlignXXX static properties of this class
  297. */
  298. get: function () {
  299. return this._horizontal;
  300. },
  301. set: function (value) {
  302. if (this._horizontal === value) {
  303. return;
  304. }
  305. this._horizontal = value;
  306. this._changedCallback();
  307. },
  308. enumerable: true,
  309. configurable: true
  310. });
  311. Object.defineProperty(PrimitiveAlignment.prototype, "vertical", {
  312. /**
  313. * Get/set the vertical alignment. Use one of the AlignXXX static properties of this class
  314. */
  315. get: function () {
  316. return this._vertical;
  317. },
  318. set: function (value) {
  319. if (this._vertical === value) {
  320. return;
  321. }
  322. this._vertical = value;
  323. this._changedCallback();
  324. },
  325. enumerable: true,
  326. configurable: true
  327. });
  328. /**
  329. * Set the horizontal alignment from a string value.
  330. * @param text can be either: 'left','right','center','stretch'
  331. */
  332. PrimitiveAlignment.prototype.setHorizontal = function (text) {
  333. var v = text.trim().toLocaleLowerCase();
  334. switch (v) {
  335. case "left":
  336. this.horizontal = PrimitiveAlignment.AlignLeft;
  337. return;
  338. case "right":
  339. this.horizontal = PrimitiveAlignment.AlignRight;
  340. return;
  341. case "center":
  342. this.horizontal = PrimitiveAlignment.AlignCenter;
  343. return;
  344. case "stretch":
  345. this.horizontal = PrimitiveAlignment.AlignStretch;
  346. return;
  347. }
  348. };
  349. /**
  350. * Set the vertical alignment from a string value.
  351. * @param text can be either: 'top','bottom','center','stretch'
  352. */
  353. PrimitiveAlignment.prototype.setVertical = function (text) {
  354. var v = text.trim().toLocaleLowerCase();
  355. switch (v) {
  356. case "top":
  357. this.vertical = PrimitiveAlignment.AlignTop;
  358. return;
  359. case "bottom":
  360. this.vertical = PrimitiveAlignment.AlignBottom;
  361. return;
  362. case "center":
  363. this.vertical = PrimitiveAlignment.AlignCenter;
  364. return;
  365. case "stretch":
  366. this.vertical = PrimitiveAlignment.AlignStretch;
  367. return;
  368. }
  369. };
  370. /**
  371. * Set the horizontal and or vertical alignments from a string value.
  372. * @param text can be: [<h:|horizontal:><left|right|center|stretch>], [<v:|vertical:><top|bottom|center|stretch>]
  373. */
  374. PrimitiveAlignment.prototype.fromString = function (value) {
  375. var m = value.trim().split(",");
  376. for (var _i = 0; _i < m.length; _i++) {
  377. var v = m[_i];
  378. v = v.toLocaleLowerCase().trim();
  379. // Horizontal
  380. var i = v.indexOf("h:");
  381. if (i === -1) {
  382. i = v.indexOf("horizontal:");
  383. }
  384. if (i !== -1) {
  385. v = v.substr(v.indexOf(":") + 1);
  386. this.setHorizontal(v);
  387. continue;
  388. }
  389. // Vertical
  390. i = v.indexOf("v:");
  391. if (i === -1) {
  392. i = v.indexOf("vertical:");
  393. }
  394. if (i !== -1) {
  395. v = v.substr(v.indexOf(":") + 1);
  396. this.setVertical(v);
  397. continue;
  398. }
  399. }
  400. };
  401. PrimitiveAlignment._AlignLeft = 1;
  402. PrimitiveAlignment._AlignTop = 1; // Same as left
  403. PrimitiveAlignment._AlignRight = 2;
  404. PrimitiveAlignment._AlignBottom = 2; // Same as right
  405. PrimitiveAlignment._AlignCenter = 3;
  406. PrimitiveAlignment._AlignStretch = 4;
  407. return PrimitiveAlignment;
  408. })();
  409. BABYLON.PrimitiveAlignment = PrimitiveAlignment;
  410. /**
  411. * Stores information about a Primitive that was intersected
  412. */
  413. var PrimitiveIntersectedInfo = (function () {
  414. function PrimitiveIntersectedInfo(prim, intersectionLocation) {
  415. this.prim = prim;
  416. this.intersectionLocation = intersectionLocation;
  417. }
  418. return PrimitiveIntersectedInfo;
  419. })();
  420. BABYLON.PrimitiveIntersectedInfo = PrimitiveIntersectedInfo;
  421. /**
  422. * Define a thickness toward every edges of a Primitive to allow margin and padding.
  423. * The thickness can be expressed as pixels, percentages, inherit the value of the parent primitive or be auto.
  424. */
  425. var PrimitiveThickness = (function () {
  426. function PrimitiveThickness(parentAccess, changedCallback) {
  427. this._parentAccess = parentAccess;
  428. this._changedCallback = changedCallback;
  429. this._pixels = new Array(4);
  430. this._percentages = new Array(4);
  431. this._setType(0, PrimitiveThickness.Auto);
  432. this._setType(1, PrimitiveThickness.Auto);
  433. this._setType(2, PrimitiveThickness.Auto);
  434. this._setType(3, PrimitiveThickness.Auto);
  435. this._pixels[0] = 0;
  436. this._pixels[1] = 0;
  437. this._pixels[2] = 0;
  438. this._pixels[3] = 0;
  439. }
  440. /**
  441. * Set the thickness from a string value
  442. * @param thickness format is "top: <value>, left:<value>, right:<value>, bottom:<value>" or "<value>" (same for all edges) each are optional, auto will be set if it's omitted.
  443. * Values are: 'auto', 'inherit', 'XX%' for percentage, 'XXpx' or 'XX' for pixels.
  444. */
  445. PrimitiveThickness.prototype.fromString = function (thickness) {
  446. this._clear();
  447. var m = thickness.trim().split(",");
  448. // Special case, one value to apply to all edges
  449. if (m.length === 1 && thickness.indexOf(":") === -1) {
  450. this._setStringValue(m[0], 0, false);
  451. this._setStringValue(m[0], 1, false);
  452. this._setStringValue(m[0], 2, false);
  453. this._setStringValue(m[0], 3, false);
  454. this._changedCallback();
  455. return;
  456. }
  457. var res = false;
  458. for (var _i = 0; _i < m.length; _i++) {
  459. var cm = m[_i];
  460. res = this._extractString(cm, false) || res;
  461. }
  462. if (!res) {
  463. throw new Error("Can't parse the string to create a PrimitiveMargin object, format must be: 'top: <value>, left:<value>, right:<value>, bottom:<value>");
  464. }
  465. // Check the margin that weren't set and set them in auto
  466. if ((this._flags & 0x000F) === 0)
  467. this._flags |= PrimitiveThickness.Pixel << 0;
  468. if ((this._flags & 0x00F0) === 0)
  469. this._flags |= PrimitiveThickness.Pixel << 4;
  470. if ((this._flags & 0x0F00) === 0)
  471. this._flags |= PrimitiveThickness.Pixel << 8;
  472. if ((this._flags & 0xF000) === 0)
  473. this._flags |= PrimitiveThickness.Pixel << 12;
  474. this._changedCallback();
  475. };
  476. /**
  477. * Set the thickness from multiple string
  478. * Possible values are: 'auto', 'inherit', 'XX%' for percentage, 'XXpx' or 'XX' for pixels.
  479. * @param top the top thickness to set
  480. * @param left the left thickness to set
  481. * @param right the right thickness to set
  482. * @param bottom the bottom thickness to set
  483. */
  484. PrimitiveThickness.prototype.fromStrings = function (top, left, right, bottom) {
  485. this._clear();
  486. this._setStringValue(top, 0, false);
  487. this._setStringValue(left, 1, false);
  488. this._setStringValue(right, 2, false);
  489. this._setStringValue(bottom, 3, false);
  490. this._changedCallback();
  491. return this;
  492. };
  493. /**
  494. * Set the thickness from pixel values
  495. * @param top the top thickness in pixels to set
  496. * @param left the left thickness in pixels to set
  497. * @param right the right thickness in pixels to set
  498. * @param bottom the bottom thickness in pixels to set
  499. */
  500. PrimitiveThickness.prototype.fromPixels = function (top, left, right, bottom) {
  501. this._clear();
  502. this._pixels[0] = top;
  503. this._pixels[1] = left;
  504. this._pixels[2] = right;
  505. this._pixels[3] = bottom;
  506. this._changedCallback();
  507. return this;
  508. };
  509. /**
  510. * Apply the same pixel value to all edges
  511. * @param margin the value to set, in pixels.
  512. */
  513. PrimitiveThickness.prototype.fromUniformPixels = function (margin) {
  514. this._clear();
  515. this._pixels[0] = margin;
  516. this._pixels[1] = margin;
  517. this._pixels[2] = margin;
  518. this._pixels[3] = margin;
  519. this._changedCallback();
  520. return this;
  521. };
  522. /**
  523. * Set all edges in auto
  524. */
  525. PrimitiveThickness.prototype.auto = function () {
  526. this._clear();
  527. this._flags = (PrimitiveThickness.Auto << 0) | (PrimitiveThickness.Auto << 4) | (PrimitiveThickness.Auto << 8) | (PrimitiveThickness.Auto << 12);
  528. this._pixels[0] = 0;
  529. this._pixels[1] = 0;
  530. this._pixels[2] = 0;
  531. this._pixels[3] = 0;
  532. this._changedCallback();
  533. return this;
  534. };
  535. PrimitiveThickness.prototype._clear = function () {
  536. this._flags = 0;
  537. this._pixels[0] = 0;
  538. this._pixels[1] = 0;
  539. this._pixels[2] = 0;
  540. this._pixels[3] = 0;
  541. this._percentages[0] = null;
  542. this._percentages[1] = null;
  543. this._percentages[2] = null;
  544. this._percentages[3] = null;
  545. };
  546. PrimitiveThickness.prototype._extractString = function (value, emitChanged) {
  547. var v = value.trim().toLocaleLowerCase();
  548. if (v.indexOf("top:") === 0) {
  549. v = v.substr(4).trim();
  550. return this._setStringValue(v, 0, emitChanged);
  551. }
  552. if (v.indexOf("left:") === 0) {
  553. v = v.substr(5).trim();
  554. return this._setStringValue(v, 1, emitChanged);
  555. }
  556. if (v.indexOf("right:") === 0) {
  557. v = v.substr(6).trim();
  558. return this._setStringValue(v, 2, emitChanged);
  559. }
  560. if (v.indexOf("bottom:") === 0) {
  561. v = v.substr(7).trim();
  562. return this._setStringValue(v, 3, emitChanged);
  563. }
  564. return false;
  565. };
  566. PrimitiveThickness.prototype._setStringValue = function (value, index, emitChanged) {
  567. // Check for auto
  568. var v = value.trim().toLocaleLowerCase();
  569. if (v === "auto") {
  570. if (this._isType(index, PrimitiveThickness.Auto)) {
  571. return true;
  572. }
  573. this._setType(index, PrimitiveThickness.Auto);
  574. this._pixels[index] = 0;
  575. if (emitChanged) {
  576. this._changedCallback();
  577. }
  578. }
  579. else if (v === "inherit") {
  580. if (this._isType(index, PrimitiveThickness.Inherit)) {
  581. return true;
  582. }
  583. this._setType(index, PrimitiveThickness.Inherit);
  584. this._pixels[index] = null;
  585. if (emitChanged) {
  586. this._changedCallback();
  587. }
  588. }
  589. else {
  590. var pI = v.indexOf("%");
  591. // Check for percentage
  592. if (pI !== -1) {
  593. var n_1 = v.substr(0, pI);
  594. var number_1 = Math.round(Number(n_1)) / 100; // Normalize the percentage to [0;1] with a 0.01 precision
  595. if (this._isType(index, PrimitiveThickness.Percentage) && (this._percentages[index] === number_1)) {
  596. return true;
  597. }
  598. this._setType(index, PrimitiveThickness.Percentage);
  599. if (isNaN(number_1)) {
  600. return false;
  601. }
  602. this._percentages[index] = number_1;
  603. if (emitChanged) {
  604. this._changedCallback();
  605. }
  606. return true;
  607. }
  608. // Check for pixel
  609. var n;
  610. pI = v.indexOf("px");
  611. if (pI !== -1) {
  612. n = v.substr(0, pI).trim();
  613. }
  614. else {
  615. n = v;
  616. }
  617. var number = Number(n);
  618. if (this._isType(index, PrimitiveThickness.Pixel) && (this._pixels[index] === number)) {
  619. return true;
  620. }
  621. if (isNaN(number)) {
  622. return false;
  623. }
  624. this._pixels[index] = number;
  625. this._setType(index, PrimitiveThickness.Pixel);
  626. if (emitChanged) {
  627. this._changedCallback();
  628. }
  629. return true;
  630. }
  631. };
  632. PrimitiveThickness.prototype._setPixels = function (value, index, emitChanged) {
  633. // Round the value because, well, it's the thing to do! Otherwise we'll have sub-pixel stuff, and the no change comparison just below will almost never work for PrimitiveThickness values inside a hierarchy of Primitives
  634. value = Math.round(value);
  635. if (this._isType(index, PrimitiveThickness.Pixel) && this._pixels[index] === value) {
  636. return;
  637. }
  638. this._setType(index, PrimitiveThickness.Pixel);
  639. this._pixels[index] = value;
  640. if (emitChanged) {
  641. this._changedCallback();
  642. }
  643. };
  644. PrimitiveThickness.prototype._setPercentage = function (value, index, emitChanged) {
  645. // Clip Value to bounds
  646. value = Math.min(1, value);
  647. value = Math.max(0, value);
  648. value = Math.round(value * 100) / 100; // 0.01 precision
  649. if (this._isType(index, PrimitiveThickness.Percentage) && this._percentages[index] === value) {
  650. return;
  651. }
  652. this._setType(index, PrimitiveThickness.Percentage);
  653. this._percentages[index] = value;
  654. if (emitChanged) {
  655. this._changedCallback();
  656. }
  657. };
  658. PrimitiveThickness.prototype._getStringValue = function (index) {
  659. var f = (this._flags >> (index * 4)) & 0xF;
  660. switch (f) {
  661. case PrimitiveThickness.Auto:
  662. return "auto";
  663. case PrimitiveThickness.Pixel:
  664. return this._pixels[index] + "px";
  665. case PrimitiveThickness.Percentage:
  666. return this._percentages[index] * 100 + "%";
  667. case PrimitiveThickness.Inherit:
  668. return "inherit";
  669. }
  670. return "";
  671. };
  672. PrimitiveThickness.prototype._isType = function (index, type) {
  673. var f = (this._flags >> (index * 4)) & 0xF;
  674. return f === type;
  675. };
  676. PrimitiveThickness.prototype._getType = function (index, processInherit) {
  677. var t = (this._flags >> (index * 4)) & 0xF;
  678. if (processInherit && (t === PrimitiveThickness.Inherit)) {
  679. var p = this._parentAccess();
  680. if (p) {
  681. return p._getType(index, true);
  682. }
  683. return PrimitiveThickness.Auto;
  684. }
  685. return t;
  686. };
  687. PrimitiveThickness.prototype._setType = function (index, type) {
  688. this._flags &= ~(0xF << (index * 4));
  689. this._flags |= type << (index * 4);
  690. };
  691. PrimitiveThickness.prototype.setTop = function (value) {
  692. if (typeof value === "string") {
  693. this._setStringValue(value, 0, true);
  694. }
  695. else {
  696. this.topPixels = value;
  697. }
  698. };
  699. PrimitiveThickness.prototype.setLeft = function (value) {
  700. if (typeof value === "string") {
  701. this._setStringValue(value, 1, true);
  702. }
  703. else {
  704. this.leftPixels = value;
  705. }
  706. };
  707. PrimitiveThickness.prototype.setRight = function (value) {
  708. if (typeof value === "string") {
  709. this._setStringValue(value, 2, true);
  710. }
  711. else {
  712. this.rightPixels = value;
  713. }
  714. };
  715. PrimitiveThickness.prototype.setBottom = function (value) {
  716. if (typeof value === "string") {
  717. this._setStringValue(value, 3, true);
  718. }
  719. else {
  720. this.bottomPixels = value;
  721. }
  722. };
  723. Object.defineProperty(PrimitiveThickness.prototype, "top", {
  724. /**
  725. * Get/set the top thickness. Possible values are: 'auto', 'inherit', 'XX%' for percentage, 'XXpx' or 'XX' for pixels.
  726. */
  727. get: function () {
  728. return this._getStringValue(0);
  729. },
  730. set: function (value) {
  731. this._setStringValue(value, 0, true);
  732. },
  733. enumerable: true,
  734. configurable: true
  735. });
  736. Object.defineProperty(PrimitiveThickness.prototype, "left", {
  737. /**
  738. * Get/set the left thickness. Possible values are: 'auto', 'inherit', 'XX%' for percentage, 'XXpx' or 'XX' for pixels.
  739. */
  740. get: function () {
  741. return this._getStringValue(1);
  742. },
  743. set: function (value) {
  744. this._setStringValue(value, 1, true);
  745. },
  746. enumerable: true,
  747. configurable: true
  748. });
  749. Object.defineProperty(PrimitiveThickness.prototype, "right", {
  750. /**
  751. * Get/set the right thickness. Possible values are: 'auto', 'inherit', 'XX%' for percentage, 'XXpx' or 'XX' for pixels.
  752. */
  753. get: function () {
  754. return this._getStringValue(2);
  755. },
  756. set: function (value) {
  757. this._setStringValue(value, 2, true);
  758. },
  759. enumerable: true,
  760. configurable: true
  761. });
  762. Object.defineProperty(PrimitiveThickness.prototype, "bottom", {
  763. /**
  764. * Get/set the bottom thickness. Possible values are: 'auto', 'inherit', 'XX%' for percentage, 'XXpx' or 'XX' for pixels.
  765. */
  766. get: function () {
  767. return this._getStringValue(3);
  768. },
  769. set: function (value) {
  770. this._setStringValue(value, 3, true);
  771. },
  772. enumerable: true,
  773. configurable: true
  774. });
  775. Object.defineProperty(PrimitiveThickness.prototype, "topPixels", {
  776. /**
  777. * Get/set the top thickness in pixel.
  778. */
  779. get: function () {
  780. return this._pixels[0];
  781. },
  782. set: function (value) {
  783. this._setPixels(value, 0, true);
  784. },
  785. enumerable: true,
  786. configurable: true
  787. });
  788. Object.defineProperty(PrimitiveThickness.prototype, "leftPixels", {
  789. /**
  790. * Get/set the left thickness in pixel.
  791. */
  792. get: function () {
  793. return this._pixels[1];
  794. },
  795. set: function (value) {
  796. this._setPixels(value, 1, true);
  797. },
  798. enumerable: true,
  799. configurable: true
  800. });
  801. Object.defineProperty(PrimitiveThickness.prototype, "rightPixels", {
  802. /**
  803. * Get/set the right thickness in pixel.
  804. */
  805. get: function () {
  806. return this._pixels[2];
  807. },
  808. set: function (value) {
  809. this._setPixels(value, 2, true);
  810. },
  811. enumerable: true,
  812. configurable: true
  813. });
  814. Object.defineProperty(PrimitiveThickness.prototype, "bottomPixels", {
  815. /**
  816. * Get/set the bottom thickness in pixel.
  817. */
  818. get: function () {
  819. return this._pixels[3];
  820. },
  821. set: function (value) {
  822. this._setPixels(value, 3, true);
  823. },
  824. enumerable: true,
  825. configurable: true
  826. });
  827. Object.defineProperty(PrimitiveThickness.prototype, "topPercentage", {
  828. /**
  829. * Get/set the top thickness in percentage.
  830. * The get will return a valid value only if the edge type is percentage.
  831. * The Set will change the edge mode if needed
  832. */
  833. get: function () {
  834. return this._percentages[0];
  835. },
  836. set: function (value) {
  837. this._setPercentage(value, 0, true);
  838. },
  839. enumerable: true,
  840. configurable: true
  841. });
  842. Object.defineProperty(PrimitiveThickness.prototype, "leftPercentage", {
  843. /**
  844. * Get/set the left thickness in percentage.
  845. * The get will return a valid value only if the edge mode is percentage.
  846. * The Set will change the edge mode if needed
  847. */
  848. get: function () {
  849. return this._percentages[1];
  850. },
  851. set: function (value) {
  852. this._setPercentage(value, 1, true);
  853. },
  854. enumerable: true,
  855. configurable: true
  856. });
  857. Object.defineProperty(PrimitiveThickness.prototype, "rightPercentage", {
  858. /**
  859. * Get/set the right thickness in percentage.
  860. * The get will return a valid value only if the edge mode is percentage.
  861. * The Set will change the edge mode if needed
  862. */
  863. get: function () {
  864. return this._percentages[2];
  865. },
  866. set: function (value) {
  867. this._setPercentage(value, 2, true);
  868. },
  869. enumerable: true,
  870. configurable: true
  871. });
  872. Object.defineProperty(PrimitiveThickness.prototype, "bottomPercentage", {
  873. /**
  874. * Get/set the bottom thickness in percentage.
  875. * The get will return a valid value only if the edge mode is percentage.
  876. * The Set will change the edge mode if needed
  877. */
  878. get: function () {
  879. return this._percentages[3];
  880. },
  881. set: function (value) {
  882. this._setPercentage(value, 3, true);
  883. },
  884. enumerable: true,
  885. configurable: true
  886. });
  887. Object.defineProperty(PrimitiveThickness.prototype, "topMode", {
  888. /**
  889. * Get/set the top mode. The setter shouldn't be used, other setters with value should be preferred
  890. */
  891. get: function () {
  892. return this._getType(0, false);
  893. },
  894. set: function (mode) {
  895. this._setType(0, mode);
  896. },
  897. enumerable: true,
  898. configurable: true
  899. });
  900. Object.defineProperty(PrimitiveThickness.prototype, "leftMode", {
  901. /**
  902. * Get/set the left mode. The setter shouldn't be used, other setters with value should be preferred
  903. */
  904. get: function () {
  905. return this._getType(1, false);
  906. },
  907. set: function (mode) {
  908. this._setType(1, mode);
  909. },
  910. enumerable: true,
  911. configurable: true
  912. });
  913. Object.defineProperty(PrimitiveThickness.prototype, "rightMode", {
  914. /**
  915. * Get/set the right mode. The setter shouldn't be used, other setters with value should be preferred
  916. */
  917. get: function () {
  918. return this._getType(2, false);
  919. },
  920. set: function (mode) {
  921. this._setType(2, mode);
  922. },
  923. enumerable: true,
  924. configurable: true
  925. });
  926. Object.defineProperty(PrimitiveThickness.prototype, "bottomMode", {
  927. /**
  928. * Get/set the bottom mode. The setter shouldn't be used, other setters with value should be preferred
  929. */
  930. get: function () {
  931. return this._getType(3, false);
  932. },
  933. set: function (mode) {
  934. this._setType(3, mode);
  935. },
  936. enumerable: true,
  937. configurable: true
  938. });
  939. PrimitiveThickness.prototype._computePixels = function (index, sourceArea, emitChanged) {
  940. var type = this._getType(index, false);
  941. if (type === PrimitiveThickness.Inherit) {
  942. this._parentAccess()._computePixels(index, sourceArea, emitChanged);
  943. return;
  944. }
  945. if (type !== PrimitiveThickness.Percentage) {
  946. return;
  947. }
  948. var pixels = ((index === 0 || index === 3) ? sourceArea.height : sourceArea.width) * this._percentages[index];
  949. this._pixels[index] = pixels;
  950. if (emitChanged) {
  951. this._changedCallback();
  952. }
  953. };
  954. /**
  955. * Compute the positioning/size of an area considering the thickness of this object and a given alignment
  956. * @param sourceArea the source area
  957. * @param contentSize the content size to position/resize
  958. * @param alignment the alignment setting
  959. * @param dstOffset the position of the content
  960. * @param dstArea the new size of the content
  961. */
  962. PrimitiveThickness.prototype.computeWithAlignment = function (sourceArea, contentSize, alignment, dstOffset, dstArea) {
  963. // Fetch some data
  964. var topType = this._getType(0, true);
  965. var leftType = this._getType(1, true);
  966. var rightType = this._getType(2, true);
  967. var bottomType = this._getType(3, true);
  968. var hasWidth = contentSize && (contentSize.width != null);
  969. var hasHeight = contentSize && (contentSize.height != null);
  970. var width = hasWidth ? contentSize.width : 0;
  971. var height = hasHeight ? contentSize.height : 0;
  972. var isTopAuto = topType === PrimitiveThickness.Auto;
  973. var isLeftAuto = leftType === PrimitiveThickness.Auto;
  974. var isRightAuto = rightType === PrimitiveThickness.Auto;
  975. var isBottomAuto = bottomType === PrimitiveThickness.Auto;
  976. switch (alignment.horizontal) {
  977. case PrimitiveAlignment.AlignLeft:
  978. {
  979. if (isLeftAuto) {
  980. dstOffset.x = 0;
  981. }
  982. else {
  983. this._computePixels(1, sourceArea, true);
  984. dstOffset.x = this.leftPixels;
  985. }
  986. dstArea.width = width;
  987. break;
  988. }
  989. case PrimitiveAlignment.AlignRight:
  990. {
  991. if (isRightAuto) {
  992. dstOffset.x = Math.round(sourceArea.width - width);
  993. }
  994. else {
  995. this._computePixels(2, sourceArea, true);
  996. dstOffset.x = Math.round(sourceArea.width - (width + this.rightPixels));
  997. }
  998. dstArea.width = width;
  999. break;
  1000. }
  1001. case PrimitiveAlignment.AlignStretch:
  1002. {
  1003. if (isLeftAuto) {
  1004. dstOffset.x = 0;
  1005. }
  1006. else {
  1007. this._computePixels(1, sourceArea, true);
  1008. dstOffset.x = this.leftPixels;
  1009. }
  1010. var right = 0;
  1011. if (!isRightAuto) {
  1012. this._computePixels(2, sourceArea, true);
  1013. right = this.rightPixels;
  1014. }
  1015. dstArea.width = sourceArea.width - (dstOffset.x + right);
  1016. break;
  1017. }
  1018. case PrimitiveAlignment.AlignCenter:
  1019. {
  1020. if (!isLeftAuto) {
  1021. this._computePixels(1, sourceArea, true);
  1022. }
  1023. if (!isRightAuto) {
  1024. this._computePixels(2, sourceArea, true);
  1025. }
  1026. var offset = (isLeftAuto ? 0 : this.leftPixels) - (isRightAuto ? 0 : this.rightPixels);
  1027. dstOffset.x = Math.round(((sourceArea.width - width) / 2) + offset);
  1028. dstArea.width = width;
  1029. break;
  1030. }
  1031. }
  1032. switch (alignment.vertical) {
  1033. case PrimitiveAlignment.AlignTop:
  1034. {
  1035. if (isTopAuto) {
  1036. dstOffset.y = sourceArea.height - height;
  1037. }
  1038. else {
  1039. this._computePixels(0, sourceArea, true);
  1040. dstOffset.y = Math.round(sourceArea.height - (height + this.topPixels));
  1041. }
  1042. dstArea.height = height;
  1043. break;
  1044. }
  1045. case PrimitiveAlignment.AlignBottom:
  1046. {
  1047. if (isBottomAuto) {
  1048. dstOffset.y = 0;
  1049. }
  1050. else {
  1051. this._computePixels(3, sourceArea, true);
  1052. dstOffset.y = this.bottomPixels;
  1053. }
  1054. dstArea.height = height;
  1055. break;
  1056. }
  1057. case PrimitiveAlignment.AlignStretch:
  1058. {
  1059. if (isBottomAuto) {
  1060. dstOffset.y = 0;
  1061. }
  1062. else {
  1063. this._computePixels(3, sourceArea, true);
  1064. dstOffset.y = this.bottomPixels;
  1065. }
  1066. var top_1 = 0;
  1067. if (!isTopAuto) {
  1068. this._computePixels(0, sourceArea, true);
  1069. top_1 = this.topPixels;
  1070. }
  1071. dstArea.height = sourceArea.height - (dstOffset.y + top_1);
  1072. break;
  1073. }
  1074. case PrimitiveAlignment.AlignCenter:
  1075. {
  1076. if (!isTopAuto) {
  1077. this._computePixels(0, sourceArea, true);
  1078. }
  1079. if (!isBottomAuto) {
  1080. this._computePixels(3, sourceArea, true);
  1081. }
  1082. var offset = (isBottomAuto ? 0 : this.bottomPixels) - (isTopAuto ? 0 : this.topPixels);
  1083. dstOffset.y = Math.round(((sourceArea.height - height) / 2) + offset);
  1084. dstArea.height = height;
  1085. break;
  1086. }
  1087. }
  1088. };
  1089. /**
  1090. * Compute an area and its position considering this thickness properties based on a given source area
  1091. * @param sourceArea the source area
  1092. * @param dstOffset the position of the resulting area
  1093. * @param dstArea the size of the resulting area
  1094. */
  1095. PrimitiveThickness.prototype.compute = function (sourceArea, dstOffset, dstArea) {
  1096. this._computePixels(0, sourceArea, true);
  1097. this._computePixels(1, sourceArea, true);
  1098. this._computePixels(2, sourceArea, true);
  1099. this._computePixels(3, sourceArea, true);
  1100. dstOffset.x = this.leftPixels;
  1101. dstArea.width = sourceArea.width - (dstOffset.x + this.rightPixels);
  1102. dstOffset.y = this.bottomPixels;
  1103. dstArea.height = sourceArea.height - (dstOffset.y + this.topPixels);
  1104. };
  1105. /**
  1106. * Compute an area considering this thickness properties based on a given source area
  1107. * @param sourceArea the source area
  1108. * @param result the resulting area
  1109. */
  1110. PrimitiveThickness.prototype.computeArea = function (sourceArea, result) {
  1111. this._computePixels(0, sourceArea, true);
  1112. this._computePixels(1, sourceArea, true);
  1113. this._computePixels(2, sourceArea, true);
  1114. this._computePixels(3, sourceArea, true);
  1115. result.width = this.leftPixels + sourceArea.width + this.rightPixels;
  1116. result.height = this.bottomPixels + sourceArea.height + this.topPixels;
  1117. };
  1118. PrimitiveThickness.prototype.enlarge = function (sourceArea, dstOffset, enlargedArea) {
  1119. this._computePixels(0, sourceArea, true);
  1120. this._computePixels(1, sourceArea, true);
  1121. this._computePixels(2, sourceArea, true);
  1122. this._computePixels(3, sourceArea, true);
  1123. dstOffset.x = this.leftPixels;
  1124. enlargedArea.width = sourceArea.width + (dstOffset.x + this.rightPixels);
  1125. dstOffset.y = this.bottomPixels;
  1126. enlargedArea.height = sourceArea.height + (dstOffset.y + this.topPixels);
  1127. };
  1128. PrimitiveThickness.Auto = 0x1;
  1129. PrimitiveThickness.Inherit = 0x2;
  1130. PrimitiveThickness.Percentage = 0x4;
  1131. PrimitiveThickness.Pixel = 0x8;
  1132. return PrimitiveThickness;
  1133. })();
  1134. BABYLON.PrimitiveThickness = PrimitiveThickness;
  1135. /**
  1136. * Main class used for the Primitive Intersection API
  1137. */
  1138. var IntersectInfo2D = (function () {
  1139. function IntersectInfo2D() {
  1140. this.findFirstOnly = false;
  1141. this.intersectHidden = false;
  1142. this.pickPosition = BABYLON.Vector2.Zero();
  1143. }
  1144. Object.defineProperty(IntersectInfo2D.prototype, "isIntersected", {
  1145. /**
  1146. * true if at least one primitive intersected during the test
  1147. */
  1148. get: function () {
  1149. return this.intersectedPrimitives && this.intersectedPrimitives.length > 0;
  1150. },
  1151. enumerable: true,
  1152. configurable: true
  1153. });
  1154. IntersectInfo2D.prototype.isPrimIntersected = function (prim) {
  1155. for (var _i = 0, _a = this.intersectedPrimitives; _i < _a.length; _i++) {
  1156. var cur = _a[_i];
  1157. if (cur.prim === prim) {
  1158. return cur.intersectionLocation;
  1159. }
  1160. }
  1161. return null;
  1162. };
  1163. // Internals, don't use
  1164. IntersectInfo2D.prototype._exit = function (firstLevel) {
  1165. if (firstLevel) {
  1166. this._globalPickPosition = null;
  1167. }
  1168. };
  1169. return IntersectInfo2D;
  1170. })();
  1171. BABYLON.IntersectInfo2D = IntersectInfo2D;
  1172. var Prim2DBase = (function (_super) {
  1173. __extends(Prim2DBase, _super);
  1174. function Prim2DBase(settings) {
  1175. // Avoid checking every time if the object exists
  1176. if (settings == null) {
  1177. settings = {};
  1178. }
  1179. // BASE CLASS CALL
  1180. _super.call(this);
  1181. // Fetch the owner, parent. There're many ways to do it and we can end up with nothing for both
  1182. var owner;
  1183. var parent;
  1184. if (Prim2DBase._isCanvasInit) {
  1185. owner = this;
  1186. parent = null;
  1187. this._canvasPreInit(settings);
  1188. }
  1189. else {
  1190. if (settings.parent != null) {
  1191. parent = settings.parent;
  1192. owner = settings.parent.owner;
  1193. if (!owner) {
  1194. throw new Error("Parent " + parent.id + " of " + settings.id + " doesn't have a valid owner!");
  1195. }
  1196. if (!(this instanceof BABYLON.Group2D) && !(this instanceof BABYLON.Sprite2D && settings.id != null && settings.id.indexOf("__cachedSpriteOfGroup__") === 0) && (owner.cachingStrategy === BABYLON.Canvas2D.CACHESTRATEGY_TOPLEVELGROUPS) && (parent === owner)) {
  1197. 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");
  1198. }
  1199. }
  1200. }
  1201. // Fields initialization
  1202. this._layoutEngine = BABYLON.CanvasLayoutEngine.Singleton;
  1203. this._size = null; //Size.Zero();
  1204. this._actualSize = null;
  1205. this._boundingSize = null;
  1206. this._layoutArea = BABYLON.Size.Zero();
  1207. this._layoutAreaPos = BABYLON.Vector2.Zero();
  1208. this._marginOffset = BABYLON.Vector2.Zero();
  1209. this._paddingOffset = BABYLON.Vector2.Zero();
  1210. this._parentPaddingOffset = BABYLON.Vector2.Zero();
  1211. this._parentContentArea = BABYLON.Size.Zero();
  1212. this._lastAutoSizeArea = BABYLON.Size.Zero();
  1213. this._contentArea = new BABYLON.Size(null, null);
  1214. this._pointerEventObservable = new BABYLON.Observable();
  1215. this._siblingDepthOffset = this._hierarchyDepthOffset = 0;
  1216. this._boundingInfo = new BABYLON.BoundingInfo2D();
  1217. this._owner = owner;
  1218. this._parent = null;
  1219. this._margin = null;
  1220. this._padding = null;
  1221. this._marginAlignment = null;
  1222. this._id = settings.id;
  1223. this.propertyChanged = new BABYLON.Observable();
  1224. this._children = new Array();
  1225. this._localTransform = new BABYLON.Matrix();
  1226. this._globalTransform = null;
  1227. this._invGlobalTransform = null;
  1228. this._globalTransformProcessStep = 0;
  1229. this._globalTransformStep = 0;
  1230. this._hierarchyDepth = 0;
  1231. this._renderGroup = null;
  1232. this._setFlags(BABYLON.SmartPropertyPrim.flagIsPickable | BABYLON.SmartPropertyPrim.flagBoundingInfoDirty);
  1233. // If the parent is given, initialize the hierarchy/owner related data
  1234. if (parent != null) {
  1235. parent.addChild(this);
  1236. this._patchHierarchy(parent.owner);
  1237. }
  1238. // If it's a group, detect its own states
  1239. if (this.owner && this instanceof BABYLON.Group2D) {
  1240. var group = this;
  1241. group.detectGroupStates();
  1242. }
  1243. // Time to insert children if some are specified
  1244. if (settings.children != null) {
  1245. for (var _i = 0, _a = settings.children; _i < _a.length; _i++) {
  1246. var child = _a[_i];
  1247. this.addChild(child);
  1248. // Good time to patch the hierarchy, it won't go very far if there's no need to
  1249. child._patchHierarchy(this.owner);
  1250. }
  1251. }
  1252. // Set the model related properties
  1253. if (settings.position != null) {
  1254. this.position = settings.position;
  1255. }
  1256. else if (settings.x != null || settings.y != null) {
  1257. this.position = new BABYLON.Vector2(settings.x || 0, settings.y || 0);
  1258. }
  1259. else {
  1260. this._position = null;
  1261. }
  1262. this.rotation = (settings.rotation == null) ? 0 : settings.rotation;
  1263. this.scale = (settings.scale == null) ? 1 : settings.scale;
  1264. this.levelVisible = (settings.isVisible == null) ? true : settings.isVisible;
  1265. this.origin = settings.origin || new BABYLON.Vector2(0.5, 0.5);
  1266. // Layout Engine
  1267. if (settings.layoutEngine != null) {
  1268. if (typeof settings.layoutEngine === "string") {
  1269. var name_1 = settings.layoutEngine.toLocaleLowerCase().trim();
  1270. if (name_1 === "canvas" || name_1 === "canvaslayoutengine") {
  1271. this.layoutEngine = BABYLON.CanvasLayoutEngine.Singleton;
  1272. }
  1273. else if (name_1.indexOf("stackpanel") === 0 || name_1.indexOf("horizontalstackpanel") === 0) {
  1274. this.layoutEngine = BABYLON.StackPanelLayoutEngine.Horizontal;
  1275. }
  1276. else if (name_1.indexOf("verticalstackpanel") === 0) {
  1277. this.layoutEngine = BABYLON.StackPanelLayoutEngine.Vertical;
  1278. }
  1279. }
  1280. else if (settings.layoutEngine instanceof BABYLON.LayoutEngineBase) {
  1281. this.layoutEngine = settings.layoutEngine;
  1282. }
  1283. }
  1284. // Set the layout/margin stuffs
  1285. if (settings.marginTop) {
  1286. this.margin.setTop(settings.marginTop);
  1287. }
  1288. if (settings.marginLeft) {
  1289. this.margin.setLeft(settings.marginLeft);
  1290. }
  1291. if (settings.marginRight) {
  1292. this.margin.setRight(settings.marginRight);
  1293. }
  1294. if (settings.marginBottom) {
  1295. this.margin.setBottom(settings.marginBottom);
  1296. }
  1297. if (settings.margin) {
  1298. if (typeof settings.margin === "string") {
  1299. this.margin.fromString(settings.margin);
  1300. }
  1301. else {
  1302. this.margin.fromUniformPixels(settings.margin);
  1303. }
  1304. }
  1305. if (settings.marginHAlignment) {
  1306. this.marginAlignment.horizontal = settings.marginHAlignment;
  1307. }
  1308. if (settings.marginVAlignment) {
  1309. this.marginAlignment.vertical = settings.marginVAlignment;
  1310. }
  1311. if (settings.marginAlignment) {
  1312. this.marginAlignment.fromString(settings.marginAlignment);
  1313. }
  1314. if (settings.paddingTop) {
  1315. this.padding.setTop(settings.paddingTop);
  1316. }
  1317. if (settings.paddingLeft) {
  1318. this.padding.setLeft(settings.paddingLeft);
  1319. }
  1320. if (settings.paddingRight) {
  1321. this.padding.setRight(settings.paddingRight);
  1322. }
  1323. if (settings.paddingBottom) {
  1324. this.padding.setBottom(settings.paddingBottom);
  1325. }
  1326. if (settings.padding) {
  1327. this.padding.fromString(settings.padding);
  1328. }
  1329. // Dirty layout and positioning
  1330. this._parentLayoutDirty();
  1331. this._positioningDirty();
  1332. }
  1333. Object.defineProperty(Prim2DBase.prototype, "actionManager", {
  1334. get: function () {
  1335. if (!this._actionManager) {
  1336. this._actionManager = new BABYLON.ActionManager(this.owner.scene);
  1337. }
  1338. return this._actionManager;
  1339. },
  1340. enumerable: true,
  1341. configurable: true
  1342. });
  1343. /**
  1344. * From 'this' primitive, traverse up (from parent to parent) until the given predicate is true
  1345. * @param predicate the predicate to test on each parent
  1346. * @return the first primitive where the predicate was successful
  1347. */
  1348. Prim2DBase.prototype.traverseUp = function (predicate) {
  1349. var p = this;
  1350. while (p != null) {
  1351. if (predicate(p)) {
  1352. return p;
  1353. }
  1354. p = p._parent;
  1355. }
  1356. return null;
  1357. };
  1358. Object.defineProperty(Prim2DBase.prototype, "owner", {
  1359. /**
  1360. * Retrieve the owner Canvas2D
  1361. */
  1362. get: function () {
  1363. return this._owner;
  1364. },
  1365. enumerable: true,
  1366. configurable: true
  1367. });
  1368. Object.defineProperty(Prim2DBase.prototype, "parent", {
  1369. /**
  1370. * Get the parent primitive (can be the Canvas, only the Canvas has no parent)
  1371. */
  1372. get: function () {
  1373. return this._parent;
  1374. },
  1375. enumerable: true,
  1376. configurable: true
  1377. });
  1378. Object.defineProperty(Prim2DBase.prototype, "children", {
  1379. /**
  1380. * The array of direct children primitives
  1381. */
  1382. get: function () {
  1383. return this._children;
  1384. },
  1385. enumerable: true,
  1386. configurable: true
  1387. });
  1388. Object.defineProperty(Prim2DBase.prototype, "id", {
  1389. /**
  1390. * The identifier of this primitive, may not be unique, it's for information purpose only
  1391. */
  1392. get: function () {
  1393. return this._id;
  1394. },
  1395. enumerable: true,
  1396. configurable: true
  1397. });
  1398. Object.defineProperty(Prim2DBase.prototype, "actualPosition", {
  1399. get: function () {
  1400. if (this._actualPosition != null) {
  1401. return this._actualPosition;
  1402. }
  1403. if (this._position != null) {
  1404. return this._position;
  1405. }
  1406. // At least return 0,0, we can't return null on actualPosition
  1407. return Prim2DBase._nullPosition;
  1408. },
  1409. /**
  1410. * DO NOT INVOKE for internal purpose only
  1411. */
  1412. set: function (val) {
  1413. this._actualPosition = val;
  1414. },
  1415. enumerable: true,
  1416. configurable: true
  1417. });
  1418. Object.defineProperty(Prim2DBase.prototype, "actualX", {
  1419. /**
  1420. * Shortcut to actualPosition.x
  1421. */
  1422. get: function () {
  1423. return this.actualPosition.x;
  1424. },
  1425. enumerable: true,
  1426. configurable: true
  1427. });
  1428. Object.defineProperty(Prim2DBase.prototype, "actualY", {
  1429. /**
  1430. * Shortcut to actualPosition.y
  1431. */
  1432. get: function () {
  1433. return this.actualPosition.y;
  1434. },
  1435. enumerable: true,
  1436. configurable: true
  1437. });
  1438. Object.defineProperty(Prim2DBase.prototype, "position", {
  1439. /**
  1440. * Position of the primitive, relative to its parent.
  1441. * BEWARE: if you change only position.x or y it won't trigger a property change and you won't have the expected behavior.
  1442. * Use this property to set a new Vector2 object, otherwise to change only the x/y use Prim2DBase.x or y properties.
  1443. * Setting this property may have no effect is specific alignment are in effect.
  1444. */
  1445. get: function () {
  1446. return this._position || Prim2DBase._nullPosition;
  1447. },
  1448. set: function (value) {
  1449. if (!this._checkPositionChange()) {
  1450. return;
  1451. }
  1452. this._position = value;
  1453. },
  1454. enumerable: true,
  1455. configurable: true
  1456. });
  1457. Object.defineProperty(Prim2DBase.prototype, "x", {
  1458. /**
  1459. * Direct access to the position.x value of the primitive
  1460. * Use this property when you only want to change one component of the position property
  1461. */
  1462. get: function () {
  1463. if (!this._position) {
  1464. return null;
  1465. }
  1466. return this._position.x;
  1467. },
  1468. set: function (value) {
  1469. if (!this._checkPositionChange()) {
  1470. return;
  1471. }
  1472. if (!this._position) {
  1473. this._position = BABYLON.Vector2.Zero();
  1474. }
  1475. if (this._position.x === value) {
  1476. return;
  1477. }
  1478. this._position.x = value;
  1479. this.markAsDirty("position");
  1480. },
  1481. enumerable: true,
  1482. configurable: true
  1483. });
  1484. Object.defineProperty(Prim2DBase.prototype, "y", {
  1485. /**
  1486. * Direct access to the position.y value of the primitive
  1487. * Use this property when you only want to change one component of the position property
  1488. */
  1489. get: function () {
  1490. if (!this._position) {
  1491. return null;
  1492. }
  1493. return this._position.y;
  1494. },
  1495. set: function (value) {
  1496. if (!this._checkPositionChange()) {
  1497. return;
  1498. }
  1499. if (!this._position) {
  1500. this._position = BABYLON.Vector2.Zero();
  1501. }
  1502. if (this._position.y === value) {
  1503. return;
  1504. }
  1505. this._position.y = value;
  1506. this.markAsDirty("position");
  1507. },
  1508. enumerable: true,
  1509. configurable: true
  1510. });
  1511. Object.defineProperty(Prim2DBase.prototype, "size", {
  1512. /**
  1513. * Size of the primitive or its bounding area
  1514. * BEWARE: if you change only size.width or height it won't trigger a property change and you won't have the expected behavior.
  1515. * Use this property to set a new Size object, otherwise to change only the width/height use Prim2DBase.width or height properties.
  1516. */
  1517. get: function () {
  1518. if (!this._size || this._size.width == null || this._size.height == null) {
  1519. if (Prim2DBase.boundinbBoxReentrency) {
  1520. return Prim2DBase.nullSize;
  1521. }
  1522. if (this._boundingSize) {
  1523. return this._boundingSize;
  1524. }
  1525. Prim2DBase.boundinbBoxReentrency = true;
  1526. var b = this.boundingInfo;
  1527. Prim2DBase.boundinbBoxReentrency = false;
  1528. return this._boundingSize;
  1529. }
  1530. return this._size;
  1531. },
  1532. set: function (value) {
  1533. this._size = value;
  1534. },
  1535. enumerable: true,
  1536. configurable: true
  1537. });
  1538. Object.defineProperty(Prim2DBase.prototype, "width", {
  1539. /**
  1540. * Direct access to the size.width value of the primitive
  1541. * Use this property when you only want to change one component of the size property
  1542. */
  1543. get: function () {
  1544. if (!this.size) {
  1545. return null;
  1546. }
  1547. return this.size.width;
  1548. },
  1549. set: function (value) {
  1550. if (!this.size) {
  1551. this.size = new BABYLON.Size(value, 0);
  1552. return;
  1553. }
  1554. if (this.size.width === value) {
  1555. return;
  1556. }
  1557. this.size.width = value;
  1558. this.markAsDirty("size");
  1559. this._positioningDirty();
  1560. },
  1561. enumerable: true,
  1562. configurable: true
  1563. });
  1564. Object.defineProperty(Prim2DBase.prototype, "height", {
  1565. /**
  1566. * Direct access to the size.height value of the primitive
  1567. * Use this property when you only want to change one component of the size property
  1568. */
  1569. get: function () {
  1570. if (!this.size) {
  1571. return null;
  1572. }
  1573. return this.size.height;
  1574. },
  1575. set: function (value) {
  1576. if (!this.size) {
  1577. this.size = new BABYLON.Size(0, value);
  1578. return;
  1579. }
  1580. if (this.size.height === value) {
  1581. return;
  1582. }
  1583. this.size.height = value;
  1584. this.markAsDirty("size");
  1585. this._positioningDirty();
  1586. },
  1587. enumerable: true,
  1588. configurable: true
  1589. });
  1590. Object.defineProperty(Prim2DBase.prototype, "rotation", {
  1591. get: function () {
  1592. return this._rotation;
  1593. },
  1594. set: function (value) {
  1595. this._rotation = value;
  1596. },
  1597. enumerable: true,
  1598. configurable: true
  1599. });
  1600. Object.defineProperty(Prim2DBase.prototype, "scale", {
  1601. get: function () {
  1602. return this._scale;
  1603. },
  1604. set: function (value) {
  1605. this._scale = value;
  1606. },
  1607. enumerable: true,
  1608. configurable: true
  1609. });
  1610. Object.defineProperty(Prim2DBase.prototype, "actualSize", {
  1611. /**
  1612. * Return the size of the primitive as it's being rendered into the target.
  1613. * This value may be different of the size property when layout/alignment is used or specific primitive types can implement a custom logic through this property.
  1614. * BEWARE: don't use the setter, it's for internal purpose only
  1615. * Note to implementers: you have to override this property and declare if necessary a @xxxxInstanceLevel decorator
  1616. */
  1617. get: function () {
  1618. if (this._actualSize) {
  1619. return this._actualSize;
  1620. }
  1621. return this._size;
  1622. },
  1623. set: function (value) {
  1624. if (this._actualSize.equals(value)) {
  1625. return;
  1626. }
  1627. this._actualSize = value;
  1628. },
  1629. enumerable: true,
  1630. configurable: true
  1631. });
  1632. Object.defineProperty(Prim2DBase.prototype, "actualZOffset", {
  1633. get: function () {
  1634. return this._zOrder || (1 - this._hierarchyDepthOffset);
  1635. },
  1636. enumerable: true,
  1637. configurable: true
  1638. });
  1639. Object.defineProperty(Prim2DBase.prototype, "minSize", {
  1640. /**
  1641. * Get or set the minimal size the Layout Engine should respect when computing the primitive's actualSize.
  1642. * The Primitive's size won't be less than specified.
  1643. * The default value depends of the Primitive type
  1644. */
  1645. get: function () {
  1646. return this._minSize;
  1647. },
  1648. set: function (value) {
  1649. if (this._minSize && value && this._minSize.equals(value)) {
  1650. return;
  1651. }
  1652. this._minSize = value;
  1653. this._parentLayoutDirty();
  1654. },
  1655. enumerable: true,
  1656. configurable: true
  1657. });
  1658. Object.defineProperty(Prim2DBase.prototype, "maxSize", {
  1659. /**
  1660. * Get or set the maximal size the Layout Engine should respect when computing the primitive's actualSize.
  1661. * The Primitive's size won't be more than specified.
  1662. * The default value depends of the Primitive type
  1663. */
  1664. get: function () {
  1665. return this._maxSize;
  1666. },
  1667. set: function (value) {
  1668. if (this._maxSize && value && this._maxSize.equals(value)) {
  1669. return;
  1670. }
  1671. this._maxSize = value;
  1672. this._parentLayoutDirty();
  1673. },
  1674. enumerable: true,
  1675. configurable: true
  1676. });
  1677. Object.defineProperty(Prim2DBase.prototype, "origin", {
  1678. /**
  1679. * The origin defines the normalized coordinate of the center of the primitive, from the bottom/left corner.
  1680. * The origin is used only to compute transformation of the primitive, it has no meaning in the primitive local frame of reference
  1681. * For instance:
  1682. * 0,0 means the center is bottom/left. Which is the default for Canvas2D instances
  1683. * 0.5,0.5 means the center is at the center of the primitive, which is default of all types of Primitives
  1684. * 0,1 means the center is top/left
  1685. * @returns The normalized center.
  1686. */
  1687. get: function () {
  1688. return this._origin;
  1689. },
  1690. set: function (value) {
  1691. this._origin = value;
  1692. },
  1693. enumerable: true,
  1694. configurable: true
  1695. });
  1696. Object.defineProperty(Prim2DBase.prototype, "levelVisible", {
  1697. get: function () {
  1698. return this._isFlagSet(BABYLON.SmartPropertyPrim.flagLevelVisible);
  1699. },
  1700. set: function (value) {
  1701. this._changeFlags(BABYLON.SmartPropertyPrim.flagLevelVisible, value);
  1702. },
  1703. enumerable: true,
  1704. configurable: true
  1705. });
  1706. Object.defineProperty(Prim2DBase.prototype, "isVisible", {
  1707. get: function () {
  1708. return this._isFlagSet(BABYLON.SmartPropertyPrim.flagIsVisible);
  1709. },
  1710. set: function (value) {
  1711. this._changeFlags(BABYLON.SmartPropertyPrim.flagIsVisible, value);
  1712. },
  1713. enumerable: true,
  1714. configurable: true
  1715. });
  1716. Object.defineProperty(Prim2DBase.prototype, "zOrder", {
  1717. get: function () {
  1718. return this._zOrder;
  1719. },
  1720. set: function (value) {
  1721. this._zOrder = value;
  1722. this.onZOrderChanged();
  1723. },
  1724. enumerable: true,
  1725. configurable: true
  1726. });
  1727. Object.defineProperty(Prim2DBase.prototype, "margin", {
  1728. get: function () {
  1729. var _this = this;
  1730. if (!this._margin) {
  1731. this._margin = new PrimitiveThickness(function () {
  1732. if (!_this.parent) {
  1733. return null;
  1734. }
  1735. return _this.parent.margin;
  1736. }, function () { return _this._positioningDirty(); });
  1737. }
  1738. return this._margin;
  1739. },
  1740. enumerable: true,
  1741. configurable: true
  1742. });
  1743. Object.defineProperty(Prim2DBase.prototype, "_hasMargin", {
  1744. get: function () {
  1745. return (this._margin !== null) || (this._marginAlignment !== null);
  1746. },
  1747. enumerable: true,
  1748. configurable: true
  1749. });
  1750. Object.defineProperty(Prim2DBase.prototype, "padding", {
  1751. get: function () {
  1752. var _this = this;
  1753. if (!this._padding) {
  1754. this._padding = new PrimitiveThickness(function () {
  1755. if (!_this.parent) {
  1756. return null;
  1757. }
  1758. return _this.parent.padding;
  1759. }, function () { return _this._positioningDirty(); });
  1760. }
  1761. return this._padding;
  1762. },
  1763. enumerable: true,
  1764. configurable: true
  1765. });
  1766. Object.defineProperty(Prim2DBase.prototype, "_hasPadding", {
  1767. get: function () {
  1768. return this._padding !== null;
  1769. },
  1770. enumerable: true,
  1771. configurable: true
  1772. });
  1773. Object.defineProperty(Prim2DBase.prototype, "marginAlignment", {
  1774. get: function () {
  1775. var _this = this;
  1776. if (!this._marginAlignment) {
  1777. this._marginAlignment = new PrimitiveAlignment(function () { return _this._positioningDirty(); });
  1778. }
  1779. return this._marginAlignment;
  1780. },
  1781. enumerable: true,
  1782. configurable: true
  1783. });
  1784. Object.defineProperty(Prim2DBase.prototype, "layoutEngine", {
  1785. /**
  1786. * Get/set the layout engine to use for this primitive.
  1787. * The default layout engine is the CanvasLayoutEngine.
  1788. */
  1789. get: function () {
  1790. if (!this._layoutEngine) {
  1791. this._layoutEngine = BABYLON.CanvasLayoutEngine.Singleton;
  1792. }
  1793. return this._layoutEngine;
  1794. },
  1795. set: function (value) {
  1796. if (this._layoutEngine === value) {
  1797. return;
  1798. }
  1799. this._changeLayoutEngine(value);
  1800. },
  1801. enumerable: true,
  1802. configurable: true
  1803. });
  1804. Object.defineProperty(Prim2DBase.prototype, "layoutArea", {
  1805. /**
  1806. * Get/set the layout are of this primitive.
  1807. * The Layout area is the zone allocated by the Layout Engine for this particular primitive. Margins/Alignment will be computed based on this area.
  1808. * The setter should only be called by a Layout Engine class.
  1809. */
  1810. get: function () {
  1811. return this._layoutArea;
  1812. },
  1813. set: function (val) {
  1814. if (this._layoutArea.equals(val)) {
  1815. return;
  1816. }
  1817. this._positioningDirty();
  1818. this._layoutArea = val;
  1819. },
  1820. enumerable: true,
  1821. configurable: true
  1822. });
  1823. Object.defineProperty(Prim2DBase.prototype, "layoutAreaPos", {
  1824. /**
  1825. * Get/set the layout area position (relative to the parent primitive).
  1826. * The setter should only be called by a Layout Engine class.
  1827. */
  1828. get: function () {
  1829. return this._layoutAreaPos;
  1830. },
  1831. set: function (val) {
  1832. if (this._layoutAreaPos.equals(val)) {
  1833. return;
  1834. }
  1835. this._positioningDirty();
  1836. this._layoutAreaPos = val;
  1837. },
  1838. enumerable: true,
  1839. configurable: true
  1840. });
  1841. Object.defineProperty(Prim2DBase.prototype, "isPickable", {
  1842. /**
  1843. * Define if the Primitive can be subject to intersection test or not (default is true)
  1844. */
  1845. get: function () {
  1846. return this._isFlagSet(BABYLON.SmartPropertyPrim.flagIsPickable);
  1847. },
  1848. set: function (value) {
  1849. this._changeFlags(BABYLON.SmartPropertyPrim.flagIsPickable, value);
  1850. },
  1851. enumerable: true,
  1852. configurable: true
  1853. });
  1854. Object.defineProperty(Prim2DBase.prototype, "hierarchyDepth", {
  1855. /**
  1856. * Return the depth level of the Primitive into the Canvas' Graph. A Canvas will be 0, its direct children 1, and so on.
  1857. */
  1858. get: function () {
  1859. return this._hierarchyDepth;
  1860. },
  1861. enumerable: true,
  1862. configurable: true
  1863. });
  1864. Object.defineProperty(Prim2DBase.prototype, "renderGroup", {
  1865. /**
  1866. * Retrieve the Group that is responsible to render this primitive
  1867. */
  1868. get: function () {
  1869. return this._renderGroup;
  1870. },
  1871. enumerable: true,
  1872. configurable: true
  1873. });
  1874. Object.defineProperty(Prim2DBase.prototype, "globalTransform", {
  1875. /**
  1876. * Get the global transformation matrix of the primitive
  1877. */
  1878. get: function () {
  1879. return this._globalTransform;
  1880. },
  1881. enumerable: true,
  1882. configurable: true
  1883. });
  1884. /**
  1885. * return the global position of the primitive, relative to its canvas
  1886. */
  1887. Prim2DBase.prototype.getGlobalPosition = function () {
  1888. var v = new BABYLON.Vector2(0, 0);
  1889. this.getGlobalPositionByRef(v);
  1890. return v;
  1891. };
  1892. /**
  1893. * return the global position of the primitive, relative to its canvas
  1894. * @param v the valid Vector2 object where the global position will be stored
  1895. */
  1896. Prim2DBase.prototype.getGlobalPositionByRef = function (v) {
  1897. v.x = this.globalTransform.m[12];
  1898. v.y = this.globalTransform.m[13];
  1899. };
  1900. Object.defineProperty(Prim2DBase.prototype, "invGlobalTransform", {
  1901. /**
  1902. * Get invert of the global transformation matrix of the primitive
  1903. */
  1904. get: function () {
  1905. return this._invGlobalTransform;
  1906. },
  1907. enumerable: true,
  1908. configurable: true
  1909. });
  1910. Object.defineProperty(Prim2DBase.prototype, "localTransform", {
  1911. /**
  1912. * Get the local transformation of the primitive
  1913. */
  1914. get: function () {
  1915. this._updateLocalTransform();
  1916. return this._localTransform;
  1917. },
  1918. enumerable: true,
  1919. configurable: true
  1920. });
  1921. Object.defineProperty(Prim2DBase.prototype, "boundingInfo", {
  1922. /**
  1923. * Get the boundingInfo associated to the primitive and its children.
  1924. * The value is supposed to be always up to date
  1925. */
  1926. get: function () {
  1927. if (this._isFlagSet(BABYLON.SmartPropertyPrim.flagBoundingInfoDirty)) {
  1928. if (this.isSizedByContent) {
  1929. this._boundingInfo.clear();
  1930. }
  1931. else {
  1932. this._boundingInfo.copyFrom(this.levelBoundingInfo);
  1933. }
  1934. var bi = this._boundingInfo;
  1935. var tps = new BABYLON.BoundingInfo2D();
  1936. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  1937. var curChild = _a[_i];
  1938. curChild.boundingInfo.transformToRef(curChild.localTransform, tps);
  1939. bi.unionToRef(tps, bi);
  1940. }
  1941. this._boundingSize = BABYLON.Size.Zero();
  1942. var m = this._boundingInfo.max();
  1943. this._boundingSize = new BABYLON.Size((!this._size || this._size.width == null) ? Math.ceil(m.x) : this._size.width, (!this._size || this._size.height == null) ? Math.ceil(m.y) : this._size.height);
  1944. this._clearFlags(BABYLON.SmartPropertyPrim.flagBoundingInfoDirty);
  1945. }
  1946. return this._boundingInfo;
  1947. },
  1948. enumerable: true,
  1949. configurable: true
  1950. });
  1951. Object.defineProperty(Prim2DBase.prototype, "isSizeAuto", {
  1952. /**
  1953. * Determine if the size is automatically computed or fixed because manually specified.
  1954. * Use the actualSize property to get the final/real size of the primitive
  1955. * @returns true if the size is automatically computed, false if it were manually specified.
  1956. */
  1957. get: function () {
  1958. return this._size == null;
  1959. },
  1960. enumerable: true,
  1961. configurable: true
  1962. });
  1963. Object.defineProperty(Prim2DBase.prototype, "isSizedByContent", {
  1964. /**
  1965. * Return true if this prim has an auto size which is set by the children's global bounding box
  1966. */
  1967. get: function () {
  1968. return (this._size == null) && (this._children.length > 0);
  1969. },
  1970. enumerable: true,
  1971. configurable: true
  1972. });
  1973. Object.defineProperty(Prim2DBase.prototype, "isPositionAuto", {
  1974. /**
  1975. * Determine if the position is automatically computed or fixed because manually specified.
  1976. * Use the actualPosition property to get the final/real position of the primitive
  1977. * @returns true if the position is automatically computed, false if it were manually specified.
  1978. */
  1979. get: function () {
  1980. return this._position == null;
  1981. },
  1982. enumerable: true,
  1983. configurable: true
  1984. });
  1985. Object.defineProperty(Prim2DBase.prototype, "pointerEventObservable", {
  1986. /**
  1987. * Interaction with the primitive can be create using this Observable. See the PrimitivePointerInfo class for more information
  1988. */
  1989. get: function () {
  1990. return this._pointerEventObservable;
  1991. },
  1992. enumerable: true,
  1993. configurable: true
  1994. });
  1995. Prim2DBase.prototype.findById = function (id) {
  1996. if (this._id === id) {
  1997. return this;
  1998. }
  1999. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  2000. var child = _a[_i];
  2001. var r = child.findById(id);
  2002. if (r != null) {
  2003. return r;
  2004. }
  2005. }
  2006. };
  2007. Prim2DBase.prototype.onZOrderChanged = function () {
  2008. };
  2009. Prim2DBase.prototype.levelIntersect = function (intersectInfo) {
  2010. return false;
  2011. };
  2012. /**
  2013. * Capture all the Events of the given PointerId for this primitive.
  2014. * Don't forget to call releasePointerEventsCapture when done.
  2015. * @param pointerId the Id of the pointer to capture the events from.
  2016. */
  2017. Prim2DBase.prototype.setPointerEventCapture = function (pointerId) {
  2018. return this.owner._setPointerCapture(pointerId, this);
  2019. };
  2020. /**
  2021. * Release a captured pointer made with setPointerEventCapture.
  2022. * @param pointerId the Id of the pointer to release the capture from.
  2023. */
  2024. Prim2DBase.prototype.releasePointerEventsCapture = function (pointerId) {
  2025. return this.owner._releasePointerCapture(pointerId, this);
  2026. };
  2027. /**
  2028. * Make an intersection test with the primitive, all inputs/outputs are stored in the IntersectInfo2D class, see its documentation for more information.
  2029. * @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.
  2030. */
  2031. Prim2DBase.prototype.intersect = function (intersectInfo) {
  2032. if (!intersectInfo) {
  2033. return false;
  2034. }
  2035. // If this is null it means this method is call for the first level, initialize stuffs
  2036. var firstLevel = !intersectInfo._globalPickPosition;
  2037. if (firstLevel) {
  2038. // 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).
  2039. intersectInfo._globalPickPosition = BABYLON.Vector2.Zero();
  2040. BABYLON.Vector2.TransformToRef(intersectInfo.pickPosition, this.globalTransform, intersectInfo._globalPickPosition);
  2041. intersectInfo._localPickPosition = intersectInfo.pickPosition.clone();
  2042. intersectInfo.intersectedPrimitives = new Array();
  2043. intersectInfo.topMostIntersectedPrimitive = null;
  2044. }
  2045. if (!intersectInfo.intersectHidden && !this.isVisible) {
  2046. return false;
  2047. }
  2048. // Fast rejection test with boundingInfo
  2049. if (this.isPickable && !this.boundingInfo.doesIntersect(intersectInfo._localPickPosition)) {
  2050. // Important to call this before each return to allow a good recursion next time this intersectInfo is reused
  2051. intersectInfo._exit(firstLevel);
  2052. return false;
  2053. }
  2054. // We hit the boundingInfo that bounds this primitive and its children, now we have to test on the primitive of this level
  2055. var levelIntersectRes = false;
  2056. if (this.isPickable) {
  2057. levelIntersectRes = this.levelIntersect(intersectInfo);
  2058. if (levelIntersectRes) {
  2059. var pii = new PrimitiveIntersectedInfo(this, intersectInfo._localPickPosition.clone());
  2060. intersectInfo.intersectedPrimitives.push(pii);
  2061. if (!intersectInfo.topMostIntersectedPrimitive || (intersectInfo.topMostIntersectedPrimitive.prim.actualZOffset > pii.prim.actualZOffset)) {
  2062. intersectInfo.topMostIntersectedPrimitive = pii;
  2063. }
  2064. // If we must stop at the first intersection, we're done, quit!
  2065. if (intersectInfo.findFirstOnly) {
  2066. intersectInfo._exit(firstLevel);
  2067. return true;
  2068. }
  2069. }
  2070. }
  2071. // Recurse to children if needed
  2072. if (!levelIntersectRes || !intersectInfo.findFirstOnly) {
  2073. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  2074. var curChild = _a[_i];
  2075. // Don't test primitive not pick able or if it's hidden and we don't test hidden ones
  2076. if (!curChild.isPickable || (!intersectInfo.intersectHidden && !curChild.isVisible)) {
  2077. continue;
  2078. }
  2079. // Must compute the localPickLocation for the children level
  2080. BABYLON.Vector2.TransformToRef(intersectInfo._globalPickPosition, curChild.invGlobalTransform, intersectInfo._localPickPosition);
  2081. // If we got an intersection with the child and we only need to find the first one, quit!
  2082. if (curChild.intersect(intersectInfo) && intersectInfo.findFirstOnly) {
  2083. intersectInfo._exit(firstLevel);
  2084. return true;
  2085. }
  2086. }
  2087. }
  2088. intersectInfo._exit(firstLevel);
  2089. return intersectInfo.isIntersected;
  2090. };
  2091. Prim2DBase.prototype.moveChild = function (child, previous) {
  2092. if (child.parent !== this) {
  2093. return false;
  2094. }
  2095. var prevOffset, nextOffset;
  2096. var childIndex = this._children.indexOf(child);
  2097. var prevIndex = previous ? this._children.indexOf(previous) : -1;
  2098. // Move to first position
  2099. if (!previous) {
  2100. prevOffset = 1;
  2101. nextOffset = this._children[1]._siblingDepthOffset;
  2102. }
  2103. else {
  2104. prevOffset = this._children[prevIndex]._siblingDepthOffset;
  2105. nextOffset = this._children[prevIndex + 1]._siblingDepthOffset;
  2106. }
  2107. child._siblingDepthOffset = (nextOffset - prevOffset) / 2;
  2108. this._children.splice(prevIndex + 1, 0, this._children.splice(childIndex, 1)[0]);
  2109. };
  2110. Prim2DBase.prototype.addChild = function (child) {
  2111. child._parent = this;
  2112. this._boundingBoxDirty();
  2113. this._children.push(child);
  2114. this._patchHierarchyDepth(child);
  2115. };
  2116. /**
  2117. * Dispose the primitive, remove it from its parent.
  2118. */
  2119. Prim2DBase.prototype.dispose = function () {
  2120. if (!_super.prototype.dispose.call(this)) {
  2121. return false;
  2122. }
  2123. if (this._actionManager) {
  2124. this._actionManager.dispose();
  2125. this._actionManager = null;
  2126. }
  2127. // If there's a parent, remove this object from its parent list
  2128. if (this._parent) {
  2129. var i = this._parent._children.indexOf(this);
  2130. if (i !== undefined) {
  2131. this._parent._children.splice(i, 1);
  2132. }
  2133. this._parent = null;
  2134. }
  2135. // Recurse dispose to children
  2136. if (this._children) {
  2137. while (this._children.length > 0) {
  2138. this._children[this._children.length - 1].dispose();
  2139. }
  2140. }
  2141. return true;
  2142. };
  2143. Prim2DBase.prototype.onPrimBecomesDirty = function () {
  2144. if (this._renderGroup) {
  2145. this._renderGroup._addPrimToDirtyList(this);
  2146. }
  2147. };
  2148. Prim2DBase.prototype._needPrepare = function () {
  2149. return this._areSomeFlagsSet(BABYLON.SmartPropertyPrim.flagVisibilityChanged | BABYLON.SmartPropertyPrim.flagModelDirty) || (this._instanceDirtyFlags !== 0) || (this._globalTransformProcessStep !== this._globalTransformStep);
  2150. };
  2151. Prim2DBase.prototype._prepareRender = function (context) {
  2152. this._prepareRenderPre(context);
  2153. this._prepareRenderPost(context);
  2154. };
  2155. Prim2DBase.prototype._prepareRenderPre = function (context) {
  2156. };
  2157. Prim2DBase.prototype._prepareRenderPost = function (context) {
  2158. // Don't recurse if it's a renderable group, the content will be processed by the group itself
  2159. if (this instanceof BABYLON.Group2D) {
  2160. var self = this;
  2161. if (self.isRenderableGroup) {
  2162. return;
  2163. }
  2164. }
  2165. // Check if we need to recurse the prepare to children primitives
  2166. // - must have children
  2167. // - the global transform of this level have changed, or
  2168. // - the visible state of primitive has changed
  2169. if (this._children.length > 0 && ((this._globalTransformProcessStep !== this._globalTransformStep) ||
  2170. this.checkPropertiesDirty(Prim2DBase.isVisibleProperty.flagId))) {
  2171. this._children.forEach(function (c) {
  2172. // As usual stop the recursion if we meet a renderable group
  2173. if (!(c instanceof BABYLON.Group2D && c.isRenderableGroup)) {
  2174. c._prepareRender(context);
  2175. }
  2176. });
  2177. }
  2178. // Finally reset the dirty flags as we've processed everything
  2179. this._clearFlags(BABYLON.SmartPropertyPrim.flagModelDirty);
  2180. this._instanceDirtyFlags = 0;
  2181. };
  2182. Prim2DBase.prototype._canvasPreInit = function (settings) {
  2183. };
  2184. Prim2DBase.CheckParent = function (parent) {
  2185. //if (!Prim2DBase._isCanvasInit && !parent) {
  2186. // 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)");
  2187. //}
  2188. };
  2189. Prim2DBase.prototype.updateCachedStatesOf = function (list, recurse) {
  2190. for (var _i = 0; _i < list.length; _i++) {
  2191. var cur = list[_i];
  2192. cur.updateCachedStates(recurse);
  2193. }
  2194. };
  2195. Prim2DBase.prototype._parentLayoutDirty = function () {
  2196. if (!this._parent || this._parent.isDisposed) {
  2197. return;
  2198. }
  2199. this._parent._setLayoutDirty();
  2200. };
  2201. Prim2DBase.prototype._setLayoutDirty = function () {
  2202. if (!this.isDirty) {
  2203. this.onPrimBecomesDirty();
  2204. }
  2205. this._setFlags(BABYLON.SmartPropertyPrim.flagLayoutDirty);
  2206. };
  2207. Prim2DBase.prototype._checkPositionChange = function () {
  2208. if (this.parent && this.parent.layoutEngine.isChildPositionAllowed === false) {
  2209. console.log("Can't manually set the position of " + this.id + ", the Layout Engine of its parent doesn't allow it");
  2210. return false;
  2211. }
  2212. return true;
  2213. };
  2214. Prim2DBase.prototype._positioningDirty = function () {
  2215. if (!this.isDirty) {
  2216. this.onPrimBecomesDirty();
  2217. }
  2218. this._setFlags(BABYLON.SmartPropertyPrim.flagPositioningDirty);
  2219. };
  2220. Prim2DBase.prototype._changeLayoutEngine = function (engine) {
  2221. this._layoutEngine = engine;
  2222. };
  2223. Prim2DBase.prototype._updateLocalTransform = function () {
  2224. var tflags = Prim2DBase.actualPositionProperty.flagId | Prim2DBase.rotationProperty.flagId | Prim2DBase.scaleProperty.flagId | Prim2DBase.originProperty.flagId;
  2225. if (this.checkPropertiesDirty(tflags)) {
  2226. this.owner.addupdateLocalTransformCounter(1);
  2227. var rot = BABYLON.Quaternion.RotationAxis(new BABYLON.Vector3(0, 0, 1), this._rotation);
  2228. var local;
  2229. var pos = this.position;
  2230. if (this._origin.x === 0 && this._origin.y === 0) {
  2231. local = BABYLON.Matrix.Compose(new BABYLON.Vector3(this._scale, this._scale, 1), rot, new BABYLON.Vector3(pos.x, pos.y, 0));
  2232. this._localTransform = local;
  2233. }
  2234. else {
  2235. // -Origin offset
  2236. var as = this.actualSize;
  2237. BABYLON.Matrix.TranslationToRef((-as.width * this._origin.x), (-as.height * this._origin.y), 0, Prim2DBase._t0);
  2238. // -Origin * rotation
  2239. rot.toRotationMatrix(Prim2DBase._t1);
  2240. Prim2DBase._t0.multiplyToRef(Prim2DBase._t1, Prim2DBase._t2);
  2241. // -Origin * rotation * scale
  2242. BABYLON.Matrix.ScalingToRef(this._scale, this._scale, 1, Prim2DBase._t0);
  2243. Prim2DBase._t2.multiplyToRef(Prim2DBase._t0, Prim2DBase._t1);
  2244. // -Origin * rotation * scale * (Origin + Position)
  2245. BABYLON.Matrix.TranslationToRef((as.width * this._origin.x) + pos.x, (as.height * this._origin.y) + pos.y, 0, Prim2DBase._t2);
  2246. Prim2DBase._t1.multiplyToRef(Prim2DBase._t2, this._localTransform);
  2247. }
  2248. this.clearPropertiesDirty(tflags);
  2249. return true;
  2250. }
  2251. return false;
  2252. };
  2253. Prim2DBase.prototype.updateCachedStates = function (recurse) {
  2254. if (this.isDisposed) {
  2255. return;
  2256. }
  2257. this.owner.addCachedGroupRenderCounter(1);
  2258. // Check if the parent is synced
  2259. if (this._parent && ((this._parent._globalTransformProcessStep !== this.owner._globalTransformProcessStep) || this._parent._areSomeFlagsSet(BABYLON.SmartPropertyPrim.flagLayoutDirty | BABYLON.SmartPropertyPrim.flagPositioningDirty))) {
  2260. this._parent.updateCachedStates(false);
  2261. }
  2262. // Update actualSize only if there' not positioning to recompute and the size changed
  2263. // Otherwise positioning will take care of it.
  2264. var sizeDirty = this.checkPropertiesDirty(Prim2DBase.sizeProperty.flagId);
  2265. if (!this._isFlagSet(BABYLON.SmartPropertyPrim.flagLayoutDirty) && !this._isFlagSet(BABYLON.SmartPropertyPrim.flagPositioningDirty) && sizeDirty) {
  2266. var size = this.size;
  2267. if (size) {
  2268. if (this.size.width != null) {
  2269. this.actualSize.width = this.size.width;
  2270. }
  2271. if (this.size.height != null) {
  2272. this.actualSize.height = this.size.height;
  2273. }
  2274. this.clearPropertiesDirty(Prim2DBase.sizeProperty.flagId);
  2275. }
  2276. }
  2277. // Check for layout update
  2278. var positioningDirty = this._isFlagSet(BABYLON.SmartPropertyPrim.flagPositioningDirty);
  2279. if (this._isFlagSet(BABYLON.SmartPropertyPrim.flagLayoutDirty)) {
  2280. this.owner.addUpdateLayoutCounter(1);
  2281. this._layoutEngine.updateLayout(this);
  2282. this._clearFlags(BABYLON.SmartPropertyPrim.flagLayoutDirty);
  2283. }
  2284. var positioningComputed = positioningDirty && !this._isFlagSet(BABYLON.SmartPropertyPrim.flagPositioningDirty);
  2285. var autoContentChanged = false;
  2286. if (this.isSizeAuto) {
  2287. autoContentChanged = (!this._lastAutoSizeArea.equals(this.size));
  2288. }
  2289. // Check for positioning update
  2290. if (!positioningComputed && (autoContentChanged || sizeDirty || this._isFlagSet(BABYLON.SmartPropertyPrim.flagPositioningDirty) || (this._parent && !this._parent.contentArea.equals(this._parentContentArea)))) {
  2291. this._updatePositioning();
  2292. this._clearFlags(BABYLON.SmartPropertyPrim.flagPositioningDirty);
  2293. if (sizeDirty) {
  2294. this.clearPropertiesDirty(Prim2DBase.sizeProperty.flagId);
  2295. }
  2296. positioningComputed = true;
  2297. }
  2298. if (positioningComputed && this._parent) {
  2299. this._parentContentArea.copyFrom(this._parent.contentArea);
  2300. }
  2301. // Check if we must update this prim
  2302. if (this === this.owner || this._globalTransformProcessStep !== this.owner._globalTransformProcessStep) {
  2303. this.owner.addUpdateGlobalTransformCounter(1);
  2304. var curVisibleState = this.isVisible;
  2305. this.isVisible = (!this._parent || this._parent.isVisible) && this.levelVisible;
  2306. // Detect a change of visibility
  2307. this._changeFlags(BABYLON.SmartPropertyPrim.flagVisibilityChanged, curVisibleState !== this.isVisible);
  2308. // Get/compute the localTransform
  2309. var localDirty = this._updateLocalTransform();
  2310. var parentPaddingChanged = false;
  2311. var parentPaddingOffset = Prim2DBase._v0;
  2312. if (this._parent) {
  2313. parentPaddingOffset = this._parent._paddingOffset;
  2314. parentPaddingChanged = !parentPaddingOffset.equals(this._parentPaddingOffset);
  2315. }
  2316. // Check if there are changes in the parent that will force us to update the global matrix
  2317. var parentDirty = (this._parent != null) ? (this._parent._globalTransformStep !== this._parentTransformStep) : false;
  2318. // Check if we have to update the globalTransform
  2319. if (!this._globalTransform || localDirty || parentDirty || parentPaddingChanged) {
  2320. var globalTransform = this._parent ? this._parent._globalTransform : null;
  2321. var localTransform;
  2322. Prim2DBase._transMtx.copyFrom(this._localTransform);
  2323. Prim2DBase._transMtx.m[12] += this._layoutAreaPos.x + this._marginOffset.x + parentPaddingOffset.x;
  2324. Prim2DBase._transMtx.m[13] += this._layoutAreaPos.y + this._marginOffset.y + parentPaddingOffset.y;
  2325. localTransform = Prim2DBase._transMtx;
  2326. this._globalTransform = this._parent ? localTransform.multiply(globalTransform) : localTransform.clone();
  2327. this._invGlobalTransform = BABYLON.Matrix.Invert(this._globalTransform);
  2328. this._globalTransformStep = this.owner._globalTransformProcessStep + 1;
  2329. this._parentTransformStep = this._parent ? this._parent._globalTransformStep : 0;
  2330. }
  2331. this._globalTransformProcessStep = this.owner._globalTransformProcessStep;
  2332. }
  2333. if (recurse) {
  2334. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  2335. var child = _a[_i];
  2336. // Stop the recursion if we meet a renderable group
  2337. child.updateCachedStates(!(child instanceof BABYLON.Group2D && child.isRenderableGroup));
  2338. }
  2339. }
  2340. };
  2341. Prim2DBase.prototype._updatePositioning = function () {
  2342. this.owner.addUpdatePositioningCounter(1);
  2343. // From this point we assume that the primitive layoutArea is computed and up to date.
  2344. // We know have to :
  2345. // 1. Determine the PaddingArea and the ActualPosition based on the margin/marginAlignment properties, which will also set the size property of the primitive
  2346. // 2. Determine the contentArea based on the padding property.
  2347. // Auto Create PaddingArea if there's no actualSize on width&|height to allocate the whole content available to the paddingArea where the actualSize is null
  2348. if (!this._hasMargin && (this.actualSize.width == null || this.actualSize.height == null)) {
  2349. if (this.actualSize.width == null) {
  2350. this.marginAlignment.horizontal = PrimitiveAlignment.AlignStretch;
  2351. }
  2352. if (this.actualSize.height == null) {
  2353. this.marginAlignment.vertical = PrimitiveAlignment.AlignStretch;
  2354. }
  2355. }
  2356. // Apply margin
  2357. if (this._hasMargin) {
  2358. this.margin.computeWithAlignment(this.layoutArea, this.size, this.marginAlignment, this._marginOffset, Prim2DBase._size);
  2359. this.actualSize = Prim2DBase._size.clone();
  2360. }
  2361. var isSizeAuto = this.isSizeAuto;
  2362. if (this._hasPadding) {
  2363. // Two cases from here: the size of the Primitive is Auto, its content can't be shrink, so me resize the primitive itself
  2364. if (isSizeAuto) {
  2365. var content = this.size.clone();
  2366. this._getActualSizeFromContentToRef(content, Prim2DBase._icArea);
  2367. this.padding.enlarge(Prim2DBase._icArea, this._paddingOffset, Prim2DBase._size);
  2368. this._contentArea.copyFrom(content);
  2369. this.actualSize = Prim2DBase._size.clone();
  2370. // Changing the padding has resize the prim, which forces us to recompute margin again
  2371. if (this._hasMargin) {
  2372. this.margin.computeWithAlignment(this.layoutArea, Prim2DBase._size, this.marginAlignment, this._marginOffset, Prim2DBase._size);
  2373. }
  2374. }
  2375. else {
  2376. this._getInitialContentAreaToRef(this.actualSize, Prim2DBase._icPos, Prim2DBase._icArea);
  2377. Prim2DBase._icArea.width = Math.max(0, Prim2DBase._icArea.width);
  2378. Prim2DBase._icArea.height = Math.max(0, Prim2DBase._icArea.height);
  2379. this.padding.compute(Prim2DBase._icArea, this._paddingOffset, Prim2DBase._size);
  2380. this._paddingOffset.x += Prim2DBase._icPos.x;
  2381. this._paddingOffset.y += Prim2DBase._icPos.y;
  2382. this._contentArea.copyFrom(Prim2DBase._size);
  2383. }
  2384. }
  2385. else {
  2386. this._getInitialContentAreaToRef(this.actualSize, Prim2DBase._icPos, Prim2DBase._icArea);
  2387. Prim2DBase._icArea.width = Math.max(0, Prim2DBase._icArea.width);
  2388. Prim2DBase._icArea.height = Math.max(0, Prim2DBase._icArea.height);
  2389. this._paddingOffset.copyFrom(Prim2DBase._icPos);
  2390. this._contentArea.copyFrom(Prim2DBase._icArea);
  2391. }
  2392. var aPos = new BABYLON.Vector2(this._layoutAreaPos.x + this._marginOffset.x, this._layoutAreaPos.y + this._marginOffset.y);
  2393. this.actualPosition = aPos;
  2394. if (isSizeAuto) {
  2395. this._lastAutoSizeArea = this.size;
  2396. }
  2397. };
  2398. Object.defineProperty(Prim2DBase.prototype, "contentArea", {
  2399. /**
  2400. * Get the content are of this primitive, this area is computed using the padding property and also possibly the primitive type itself.
  2401. * Children of this primitive will be positioned relative to the bottom/left corner of this area.
  2402. */
  2403. get: function () {
  2404. // Check for positioning update
  2405. if (this._isFlagSet(BABYLON.SmartPropertyPrim.flagPositioningDirty)) {
  2406. this._updatePositioning();
  2407. this._clearFlags(BABYLON.SmartPropertyPrim.flagPositioningDirty);
  2408. }
  2409. return this._contentArea;
  2410. },
  2411. enumerable: true,
  2412. configurable: true
  2413. });
  2414. Prim2DBase.prototype._patchHierarchy = function (owner) {
  2415. this._owner = owner;
  2416. // The only place we initialize the _renderGroup is this method, if it's set, we already been there, no need to execute more
  2417. if (this._renderGroup != null) {
  2418. return;
  2419. }
  2420. if (this instanceof BABYLON.Group2D) {
  2421. var group = this;
  2422. group.detectGroupStates();
  2423. if (group._trackedNode && !group._isFlagSet(BABYLON.SmartPropertyPrim.flagTrackedGroup)) {
  2424. group.owner._registerTrackedNode(this);
  2425. }
  2426. }
  2427. if (this._parent) {
  2428. this._renderGroup = this.parent.traverseUp(function (p) { return p instanceof BABYLON.Group2D && p.isRenderableGroup; });
  2429. this._parentLayoutDirty();
  2430. }
  2431. // Make sure the prim is in the dirtyList if it should be
  2432. if (this._renderGroup && this.isDirty) {
  2433. var list = this._renderGroup._renderableData._primDirtyList;
  2434. var i = list.indexOf(this);
  2435. if (i === -1) {
  2436. list.push(this);
  2437. }
  2438. }
  2439. // Recurse
  2440. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  2441. var child = _a[_i];
  2442. this._patchHierarchyDepth(child);
  2443. child._patchHierarchy(owner);
  2444. }
  2445. };
  2446. Prim2DBase.prototype._patchHierarchyDepth = function (child) {
  2447. child._hierarchyDepth = this._hierarchyDepth + 1;
  2448. child._hierarchyDepthOffset = this._hierarchyDepthOffset + ((this._children.indexOf(child) + 1) * this._siblingDepthOffset);
  2449. child._siblingDepthOffset = this._siblingDepthOffset / BABYLON.Canvas2D.hierarchyLevelMaxSiblingCount;
  2450. };
  2451. /**
  2452. * This method is used to alter the contentArea of the Primitive before margin is applied.
  2453. * In most of the case you won't need to override this method, but it can prove some usefulness, check the Rectangle2D class for a concrete application.
  2454. * @param primSize the current size of the primitive
  2455. * @param initialContentPosition the position of the initial content area to compute, a valid object is passed, you have to set its properties. PLEASE ROUND the values, we're talking about pixels and fraction of them is not a good thing!
  2456. * @param initialContentArea the size of the initial content area to compute, a valid object is passed, you have to set its properties. PLEASE ROUND the values, we're talking about pixels and fraction of them is not a good thing!
  2457. */
  2458. Prim2DBase.prototype._getInitialContentAreaToRef = function (primSize, initialContentPosition, initialContentArea) {
  2459. initialContentArea.copyFrom(primSize);
  2460. initialContentPosition.x = initialContentPosition.y = 0;
  2461. };
  2462. /**
  2463. * This method is used to calculate the new size of the primitive based on the content which must stay the same
  2464. * Check the Rectangle2D implementation for a concrete application.
  2465. * @param primSize the current size of the primitive
  2466. * @param newPrimSize the new size of the primitive. PLEASE ROUND THE values, we're talking about pixels and fraction of them are not our friends!
  2467. */
  2468. Prim2DBase.prototype._getActualSizeFromContentToRef = function (primSize, newPrimSize) {
  2469. newPrimSize.copyFrom(primSize);
  2470. };
  2471. Prim2DBase.PRIM2DBASE_PROPCOUNT = 15;
  2472. Prim2DBase._nullPosition = BABYLON.Vector2.Zero();
  2473. Prim2DBase.boundinbBoxReentrency = false;
  2474. Prim2DBase.nullSize = BABYLON.Size.Zero();
  2475. Prim2DBase._isCanvasInit = false;
  2476. Prim2DBase._t0 = new BABYLON.Matrix();
  2477. Prim2DBase._t1 = new BABYLON.Matrix();
  2478. Prim2DBase._t2 = new BABYLON.Matrix();
  2479. Prim2DBase._v0 = BABYLON.Vector2.Zero(); // Must stay with the value 0,0
  2480. Prim2DBase._transMtx = BABYLON.Matrix.Zero();
  2481. Prim2DBase._icPos = BABYLON.Vector2.Zero();
  2482. Prim2DBase._icArea = BABYLON.Size.Zero();
  2483. Prim2DBase._size = BABYLON.Size.Zero();
  2484. __decorate([
  2485. BABYLON.instanceLevelProperty(1, function (pi) { return Prim2DBase.actualPositionProperty = pi; }, false, true)
  2486. ], Prim2DBase.prototype, "actualPosition", null);
  2487. __decorate([
  2488. BABYLON.dynamicLevelProperty(1, function (pi) { return Prim2DBase.positionProperty = pi; }, false, true)
  2489. ], Prim2DBase.prototype, "position", null);
  2490. __decorate([
  2491. BABYLON.dynamicLevelProperty(2, function (pi) { return Prim2DBase.sizeProperty = pi; }, false, true)
  2492. ], Prim2DBase.prototype, "size", null);
  2493. __decorate([
  2494. BABYLON.instanceLevelProperty(3, function (pi) { return Prim2DBase.rotationProperty = pi; }, false, true)
  2495. ], Prim2DBase.prototype, "rotation", null);
  2496. __decorate([
  2497. BABYLON.instanceLevelProperty(4, function (pi) { return Prim2DBase.scaleProperty = pi; }, false, true)
  2498. ], Prim2DBase.prototype, "scale", null);
  2499. __decorate([
  2500. BABYLON.dynamicLevelProperty(5, function (pi) { return Prim2DBase.originProperty = pi; }, false, true)
  2501. ], Prim2DBase.prototype, "origin", null);
  2502. __decorate([
  2503. BABYLON.dynamicLevelProperty(6, function (pi) { return Prim2DBase.levelVisibleProperty = pi; })
  2504. ], Prim2DBase.prototype, "levelVisible", null);
  2505. __decorate([
  2506. BABYLON.instanceLevelProperty(7, function (pi) { return Prim2DBase.isVisibleProperty = pi; })
  2507. ], Prim2DBase.prototype, "isVisible", null);
  2508. __decorate([
  2509. BABYLON.instanceLevelProperty(8, function (pi) { return Prim2DBase.zOrderProperty = pi; })
  2510. ], Prim2DBase.prototype, "zOrder", null);
  2511. __decorate([
  2512. BABYLON.dynamicLevelProperty(9, function (pi) { return Prim2DBase.marginProperty = pi; })
  2513. ], Prim2DBase.prototype, "margin", null);
  2514. __decorate([
  2515. BABYLON.dynamicLevelProperty(10, function (pi) { return Prim2DBase.paddingProperty = pi; })
  2516. ], Prim2DBase.prototype, "padding", null);
  2517. __decorate([
  2518. BABYLON.dynamicLevelProperty(11, function (pi) { return Prim2DBase.marginAlignmentProperty = pi; })
  2519. ], Prim2DBase.prototype, "marginAlignment", null);
  2520. Prim2DBase = __decorate([
  2521. BABYLON.className("Prim2DBase")
  2522. ], Prim2DBase);
  2523. return Prim2DBase;
  2524. })(BABYLON.SmartPropertyPrim);
  2525. BABYLON.Prim2DBase = Prim2DBase;
  2526. })(BABYLON || (BABYLON = {}));