control.ts 59 KB

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