babylon.prim2dBase.ts 104 KB

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