control.ts 52 KB

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