control.ts 66 KB

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