control.ts 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142
  1. import { Nullable } from "babylonjs/types";
  2. import { Observable, Observer } from "babylonjs/Misc/observable";
  3. import { Vector2, Vector3, Matrix } from "babylonjs/Maths/math.vector";
  4. import { PointerEventTypes, PointerInfoBase } from 'babylonjs/Events/pointerEvents';
  5. import { Logger } from "babylonjs/Misc/logger";
  6. import { Tools } from "babylonjs/Misc/tools";
  7. import { TransformNode } from "babylonjs/Meshes/transformNode";
  8. import { Scene } from "babylonjs/scene";
  9. import { Container } from "./container";
  10. import { AdvancedDynamicTexture } from "../advancedDynamicTexture";
  11. import { ValueAndUnit } from "../valueAndUnit";
  12. import { Measure } from "../measure";
  13. import { Style } from "../style";
  14. import { Matrix2D, Vector2WithInfo } from "../math2D";
  15. import { _TypeStore } from 'babylonjs/Misc/typeStore';
  16. import { SerializationHelper, serialize } from 'babylonjs/Misc/decorators';
  17. /**
  18. * Root class used for all 2D controls
  19. * @see https://doc.babylonjs.com/how_to/gui#controls
  20. */
  21. export class Control {
  22. /**
  23. * Gets or sets a boolean indicating if alpha must be an inherited value (false by default)
  24. */
  25. public static AllowAlphaInheritance = false;
  26. private _alpha = 1;
  27. private _alphaSet = false;
  28. private _zIndex = 0;
  29. /** @hidden */
  30. public _host: AdvancedDynamicTexture;
  31. /** Gets or sets the control parent */
  32. public parent: Nullable<Container>;
  33. /** @hidden */
  34. public _currentMeasure = Measure.Empty();
  35. private _fontFamily = "Arial";
  36. private _fontStyle = "";
  37. private _fontWeight = "";
  38. private _fontSize = new ValueAndUnit(18, ValueAndUnit.UNITMODE_PIXEL, false);
  39. private _font: string;
  40. /** @hidden */
  41. public _width = new ValueAndUnit(1, ValueAndUnit.UNITMODE_PERCENTAGE, false);
  42. /** @hidden */
  43. public _height = new ValueAndUnit(1, ValueAndUnit.UNITMODE_PERCENTAGE, false);
  44. /** @hidden */
  45. protected _fontOffset: { ascent: number, height: number, descent: number };
  46. private _color = "";
  47. private _style: Nullable<Style> = null;
  48. private _styleObserver: Nullable<Observer<Style>>;
  49. /** @hidden */
  50. protected _horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_CENTER;
  51. /** @hidden */
  52. protected _verticalAlignment = Control.VERTICAL_ALIGNMENT_CENTER;
  53. /** @hidden */
  54. protected _isDirty = true;
  55. /** @hidden */
  56. protected _wasDirty = false;
  57. /** @hidden */
  58. public _tempParentMeasure = Measure.Empty();
  59. /** @hidden */
  60. public _prevCurrentMeasureTransformedIntoGlobalSpace = Measure.Empty();
  61. /** @hidden */
  62. protected _cachedParentMeasure = Measure.Empty();
  63. private _paddingLeft = new ValueAndUnit(0);
  64. private _paddingRight = new ValueAndUnit(0);
  65. private _paddingTop = new ValueAndUnit(0);
  66. private _paddingBottom = new ValueAndUnit(0);
  67. /** @hidden */
  68. public _left = new ValueAndUnit(0);
  69. /** @hidden */
  70. public _top = new ValueAndUnit(0);
  71. private _scaleX = 1.0;
  72. private _scaleY = 1.0;
  73. private _rotation = 0;
  74. private _transformCenterX = 0.5;
  75. private _transformCenterY = 0.5;
  76. /** @hidden */
  77. public _transformMatrix = Matrix2D.Identity();
  78. /** @hidden */
  79. protected _invertTransformMatrix = Matrix2D.Identity();
  80. /** @hidden */
  81. protected _transformedPosition = Vector2.Zero();
  82. private _isMatrixDirty = true;
  83. private _cachedOffsetX: number;
  84. private _cachedOffsetY: number;
  85. private _isVisible = true;
  86. private _isHighlighted = false;
  87. /** @hidden */
  88. public _linkedMesh: Nullable<TransformNode>;
  89. private _fontSet = false;
  90. private _dummyVector2 = Vector2.Zero();
  91. private _downCount = 0;
  92. private _enterCount = -1;
  93. private _doNotRender = false;
  94. private _downPointerIds: { [id: number]: boolean } = {};
  95. protected _isEnabled = true;
  96. protected _disabledColor = "#9a9a9a";
  97. protected _disabledColorItem = "#6a6a6a";
  98. /** @hidden */
  99. protected _rebuildLayout = false;
  100. /** @hidden */
  101. public _customData: any = {};
  102. /** @hidden */
  103. public _isClipped = false;
  104. /** @hidden */
  105. public _automaticSize = false;
  106. /** @hidden */
  107. public _tag: any;
  108. /**
  109. * Gets or sets the unique id of the node. Please note that this number will be updated when the control is added to a container
  110. */
  111. public uniqueId: number;
  112. /**
  113. * Gets or sets an object used to store user defined information for the node
  114. */
  115. @serialize()
  116. public metadata: any = null;
  117. /** Gets or sets a boolean indicating if the control can be hit with pointer events */
  118. @serialize()
  119. public isHitTestVisible = true;
  120. /** Gets or sets a boolean indicating if the control can block pointer events */
  121. @serialize()
  122. public isPointerBlocker = false;
  123. /** Gets or sets a boolean indicating if the control can be focusable */
  124. @serialize()
  125. public isFocusInvisible = false;
  126. /**
  127. * Gets or sets a boolean indicating if the children are clipped to the current control bounds.
  128. * Please note that not clipping children may generate issues with adt.useInvalidateRectOptimization so it is recommended to turn this optimization off if you want to use unclipped children
  129. */
  130. @serialize()
  131. public clipChildren = true;
  132. /**
  133. * Gets or sets a boolean indicating that control content must be clipped
  134. * Please note that not clipping children may generate issues with adt.useInvalidateRectOptimization so it is recommended to turn this optimization off if you want to use unclipped children
  135. */
  136. @serialize()
  137. public clipContent = true;
  138. /**
  139. * Gets or sets a boolean indicating that the current control should cache its rendering (useful when the control does not change often)
  140. */
  141. @serialize()
  142. public useBitmapCache = false;
  143. private _cacheData: Nullable<ImageData>;
  144. private _shadowOffsetX = 0;
  145. /** Gets or sets a value indicating the offset to apply on X axis to render the shadow */
  146. @serialize()
  147. public get shadowOffsetX() {
  148. return this._shadowOffsetX;
  149. }
  150. public set shadowOffsetX(value: number) {
  151. if (this._shadowOffsetX === value) {
  152. return;
  153. }
  154. this._shadowOffsetX = value;
  155. this._markAsDirty();
  156. }
  157. private _shadowOffsetY = 0;
  158. /** Gets or sets a value indicating the offset to apply on Y axis to render the shadow */
  159. @serialize()
  160. public get shadowOffsetY() {
  161. return this._shadowOffsetY;
  162. }
  163. public set shadowOffsetY(value: number) {
  164. if (this._shadowOffsetY === value) {
  165. return;
  166. }
  167. this._shadowOffsetY = value;
  168. this._markAsDirty();
  169. }
  170. private _shadowBlur = 0;
  171. /** Gets or sets a value indicating the amount of blur to use to render the shadow */
  172. @serialize()
  173. public get shadowBlur() {
  174. return this._shadowBlur;
  175. }
  176. public set shadowBlur(value: number) {
  177. if (this._shadowBlur === value) {
  178. return;
  179. }
  180. this._shadowBlur = value;
  181. this._markAsDirty();
  182. }
  183. private _shadowColor = 'black';
  184. /** Gets or sets a value indicating the color of the shadow (black by default ie. "#000") */
  185. @serialize()
  186. public get shadowColor() {
  187. return this._shadowColor;
  188. }
  189. public set shadowColor(value: string) {
  190. if (this._shadowColor === value) {
  191. return;
  192. }
  193. this._shadowColor = value;
  194. this._markAsDirty();
  195. }
  196. /** Gets or sets the cursor to use when the control is hovered */
  197. @serialize()
  198. public hoverCursor = "";
  199. /** @hidden */
  200. protected _linkOffsetX = new ValueAndUnit(0);
  201. /** @hidden */
  202. protected _linkOffsetY = new ValueAndUnit(0);
  203. // Properties
  204. /** Gets the control type name */
  205. public get typeName(): string {
  206. return this._getTypeName();
  207. }
  208. /**
  209. * Get the current class name of the control.
  210. * @returns current class name
  211. */
  212. public getClassName(): string {
  213. return this._getTypeName();
  214. }
  215. /**
  216. * An event triggered when pointer wheel is scrolled
  217. */
  218. public onWheelObservable = new Observable<Vector2>();
  219. /**
  220. * An event triggered when the pointer move over the control.
  221. */
  222. public onPointerMoveObservable = new Observable<Vector2>();
  223. /**
  224. * An event triggered when the pointer move out of the control.
  225. */
  226. public onPointerOutObservable = new Observable<Control>();
  227. /**
  228. * An event triggered when the pointer taps the control
  229. */
  230. public onPointerDownObservable = new Observable<Vector2WithInfo>();
  231. /**
  232. * An event triggered when pointer up
  233. */
  234. public onPointerUpObservable = new Observable<Vector2WithInfo>();
  235. /**
  236. * An event triggered when a control is clicked on
  237. */
  238. public onPointerClickObservable = new Observable<Vector2WithInfo>();
  239. /**
  240. * An event triggered when pointer enters the control
  241. */
  242. public onPointerEnterObservable = new Observable<Control>();
  243. /**
  244. * An event triggered when the control is marked as dirty
  245. */
  246. public onDirtyObservable = new Observable<Control>();
  247. /**
  248. * An event triggered before drawing the control
  249. */
  250. public onBeforeDrawObservable = new Observable<Control>();
  251. /**
  252. * An event triggered after the control was drawn
  253. */
  254. public onAfterDrawObservable = new Observable<Control>();
  255. /**
  256. * An event triggered when the control has been disposed
  257. */
  258. public onDisposeObservable = new Observable<Control>();
  259. /**
  260. * Get the hosting AdvancedDynamicTexture
  261. */
  262. public get host(): AdvancedDynamicTexture {
  263. return this._host;
  264. }
  265. /** Gets or set information about font offsets (used to render and align text) */
  266. @serialize()
  267. public get fontOffset(): { ascent: number, height: number, descent: number } {
  268. return this._fontOffset;
  269. }
  270. public set fontOffset(offset: { ascent: number, height: number, descent: number }) {
  271. this._fontOffset = offset;
  272. }
  273. /** Gets or sets alpha value for the control (1 means opaque and 0 means entirely transparent) */
  274. @serialize()
  275. public get alpha(): number {
  276. return this._alpha;
  277. }
  278. public set alpha(value: number) {
  279. if (this._alpha === value) {
  280. return;
  281. }
  282. this._alphaSet = true;
  283. this._alpha = value;
  284. this._markAsDirty();
  285. }
  286. /**
  287. * Gets or sets a boolean indicating that we want to highlight the control (mostly for debugging purpose)
  288. */
  289. public get isHighlighted(): boolean {
  290. return this._isHighlighted;
  291. }
  292. public set isHighlighted(value: boolean) {
  293. if (this._isHighlighted === value) {
  294. return;
  295. }
  296. this._isHighlighted = value;
  297. this._markAsDirty();
  298. }
  299. /** Gets or sets a value indicating the scale factor on X axis (1 by default)
  300. * @see https://doc.babylonjs.com/how_to/gui#rotation-and-scaling
  301. */
  302. @serialize()
  303. public get scaleX(): number {
  304. return this._scaleX;
  305. }
  306. public set scaleX(value: number) {
  307. if (this._scaleX === value) {
  308. return;
  309. }
  310. this._scaleX = value;
  311. this._markAsDirty();
  312. this._markMatrixAsDirty();
  313. }
  314. /** Gets or sets a value indicating the scale factor on Y axis (1 by default)
  315. * @see https://doc.babylonjs.com/how_to/gui#rotation-and-scaling
  316. */
  317. @serialize()
  318. public get scaleY(): number {
  319. return this._scaleY;
  320. }
  321. public set scaleY(value: number) {
  322. if (this._scaleY === value) {
  323. return;
  324. }
  325. this._scaleY = value;
  326. this._markAsDirty();
  327. this._markMatrixAsDirty();
  328. }
  329. /** Gets or sets the rotation angle (0 by default)
  330. * @see https://doc.babylonjs.com/how_to/gui#rotation-and-scaling
  331. */
  332. @serialize()
  333. public get rotation(): number {
  334. return this._rotation;
  335. }
  336. public set rotation(value: number) {
  337. if (this._rotation === value) {
  338. return;
  339. }
  340. this._rotation = value;
  341. this._markAsDirty();
  342. this._markMatrixAsDirty();
  343. }
  344. /** Gets or sets the transformation center on Y axis (0 by default)
  345. * @see https://doc.babylonjs.com/how_to/gui#rotation-and-scaling
  346. */
  347. @serialize()
  348. public get transformCenterY(): number {
  349. return this._transformCenterY;
  350. }
  351. public set transformCenterY(value: number) {
  352. if (this._transformCenterY === value) {
  353. return;
  354. }
  355. this._transformCenterY = value;
  356. this._markAsDirty();
  357. this._markMatrixAsDirty();
  358. }
  359. /** Gets or sets the transformation center on X axis (0 by default)
  360. * @see https://doc.babylonjs.com/how_to/gui#rotation-and-scaling
  361. */
  362. @serialize()
  363. public get transformCenterX(): number {
  364. return this._transformCenterX;
  365. }
  366. public set transformCenterX(value: number) {
  367. if (this._transformCenterX === value) {
  368. return;
  369. }
  370. this._transformCenterX = value;
  371. this._markAsDirty();
  372. this._markMatrixAsDirty();
  373. }
  374. /**
  375. * Gets or sets the horizontal alignment
  376. * @see https://doc.babylonjs.com/how_to/gui#alignments
  377. */
  378. @serialize()
  379. public get horizontalAlignment(): number {
  380. return this._horizontalAlignment;
  381. }
  382. public set horizontalAlignment(value: number) {
  383. if (this._horizontalAlignment === value) {
  384. return;
  385. }
  386. this._horizontalAlignment = value;
  387. this._markAsDirty();
  388. }
  389. /**
  390. * Gets or sets the vertical alignment
  391. * @see https://doc.babylonjs.com/how_to/gui#alignments
  392. */
  393. @serialize()
  394. public get verticalAlignment(): number {
  395. return this._verticalAlignment;
  396. }
  397. public set verticalAlignment(value: number) {
  398. if (this._verticalAlignment === value) {
  399. return;
  400. }
  401. this._verticalAlignment = value;
  402. this._markAsDirty();
  403. }
  404. /**
  405. * Gets or sets a fixed ratio for this control.
  406. * When different from 0, the ratio is used to compute the "second" dimension.
  407. * The first dimension used in the computation is the last one set (by setting width / widthInPixels or height / heightInPixels), and the
  408. * second dimension is computed as first dimension * fixedRatio
  409. */
  410. @serialize()
  411. public fixedRatio = 0;
  412. private _fixedRatioMasterIsWidth = true;
  413. /**
  414. * Gets or sets control width
  415. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  416. */
  417. @serialize()
  418. public get width(): string | number {
  419. return this._width.toString(this._host);
  420. }
  421. public set width(value: string | number) {
  422. this._fixedRatioMasterIsWidth = true;
  423. if (this._width.toString(this._host) === value) {
  424. return;
  425. }
  426. if (this._width.fromString(value)) {
  427. this._markAsDirty();
  428. }
  429. }
  430. /**
  431. * Gets or sets the control width in pixel
  432. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  433. */
  434. public get widthInPixels(): number {
  435. return this._width.getValueInPixel(this._host, this._cachedParentMeasure.width);
  436. }
  437. public set widthInPixels(value: number) {
  438. if (isNaN(value)) {
  439. return;
  440. }
  441. this._fixedRatioMasterIsWidth = true;
  442. this.width = value + "px";
  443. }
  444. /**
  445. * Gets or sets control height
  446. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  447. */
  448. @serialize()
  449. public get height(): string | number {
  450. return this._height.toString(this._host);
  451. }
  452. public set height(value: string | number) {
  453. this._fixedRatioMasterIsWidth = false;
  454. if (this._height.toString(this._host) === value) {
  455. return;
  456. }
  457. if (this._height.fromString(value)) {
  458. this._markAsDirty();
  459. }
  460. }
  461. /**
  462. * Gets or sets control height in pixel
  463. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  464. */
  465. public get heightInPixels(): number {
  466. return this._height.getValueInPixel(this._host, this._cachedParentMeasure.height);
  467. }
  468. public set heightInPixels(value: number) {
  469. if (isNaN(value)) {
  470. return;
  471. }
  472. this._fixedRatioMasterIsWidth = false;
  473. this.height = value + "px";
  474. }
  475. /** Gets or set font family */
  476. @serialize()
  477. public get fontFamily(): string {
  478. if (!this._fontSet) {
  479. return "";
  480. }
  481. return this._fontFamily;
  482. }
  483. public set fontFamily(value: string) {
  484. if (this._fontFamily === value) {
  485. return;
  486. }
  487. this._fontFamily = value;
  488. this._resetFontCache();
  489. }
  490. /** Gets or sets font style */
  491. @serialize()
  492. public get fontStyle(): string {
  493. return this._fontStyle;
  494. }
  495. public set fontStyle(value: string) {
  496. if (this._fontStyle === value) {
  497. return;
  498. }
  499. this._fontStyle = value;
  500. this._resetFontCache();
  501. }
  502. /** Gets or sets font weight */
  503. @serialize()
  504. public get fontWeight(): string {
  505. return this._fontWeight;
  506. }
  507. public set fontWeight(value: string) {
  508. if (this._fontWeight === value) {
  509. return;
  510. }
  511. this._fontWeight = value;
  512. this._resetFontCache();
  513. }
  514. /**
  515. * Gets or sets style
  516. * @see https://doc.babylonjs.com/how_to/gui#styles
  517. */
  518. @serialize()
  519. public get style(): Nullable<Style> {
  520. return this._style;
  521. }
  522. public set style(value: Nullable<Style>) {
  523. if (this._style) {
  524. this._style.onChangedObservable.remove(this._styleObserver);
  525. this._styleObserver = null;
  526. }
  527. this._style = value;
  528. if (this._style) {
  529. this._styleObserver = this._style.onChangedObservable.add(() => {
  530. this._markAsDirty();
  531. this._resetFontCache();
  532. });
  533. }
  534. this._markAsDirty();
  535. this._resetFontCache();
  536. }
  537. /** @hidden */
  538. public get _isFontSizeInPercentage(): boolean {
  539. return this._fontSize.isPercentage;
  540. }
  541. /** Gets or sets font size in pixels */
  542. public get fontSizeInPixels(): number {
  543. let fontSizeToUse = this._style ? this._style._fontSize : this._fontSize;
  544. if (fontSizeToUse.isPixel) {
  545. return fontSizeToUse.getValue(this._host);
  546. }
  547. return fontSizeToUse.getValueInPixel(this._host, this._tempParentMeasure.height || this._cachedParentMeasure.height);
  548. }
  549. public set fontSizeInPixels(value: number) {
  550. if (isNaN(value)) {
  551. return;
  552. }
  553. this.fontSize = value + "px";
  554. }
  555. /** Gets or sets font size */
  556. @serialize()
  557. public get fontSize(): string | number {
  558. return this._fontSize.toString(this._host);
  559. }
  560. public set fontSize(value: string | number) {
  561. if (this._fontSize.toString(this._host) === value) {
  562. return;
  563. }
  564. if (this._fontSize.fromString(value)) {
  565. this._markAsDirty();
  566. this._resetFontCache();
  567. }
  568. }
  569. /** Gets or sets foreground color */
  570. @serialize()
  571. public get color(): string {
  572. return this._color;
  573. }
  574. public set color(value: string) {
  575. if (this._color === value) {
  576. return;
  577. }
  578. this._color = value;
  579. this._markAsDirty();
  580. }
  581. /** Gets or sets z index which is used to reorder controls on the z axis */
  582. @serialize()
  583. public get zIndex(): number {
  584. return this._zIndex;
  585. }
  586. public set zIndex(value: number) {
  587. if (this.zIndex === value) {
  588. return;
  589. }
  590. this._zIndex = value;
  591. if (this.parent) {
  592. this.parent._reOrderControl(this);
  593. }
  594. }
  595. /** Gets or sets a boolean indicating if the control can be rendered */
  596. @serialize()
  597. public get notRenderable(): boolean {
  598. return this._doNotRender;
  599. }
  600. public set notRenderable(value: boolean) {
  601. if (this._doNotRender === value) {
  602. return;
  603. }
  604. this._doNotRender = value;
  605. this._markAsDirty();
  606. }
  607. /** Gets or sets a boolean indicating if the control is visible */
  608. @serialize()
  609. public get isVisible(): boolean {
  610. return this._isVisible;
  611. }
  612. public set isVisible(value: boolean) {
  613. if (this._isVisible === value) {
  614. return;
  615. }
  616. this._isVisible = value;
  617. this._markAsDirty(true);
  618. }
  619. /** Gets a boolean indicating that the control needs to update its rendering */
  620. public get isDirty(): boolean {
  621. return this._isDirty;
  622. }
  623. /**
  624. * Gets the current linked mesh (or null if none)
  625. */
  626. public get linkedMesh(): Nullable<TransformNode> {
  627. return this._linkedMesh;
  628. }
  629. /**
  630. * Gets or sets a value indicating the padding to use on the left of the control
  631. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  632. */
  633. @serialize()
  634. public get paddingLeft(): string | number {
  635. return this._paddingLeft.toString(this._host);
  636. }
  637. public set paddingLeft(value: string | number) {
  638. if (this._paddingLeft.fromString(value)) {
  639. this._markAsDirty();
  640. }
  641. }
  642. /**
  643. * Gets or sets a value indicating the padding in pixels to use on the left of the control
  644. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  645. */
  646. public get paddingLeftInPixels(): number {
  647. return this._paddingLeft.getValueInPixel(this._host, this._cachedParentMeasure.width);
  648. }
  649. public set paddingLeftInPixels(value: number) {
  650. if (isNaN(value)) {
  651. return;
  652. }
  653. this.paddingLeft = value + "px";
  654. }
  655. /**
  656. * Gets or sets a value indicating the padding to use on the right of the control
  657. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  658. */
  659. @serialize()
  660. public get paddingRight(): string | number {
  661. return this._paddingRight.toString(this._host);
  662. }
  663. public set paddingRight(value: string | number) {
  664. if (this._paddingRight.fromString(value)) {
  665. this._markAsDirty();
  666. }
  667. }
  668. /**
  669. * Gets or sets a value indicating the padding in pixels to use on the right of the control
  670. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  671. */
  672. public get paddingRightInPixels(): number {
  673. return this._paddingRight.getValueInPixel(this._host, this._cachedParentMeasure.width);
  674. }
  675. public set paddingRightInPixels(value: number) {
  676. if (isNaN(value)) {
  677. return;
  678. }
  679. this.paddingRight = value + "px";
  680. }
  681. /**
  682. * Gets or sets a value indicating the padding to use on the top of the control
  683. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  684. */
  685. @serialize()
  686. public get paddingTop(): string | number {
  687. return this._paddingTop.toString(this._host);
  688. }
  689. public set paddingTop(value: string | number) {
  690. if (this._paddingTop.fromString(value)) {
  691. this._markAsDirty();
  692. }
  693. }
  694. /**
  695. * Gets or sets a value indicating the padding in pixels to use on the top of the control
  696. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  697. */
  698. public get paddingTopInPixels(): number {
  699. return this._paddingTop.getValueInPixel(this._host, this._cachedParentMeasure.height);
  700. }
  701. public set paddingTopInPixels(value: number) {
  702. if (isNaN(value)) {
  703. return;
  704. }
  705. this.paddingTop = value + "px";
  706. }
  707. /**
  708. * Gets or sets a value indicating the padding to use on the bottom of the control
  709. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  710. */
  711. @serialize()
  712. public get paddingBottom(): string | number {
  713. return this._paddingBottom.toString(this._host);
  714. }
  715. public set paddingBottom(value: string | number) {
  716. if (this._paddingBottom.fromString(value)) {
  717. this._markAsDirty();
  718. }
  719. }
  720. /**
  721. * Gets or sets a value indicating the padding in pixels to use on the bottom of the control
  722. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  723. */
  724. public get paddingBottomInPixels(): number {
  725. return this._paddingBottom.getValueInPixel(this._host, this._cachedParentMeasure.height);
  726. }
  727. public set paddingBottomInPixels(value: number) {
  728. if (isNaN(value)) {
  729. return;
  730. }
  731. this.paddingBottom = value + "px";
  732. }
  733. /**
  734. * Gets or sets a value indicating the left coordinate of the control
  735. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  736. */
  737. @serialize()
  738. public get left(): string | number {
  739. return this._left.toString(this._host);
  740. }
  741. public set left(value: string | number) {
  742. if (this._left.fromString(value)) {
  743. this._markAsDirty();
  744. }
  745. }
  746. /**
  747. * Gets or sets a value indicating the left coordinate in pixels of the control
  748. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  749. */
  750. public get leftInPixels(): number {
  751. return this._left.getValueInPixel(this._host, this._cachedParentMeasure.width);
  752. }
  753. public set leftInPixels(value: number) {
  754. if (isNaN(value)) {
  755. return;
  756. }
  757. this.left = value + "px";
  758. }
  759. /**
  760. * Gets or sets a value indicating the top coordinate of the control
  761. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  762. */
  763. @serialize()
  764. public get top(): string | number {
  765. return this._top.toString(this._host);
  766. }
  767. public set top(value: string | number) {
  768. if (this._top.fromString(value)) {
  769. this._markAsDirty();
  770. }
  771. }
  772. /**
  773. * Gets or sets a value indicating the top coordinate in pixels of the control
  774. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  775. */
  776. public get topInPixels(): number {
  777. return this._top.getValueInPixel(this._host, this._cachedParentMeasure.height);
  778. }
  779. public set topInPixels(value: number) {
  780. if (isNaN(value)) {
  781. return;
  782. }
  783. this.top = value + "px";
  784. }
  785. /**
  786. * Gets or sets a value indicating the offset on X axis to the linked mesh
  787. * @see https://doc.babylonjs.com/how_to/gui#tracking-positions
  788. */
  789. @serialize()
  790. public get linkOffsetX(): string | number {
  791. return this._linkOffsetX.toString(this._host);
  792. }
  793. public set linkOffsetX(value: string | number) {
  794. if (this._linkOffsetX.fromString(value)) {
  795. this._markAsDirty();
  796. }
  797. }
  798. /**
  799. * Gets or sets a value indicating the offset in pixels on X axis to the linked mesh
  800. * @see https://doc.babylonjs.com/how_to/gui#tracking-positions
  801. */
  802. public get linkOffsetXInPixels(): number {
  803. return this._linkOffsetX.getValueInPixel(this._host, this._cachedParentMeasure.width);
  804. }
  805. public set linkOffsetXInPixels(value: number) {
  806. if (isNaN(value)) {
  807. return;
  808. }
  809. this.linkOffsetX = value + "px";
  810. }
  811. /**
  812. * Gets or sets a value indicating the offset on Y axis to the linked mesh
  813. * @see https://doc.babylonjs.com/how_to/gui#tracking-positions
  814. */
  815. @serialize()
  816. public get linkOffsetY(): string | number {
  817. return this._linkOffsetY.toString(this._host);
  818. }
  819. public set linkOffsetY(value: string | number) {
  820. if (this._linkOffsetY.fromString(value)) {
  821. this._markAsDirty();
  822. }
  823. }
  824. /**
  825. * Gets or sets a value indicating the offset in pixels on Y axis to the linked mesh
  826. * @see https://doc.babylonjs.com/how_to/gui#tracking-positions
  827. */
  828. public get linkOffsetYInPixels(): number {
  829. return this._linkOffsetY.getValueInPixel(this._host, this._cachedParentMeasure.height);
  830. }
  831. public set linkOffsetYInPixels(value: number) {
  832. if (isNaN(value)) {
  833. return;
  834. }
  835. this.linkOffsetY = value + "px";
  836. }
  837. /** Gets the center coordinate on X axis */
  838. public get centerX(): number {
  839. return this._currentMeasure.left + this._currentMeasure.width / 2;
  840. }
  841. /** Gets the center coordinate on Y axis */
  842. public get centerY(): number {
  843. return this._currentMeasure.top + this._currentMeasure.height / 2;
  844. }
  845. /** Gets or sets if control is Enabled */
  846. @serialize()
  847. public get isEnabled(): boolean {
  848. return this._isEnabled;
  849. }
  850. public set isEnabled(value: boolean) {
  851. if (this._isEnabled === value) {
  852. return;
  853. }
  854. this._isEnabled = value;
  855. this._markAsDirty();
  856. }
  857. /** Gets or sets background color of control if it's disabled */
  858. @serialize()
  859. public get disabledColor(): string {
  860. return this._disabledColor;
  861. }
  862. public set disabledColor(value: string) {
  863. if (this._disabledColor === value) {
  864. return;
  865. }
  866. this._disabledColor = value;
  867. this._markAsDirty();
  868. }
  869. /** Gets or sets front color of control if it's disabled */
  870. @serialize()
  871. public get disabledColorItem(): string {
  872. return this._disabledColorItem;
  873. }
  874. public set disabledColorItem(value: string) {
  875. if (this._disabledColorItem === value) {
  876. return;
  877. }
  878. this._disabledColorItem = value;
  879. this._markAsDirty();
  880. }
  881. // Functions
  882. /**
  883. * Creates a new control
  884. * @param name defines the name of the control
  885. */
  886. constructor(
  887. /** defines the name of the control */
  888. public name?: string) {
  889. }
  890. /** @hidden */
  891. protected _getTypeName(): string {
  892. return "Control";
  893. }
  894. /**
  895. * Gets the first ascendant in the hierarchy of the given type
  896. * @param className defines the required type
  897. * @returns the ascendant or null if not found
  898. */
  899. public getAscendantOfClass(className: string): Nullable<Control> {
  900. if (!this.parent) {
  901. return null;
  902. }
  903. if (this.parent.getClassName() === className) {
  904. return this.parent;
  905. }
  906. return this.parent.getAscendantOfClass(className);
  907. }
  908. /** @hidden */
  909. public _resetFontCache(): void {
  910. this._fontSet = true;
  911. this._markAsDirty();
  912. }
  913. /**
  914. * Determines if a container is an ascendant of the current control
  915. * @param container defines the container to look for
  916. * @returns true if the container is one of the ascendant of the control
  917. */
  918. public isAscendant(container: Control): boolean {
  919. if (!this.parent) {
  920. return false;
  921. }
  922. if (this.parent === container) {
  923. return true;
  924. }
  925. return this.parent.isAscendant(container);
  926. }
  927. /**
  928. * Gets coordinates in local control space
  929. * @param globalCoordinates defines the coordinates to transform
  930. * @returns the new coordinates in local space
  931. */
  932. public getLocalCoordinates(globalCoordinates: Vector2): Vector2 {
  933. var result = Vector2.Zero();
  934. this.getLocalCoordinatesToRef(globalCoordinates, result);
  935. return result;
  936. }
  937. /**
  938. * Gets coordinates in local control space
  939. * @param globalCoordinates defines the coordinates to transform
  940. * @param result defines the target vector2 where to store the result
  941. * @returns the current control
  942. */
  943. public getLocalCoordinatesToRef(globalCoordinates: Vector2, result: Vector2): Control {
  944. result.x = globalCoordinates.x - this._currentMeasure.left;
  945. result.y = globalCoordinates.y - this._currentMeasure.top;
  946. return this;
  947. }
  948. /**
  949. * Gets coordinates in parent local control space
  950. * @param globalCoordinates defines the coordinates to transform
  951. * @returns the new coordinates in parent local space
  952. */
  953. public getParentLocalCoordinates(globalCoordinates: Vector2): Vector2 {
  954. var result = Vector2.Zero();
  955. result.x = globalCoordinates.x - this._cachedParentMeasure.left;
  956. result.y = globalCoordinates.y - this._cachedParentMeasure.top;
  957. return result;
  958. }
  959. /**
  960. * Move the current control to a vector3 position projected onto the screen.
  961. * @param position defines the target position
  962. * @param scene defines the hosting scene
  963. */
  964. public moveToVector3(position: Vector3, scene: Scene): void {
  965. if (!this._host || this.parent !== this._host._rootContainer) {
  966. Tools.Error("Cannot move a control to a vector3 if the control is not at root level");
  967. return;
  968. }
  969. this.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  970. this.verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  971. var globalViewport = this._host._getGlobalViewport(scene);
  972. var projectedPosition = Vector3.Project(position, Matrix.Identity(), scene.getTransformMatrix(), globalViewport);
  973. this._moveToProjectedPosition(projectedPosition);
  974. if (projectedPosition.z < 0 || projectedPosition.z > 1) {
  975. this.notRenderable = true;
  976. return;
  977. }
  978. this.notRenderable = false;
  979. }
  980. /**
  981. * Will store all controls that have this control as ascendant in a given array
  982. * @param results defines the array where to store the descendants
  983. * @param directDescendantsOnly defines if true only direct descendants of 'this' will be considered, if false direct and also indirect (children of children, an so on in a recursive manner) descendants of 'this' will be considered
  984. * @param predicate defines an optional predicate that will be called on every evaluated child, the predicate must return true for a given child to be part of the result, otherwise it will be ignored
  985. */
  986. public getDescendantsToRef(results: Control[], directDescendantsOnly: boolean = false, predicate?: (control: Control) => boolean): void {
  987. // Do nothing by default
  988. }
  989. /**
  990. * Will return all controls that have this control as ascendant
  991. * @param directDescendantsOnly defines if true only direct descendants of 'this' will be considered, if false direct and also indirect (children of children, an so on in a recursive manner) descendants of 'this' will be considered
  992. * @param predicate defines an optional predicate that will be called on every evaluated child, the predicate must return true for a given child to be part of the result, otherwise it will be ignored
  993. * @return all child controls
  994. */
  995. public getDescendants(directDescendantsOnly?: boolean, predicate?: (control: Control) => boolean): Control[] {
  996. var results = new Array<Control>();
  997. this.getDescendantsToRef(results, directDescendantsOnly, predicate);
  998. return results;
  999. }
  1000. /**
  1001. * Link current control with a target mesh
  1002. * @param mesh defines the mesh to link with
  1003. * @see https://doc.babylonjs.com/how_to/gui#tracking-positions
  1004. */
  1005. public linkWithMesh(mesh: Nullable<TransformNode>): void {
  1006. if (!this._host || this.parent && this.parent !== this._host._rootContainer) {
  1007. if (mesh) {
  1008. Tools.Error("Cannot link a control to a mesh if the control is not at root level");
  1009. }
  1010. return;
  1011. }
  1012. var index = this._host._linkedControls.indexOf(this);
  1013. if (index !== -1) {
  1014. this._linkedMesh = mesh;
  1015. if (!mesh) {
  1016. this._host._linkedControls.splice(index, 1);
  1017. }
  1018. return;
  1019. } else if (!mesh) {
  1020. return;
  1021. }
  1022. this.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  1023. this.verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  1024. this._linkedMesh = mesh;
  1025. this._host._linkedControls.push(this);
  1026. }
  1027. /** @hidden */
  1028. public _moveToProjectedPosition(projectedPosition: Vector3): void {
  1029. let oldLeft = this._left.getValue(this._host);
  1030. let oldTop = this._top.getValue(this._host);
  1031. var newLeft = ((projectedPosition.x + this._linkOffsetX.getValue(this._host)) - this._currentMeasure.width / 2);
  1032. var newTop = ((projectedPosition.y + this._linkOffsetY.getValue(this._host)) - this._currentMeasure.height / 2);
  1033. if (this._left.ignoreAdaptiveScaling && this._top.ignoreAdaptiveScaling) {
  1034. if (Math.abs(newLeft - oldLeft) < 0.5) {
  1035. newLeft = oldLeft;
  1036. }
  1037. if (Math.abs(newTop - oldTop) < 0.5) {
  1038. newTop = oldTop;
  1039. }
  1040. }
  1041. this.left = newLeft + "px";
  1042. this.top = newTop + "px";
  1043. this._left.ignoreAdaptiveScaling = true;
  1044. this._top.ignoreAdaptiveScaling = true;
  1045. this._markAsDirty();
  1046. }
  1047. /** @hidden */
  1048. public _offsetLeft(offset: number) {
  1049. this._isDirty = true;
  1050. this._currentMeasure.left += offset;
  1051. }
  1052. /** @hidden */
  1053. public _offsetTop(offset: number) {
  1054. this._isDirty = true;
  1055. this._currentMeasure.top += offset;
  1056. }
  1057. /** @hidden */
  1058. public _markMatrixAsDirty(): void {
  1059. this._isMatrixDirty = true;
  1060. this._flagDescendantsAsMatrixDirty();
  1061. }
  1062. /** @hidden */
  1063. public _flagDescendantsAsMatrixDirty(): void {
  1064. // No child
  1065. }
  1066. /** @hidden */
  1067. public _intersectsRect(rect: Measure) {
  1068. // Rotate the control's current measure into local space and check if it intersects the passed in rectangle
  1069. this._currentMeasure.transformToRef(this._transformMatrix, this._tmpMeasureA);
  1070. if (this._tmpMeasureA.left >= rect.left + rect.width) {
  1071. return false;
  1072. }
  1073. if (this._tmpMeasureA.top >= rect.top + rect.height) {
  1074. return false;
  1075. }
  1076. if (this._tmpMeasureA.left + this._tmpMeasureA.width <= rect.left) {
  1077. return false;
  1078. }
  1079. if (this._tmpMeasureA.top + this._tmpMeasureA.height <= rect.top) {
  1080. return false;
  1081. }
  1082. return true;
  1083. }
  1084. /** @hidden */
  1085. protected invalidateRect() {
  1086. this._transform();
  1087. if (this.host && this.host.useInvalidateRectOptimization) {
  1088. // Rotate by transform to get the measure transformed to global space
  1089. this._currentMeasure.transformToRef(this._transformMatrix, this._tmpMeasureA);
  1090. // get the boudning box of the current measure and last frames measure in global space and invalidate it
  1091. // the previous measure is used to properly clear a control that is scaled down
  1092. Measure.CombineToRef(this._tmpMeasureA, this._prevCurrentMeasureTransformedIntoGlobalSpace, this._tmpMeasureA);
  1093. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  1094. // Expand rect based on shadows
  1095. var shadowOffsetX = this.shadowOffsetX;
  1096. var shadowOffsetY = this.shadowOffsetY;
  1097. var shadowBlur = this.shadowBlur;
  1098. var leftShadowOffset = Math.min(Math.min(shadowOffsetX, 0) - shadowBlur * 2, 0);
  1099. var rightShadowOffset = Math.max(Math.max(shadowOffsetX, 0) + shadowBlur * 2, 0);
  1100. var topShadowOffset = Math.min(Math.min(shadowOffsetY, 0) - shadowBlur * 2, 0);
  1101. var bottomShadowOffset = Math.max(Math.max(shadowOffsetY, 0) + shadowBlur * 2, 0);
  1102. this.host.invalidateRect(
  1103. Math.floor(this._tmpMeasureA.left + leftShadowOffset),
  1104. Math.floor(this._tmpMeasureA.top + topShadowOffset),
  1105. Math.ceil(this._tmpMeasureA.left + this._tmpMeasureA.width + rightShadowOffset),
  1106. Math.ceil(this._tmpMeasureA.top + this._tmpMeasureA.height + bottomShadowOffset),
  1107. );
  1108. } else {
  1109. this.host.invalidateRect(
  1110. Math.floor(this._tmpMeasureA.left),
  1111. Math.floor(this._tmpMeasureA.top),
  1112. Math.ceil(this._tmpMeasureA.left + this._tmpMeasureA.width),
  1113. Math.ceil(this._tmpMeasureA.top + this._tmpMeasureA.height),
  1114. );
  1115. }
  1116. }
  1117. }
  1118. /** @hidden */
  1119. public _markAsDirty(force = false): void {
  1120. if (!this._isVisible && !force) {
  1121. return;
  1122. }
  1123. this._isDirty = true;
  1124. // Redraw only this rectangle
  1125. if (this._host) {
  1126. this._host.markAsDirty();
  1127. }
  1128. }
  1129. /** @hidden */
  1130. public _markAllAsDirty(): void {
  1131. this._markAsDirty();
  1132. if (this._font) {
  1133. this._prepareFont();
  1134. }
  1135. }
  1136. /** @hidden */
  1137. public _link(host: AdvancedDynamicTexture): void {
  1138. this._host = host;
  1139. if (this._host) {
  1140. this.uniqueId = this._host.getScene()!.getUniqueId();
  1141. }
  1142. }
  1143. /** @hidden */
  1144. protected _transform(context?: CanvasRenderingContext2D): void {
  1145. if (!this._isMatrixDirty && this._scaleX === 1 && this._scaleY === 1 && this._rotation === 0) {
  1146. return;
  1147. }
  1148. // postTranslate
  1149. var offsetX = this._currentMeasure.width * this._transformCenterX + this._currentMeasure.left;
  1150. var offsetY = this._currentMeasure.height * this._transformCenterY + this._currentMeasure.top;
  1151. if (context) {
  1152. context.translate(offsetX, offsetY);
  1153. // rotate
  1154. context.rotate(this._rotation);
  1155. // scale
  1156. context.scale(this._scaleX, this._scaleY);
  1157. // preTranslate
  1158. context.translate(-offsetX, -offsetY);
  1159. }
  1160. // Need to update matrices?
  1161. if (this._isMatrixDirty || this._cachedOffsetX !== offsetX || this._cachedOffsetY !== offsetY) {
  1162. this._cachedOffsetX = offsetX;
  1163. this._cachedOffsetY = offsetY;
  1164. this._isMatrixDirty = false;
  1165. this._flagDescendantsAsMatrixDirty();
  1166. Matrix2D.ComposeToRef(-offsetX, -offsetY, this._rotation, this._scaleX, this._scaleY, this.parent ? this.parent._transformMatrix : null, this._transformMatrix);
  1167. this._transformMatrix.invertToRef(this._invertTransformMatrix);
  1168. }
  1169. }
  1170. /** @hidden */
  1171. public _renderHighlight(context: CanvasRenderingContext2D): void {
  1172. if (!this.isHighlighted) {
  1173. return;
  1174. }
  1175. context.save();
  1176. context.strokeStyle = "#4affff";
  1177. context.lineWidth = 2;
  1178. this._renderHighlightSpecific(context);
  1179. context.restore();
  1180. }
  1181. /** @hidden */
  1182. public _renderHighlightSpecific(context: CanvasRenderingContext2D): void {
  1183. context.strokeRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  1184. }
  1185. /** @hidden */
  1186. protected _applyStates(context: CanvasRenderingContext2D): void {
  1187. if (this._isFontSizeInPercentage) {
  1188. this._fontSet = true;
  1189. }
  1190. if (this._fontSet) {
  1191. this._prepareFont();
  1192. this._fontSet = false;
  1193. }
  1194. if (this._font) {
  1195. context.font = this._font;
  1196. }
  1197. if (this._color) {
  1198. context.fillStyle = this._color;
  1199. }
  1200. if (Control.AllowAlphaInheritance) {
  1201. context.globalAlpha *= this._alpha;
  1202. } else if (this._alphaSet) {
  1203. context.globalAlpha = this.parent ? this.parent.alpha * this._alpha : this._alpha;
  1204. }
  1205. }
  1206. /** @hidden */
  1207. public _layout(parentMeasure: Measure, context: CanvasRenderingContext2D): boolean {
  1208. if (!this.isDirty && (!this.isVisible || this.notRenderable)) {
  1209. return false;
  1210. }
  1211. if (this._isDirty || !this._cachedParentMeasure.isEqualsTo(parentMeasure)) {
  1212. this.host._numLayoutCalls++;
  1213. this._currentMeasure.addAndTransformToRef(this._transformMatrix,
  1214. -this.paddingLeftInPixels | 0,
  1215. -this.paddingTopInPixels | 0,
  1216. this.paddingRightInPixels | 0,
  1217. this.paddingBottomInPixels | 0,
  1218. this._prevCurrentMeasureTransformedIntoGlobalSpace);
  1219. context.save();
  1220. this._applyStates(context);
  1221. let rebuildCount = 0;
  1222. do {
  1223. this._rebuildLayout = false;
  1224. this._processMeasures(parentMeasure, context);
  1225. rebuildCount++;
  1226. }
  1227. while (this._rebuildLayout && rebuildCount < 3);
  1228. if (rebuildCount >= 3) {
  1229. Logger.Error(`Layout cycle detected in GUI (Control name=${this.name}, uniqueId=${this.uniqueId})`);
  1230. }
  1231. context.restore();
  1232. this.invalidateRect();
  1233. this._evaluateClippingState(parentMeasure);
  1234. }
  1235. this._wasDirty = this._isDirty;
  1236. this._isDirty = false;
  1237. return true;
  1238. }
  1239. /** @hidden */
  1240. protected _processMeasures(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1241. this._currentMeasure.copyFrom(parentMeasure);
  1242. // Let children take some pre-measurement actions
  1243. this._preMeasure(parentMeasure, context);
  1244. this._measure();
  1245. this._computeAlignment(parentMeasure, context);
  1246. // Convert to int values
  1247. this._currentMeasure.left = this._currentMeasure.left | 0;
  1248. this._currentMeasure.top = this._currentMeasure.top | 0;
  1249. this._currentMeasure.width = this._currentMeasure.width | 0;
  1250. this._currentMeasure.height = this._currentMeasure.height | 0;
  1251. // Let children add more features
  1252. this._additionalProcessing(parentMeasure, context);
  1253. this._cachedParentMeasure.copyFrom(parentMeasure);
  1254. if (this.onDirtyObservable.hasObservers()) {
  1255. this.onDirtyObservable.notifyObservers(this);
  1256. }
  1257. }
  1258. protected _evaluateClippingState(parentMeasure: Measure) {
  1259. if (this.parent && this.parent.clipChildren) {
  1260. // Early clip
  1261. if (this._currentMeasure.left > parentMeasure.left + parentMeasure.width) {
  1262. this._isClipped = true;
  1263. return;
  1264. }
  1265. if (this._currentMeasure.left + this._currentMeasure.width < parentMeasure.left) {
  1266. this._isClipped = true;
  1267. return;
  1268. }
  1269. if (this._currentMeasure.top > parentMeasure.top + parentMeasure.height) {
  1270. this._isClipped = true;
  1271. return;
  1272. }
  1273. if (this._currentMeasure.top + this._currentMeasure.height < parentMeasure.top) {
  1274. this._isClipped = true;
  1275. return;
  1276. }
  1277. }
  1278. this._isClipped = false;
  1279. }
  1280. /** @hidden */
  1281. public _measure(): void {
  1282. // Width / Height
  1283. if (this._width.isPixel) {
  1284. this._currentMeasure.width = this._width.getValue(this._host);
  1285. } else {
  1286. this._currentMeasure.width *= this._width.getValue(this._host);
  1287. }
  1288. if (this._height.isPixel) {
  1289. this._currentMeasure.height = this._height.getValue(this._host);
  1290. } else {
  1291. this._currentMeasure.height *= this._height.getValue(this._host);
  1292. }
  1293. if (this.fixedRatio !== 0) {
  1294. if (this._fixedRatioMasterIsWidth) {
  1295. this._currentMeasure.height = this._currentMeasure.width * this.fixedRatio;
  1296. } else {
  1297. this._currentMeasure.width = this._currentMeasure.height * this.fixedRatio;
  1298. }
  1299. }
  1300. }
  1301. /** @hidden */
  1302. protected _computeAlignment(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1303. var width = this._currentMeasure.width;
  1304. var height = this._currentMeasure.height;
  1305. var parentWidth = parentMeasure.width;
  1306. var parentHeight = parentMeasure.height;
  1307. // Left / top
  1308. var x = 0;
  1309. var y = 0;
  1310. switch (this.horizontalAlignment) {
  1311. case Control.HORIZONTAL_ALIGNMENT_LEFT:
  1312. x = 0;
  1313. break;
  1314. case Control.HORIZONTAL_ALIGNMENT_RIGHT:
  1315. x = parentWidth - width;
  1316. break;
  1317. case Control.HORIZONTAL_ALIGNMENT_CENTER:
  1318. x = (parentWidth - width) / 2;
  1319. break;
  1320. }
  1321. switch (this.verticalAlignment) {
  1322. case Control.VERTICAL_ALIGNMENT_TOP:
  1323. y = 0;
  1324. break;
  1325. case Control.VERTICAL_ALIGNMENT_BOTTOM:
  1326. y = parentHeight - height;
  1327. break;
  1328. case Control.VERTICAL_ALIGNMENT_CENTER:
  1329. y = (parentHeight - height) / 2;
  1330. break;
  1331. }
  1332. if (this._paddingLeft.isPixel) {
  1333. this._currentMeasure.left += this._paddingLeft.getValue(this._host);
  1334. this._currentMeasure.width -= this._paddingLeft.getValue(this._host);
  1335. } else {
  1336. this._currentMeasure.left += parentWidth * this._paddingLeft.getValue(this._host);
  1337. this._currentMeasure.width -= parentWidth * this._paddingLeft.getValue(this._host);
  1338. }
  1339. if (this._paddingRight.isPixel) {
  1340. this._currentMeasure.width -= this._paddingRight.getValue(this._host);
  1341. } else {
  1342. this._currentMeasure.width -= parentWidth * this._paddingRight.getValue(this._host);
  1343. }
  1344. if (this._paddingTop.isPixel) {
  1345. this._currentMeasure.top += this._paddingTop.getValue(this._host);
  1346. this._currentMeasure.height -= this._paddingTop.getValue(this._host);
  1347. } else {
  1348. this._currentMeasure.top += parentHeight * this._paddingTop.getValue(this._host);
  1349. this._currentMeasure.height -= parentHeight * this._paddingTop.getValue(this._host);
  1350. }
  1351. if (this._paddingBottom.isPixel) {
  1352. this._currentMeasure.height -= this._paddingBottom.getValue(this._host);
  1353. } else {
  1354. this._currentMeasure.height -= parentHeight * this._paddingBottom.getValue(this._host);
  1355. }
  1356. if (this._left.isPixel) {
  1357. this._currentMeasure.left += this._left.getValue(this._host);
  1358. } else {
  1359. this._currentMeasure.left += parentWidth * this._left.getValue(this._host);
  1360. }
  1361. if (this._top.isPixel) {
  1362. this._currentMeasure.top += this._top.getValue(this._host);
  1363. } else {
  1364. this._currentMeasure.top += parentHeight * this._top.getValue(this._host);
  1365. }
  1366. this._currentMeasure.left += x;
  1367. this._currentMeasure.top += y;
  1368. }
  1369. /** @hidden */
  1370. protected _preMeasure(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1371. // Do nothing
  1372. }
  1373. /** @hidden */
  1374. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1375. // Do nothing
  1376. }
  1377. /** @hidden */
  1378. protected _clipForChildren(context: CanvasRenderingContext2D): void {
  1379. // DO nothing
  1380. }
  1381. private static _ClipMeasure = new Measure(0, 0, 0, 0);
  1382. private _tmpMeasureA = new Measure(0, 0, 0, 0);
  1383. private _clip(context: CanvasRenderingContext2D, invalidatedRectangle?: Nullable<Measure>) {
  1384. context.beginPath();
  1385. Control._ClipMeasure.copyFrom(this._currentMeasure);
  1386. if (invalidatedRectangle) {
  1387. // Rotate the invalidated rect into the control's space
  1388. invalidatedRectangle.transformToRef(this._invertTransformMatrix, this._tmpMeasureA);
  1389. // Get the intersection of the rect in context space and the current context
  1390. var intersection = new Measure(0, 0, 0, 0);
  1391. intersection.left = Math.max(this._tmpMeasureA.left, this._currentMeasure.left);
  1392. intersection.top = Math.max(this._tmpMeasureA.top, this._currentMeasure.top);
  1393. intersection.width = Math.min(this._tmpMeasureA.left + this._tmpMeasureA.width, this._currentMeasure.left + this._currentMeasure.width) - intersection.left;
  1394. intersection.height = Math.min(this._tmpMeasureA.top + this._tmpMeasureA.height, this._currentMeasure.top + this._currentMeasure.height) - intersection.top;
  1395. Control._ClipMeasure.copyFrom(intersection);
  1396. }
  1397. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  1398. var shadowOffsetX = this.shadowOffsetX;
  1399. var shadowOffsetY = this.shadowOffsetY;
  1400. var shadowBlur = this.shadowBlur;
  1401. var leftShadowOffset = Math.min(Math.min(shadowOffsetX, 0) - shadowBlur * 2, 0);
  1402. var rightShadowOffset = Math.max(Math.max(shadowOffsetX, 0) + shadowBlur * 2, 0);
  1403. var topShadowOffset = Math.min(Math.min(shadowOffsetY, 0) - shadowBlur * 2, 0);
  1404. var bottomShadowOffset = Math.max(Math.max(shadowOffsetY, 0) + shadowBlur * 2, 0);
  1405. context.rect(
  1406. Control._ClipMeasure.left + leftShadowOffset,
  1407. Control._ClipMeasure.top + topShadowOffset,
  1408. Control._ClipMeasure.width + rightShadowOffset - leftShadowOffset,
  1409. Control._ClipMeasure.height + bottomShadowOffset - topShadowOffset
  1410. );
  1411. } else {
  1412. context.rect(Control._ClipMeasure.left, Control._ClipMeasure.top, Control._ClipMeasure.width, Control._ClipMeasure.height);
  1413. }
  1414. context.clip();
  1415. }
  1416. /** @hidden */
  1417. public _render(context: CanvasRenderingContext2D, invalidatedRectangle?: Nullable<Measure>): boolean {
  1418. if (!this.isVisible || this.notRenderable || this._isClipped) {
  1419. this._isDirty = false;
  1420. return false;
  1421. }
  1422. this.host._numRenderCalls++;
  1423. context.save();
  1424. this._applyStates(context);
  1425. // Transform
  1426. this._transform(context);
  1427. // Clip
  1428. if (this.clipContent) {
  1429. this._clip(context, invalidatedRectangle);
  1430. }
  1431. if (this.onBeforeDrawObservable.hasObservers()) {
  1432. this.onBeforeDrawObservable.notifyObservers(this);
  1433. }
  1434. if (this.useBitmapCache && !this._wasDirty && this._cacheData) {
  1435. context.putImageData(this._cacheData, this._currentMeasure.left, this._currentMeasure.top);
  1436. } else {
  1437. this._draw(context, invalidatedRectangle);
  1438. }
  1439. if (this.useBitmapCache && this._wasDirty) {
  1440. this._cacheData = context.getImageData(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  1441. }
  1442. this._renderHighlight(context);
  1443. if (this.onAfterDrawObservable.hasObservers()) {
  1444. this.onAfterDrawObservable.notifyObservers(this);
  1445. }
  1446. context.restore();
  1447. return true;
  1448. }
  1449. /** @hidden */
  1450. public _draw(context: CanvasRenderingContext2D, invalidatedRectangle?: Nullable<Measure>): void {
  1451. // Do nothing
  1452. }
  1453. /**
  1454. * Tests if a given coordinates belong to the current control
  1455. * @param x defines x coordinate to test
  1456. * @param y defines y coordinate to test
  1457. * @returns true if the coordinates are inside the control
  1458. */
  1459. public contains(x: number, y: number): boolean {
  1460. // Invert transform
  1461. this._invertTransformMatrix.transformCoordinates(x, y, this._transformedPosition);
  1462. x = this._transformedPosition.x;
  1463. y = this._transformedPosition.y;
  1464. // Check
  1465. if (x < this._currentMeasure.left) {
  1466. return false;
  1467. }
  1468. if (x > this._currentMeasure.left + this._currentMeasure.width) {
  1469. return false;
  1470. }
  1471. if (y < this._currentMeasure.top) {
  1472. return false;
  1473. }
  1474. if (y > this._currentMeasure.top + this._currentMeasure.height) {
  1475. return false;
  1476. }
  1477. if (this.isPointerBlocker) {
  1478. this._host._shouldBlockPointer = true;
  1479. }
  1480. return true;
  1481. }
  1482. /** @hidden */
  1483. public _processPicking(x: number, y: number, pi: PointerInfoBase, type: number, pointerId: number, buttonIndex: number, deltaX?: number, deltaY?: number): boolean {
  1484. if (!this._isEnabled) {
  1485. return false;
  1486. }
  1487. if (!this.isHitTestVisible || !this.isVisible || this._doNotRender) {
  1488. return false;
  1489. }
  1490. if (!this.contains(x, y)) {
  1491. return false;
  1492. }
  1493. this._processObservables(type, x, y, pi, pointerId, buttonIndex, deltaX, deltaY);
  1494. return true;
  1495. }
  1496. /** @hidden */
  1497. public _onPointerMove(target: Control, coordinates: Vector2, pointerId: number, pi: PointerInfoBase): void {
  1498. var canNotify: boolean = this.onPointerMoveObservable.notifyObservers(coordinates, -1, target, this, pi);
  1499. if (canNotify && this.parent != null) { this.parent._onPointerMove(target, coordinates, pointerId, pi); }
  1500. }
  1501. /** @hidden */
  1502. public _onPointerEnter(target: Control, pi: PointerInfoBase): boolean {
  1503. if (!this._isEnabled) {
  1504. return false;
  1505. }
  1506. if (this._enterCount > 0) {
  1507. return false;
  1508. }
  1509. if (this._enterCount === -1) { // -1 is for touch input, we are now sure we are with a mouse or pencil
  1510. this._enterCount = 0;
  1511. }
  1512. this._enterCount++;
  1513. var canNotify: boolean = this.onPointerEnterObservable.notifyObservers(this, -1, target, this, pi);
  1514. if (canNotify && this.parent != null) { this.parent._onPointerEnter(target, pi); }
  1515. return true;
  1516. }
  1517. /** @hidden */
  1518. public _onPointerOut(target: Control, pi: Nullable<PointerInfoBase>, force = false): void {
  1519. if (!force && (!this._isEnabled || target === this)) {
  1520. return;
  1521. }
  1522. this._enterCount = 0;
  1523. var canNotify: boolean = true;
  1524. if (!target.isAscendant(this)) {
  1525. canNotify = this.onPointerOutObservable.notifyObservers(this, -1, target, this, pi);
  1526. }
  1527. if (canNotify && this.parent != null) {
  1528. this.parent._onPointerOut(target, pi, force);
  1529. }
  1530. }
  1531. /** @hidden */
  1532. public _onPointerDown(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number, pi: PointerInfoBase): boolean {
  1533. // Prevent pointerout to lose control context.
  1534. // Event redundancy is checked inside the function.
  1535. this._onPointerEnter(this, pi);
  1536. if (this._downCount !== 0) {
  1537. return false;
  1538. }
  1539. this._downCount++;
  1540. this._downPointerIds[pointerId] = true;
  1541. var canNotify: boolean = this.onPointerDownObservable.notifyObservers(new Vector2WithInfo(coordinates, buttonIndex), -1, target, this, pi);
  1542. if (canNotify && this.parent != null) { this.parent._onPointerDown(target, coordinates, pointerId, buttonIndex, pi); }
  1543. return true;
  1544. }
  1545. /** @hidden */
  1546. public _onPointerUp(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number, notifyClick: boolean, pi?: PointerInfoBase): void {
  1547. if (!this._isEnabled) {
  1548. return;
  1549. }
  1550. this._downCount = 0;
  1551. delete this._downPointerIds[pointerId];
  1552. var canNotifyClick: boolean = notifyClick;
  1553. if (notifyClick && (this._enterCount > 0 || this._enterCount === -1)) {
  1554. canNotifyClick = this.onPointerClickObservable.notifyObservers(new Vector2WithInfo(coordinates, buttonIndex), -1, target, this, pi);
  1555. }
  1556. var canNotify: boolean = this.onPointerUpObservable.notifyObservers(new Vector2WithInfo(coordinates, buttonIndex), -1, target, this, pi);
  1557. if (canNotify && this.parent != null) { this.parent._onPointerUp(target, coordinates, pointerId, buttonIndex, canNotifyClick, pi); }
  1558. }
  1559. /** @hidden */
  1560. public _forcePointerUp(pointerId: Nullable<number> = null) {
  1561. if (pointerId !== null) {
  1562. this._onPointerUp(this, Vector2.Zero(), pointerId, 0, true);
  1563. } else {
  1564. for (var key in this._downPointerIds) {
  1565. this._onPointerUp(this, Vector2.Zero(), +key as number, 0, true);
  1566. }
  1567. }
  1568. }
  1569. /** @hidden */
  1570. public _onWheelScroll(deltaX?: number, deltaY?: number): void {
  1571. if (!this._isEnabled) {
  1572. return;
  1573. }
  1574. var canNotify: boolean = this.onWheelObservable.notifyObservers(new Vector2(deltaX, deltaY));
  1575. if (canNotify && this.parent != null) { this.parent._onWheelScroll(deltaX, deltaY); }
  1576. }
  1577. /** @hidden */
  1578. public _onCanvasBlur(): void {}
  1579. /** @hidden */
  1580. public _processObservables(type: number, x: number, y: number, pi: PointerInfoBase, pointerId: number, buttonIndex: number, deltaX?: number, deltaY?: number): boolean {
  1581. if (!this._isEnabled) {
  1582. return false;
  1583. }
  1584. this._dummyVector2.copyFromFloats(x, y);
  1585. if (type === PointerEventTypes.POINTERMOVE) {
  1586. this._onPointerMove(this, this._dummyVector2, pointerId, pi);
  1587. var previousControlOver = this._host._lastControlOver[pointerId];
  1588. if (previousControlOver && previousControlOver !== this) {
  1589. previousControlOver._onPointerOut(this, pi);
  1590. }
  1591. if (previousControlOver !== this) {
  1592. this._onPointerEnter(this, pi);
  1593. }
  1594. this._host._lastControlOver[pointerId] = this;
  1595. return true;
  1596. }
  1597. if (type === PointerEventTypes.POINTERDOWN) {
  1598. this._onPointerDown(this, this._dummyVector2, pointerId, buttonIndex, pi);
  1599. this._host._registerLastControlDown(this, pointerId);
  1600. this._host._lastPickedControl = this;
  1601. return true;
  1602. }
  1603. if (type === PointerEventTypes.POINTERUP) {
  1604. if (this._host._lastControlDown[pointerId]) {
  1605. this._host._lastControlDown[pointerId]._onPointerUp(this, this._dummyVector2, pointerId, buttonIndex, true, pi);
  1606. }
  1607. delete this._host._lastControlDown[pointerId];
  1608. return true;
  1609. }
  1610. if (type === PointerEventTypes.POINTERWHEEL) {
  1611. if (this._host._lastControlOver[pointerId]) {
  1612. this._host._lastControlOver[pointerId]._onWheelScroll(deltaX, deltaY);
  1613. return true;
  1614. }
  1615. }
  1616. return false;
  1617. }
  1618. private _prepareFont() {
  1619. if (!this._font && !this._fontSet) {
  1620. return;
  1621. }
  1622. if (this._style) {
  1623. this._font = this._style.fontStyle + " " + this._style.fontWeight + " " + this.fontSizeInPixels + "px " + this._style.fontFamily;
  1624. } else {
  1625. this._font = this._fontStyle + " " + this._fontWeight + " " + this.fontSizeInPixels + "px " + this._fontFamily;
  1626. }
  1627. this._fontOffset = Control._GetFontOffset(this._font);
  1628. }
  1629. /**
  1630. * Serializes the current control
  1631. * @param serializationObject defined the JSON serialized object
  1632. */
  1633. public serialize(serializationObject: any) {
  1634. SerializationHelper.Serialize(this, serializationObject);
  1635. serializationObject.name = this.name;
  1636. serializationObject.className = this.getClassName();
  1637. }
  1638. /** Releases associated resources */
  1639. public dispose() {
  1640. this.onDirtyObservable.clear();
  1641. this.onBeforeDrawObservable.clear();
  1642. this.onAfterDrawObservable.clear();
  1643. this.onPointerDownObservable.clear();
  1644. this.onPointerEnterObservable.clear();
  1645. this.onPointerMoveObservable.clear();
  1646. this.onPointerOutObservable.clear();
  1647. this.onPointerUpObservable.clear();
  1648. this.onPointerClickObservable.clear();
  1649. this.onWheelObservable.clear();
  1650. if (this._styleObserver && this._style) {
  1651. this._style.onChangedObservable.remove(this._styleObserver);
  1652. this._styleObserver = null;
  1653. }
  1654. if (this.parent) {
  1655. this.parent.removeControl(this);
  1656. this.parent = null;
  1657. }
  1658. if (this._host) {
  1659. var index = this._host._linkedControls.indexOf(this);
  1660. if (index > -1) {
  1661. this.linkWithMesh(null);
  1662. }
  1663. }
  1664. // Callback
  1665. this.onDisposeObservable.notifyObservers(this);
  1666. this.onDisposeObservable.clear();
  1667. }
  1668. // Statics
  1669. private static _HORIZONTAL_ALIGNMENT_LEFT = 0;
  1670. private static _HORIZONTAL_ALIGNMENT_RIGHT = 1;
  1671. private static _HORIZONTAL_ALIGNMENT_CENTER = 2;
  1672. private static _VERTICAL_ALIGNMENT_TOP = 0;
  1673. private static _VERTICAL_ALIGNMENT_BOTTOM = 1;
  1674. private static _VERTICAL_ALIGNMENT_CENTER = 2;
  1675. /** HORIZONTAL_ALIGNMENT_LEFT */
  1676. public static get HORIZONTAL_ALIGNMENT_LEFT(): number {
  1677. return Control._HORIZONTAL_ALIGNMENT_LEFT;
  1678. }
  1679. /** HORIZONTAL_ALIGNMENT_RIGHT */
  1680. public static get HORIZONTAL_ALIGNMENT_RIGHT(): number {
  1681. return Control._HORIZONTAL_ALIGNMENT_RIGHT;
  1682. }
  1683. /** HORIZONTAL_ALIGNMENT_CENTER */
  1684. public static get HORIZONTAL_ALIGNMENT_CENTER(): number {
  1685. return Control._HORIZONTAL_ALIGNMENT_CENTER;
  1686. }
  1687. /** VERTICAL_ALIGNMENT_TOP */
  1688. public static get VERTICAL_ALIGNMENT_TOP(): number {
  1689. return Control._VERTICAL_ALIGNMENT_TOP;
  1690. }
  1691. /** VERTICAL_ALIGNMENT_BOTTOM */
  1692. public static get VERTICAL_ALIGNMENT_BOTTOM(): number {
  1693. return Control._VERTICAL_ALIGNMENT_BOTTOM;
  1694. }
  1695. /** VERTICAL_ALIGNMENT_CENTER */
  1696. public static get VERTICAL_ALIGNMENT_CENTER(): number {
  1697. return Control._VERTICAL_ALIGNMENT_CENTER;
  1698. }
  1699. private static _FontHeightSizes: { [key: string]: { ascent: number, height: number, descent: number } } = {};
  1700. /** @hidden */
  1701. public static _GetFontOffset(font: string): { ascent: number, height: number, descent: number } {
  1702. if (Control._FontHeightSizes[font]) {
  1703. return Control._FontHeightSizes[font];
  1704. }
  1705. var text = document.createElement("span");
  1706. text.innerHTML = "Hg";
  1707. text.style.font = font;
  1708. var block = document.createElement("div");
  1709. block.style.display = "inline-block";
  1710. block.style.width = "1px";
  1711. block.style.height = "0px";
  1712. block.style.verticalAlign = "bottom";
  1713. var div = document.createElement("div");
  1714. div.style.whiteSpace = "nowrap";
  1715. div.appendChild(text);
  1716. div.appendChild(block);
  1717. document.body.appendChild(div);
  1718. var fontAscent = 0;
  1719. var fontHeight = 0;
  1720. try {
  1721. fontHeight = block.getBoundingClientRect().top - text.getBoundingClientRect().top;
  1722. block.style.verticalAlign = "baseline";
  1723. fontAscent = block.getBoundingClientRect().top - text.getBoundingClientRect().top;
  1724. } finally {
  1725. document.body.removeChild(div);
  1726. }
  1727. var result = { ascent: fontAscent, height: fontHeight, descent: fontHeight - fontAscent };
  1728. Control._FontHeightSizes[font] = result;
  1729. return result;
  1730. }
  1731. /** @hidden */
  1732. public _parseFromContent(serializedObject: any, host: AdvancedDynamicTexture) {
  1733. // Nothing here but children can overwrite it
  1734. }
  1735. /**
  1736. * Creates a Control from parsed data
  1737. * @param serializedObject defines parsed data
  1738. * @param host defines the hosting AdvancedDynamicTexture
  1739. * @returns a new Control
  1740. */
  1741. public static Parse(serializedObject: any, host: AdvancedDynamicTexture): Control {
  1742. let controlType = Tools.Instantiate("BABYLON.GUI." + serializedObject.className);
  1743. let control = SerializationHelper.Parse(() => new controlType, serializedObject, null);
  1744. control.name = serializedObject.name;
  1745. control._parseFromContent(serializedObject, host);
  1746. return control;
  1747. }
  1748. /**
  1749. * Creates a stack panel that can be used to render headers
  1750. * @param control defines the control to associate with the header
  1751. * @param text defines the text of the header
  1752. * @param size defines the size of the header
  1753. * @param options defines options used to configure the header
  1754. * @returns a new StackPanel
  1755. * @ignore
  1756. * @hidden
  1757. */
  1758. public static AddHeader: (control: Control, text: string, size: string | number, options: { isHorizontal: boolean, controlFirst: boolean }) => any = () => { };
  1759. /** @hidden */
  1760. protected static drawEllipse(x: number, y: number, width: number, height: number, context: CanvasRenderingContext2D): void {
  1761. context.translate(x, y);
  1762. context.scale(width, height);
  1763. context.beginPath();
  1764. context.arc(0, 0, 1, 0, 2 * Math.PI);
  1765. context.closePath();
  1766. context.scale(1 / width, 1 / height);
  1767. context.translate(-x, -y);
  1768. }
  1769. }
  1770. _TypeStore.RegisteredTypes["BABYLON.GUI.Control"] = Control;