control.ts 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159
  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. public get fontFamily(): string {
  477. if (!this._fontSet) {
  478. return "";
  479. }
  480. return this._fontFamily;
  481. }
  482. public set fontFamily(value: string) {
  483. if (this._fontFamily === value) {
  484. return;
  485. }
  486. this._fontFamily = value;
  487. this._resetFontCache();
  488. }
  489. /** Gets or sets font style */
  490. public get fontStyle(): string {
  491. return this._fontStyle;
  492. }
  493. public set fontStyle(value: string) {
  494. if (this._fontStyle === value) {
  495. return;
  496. }
  497. this._fontStyle = value;
  498. this._resetFontCache();
  499. }
  500. /** Gets or sets font weight */
  501. public get fontWeight(): string {
  502. return this._fontWeight;
  503. }
  504. public set fontWeight(value: string) {
  505. if (this._fontWeight === value) {
  506. return;
  507. }
  508. this._fontWeight = value;
  509. this._resetFontCache();
  510. }
  511. /**
  512. * Gets or sets style
  513. * @see https://doc.babylonjs.com/how_to/gui#styles
  514. */
  515. @serialize()
  516. public get style(): Nullable<Style> {
  517. return this._style;
  518. }
  519. public set style(value: Nullable<Style>) {
  520. if (this._style) {
  521. this._style.onChangedObservable.remove(this._styleObserver);
  522. this._styleObserver = null;
  523. }
  524. this._style = value;
  525. if (this._style) {
  526. this._styleObserver = this._style.onChangedObservable.add(() => {
  527. this._markAsDirty();
  528. this._resetFontCache();
  529. });
  530. }
  531. this._markAsDirty();
  532. this._resetFontCache();
  533. }
  534. /** @hidden */
  535. public get _isFontSizeInPercentage(): boolean {
  536. return this._fontSize.isPercentage;
  537. }
  538. /** Gets or sets font size in pixels */
  539. public get fontSizeInPixels(): number {
  540. let fontSizeToUse = this._style ? this._style._fontSize : this._fontSize;
  541. if (fontSizeToUse.isPixel) {
  542. return fontSizeToUse.getValue(this._host);
  543. }
  544. return fontSizeToUse.getValueInPixel(this._host, this._tempParentMeasure.height || this._cachedParentMeasure.height);
  545. }
  546. public set fontSizeInPixels(value: number) {
  547. if (isNaN(value)) {
  548. return;
  549. }
  550. this.fontSize = value + "px";
  551. }
  552. /** Gets or sets font size */
  553. public get fontSize(): string | number {
  554. return this._fontSize.toString(this._host);
  555. }
  556. public set fontSize(value: string | number) {
  557. if (this._fontSize.toString(this._host) === value) {
  558. return;
  559. }
  560. if (this._fontSize.fromString(value)) {
  561. this._markAsDirty();
  562. this._resetFontCache();
  563. }
  564. }
  565. /** Gets or sets foreground color */
  566. @serialize()
  567. public get color(): string {
  568. return this._color;
  569. }
  570. public set color(value: string) {
  571. if (this._color === value) {
  572. return;
  573. }
  574. this._color = value;
  575. this._markAsDirty();
  576. }
  577. /** Gets or sets z index which is used to reorder controls on the z axis */
  578. @serialize()
  579. public get zIndex(): number {
  580. return this._zIndex;
  581. }
  582. public set zIndex(value: number) {
  583. if (this.zIndex === value) {
  584. return;
  585. }
  586. this._zIndex = value;
  587. if (this.parent) {
  588. this.parent._reOrderControl(this);
  589. }
  590. }
  591. /** Gets or sets a boolean indicating if the control can be rendered */
  592. @serialize()
  593. public get notRenderable(): boolean {
  594. return this._doNotRender;
  595. }
  596. public set notRenderable(value: boolean) {
  597. if (this._doNotRender === value) {
  598. return;
  599. }
  600. this._doNotRender = value;
  601. this._markAsDirty();
  602. }
  603. /** Gets or sets a boolean indicating if the control is visible */
  604. @serialize()
  605. public get isVisible(): boolean {
  606. return this._isVisible;
  607. }
  608. public set isVisible(value: boolean) {
  609. if (this._isVisible === value) {
  610. return;
  611. }
  612. this._isVisible = value;
  613. this._markAsDirty(true);
  614. }
  615. /** Gets a boolean indicating that the control needs to update its rendering */
  616. public get isDirty(): boolean {
  617. return this._isDirty;
  618. }
  619. /**
  620. * Gets the current linked mesh (or null if none)
  621. */
  622. public get linkedMesh(): Nullable<TransformNode> {
  623. return this._linkedMesh;
  624. }
  625. /**
  626. * Gets or sets a value indicating the padding to use on the left of the control
  627. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  628. */
  629. @serialize()
  630. public get paddingLeft(): string | number {
  631. return this._paddingLeft.toString(this._host);
  632. }
  633. public set paddingLeft(value: string | number) {
  634. if (this._paddingLeft.fromString(value)) {
  635. this._markAsDirty();
  636. }
  637. }
  638. /**
  639. * Gets or sets a value indicating the padding in pixels to use on the left of the control
  640. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  641. */
  642. public get paddingLeftInPixels(): number {
  643. return this._paddingLeft.getValueInPixel(this._host, this._cachedParentMeasure.width);
  644. }
  645. public set paddingLeftInPixels(value: number) {
  646. if (isNaN(value)) {
  647. return;
  648. }
  649. this.paddingLeft = value + "px";
  650. }
  651. /**
  652. * Gets or sets a value indicating the padding to use on the right of the control
  653. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  654. */
  655. @serialize()
  656. public get paddingRight(): string | number {
  657. return this._paddingRight.toString(this._host);
  658. }
  659. public set paddingRight(value: string | number) {
  660. if (this._paddingRight.fromString(value)) {
  661. this._markAsDirty();
  662. }
  663. }
  664. /**
  665. * Gets or sets a value indicating the padding in pixels to use on the right of the control
  666. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  667. */
  668. public get paddingRightInPixels(): number {
  669. return this._paddingRight.getValueInPixel(this._host, this._cachedParentMeasure.width);
  670. }
  671. public set paddingRightInPixels(value: number) {
  672. if (isNaN(value)) {
  673. return;
  674. }
  675. this.paddingRight = value + "px";
  676. }
  677. /**
  678. * Gets or sets a value indicating the padding to use on the top of the control
  679. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  680. */
  681. @serialize()
  682. public get paddingTop(): string | number {
  683. return this._paddingTop.toString(this._host);
  684. }
  685. public set paddingTop(value: string | number) {
  686. if (this._paddingTop.fromString(value)) {
  687. this._markAsDirty();
  688. }
  689. }
  690. /**
  691. * Gets or sets a value indicating the padding in pixels to use on the top of the control
  692. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  693. */
  694. public get paddingTopInPixels(): number {
  695. return this._paddingTop.getValueInPixel(this._host, this._cachedParentMeasure.height);
  696. }
  697. public set paddingTopInPixels(value: number) {
  698. if (isNaN(value)) {
  699. return;
  700. }
  701. this.paddingTop = value + "px";
  702. }
  703. /**
  704. * Gets or sets a value indicating the padding to use on the bottom of the control
  705. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  706. */
  707. @serialize()
  708. public get paddingBottom(): string | number {
  709. return this._paddingBottom.toString(this._host);
  710. }
  711. public set paddingBottom(value: string | number) {
  712. if (this._paddingBottom.fromString(value)) {
  713. this._markAsDirty();
  714. }
  715. }
  716. /**
  717. * Gets or sets a value indicating the padding in pixels to use on the bottom of the control
  718. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  719. */
  720. public get paddingBottomInPixels(): number {
  721. return this._paddingBottom.getValueInPixel(this._host, this._cachedParentMeasure.height);
  722. }
  723. public set paddingBottomInPixels(value: number) {
  724. if (isNaN(value)) {
  725. return;
  726. }
  727. this.paddingBottom = value + "px";
  728. }
  729. /**
  730. * Gets or sets a value indicating the left coordinate of the control
  731. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  732. */
  733. @serialize()
  734. public get left(): string | number {
  735. return this._left.toString(this._host);
  736. }
  737. public set left(value: string | number) {
  738. if (this._left.fromString(value)) {
  739. this._markAsDirty();
  740. }
  741. }
  742. /**
  743. * Gets or sets a value indicating the left coordinate in pixels of the control
  744. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  745. */
  746. public get leftInPixels(): number {
  747. return this._left.getValueInPixel(this._host, this._cachedParentMeasure.width);
  748. }
  749. public set leftInPixels(value: number) {
  750. if (isNaN(value)) {
  751. return;
  752. }
  753. this.left = value + "px";
  754. }
  755. /**
  756. * Gets or sets a value indicating the top coordinate of the control
  757. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  758. */
  759. @serialize()
  760. public get top(): string | number {
  761. return this._top.toString(this._host);
  762. }
  763. public set top(value: string | number) {
  764. if (this._top.fromString(value)) {
  765. this._markAsDirty();
  766. }
  767. }
  768. /**
  769. * Gets or sets a value indicating the top coordinate in pixels of the control
  770. * @see https://doc.babylonjs.com/how_to/gui#position-and-size
  771. */
  772. public get topInPixels(): number {
  773. return this._top.getValueInPixel(this._host, this._cachedParentMeasure.height);
  774. }
  775. public set topInPixels(value: number) {
  776. if (isNaN(value)) {
  777. return;
  778. }
  779. this.top = value + "px";
  780. }
  781. /**
  782. * Gets or sets a value indicating the offset on X axis to the linked mesh
  783. * @see https://doc.babylonjs.com/how_to/gui#tracking-positions
  784. */
  785. @serialize()
  786. public get linkOffsetX(): string | number {
  787. return this._linkOffsetX.toString(this._host);
  788. }
  789. public set linkOffsetX(value: string | number) {
  790. if (this._linkOffsetX.fromString(value)) {
  791. this._markAsDirty();
  792. }
  793. }
  794. /**
  795. * Gets or sets a value indicating the offset in pixels on X axis to the linked mesh
  796. * @see https://doc.babylonjs.com/how_to/gui#tracking-positions
  797. */
  798. public get linkOffsetXInPixels(): number {
  799. return this._linkOffsetX.getValueInPixel(this._host, this._cachedParentMeasure.width);
  800. }
  801. public set linkOffsetXInPixels(value: number) {
  802. if (isNaN(value)) {
  803. return;
  804. }
  805. this.linkOffsetX = value + "px";
  806. }
  807. /**
  808. * Gets or sets a value indicating the offset on Y axis to the linked mesh
  809. * @see https://doc.babylonjs.com/how_to/gui#tracking-positions
  810. */
  811. @serialize()
  812. public get linkOffsetY(): string | number {
  813. return this._linkOffsetY.toString(this._host);
  814. }
  815. public set linkOffsetY(value: string | number) {
  816. if (this._linkOffsetY.fromString(value)) {
  817. this._markAsDirty();
  818. }
  819. }
  820. /**
  821. * Gets or sets a value indicating the offset in pixels on Y axis to the linked mesh
  822. * @see https://doc.babylonjs.com/how_to/gui#tracking-positions
  823. */
  824. public get linkOffsetYInPixels(): number {
  825. return this._linkOffsetY.getValueInPixel(this._host, this._cachedParentMeasure.height);
  826. }
  827. public set linkOffsetYInPixels(value: number) {
  828. if (isNaN(value)) {
  829. return;
  830. }
  831. this.linkOffsetY = value + "px";
  832. }
  833. /** Gets the center coordinate on X axis */
  834. public get centerX(): number {
  835. return this._currentMeasure.left + this._currentMeasure.width / 2;
  836. }
  837. /** Gets the center coordinate on Y axis */
  838. public get centerY(): number {
  839. return this._currentMeasure.top + this._currentMeasure.height / 2;
  840. }
  841. /** Gets or sets if control is Enabled */
  842. @serialize()
  843. public get isEnabled(): boolean {
  844. return this._isEnabled;
  845. }
  846. public set isEnabled(value: boolean) {
  847. if (this._isEnabled === value) {
  848. return;
  849. }
  850. this._isEnabled = value;
  851. this._markAsDirty();
  852. }
  853. /** Gets or sets background color of control if it's disabled */
  854. @serialize()
  855. public get disabledColor(): string {
  856. return this._disabledColor;
  857. }
  858. public set disabledColor(value: string) {
  859. if (this._disabledColor === value) {
  860. return;
  861. }
  862. this._disabledColor = value;
  863. this._markAsDirty();
  864. }
  865. /** Gets or sets front color of control if it's disabled */
  866. @serialize()
  867. public get disabledColorItem(): string {
  868. return this._disabledColorItem;
  869. }
  870. public set disabledColorItem(value: string) {
  871. if (this._disabledColorItem === value) {
  872. return;
  873. }
  874. this._disabledColorItem = value;
  875. this._markAsDirty();
  876. }
  877. // Functions
  878. /**
  879. * Creates a new control
  880. * @param name defines the name of the control
  881. */
  882. constructor(
  883. /** defines the name of the control */
  884. public name?: string) {
  885. }
  886. /** @hidden */
  887. protected _getTypeName(): string {
  888. return "Control";
  889. }
  890. /**
  891. * Gets the first ascendant in the hierarchy of the given type
  892. * @param className defines the required type
  893. * @returns the ascendant or null if not found
  894. */
  895. public getAscendantOfClass(className: string): Nullable<Control> {
  896. if (!this.parent) {
  897. return null;
  898. }
  899. if (this.parent.getClassName() === className) {
  900. return this.parent;
  901. }
  902. return this.parent.getAscendantOfClass(className);
  903. }
  904. /** @hidden */
  905. public _resetFontCache(): void {
  906. this._fontSet = true;
  907. this._markAsDirty();
  908. }
  909. /**
  910. * Determines if a container is an ascendant of the current control
  911. * @param container defines the container to look for
  912. * @returns true if the container is one of the ascendant of the control
  913. */
  914. public isAscendant(container: Control): boolean {
  915. if (!this.parent) {
  916. return false;
  917. }
  918. if (this.parent === container) {
  919. return true;
  920. }
  921. return this.parent.isAscendant(container);
  922. }
  923. /**
  924. * Gets coordinates in local control space
  925. * @param globalCoordinates defines the coordinates to transform
  926. * @returns the new coordinates in local space
  927. */
  928. public getLocalCoordinates(globalCoordinates: Vector2): Vector2 {
  929. var result = Vector2.Zero();
  930. this.getLocalCoordinatesToRef(globalCoordinates, result);
  931. return result;
  932. }
  933. /**
  934. * Gets coordinates in local control space
  935. * @param globalCoordinates defines the coordinates to transform
  936. * @param result defines the target vector2 where to store the result
  937. * @returns the current control
  938. */
  939. public getLocalCoordinatesToRef(globalCoordinates: Vector2, result: Vector2): Control {
  940. result.x = globalCoordinates.x - this._currentMeasure.left;
  941. result.y = globalCoordinates.y - this._currentMeasure.top;
  942. return this;
  943. }
  944. /**
  945. * Gets coordinates in parent local control space
  946. * @param globalCoordinates defines the coordinates to transform
  947. * @returns the new coordinates in parent local space
  948. */
  949. public getParentLocalCoordinates(globalCoordinates: Vector2): Vector2 {
  950. var result = Vector2.Zero();
  951. result.x = globalCoordinates.x - this._cachedParentMeasure.left;
  952. result.y = globalCoordinates.y - this._cachedParentMeasure.top;
  953. return result;
  954. }
  955. /**
  956. * Move the current control to a vector3 position projected onto the screen.
  957. * @param position defines the target position
  958. * @param scene defines the hosting scene
  959. */
  960. public moveToVector3(position: Vector3, scene: Scene): void {
  961. if (!this._host || this.parent !== this._host._rootContainer) {
  962. Tools.Error("Cannot move a control to a vector3 if the control is not at root level");
  963. return;
  964. }
  965. this.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  966. this.verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  967. var globalViewport = this._host._getGlobalViewport(scene);
  968. var projectedPosition = Vector3.Project(position, Matrix.Identity(), scene.getTransformMatrix(), globalViewport);
  969. this._moveToProjectedPosition(projectedPosition);
  970. if (projectedPosition.z < 0 || projectedPosition.z > 1) {
  971. this.notRenderable = true;
  972. return;
  973. }
  974. this.notRenderable = false;
  975. }
  976. /**
  977. * Will store all controls that have this control as ascendant in a given array
  978. * @param results defines the array where to store the descendants
  979. * @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
  980. * @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
  981. */
  982. public getDescendantsToRef(results: Control[], directDescendantsOnly: boolean = false, predicate?: (control: Control) => boolean): void {
  983. // Do nothing by default
  984. }
  985. /**
  986. * Will return all controls that have this control as ascendant
  987. * @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
  988. * @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
  989. * @return all child controls
  990. */
  991. public getDescendants(directDescendantsOnly?: boolean, predicate?: (control: Control) => boolean): Control[] {
  992. var results = new Array<Control>();
  993. this.getDescendantsToRef(results, directDescendantsOnly, predicate);
  994. return results;
  995. }
  996. /**
  997. * Link current control with a target mesh
  998. * @param mesh defines the mesh to link with
  999. * @see https://doc.babylonjs.com/how_to/gui#tracking-positions
  1000. */
  1001. public linkWithMesh(mesh: Nullable<TransformNode>): void {
  1002. if (!this._host || this.parent && this.parent !== this._host._rootContainer) {
  1003. if (mesh) {
  1004. Tools.Error("Cannot link a control to a mesh if the control is not at root level");
  1005. }
  1006. return;
  1007. }
  1008. var index = this._host._linkedControls.indexOf(this);
  1009. if (index !== -1) {
  1010. this._linkedMesh = mesh;
  1011. if (!mesh) {
  1012. this._host._linkedControls.splice(index, 1);
  1013. }
  1014. return;
  1015. } else if (!mesh) {
  1016. return;
  1017. }
  1018. this.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  1019. this.verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  1020. this._linkedMesh = mesh;
  1021. this._host._linkedControls.push(this);
  1022. }
  1023. /** @hidden */
  1024. public _moveToProjectedPosition(projectedPosition: Vector3): void {
  1025. let oldLeft = this._left.getValue(this._host);
  1026. let oldTop = this._top.getValue(this._host);
  1027. var newLeft = ((projectedPosition.x + this._linkOffsetX.getValue(this._host)) - this._currentMeasure.width / 2);
  1028. var newTop = ((projectedPosition.y + this._linkOffsetY.getValue(this._host)) - this._currentMeasure.height / 2);
  1029. if (this._left.ignoreAdaptiveScaling && this._top.ignoreAdaptiveScaling) {
  1030. if (Math.abs(newLeft - oldLeft) < 0.5) {
  1031. newLeft = oldLeft;
  1032. }
  1033. if (Math.abs(newTop - oldTop) < 0.5) {
  1034. newTop = oldTop;
  1035. }
  1036. }
  1037. this.left = newLeft + "px";
  1038. this.top = newTop + "px";
  1039. this._left.ignoreAdaptiveScaling = true;
  1040. this._top.ignoreAdaptiveScaling = true;
  1041. this._markAsDirty();
  1042. }
  1043. /** @hidden */
  1044. public _offsetLeft(offset: number) {
  1045. this._isDirty = true;
  1046. this._currentMeasure.left += offset;
  1047. }
  1048. /** @hidden */
  1049. public _offsetTop(offset: number) {
  1050. this._isDirty = true;
  1051. this._currentMeasure.top += offset;
  1052. }
  1053. /** @hidden */
  1054. public _markMatrixAsDirty(): void {
  1055. this._isMatrixDirty = true;
  1056. this._flagDescendantsAsMatrixDirty();
  1057. }
  1058. /** @hidden */
  1059. public _flagDescendantsAsMatrixDirty(): void {
  1060. // No child
  1061. }
  1062. /** @hidden */
  1063. public _intersectsRect(rect: Measure) {
  1064. // Rotate the control's current measure into local space and check if it intersects the passed in rectangle
  1065. this._currentMeasure.transformToRef(this._transformMatrix, this._tmpMeasureA);
  1066. if (this._tmpMeasureA.left >= rect.left + rect.width) {
  1067. return false;
  1068. }
  1069. if (this._tmpMeasureA.top >= rect.top + rect.height) {
  1070. return false;
  1071. }
  1072. if (this._tmpMeasureA.left + this._tmpMeasureA.width <= rect.left) {
  1073. return false;
  1074. }
  1075. if (this._tmpMeasureA.top + this._tmpMeasureA.height <= rect.top) {
  1076. return false;
  1077. }
  1078. return true;
  1079. }
  1080. /** @hidden */
  1081. protected invalidateRect() {
  1082. this._transform();
  1083. if (this.host && this.host.useInvalidateRectOptimization) {
  1084. // Rotate by transform to get the measure transformed to global space
  1085. this._currentMeasure.transformToRef(this._transformMatrix, this._tmpMeasureA);
  1086. // get the boudning box of the current measure and last frames measure in global space and invalidate it
  1087. // the previous measure is used to properly clear a control that is scaled down
  1088. Measure.CombineToRef(this._tmpMeasureA, this._prevCurrentMeasureTransformedIntoGlobalSpace, this._tmpMeasureA);
  1089. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  1090. // Expand rect based on shadows
  1091. var shadowOffsetX = this.shadowOffsetX;
  1092. var shadowOffsetY = this.shadowOffsetY;
  1093. var shadowBlur = this.shadowBlur;
  1094. var leftShadowOffset = Math.min(Math.min(shadowOffsetX, 0) - shadowBlur * 2, 0);
  1095. var rightShadowOffset = Math.max(Math.max(shadowOffsetX, 0) + shadowBlur * 2, 0);
  1096. var topShadowOffset = Math.min(Math.min(shadowOffsetY, 0) - shadowBlur * 2, 0);
  1097. var bottomShadowOffset = Math.max(Math.max(shadowOffsetY, 0) + shadowBlur * 2, 0);
  1098. this.host.invalidateRect(
  1099. Math.floor(this._tmpMeasureA.left + leftShadowOffset),
  1100. Math.floor(this._tmpMeasureA.top + topShadowOffset),
  1101. Math.ceil(this._tmpMeasureA.left + this._tmpMeasureA.width + rightShadowOffset),
  1102. Math.ceil(this._tmpMeasureA.top + this._tmpMeasureA.height + bottomShadowOffset),
  1103. );
  1104. } else {
  1105. this.host.invalidateRect(
  1106. Math.floor(this._tmpMeasureA.left),
  1107. Math.floor(this._tmpMeasureA.top),
  1108. Math.ceil(this._tmpMeasureA.left + this._tmpMeasureA.width),
  1109. Math.ceil(this._tmpMeasureA.top + this._tmpMeasureA.height),
  1110. );
  1111. }
  1112. }
  1113. }
  1114. /** @hidden */
  1115. public _markAsDirty(force = false): void {
  1116. if (!this._isVisible && !force) {
  1117. return;
  1118. }
  1119. this._isDirty = true;
  1120. // Redraw only this rectangle
  1121. if (this._host) {
  1122. this._host.markAsDirty();
  1123. }
  1124. }
  1125. /** @hidden */
  1126. public _markAllAsDirty(): void {
  1127. this._markAsDirty();
  1128. if (this._font) {
  1129. this._prepareFont();
  1130. }
  1131. }
  1132. /** @hidden */
  1133. public _link(host: AdvancedDynamicTexture): void {
  1134. this._host = host;
  1135. if (this._host) {
  1136. this.uniqueId = this._host.getScene()!.getUniqueId();
  1137. }
  1138. }
  1139. /** @hidden */
  1140. protected _transform(context?: CanvasRenderingContext2D): void {
  1141. if (!this._isMatrixDirty && this._scaleX === 1 && this._scaleY === 1 && this._rotation === 0) {
  1142. return;
  1143. }
  1144. // postTranslate
  1145. var offsetX = this._currentMeasure.width * this._transformCenterX + this._currentMeasure.left;
  1146. var offsetY = this._currentMeasure.height * this._transformCenterY + this._currentMeasure.top;
  1147. if (context) {
  1148. context.translate(offsetX, offsetY);
  1149. // rotate
  1150. context.rotate(this._rotation);
  1151. // scale
  1152. context.scale(this._scaleX, this._scaleY);
  1153. // preTranslate
  1154. context.translate(-offsetX, -offsetY);
  1155. }
  1156. // Need to update matrices?
  1157. if (this._isMatrixDirty || this._cachedOffsetX !== offsetX || this._cachedOffsetY !== offsetY) {
  1158. this._cachedOffsetX = offsetX;
  1159. this._cachedOffsetY = offsetY;
  1160. this._isMatrixDirty = false;
  1161. this._flagDescendantsAsMatrixDirty();
  1162. Matrix2D.ComposeToRef(-offsetX, -offsetY, this._rotation, this._scaleX, this._scaleY, this.parent ? this.parent._transformMatrix : null, this._transformMatrix);
  1163. this._transformMatrix.invertToRef(this._invertTransformMatrix);
  1164. }
  1165. }
  1166. /** @hidden */
  1167. public _renderHighlight(context: CanvasRenderingContext2D): void {
  1168. if (!this.isHighlighted) {
  1169. return;
  1170. }
  1171. context.save();
  1172. context.strokeStyle = "#4affff";
  1173. context.lineWidth = 2;
  1174. this._renderHighlightSpecific(context);
  1175. context.restore();
  1176. }
  1177. /** @hidden */
  1178. public _renderHighlightSpecific(context: CanvasRenderingContext2D): void {
  1179. context.strokeRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  1180. }
  1181. /** @hidden */
  1182. protected _applyStates(context: CanvasRenderingContext2D): void {
  1183. if (this._isFontSizeInPercentage) {
  1184. this._fontSet = true;
  1185. }
  1186. if (this._fontSet) {
  1187. this._prepareFont();
  1188. this._fontSet = false;
  1189. }
  1190. if (this._font) {
  1191. context.font = this._font;
  1192. }
  1193. if (this._color) {
  1194. context.fillStyle = this._color;
  1195. }
  1196. if (Control.AllowAlphaInheritance) {
  1197. context.globalAlpha *= this._alpha;
  1198. } else if (this._alphaSet) {
  1199. context.globalAlpha = this.parent ? this.parent.alpha * this._alpha : this._alpha;
  1200. }
  1201. }
  1202. /** @hidden */
  1203. public _layout(parentMeasure: Measure, context: CanvasRenderingContext2D): boolean {
  1204. if (!this.isDirty && (!this.isVisible || this.notRenderable)) {
  1205. return false;
  1206. }
  1207. if (this._isDirty || !this._cachedParentMeasure.isEqualsTo(parentMeasure)) {
  1208. this.host._numLayoutCalls++;
  1209. this._currentMeasure.addAndTransformToRef(this._transformMatrix,
  1210. -this.paddingLeftInPixels | 0,
  1211. -this.paddingTopInPixels | 0,
  1212. this.paddingRightInPixels | 0,
  1213. this.paddingBottomInPixels | 0,
  1214. this._prevCurrentMeasureTransformedIntoGlobalSpace);
  1215. context.save();
  1216. this._applyStates(context);
  1217. let rebuildCount = 0;
  1218. do {
  1219. this._rebuildLayout = false;
  1220. this._processMeasures(parentMeasure, context);
  1221. rebuildCount++;
  1222. }
  1223. while (this._rebuildLayout && rebuildCount < 3);
  1224. if (rebuildCount >= 3) {
  1225. Logger.Error(`Layout cycle detected in GUI (Control name=${this.name}, uniqueId=${this.uniqueId})`);
  1226. }
  1227. context.restore();
  1228. this.invalidateRect();
  1229. this._evaluateClippingState(parentMeasure);
  1230. }
  1231. this._wasDirty = this._isDirty;
  1232. this._isDirty = false;
  1233. return true;
  1234. }
  1235. /** @hidden */
  1236. protected _processMeasures(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1237. this._currentMeasure.copyFrom(parentMeasure);
  1238. // Let children take some pre-measurement actions
  1239. this._preMeasure(parentMeasure, context);
  1240. this._measure();
  1241. this._computeAlignment(parentMeasure, context);
  1242. // Convert to int values
  1243. this._currentMeasure.left = this._currentMeasure.left | 0;
  1244. this._currentMeasure.top = this._currentMeasure.top | 0;
  1245. this._currentMeasure.width = this._currentMeasure.width | 0;
  1246. this._currentMeasure.height = this._currentMeasure.height | 0;
  1247. // Let children add more features
  1248. this._additionalProcessing(parentMeasure, context);
  1249. this._cachedParentMeasure.copyFrom(parentMeasure);
  1250. if (this.onDirtyObservable.hasObservers()) {
  1251. this.onDirtyObservable.notifyObservers(this);
  1252. }
  1253. }
  1254. protected _evaluateClippingState(parentMeasure: Measure) {
  1255. if (this.parent && this.parent.clipChildren) {
  1256. // Early clip
  1257. if (this._currentMeasure.left > parentMeasure.left + parentMeasure.width) {
  1258. this._isClipped = true;
  1259. return;
  1260. }
  1261. if (this._currentMeasure.left + this._currentMeasure.width < parentMeasure.left) {
  1262. this._isClipped = true;
  1263. return;
  1264. }
  1265. if (this._currentMeasure.top > parentMeasure.top + parentMeasure.height) {
  1266. this._isClipped = true;
  1267. return;
  1268. }
  1269. if (this._currentMeasure.top + this._currentMeasure.height < parentMeasure.top) {
  1270. this._isClipped = true;
  1271. return;
  1272. }
  1273. }
  1274. this._isClipped = false;
  1275. }
  1276. /** @hidden */
  1277. public _measure(): void {
  1278. // Width / Height
  1279. if (this._width.isPixel) {
  1280. this._currentMeasure.width = this._width.getValue(this._host);
  1281. } else {
  1282. this._currentMeasure.width *= this._width.getValue(this._host);
  1283. }
  1284. if (this._height.isPixel) {
  1285. this._currentMeasure.height = this._height.getValue(this._host);
  1286. } else {
  1287. this._currentMeasure.height *= this._height.getValue(this._host);
  1288. }
  1289. if (this.fixedRatio !== 0) {
  1290. if (this._fixedRatioMasterIsWidth) {
  1291. this._currentMeasure.height = this._currentMeasure.width * this.fixedRatio;
  1292. } else {
  1293. this._currentMeasure.width = this._currentMeasure.height * this.fixedRatio;
  1294. }
  1295. }
  1296. }
  1297. /** @hidden */
  1298. protected _computeAlignment(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1299. var width = this._currentMeasure.width;
  1300. var height = this._currentMeasure.height;
  1301. var parentWidth = parentMeasure.width;
  1302. var parentHeight = parentMeasure.height;
  1303. // Left / top
  1304. var x = 0;
  1305. var y = 0;
  1306. switch (this.horizontalAlignment) {
  1307. case Control.HORIZONTAL_ALIGNMENT_LEFT:
  1308. x = 0;
  1309. break;
  1310. case Control.HORIZONTAL_ALIGNMENT_RIGHT:
  1311. x = parentWidth - width;
  1312. break;
  1313. case Control.HORIZONTAL_ALIGNMENT_CENTER:
  1314. x = (parentWidth - width) / 2;
  1315. break;
  1316. }
  1317. switch (this.verticalAlignment) {
  1318. case Control.VERTICAL_ALIGNMENT_TOP:
  1319. y = 0;
  1320. break;
  1321. case Control.VERTICAL_ALIGNMENT_BOTTOM:
  1322. y = parentHeight - height;
  1323. break;
  1324. case Control.VERTICAL_ALIGNMENT_CENTER:
  1325. y = (parentHeight - height) / 2;
  1326. break;
  1327. }
  1328. if (this._paddingLeft.isPixel) {
  1329. this._currentMeasure.left += this._paddingLeft.getValue(this._host);
  1330. this._currentMeasure.width -= this._paddingLeft.getValue(this._host);
  1331. } else {
  1332. this._currentMeasure.left += parentWidth * this._paddingLeft.getValue(this._host);
  1333. this._currentMeasure.width -= parentWidth * this._paddingLeft.getValue(this._host);
  1334. }
  1335. if (this._paddingRight.isPixel) {
  1336. this._currentMeasure.width -= this._paddingRight.getValue(this._host);
  1337. } else {
  1338. this._currentMeasure.width -= parentWidth * this._paddingRight.getValue(this._host);
  1339. }
  1340. if (this._paddingTop.isPixel) {
  1341. this._currentMeasure.top += this._paddingTop.getValue(this._host);
  1342. this._currentMeasure.height -= this._paddingTop.getValue(this._host);
  1343. } else {
  1344. this._currentMeasure.top += parentHeight * this._paddingTop.getValue(this._host);
  1345. this._currentMeasure.height -= parentHeight * this._paddingTop.getValue(this._host);
  1346. }
  1347. if (this._paddingBottom.isPixel) {
  1348. this._currentMeasure.height -= this._paddingBottom.getValue(this._host);
  1349. } else {
  1350. this._currentMeasure.height -= parentHeight * this._paddingBottom.getValue(this._host);
  1351. }
  1352. if (this._left.isPixel) {
  1353. this._currentMeasure.left += this._left.getValue(this._host);
  1354. } else {
  1355. this._currentMeasure.left += parentWidth * this._left.getValue(this._host);
  1356. }
  1357. if (this._top.isPixel) {
  1358. this._currentMeasure.top += this._top.getValue(this._host);
  1359. } else {
  1360. this._currentMeasure.top += parentHeight * this._top.getValue(this._host);
  1361. }
  1362. this._currentMeasure.left += x;
  1363. this._currentMeasure.top += y;
  1364. }
  1365. /** @hidden */
  1366. protected _preMeasure(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1367. // Do nothing
  1368. }
  1369. /** @hidden */
  1370. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  1371. // Do nothing
  1372. }
  1373. /** @hidden */
  1374. protected _clipForChildren(context: CanvasRenderingContext2D): void {
  1375. // DO nothing
  1376. }
  1377. private static _ClipMeasure = new Measure(0, 0, 0, 0);
  1378. private _tmpMeasureA = new Measure(0, 0, 0, 0);
  1379. private _clip(context: CanvasRenderingContext2D, invalidatedRectangle?: Nullable<Measure>) {
  1380. context.beginPath();
  1381. Control._ClipMeasure.copyFrom(this._currentMeasure);
  1382. if (invalidatedRectangle) {
  1383. // Rotate the invalidated rect into the control's space
  1384. invalidatedRectangle.transformToRef(this._invertTransformMatrix, this._tmpMeasureA);
  1385. // Get the intersection of the rect in context space and the current context
  1386. var intersection = new Measure(0, 0, 0, 0);
  1387. intersection.left = Math.max(this._tmpMeasureA.left, this._currentMeasure.left);
  1388. intersection.top = Math.max(this._tmpMeasureA.top, this._currentMeasure.top);
  1389. intersection.width = Math.min(this._tmpMeasureA.left + this._tmpMeasureA.width, this._currentMeasure.left + this._currentMeasure.width) - intersection.left;
  1390. intersection.height = Math.min(this._tmpMeasureA.top + this._tmpMeasureA.height, this._currentMeasure.top + this._currentMeasure.height) - intersection.top;
  1391. Control._ClipMeasure.copyFrom(intersection);
  1392. }
  1393. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  1394. var shadowOffsetX = this.shadowOffsetX;
  1395. var shadowOffsetY = this.shadowOffsetY;
  1396. var shadowBlur = this.shadowBlur;
  1397. var leftShadowOffset = Math.min(Math.min(shadowOffsetX, 0) - shadowBlur * 2, 0);
  1398. var rightShadowOffset = Math.max(Math.max(shadowOffsetX, 0) + shadowBlur * 2, 0);
  1399. var topShadowOffset = Math.min(Math.min(shadowOffsetY, 0) - shadowBlur * 2, 0);
  1400. var bottomShadowOffset = Math.max(Math.max(shadowOffsetY, 0) + shadowBlur * 2, 0);
  1401. context.rect(
  1402. Control._ClipMeasure.left + leftShadowOffset,
  1403. Control._ClipMeasure.top + topShadowOffset,
  1404. Control._ClipMeasure.width + rightShadowOffset - leftShadowOffset,
  1405. Control._ClipMeasure.height + bottomShadowOffset - topShadowOffset
  1406. );
  1407. } else {
  1408. context.rect(Control._ClipMeasure.left, Control._ClipMeasure.top, Control._ClipMeasure.width, Control._ClipMeasure.height);
  1409. }
  1410. context.clip();
  1411. }
  1412. /** @hidden */
  1413. public _render(context: CanvasRenderingContext2D, invalidatedRectangle?: Nullable<Measure>): boolean {
  1414. if (!this.isVisible || this.notRenderable || this._isClipped) {
  1415. this._isDirty = false;
  1416. return false;
  1417. }
  1418. this.host._numRenderCalls++;
  1419. context.save();
  1420. this._applyStates(context);
  1421. // Transform
  1422. this._transform(context);
  1423. // Clip
  1424. if (this.clipContent) {
  1425. this._clip(context, invalidatedRectangle);
  1426. }
  1427. if (this.onBeforeDrawObservable.hasObservers()) {
  1428. this.onBeforeDrawObservable.notifyObservers(this);
  1429. }
  1430. if (this.useBitmapCache && !this._wasDirty && this._cacheData) {
  1431. context.putImageData(this._cacheData, this._currentMeasure.left, this._currentMeasure.top);
  1432. } else {
  1433. this._draw(context, invalidatedRectangle);
  1434. }
  1435. if (this.useBitmapCache && this._wasDirty) {
  1436. this._cacheData = context.getImageData(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  1437. }
  1438. this._renderHighlight(context);
  1439. if (this.onAfterDrawObservable.hasObservers()) {
  1440. this.onAfterDrawObservable.notifyObservers(this);
  1441. }
  1442. context.restore();
  1443. return true;
  1444. }
  1445. /** @hidden */
  1446. public _draw(context: CanvasRenderingContext2D, invalidatedRectangle?: Nullable<Measure>): void {
  1447. // Do nothing
  1448. }
  1449. /**
  1450. * Tests if a given coordinates belong to the current control
  1451. * @param x defines x coordinate to test
  1452. * @param y defines y coordinate to test
  1453. * @returns true if the coordinates are inside the control
  1454. */
  1455. public contains(x: number, y: number): boolean {
  1456. // Invert transform
  1457. this._invertTransformMatrix.transformCoordinates(x, y, this._transformedPosition);
  1458. x = this._transformedPosition.x;
  1459. y = this._transformedPosition.y;
  1460. // Check
  1461. if (x < this._currentMeasure.left) {
  1462. return false;
  1463. }
  1464. if (x > this._currentMeasure.left + this._currentMeasure.width) {
  1465. return false;
  1466. }
  1467. if (y < this._currentMeasure.top) {
  1468. return false;
  1469. }
  1470. if (y > this._currentMeasure.top + this._currentMeasure.height) {
  1471. return false;
  1472. }
  1473. if (this.isPointerBlocker) {
  1474. this._host._shouldBlockPointer = true;
  1475. }
  1476. return true;
  1477. }
  1478. /** @hidden */
  1479. public _processPicking(x: number, y: number, pi: PointerInfoBase, type: number, pointerId: number, buttonIndex: number, deltaX?: number, deltaY?: number): boolean {
  1480. if (!this._isEnabled) {
  1481. return false;
  1482. }
  1483. if (!this.isHitTestVisible || !this.isVisible || this._doNotRender) {
  1484. return false;
  1485. }
  1486. if (!this.contains(x, y)) {
  1487. return false;
  1488. }
  1489. this._processObservables(type, x, y, pi, pointerId, buttonIndex, deltaX, deltaY);
  1490. return true;
  1491. }
  1492. /** @hidden */
  1493. public _onPointerMove(target: Control, coordinates: Vector2, pointerId: number, pi: PointerInfoBase): void {
  1494. var canNotify: boolean = this.onPointerMoveObservable.notifyObservers(coordinates, -1, target, this, pi);
  1495. if (canNotify && this.parent != null) { this.parent._onPointerMove(target, coordinates, pointerId, pi); }
  1496. }
  1497. /** @hidden */
  1498. public _onPointerEnter(target: Control, pi: PointerInfoBase): boolean {
  1499. if (!this._isEnabled) {
  1500. return false;
  1501. }
  1502. if (this._enterCount > 0) {
  1503. return false;
  1504. }
  1505. if (this._enterCount === -1) { // -1 is for touch input, we are now sure we are with a mouse or pencil
  1506. this._enterCount = 0;
  1507. }
  1508. this._enterCount++;
  1509. var canNotify: boolean = this.onPointerEnterObservable.notifyObservers(this, -1, target, this, pi);
  1510. if (canNotify && this.parent != null) { this.parent._onPointerEnter(target, pi); }
  1511. return true;
  1512. }
  1513. /** @hidden */
  1514. public _onPointerOut(target: Control, pi: Nullable<PointerInfoBase>, force = false): void {
  1515. if (!force && (!this._isEnabled || target === this)) {
  1516. return;
  1517. }
  1518. this._enterCount = 0;
  1519. var canNotify: boolean = true;
  1520. if (!target.isAscendant(this)) {
  1521. canNotify = this.onPointerOutObservable.notifyObservers(this, -1, target, this, pi);
  1522. }
  1523. if (canNotify && this.parent != null) {
  1524. this.parent._onPointerOut(target, pi, force);
  1525. }
  1526. }
  1527. /** @hidden */
  1528. public _onPointerDown(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number, pi: PointerInfoBase): boolean {
  1529. // Prevent pointerout to lose control context.
  1530. // Event redundancy is checked inside the function.
  1531. this._onPointerEnter(this, pi);
  1532. if (this._downCount !== 0) {
  1533. return false;
  1534. }
  1535. this._downCount++;
  1536. this._downPointerIds[pointerId] = true;
  1537. var canNotify: boolean = this.onPointerDownObservable.notifyObservers(new Vector2WithInfo(coordinates, buttonIndex), -1, target, this, pi);
  1538. if (canNotify && this.parent != null) { this.parent._onPointerDown(target, coordinates, pointerId, buttonIndex, pi); }
  1539. return true;
  1540. }
  1541. /** @hidden */
  1542. public _onPointerUp(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number, notifyClick: boolean, pi?: PointerInfoBase): void {
  1543. if (!this._isEnabled) {
  1544. return;
  1545. }
  1546. this._downCount = 0;
  1547. delete this._downPointerIds[pointerId];
  1548. var canNotifyClick: boolean = notifyClick;
  1549. if (notifyClick && (this._enterCount > 0 || this._enterCount === -1)) {
  1550. canNotifyClick = this.onPointerClickObservable.notifyObservers(new Vector2WithInfo(coordinates, buttonIndex), -1, target, this, pi);
  1551. }
  1552. var canNotify: boolean = this.onPointerUpObservable.notifyObservers(new Vector2WithInfo(coordinates, buttonIndex), -1, target, this, pi);
  1553. if (canNotify && this.parent != null) { this.parent._onPointerUp(target, coordinates, pointerId, buttonIndex, canNotifyClick, pi); }
  1554. }
  1555. /** @hidden */
  1556. public _forcePointerUp(pointerId: Nullable<number> = null) {
  1557. if (pointerId !== null) {
  1558. this._onPointerUp(this, Vector2.Zero(), pointerId, 0, true);
  1559. } else {
  1560. for (var key in this._downPointerIds) {
  1561. this._onPointerUp(this, Vector2.Zero(), +key as number, 0, true);
  1562. }
  1563. }
  1564. }
  1565. /** @hidden */
  1566. public _onWheelScroll(deltaX?: number, deltaY?: number): void {
  1567. if (!this._isEnabled) {
  1568. return;
  1569. }
  1570. var canNotify: boolean = this.onWheelObservable.notifyObservers(new Vector2(deltaX, deltaY));
  1571. if (canNotify && this.parent != null) { this.parent._onWheelScroll(deltaX, deltaY); }
  1572. }
  1573. /** @hidden */
  1574. public _onCanvasBlur(): void {}
  1575. /** @hidden */
  1576. public _processObservables(type: number, x: number, y: number, pi: PointerInfoBase, pointerId: number, buttonIndex: number, deltaX?: number, deltaY?: number): boolean {
  1577. if (!this._isEnabled) {
  1578. return false;
  1579. }
  1580. this._dummyVector2.copyFromFloats(x, y);
  1581. if (type === PointerEventTypes.POINTERMOVE) {
  1582. this._onPointerMove(this, this._dummyVector2, pointerId, pi);
  1583. var previousControlOver = this._host._lastControlOver[pointerId];
  1584. if (previousControlOver && previousControlOver !== this) {
  1585. previousControlOver._onPointerOut(this, pi);
  1586. }
  1587. if (previousControlOver !== this) {
  1588. this._onPointerEnter(this, pi);
  1589. }
  1590. this._host._lastControlOver[pointerId] = this;
  1591. return true;
  1592. }
  1593. if (type === PointerEventTypes.POINTERDOWN) {
  1594. this._onPointerDown(this, this._dummyVector2, pointerId, buttonIndex, pi);
  1595. this._host._registerLastControlDown(this, pointerId);
  1596. this._host._lastPickedControl = this;
  1597. return true;
  1598. }
  1599. if (type === PointerEventTypes.POINTERUP) {
  1600. if (this._host._lastControlDown[pointerId]) {
  1601. this._host._lastControlDown[pointerId]._onPointerUp(this, this._dummyVector2, pointerId, buttonIndex, true, pi);
  1602. }
  1603. delete this._host._lastControlDown[pointerId];
  1604. return true;
  1605. }
  1606. if (type === PointerEventTypes.POINTERWHEEL) {
  1607. if (this._host._lastControlOver[pointerId]) {
  1608. this._host._lastControlOver[pointerId]._onWheelScroll(deltaX, deltaY);
  1609. return true;
  1610. }
  1611. }
  1612. return false;
  1613. }
  1614. private _prepareFont() {
  1615. if (!this._font && !this._fontSet) {
  1616. return;
  1617. }
  1618. if (this._style) {
  1619. this._font = this._style.fontStyle + " " + this._style.fontWeight + " " + this.fontSizeInPixels + "px " + this._style.fontFamily;
  1620. } else {
  1621. this._font = this._fontStyle + " " + this._fontWeight + " " + this.fontSizeInPixels + "px " + this._fontFamily;
  1622. }
  1623. this._fontOffset = Control._GetFontOffset(this._font);
  1624. }
  1625. /**
  1626. * Serializes the current control
  1627. * @param serializationObject defined the JSON serialized object
  1628. */
  1629. public serialize(serializationObject: any) {
  1630. SerializationHelper.Serialize(this, serializationObject);
  1631. serializationObject.name = this.name;
  1632. serializationObject.className = this.getClassName();
  1633. if (this._font) {
  1634. serializationObject.fontFamily = this.fontFamily;
  1635. serializationObject.fontSize = this.fontSize;
  1636. serializationObject.fontWeight = this.fontWeight;
  1637. serializationObject.fontStyle = this.fontStyle;
  1638. }
  1639. }
  1640. /** @hidden */
  1641. public _parseFromContent(serializedObject: any, host: AdvancedDynamicTexture) {
  1642. if (serializedObject.fontFamily) {
  1643. this.fontFamily = serializedObject.fontFamily;
  1644. }
  1645. if (serializedObject.fontSize) {
  1646. this.fontSize = serializedObject.fontSize;
  1647. }
  1648. if (serializedObject.fontWeight) {
  1649. this.fontWeight = serializedObject.fontWeight;
  1650. }
  1651. if (serializedObject.fontStyle) {
  1652. this.fontStyle = serializedObject.fontStyle;
  1653. }
  1654. }
  1655. /** Releases associated resources */
  1656. public dispose() {
  1657. this.onDirtyObservable.clear();
  1658. this.onBeforeDrawObservable.clear();
  1659. this.onAfterDrawObservable.clear();
  1660. this.onPointerDownObservable.clear();
  1661. this.onPointerEnterObservable.clear();
  1662. this.onPointerMoveObservable.clear();
  1663. this.onPointerOutObservable.clear();
  1664. this.onPointerUpObservable.clear();
  1665. this.onPointerClickObservable.clear();
  1666. this.onWheelObservable.clear();
  1667. if (this._styleObserver && this._style) {
  1668. this._style.onChangedObservable.remove(this._styleObserver);
  1669. this._styleObserver = null;
  1670. }
  1671. if (this.parent) {
  1672. this.parent.removeControl(this);
  1673. this.parent = null;
  1674. }
  1675. if (this._host) {
  1676. var index = this._host._linkedControls.indexOf(this);
  1677. if (index > -1) {
  1678. this.linkWithMesh(null);
  1679. }
  1680. }
  1681. // Callback
  1682. this.onDisposeObservable.notifyObservers(this);
  1683. this.onDisposeObservable.clear();
  1684. }
  1685. // Statics
  1686. private static _HORIZONTAL_ALIGNMENT_LEFT = 0;
  1687. private static _HORIZONTAL_ALIGNMENT_RIGHT = 1;
  1688. private static _HORIZONTAL_ALIGNMENT_CENTER = 2;
  1689. private static _VERTICAL_ALIGNMENT_TOP = 0;
  1690. private static _VERTICAL_ALIGNMENT_BOTTOM = 1;
  1691. private static _VERTICAL_ALIGNMENT_CENTER = 2;
  1692. /** HORIZONTAL_ALIGNMENT_LEFT */
  1693. public static get HORIZONTAL_ALIGNMENT_LEFT(): number {
  1694. return Control._HORIZONTAL_ALIGNMENT_LEFT;
  1695. }
  1696. /** HORIZONTAL_ALIGNMENT_RIGHT */
  1697. public static get HORIZONTAL_ALIGNMENT_RIGHT(): number {
  1698. return Control._HORIZONTAL_ALIGNMENT_RIGHT;
  1699. }
  1700. /** HORIZONTAL_ALIGNMENT_CENTER */
  1701. public static get HORIZONTAL_ALIGNMENT_CENTER(): number {
  1702. return Control._HORIZONTAL_ALIGNMENT_CENTER;
  1703. }
  1704. /** VERTICAL_ALIGNMENT_TOP */
  1705. public static get VERTICAL_ALIGNMENT_TOP(): number {
  1706. return Control._VERTICAL_ALIGNMENT_TOP;
  1707. }
  1708. /** VERTICAL_ALIGNMENT_BOTTOM */
  1709. public static get VERTICAL_ALIGNMENT_BOTTOM(): number {
  1710. return Control._VERTICAL_ALIGNMENT_BOTTOM;
  1711. }
  1712. /** VERTICAL_ALIGNMENT_CENTER */
  1713. public static get VERTICAL_ALIGNMENT_CENTER(): number {
  1714. return Control._VERTICAL_ALIGNMENT_CENTER;
  1715. }
  1716. private static _FontHeightSizes: { [key: string]: { ascent: number, height: number, descent: number } } = {};
  1717. /** @hidden */
  1718. public static _GetFontOffset(font: string): { ascent: number, height: number, descent: number } {
  1719. if (Control._FontHeightSizes[font]) {
  1720. return Control._FontHeightSizes[font];
  1721. }
  1722. var text = document.createElement("span");
  1723. text.innerHTML = "Hg";
  1724. text.style.font = font;
  1725. var block = document.createElement("div");
  1726. block.style.display = "inline-block";
  1727. block.style.width = "1px";
  1728. block.style.height = "0px";
  1729. block.style.verticalAlign = "bottom";
  1730. var div = document.createElement("div");
  1731. div.style.whiteSpace = "nowrap";
  1732. div.appendChild(text);
  1733. div.appendChild(block);
  1734. document.body.appendChild(div);
  1735. var fontAscent = 0;
  1736. var fontHeight = 0;
  1737. try {
  1738. fontHeight = block.getBoundingClientRect().top - text.getBoundingClientRect().top;
  1739. block.style.verticalAlign = "baseline";
  1740. fontAscent = block.getBoundingClientRect().top - text.getBoundingClientRect().top;
  1741. } finally {
  1742. document.body.removeChild(div);
  1743. }
  1744. var result = { ascent: fontAscent, height: fontHeight, descent: fontHeight - fontAscent };
  1745. Control._FontHeightSizes[font] = result;
  1746. return result;
  1747. }
  1748. /**
  1749. * Creates a Control from parsed data
  1750. * @param serializedObject defines parsed data
  1751. * @param host defines the hosting AdvancedDynamicTexture
  1752. * @returns a new Control
  1753. */
  1754. public static Parse(serializedObject: any, host: AdvancedDynamicTexture): Control {
  1755. let controlType = Tools.Instantiate("BABYLON.GUI." + serializedObject.className);
  1756. let control = SerializationHelper.Parse(() => new controlType(), serializedObject, null);
  1757. control.name = serializedObject.name;
  1758. control._parseFromContent(serializedObject, host);
  1759. return control;
  1760. }
  1761. /**
  1762. * Creates a stack panel that can be used to render headers
  1763. * @param control defines the control to associate with the header
  1764. * @param text defines the text of the header
  1765. * @param size defines the size of the header
  1766. * @param options defines options used to configure the header
  1767. * @returns a new StackPanel
  1768. * @ignore
  1769. * @hidden
  1770. */
  1771. public static AddHeader: (control: Control, text: string, size: string | number, options: { isHorizontal: boolean, controlFirst: boolean }) => any = () => { };
  1772. /** @hidden */
  1773. protected static drawEllipse(x: number, y: number, width: number, height: number, context: CanvasRenderingContext2D): void {
  1774. context.translate(x, y);
  1775. context.scale(width, height);
  1776. context.beginPath();
  1777. context.arc(0, 0, 1, 0, 2 * Math.PI);
  1778. context.closePath();
  1779. context.scale(1 / width, 1 / height);
  1780. context.translate(-x, -y);
  1781. }
  1782. }
  1783. _TypeStore.RegisteredTypes["BABYLON.GUI.Control"] = Control;