control.ts 53 KB

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