control.ts 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class Control {
  4. private _alpha = 1;
  5. private _alphaSet = false;
  6. private _zIndex = 0;
  7. public _root: Nullable<Container>;
  8. public _host: AdvancedDynamicTexture;
  9. public parent: Nullable<Container>;
  10. public _currentMeasure = Measure.Empty();
  11. private _fontFamily = "Arial";
  12. private _fontStyle = "";
  13. private _fontSize = new ValueAndUnit(18, ValueAndUnit.UNITMODE_PIXEL, false);
  14. private _font: string;
  15. public _width = new ValueAndUnit(1, ValueAndUnit.UNITMODE_PERCENTAGE, false);
  16. public _height = new ValueAndUnit(1, ValueAndUnit.UNITMODE_PERCENTAGE, false);
  17. protected _fontOffset: { ascent: number, height: number, descent: number };
  18. private _color = "";
  19. protected _horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_CENTER;
  20. protected _verticalAlignment = Control.VERTICAL_ALIGNMENT_CENTER;
  21. private _isDirty = true;
  22. protected _cachedParentMeasure = Measure.Empty();
  23. private _paddingLeft = new ValueAndUnit(0);
  24. private _paddingRight = new ValueAndUnit(0);
  25. private _paddingTop = new ValueAndUnit(0);
  26. private _paddingBottom = new ValueAndUnit(0);
  27. public _left = new ValueAndUnit(0);
  28. public _top = new ValueAndUnit(0);
  29. private _scaleX = 1.0;
  30. private _scaleY = 1.0;
  31. private _rotation = 0;
  32. private _transformCenterX = 0.5;
  33. private _transformCenterY = 0.5;
  34. private _transformMatrix = Matrix2D.Identity();
  35. protected _invertTransformMatrix = Matrix2D.Identity();
  36. protected _transformedPosition = Vector2.Zero();
  37. private _isMatrixDirty = true;
  38. private _cachedOffsetX: number;
  39. private _cachedOffsetY: number;
  40. private _isVisible = true;
  41. public _linkedMesh: Nullable<AbstractMesh>;
  42. private _fontSet = false;
  43. private _dummyVector2 = Vector2.Zero();
  44. private _downCount = 0;
  45. private _enterCount = 0;
  46. private _doNotRender = false;
  47. public isHitTestVisible = true;
  48. public isPointerBlocker = false;
  49. public isFocusInvisible = false;
  50. public shadowOffsetX = 0;
  51. public shadowOffsetY = 0;
  52. public shadowBlur = 0;
  53. public shadowColor = '#000';
  54. protected _linkOffsetX = new ValueAndUnit(0);
  55. protected _linkOffsetY = new ValueAndUnit(0);
  56. // Properties
  57. public get typeName(): string {
  58. return this._getTypeName();
  59. }
  60. /**
  61. * An event triggered when the pointer move over the control.
  62. * @type {BABYLON.Observable}
  63. */
  64. public onPointerMoveObservable = new Observable<Vector2>();
  65. /**
  66. * An event triggered when the pointer move out of the control.
  67. * @type {BABYLON.Observable}
  68. */
  69. public onPointerOutObservable = new Observable<Control>();
  70. /**
  71. * An event triggered when the pointer taps the control
  72. * @type {BABYLON.Observable}
  73. */
  74. public onPointerDownObservable = new Observable<Vector2WithInfo>();
  75. /**
  76. * An event triggered when pointer up
  77. * @type {BABYLON.Observable}
  78. */
  79. public onPointerUpObservable = new Observable<Vector2WithInfo>();
  80. /**
  81. * An event triggered when pointer enters the control
  82. * @type {BABYLON.Observable}
  83. */
  84. public onPointerEnterObservable = new Observable<Control>();
  85. /**
  86. * An event triggered when the control is marked as dirty
  87. * @type {BABYLON.Observable}
  88. */
  89. public onDirtyObservable = new Observable<Control>();
  90. /**
  91. * An event triggered after the control is drawn
  92. * @type {BABYLON.Observable}
  93. */
  94. public onAfterDrawObservable = new Observable<Control>();
  95. public get alpha(): number {
  96. return this._alpha;
  97. }
  98. public set alpha(value: number) {
  99. if (this._alpha === value) {
  100. return;
  101. }
  102. this._alphaSet = true;
  103. this._alpha = value;
  104. this._markAsDirty();
  105. }
  106. public get scaleX(): number {
  107. return this._scaleX;
  108. }
  109. public set scaleX(value: number) {
  110. if (this._scaleX === value) {
  111. return;
  112. }
  113. this._scaleX = value;
  114. this._markAsDirty();
  115. this._markMatrixAsDirty();
  116. }
  117. public get scaleY(): number {
  118. return this._scaleY;
  119. }
  120. public set scaleY(value: number) {
  121. if (this._scaleY === value) {
  122. return;
  123. }
  124. this._scaleY = value;
  125. this._markAsDirty();
  126. this._markMatrixAsDirty();
  127. }
  128. public get rotation(): number {
  129. return this._rotation;
  130. }
  131. public set rotation(value: number) {
  132. if (this._rotation === value) {
  133. return;
  134. }
  135. this._rotation = value;
  136. this._markAsDirty();
  137. this._markMatrixAsDirty();
  138. }
  139. public get transformCenterY(): number {
  140. return this._transformCenterY;
  141. }
  142. public set transformCenterY(value: number) {
  143. if (this._transformCenterY === value) {
  144. return;
  145. }
  146. this._transformCenterY = value;
  147. this._markAsDirty();
  148. this._markMatrixAsDirty();
  149. }
  150. public get transformCenterX(): number {
  151. return this._transformCenterX;
  152. }
  153. public set transformCenterX(value: number) {
  154. if (this._transformCenterX === value) {
  155. return;
  156. }
  157. this._transformCenterX = value;
  158. this._markAsDirty();
  159. this._markMatrixAsDirty();
  160. }
  161. public get horizontalAlignment(): number {
  162. return this._horizontalAlignment;
  163. }
  164. public set horizontalAlignment(value: number) {
  165. if (this._horizontalAlignment === value) {
  166. return;
  167. }
  168. this._horizontalAlignment = value;
  169. this._markAsDirty();
  170. }
  171. public get verticalAlignment(): number {
  172. return this._verticalAlignment;
  173. }
  174. public set verticalAlignment(value: number) {
  175. if (this._verticalAlignment === value) {
  176. return;
  177. }
  178. this._verticalAlignment = value;
  179. this._markAsDirty();
  180. }
  181. public get width(): string | number {
  182. return this._width.toString(this._host);
  183. }
  184. public get widthInPixels(): number {
  185. return this._width.getValueInPixel(this._host, this._cachedParentMeasure.width);
  186. }
  187. public set width(value: string | number) {
  188. if (this._width.toString(this._host) === value) {
  189. return;
  190. }
  191. if (this._width.fromString(value)) {
  192. this._markAsDirty();
  193. }
  194. }
  195. public get height(): string | number {
  196. return this._height.toString(this._host);
  197. }
  198. public get heightInPixels(): number {
  199. return this._height.getValueInPixel(this._host, this._cachedParentMeasure.height);
  200. }
  201. public set height(value: string | number) {
  202. if (this._height.toString(this._host) === value) {
  203. return;
  204. }
  205. if (this._height.fromString(value)) {
  206. this._markAsDirty();
  207. }
  208. }
  209. public get fontFamily(): string {
  210. return this._fontFamily;
  211. }
  212. public set fontFamily(value: string) {
  213. if (this._fontFamily === value) {
  214. return;
  215. }
  216. this._fontFamily = value;
  217. this._fontSet = true;
  218. }
  219. public get fontStyle(): string {
  220. return this._fontStyle;
  221. }
  222. public set fontStyle(value: string) {
  223. if (this._fontStyle === value) {
  224. return;
  225. }
  226. this._fontStyle = value;
  227. this._fontSet = true;
  228. }
  229. public get fontSizeInPixels(): number {
  230. return this._fontSize.getValueInPixel(this._host, 100);
  231. }
  232. public get fontSize(): string | number {
  233. return this._fontSize.toString(this._host);
  234. }
  235. public set fontSize(value: string | number) {
  236. if (this._fontSize.toString(this._host) === value) {
  237. return;
  238. }
  239. if (this._fontSize.fromString(value)) {
  240. this._markAsDirty();
  241. this._fontSet = true;
  242. }
  243. }
  244. public get color(): string {
  245. return this._color;
  246. }
  247. public set color(value: string) {
  248. if (this._color === value) {
  249. return;
  250. }
  251. this._color = value;
  252. this._markAsDirty();
  253. }
  254. public get zIndex(): number {
  255. return this._zIndex;
  256. }
  257. public set zIndex(value: number) {
  258. if (this.zIndex === value) {
  259. return;
  260. }
  261. this._zIndex = value;
  262. if (this._root) {
  263. this._root._reOrderControl(this);
  264. }
  265. }
  266. public get notRenderable(): boolean {
  267. return this._doNotRender;
  268. }
  269. public set notRenderable(value: boolean) {
  270. if (this._doNotRender === value) {
  271. return;
  272. }
  273. this._doNotRender = value;
  274. this._markAsDirty();
  275. }
  276. public get isVisible(): boolean {
  277. return this._isVisible;
  278. }
  279. public set isVisible(value: boolean) {
  280. if (this._isVisible === value) {
  281. return;
  282. }
  283. this._isVisible = value;
  284. this._markAsDirty();
  285. }
  286. public get isDirty(): boolean {
  287. return this._isDirty;
  288. }
  289. public get paddingLeft(): string | number {
  290. return this._paddingLeft.toString(this._host);
  291. }
  292. public get paddingLeftInPixels(): number {
  293. return this._paddingLeft.getValueInPixel(this._host, this._cachedParentMeasure.width);
  294. }
  295. public set paddingLeft(value: string | number) {
  296. if (this._paddingLeft.fromString(value)) {
  297. this._markAsDirty();
  298. }
  299. }
  300. public get paddingRight(): string | number {
  301. return this._paddingRight.toString(this._host);
  302. }
  303. public get paddingRightInPixels(): number {
  304. return this._paddingRight.getValueInPixel(this._host, this._cachedParentMeasure.width);
  305. }
  306. public set paddingRight(value: string | number) {
  307. if (this._paddingRight.fromString(value)) {
  308. this._markAsDirty();
  309. }
  310. }
  311. public get paddingTop(): string | number {
  312. return this._paddingTop.toString(this._host);
  313. }
  314. public get paddingTopInPixels(): number {
  315. return this._paddingTop.getValueInPixel(this._host, this._cachedParentMeasure.height);
  316. }
  317. public set paddingTop(value: string | number) {
  318. if (this._paddingTop.fromString(value)) {
  319. this._markAsDirty();
  320. }
  321. }
  322. public get paddingBottom(): string | number {
  323. return this._paddingBottom.toString(this._host);
  324. }
  325. public get paddingBottomInPixels(): number {
  326. return this._paddingBottom.getValueInPixel(this._host, this._cachedParentMeasure.height);
  327. }
  328. public set paddingBottom(value: string | number) {
  329. if (this._paddingBottom.fromString(value)) {
  330. this._markAsDirty();
  331. }
  332. }
  333. public get left(): string | number {
  334. return this._left.toString(this._host);
  335. }
  336. public get leftInPixels(): number {
  337. return this._left.getValueInPixel(this._host, this._cachedParentMeasure.width);
  338. }
  339. public set left(value: string | number) {
  340. if (this._left.fromString(value)) {
  341. this._markAsDirty();
  342. }
  343. }
  344. public get top(): string | number {
  345. return this._top.toString(this._host);
  346. }
  347. public get topInPixels(): number {
  348. return this._top.getValueInPixel(this._host, this._cachedParentMeasure.height);
  349. }
  350. public set top(value: string | number) {
  351. if (this._top.fromString(value)) {
  352. this._markAsDirty();
  353. }
  354. }
  355. public get linkOffsetX(): string | number {
  356. return this._linkOffsetX.toString(this._host);
  357. }
  358. public get linkOffsetXInPixels(): number {
  359. return this._linkOffsetX.getValueInPixel(this._host, this._cachedParentMeasure.width);
  360. }
  361. public set linkOffsetX(value: string | number) {
  362. if (this._linkOffsetX.fromString(value)) {
  363. this._markAsDirty();
  364. }
  365. }
  366. public get linkOffsetY(): string | number {
  367. return this._linkOffsetY.toString(this._host);
  368. }
  369. public get linkOffsetYInPixels(): number {
  370. return this._linkOffsetY.getValueInPixel(this._host, this._cachedParentMeasure.height);
  371. }
  372. public set linkOffsetY(value: string | number) {
  373. if (this._linkOffsetY.fromString(value)) {
  374. this._markAsDirty();
  375. }
  376. }
  377. public get centerX(): number {
  378. return this._currentMeasure.left + this._currentMeasure.width / 2;
  379. }
  380. public get centerY(): number {
  381. return this._currentMeasure.top + this._currentMeasure.height / 2;
  382. }
  383. // Functions
  384. constructor(public name?: string) {
  385. }
  386. protected _getTypeName(): string {
  387. return "Control";
  388. }
  389. public getLocalCoordinates(globalCoordinates: Vector2): Vector2 {
  390. var result = Vector2.Zero();
  391. this.getLocalCoordinatesToRef(globalCoordinates, result);
  392. return result;
  393. }
  394. public getLocalCoordinatesToRef(globalCoordinates: Vector2, result: Vector2): Control {
  395. result.x = globalCoordinates.x - this._currentMeasure.left;
  396. result.y = globalCoordinates.y - this._currentMeasure.top;
  397. return this;
  398. }
  399. public getParentLocalCoordinates(globalCoordinates: Vector2): Vector2 {
  400. var result = Vector2.Zero();
  401. result.x = globalCoordinates.x - this._cachedParentMeasure.left;
  402. result.y = globalCoordinates.y - this._cachedParentMeasure.top;
  403. return result;
  404. }
  405. public moveToVector3(position: Vector3, scene: Scene): void {
  406. if (!this._host || this._root !== this._host._rootContainer) {
  407. Tools.Error("Cannot move a control to a vector3 if the control is not at root level");
  408. return;
  409. }
  410. this.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  411. this.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
  412. var globalViewport = this._host._getGlobalViewport(scene);
  413. var projectedPosition = Vector3.Project(position, Matrix.Identity(), scene.getTransformMatrix(), globalViewport);
  414. this._moveToProjectedPosition(projectedPosition);
  415. if (projectedPosition.z < 0 || projectedPosition.z > 1) {
  416. this.notRenderable = true;
  417. return;
  418. }
  419. this.notRenderable = false;
  420. }
  421. public linkWithMesh(mesh: Nullable<AbstractMesh>): void {
  422. if (!this._host || this._root && this._root !== this._host._rootContainer) {
  423. Tools.Error("Cannot link a control to a mesh if the control is not at root level");
  424. return;
  425. }
  426. var index = this._host._linkedControls.indexOf(this);
  427. if (index !== -1) {
  428. this._linkedMesh = mesh;
  429. if (!mesh) {
  430. this._host._linkedControls.splice(index, 1);
  431. }
  432. return;
  433. }
  434. this.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  435. this.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
  436. this._linkedMesh = mesh;
  437. this._host._linkedControls.push(this);
  438. }
  439. public _moveToProjectedPosition(projectedPosition: Vector3): void {
  440. this.left = ((projectedPosition.x + this._linkOffsetX.getValue(this._host)) - this._currentMeasure.width / 2) + "px";
  441. this.top = ((projectedPosition.y + this._linkOffsetY.getValue(this._host)) - this._currentMeasure.height / 2) + "px";
  442. this._left.ignoreAdaptiveScaling = true;
  443. this._top.ignoreAdaptiveScaling = true;
  444. }
  445. public _markMatrixAsDirty(): void {
  446. this._isMatrixDirty = true;
  447. this._markAsDirty();
  448. }
  449. public _markAsDirty(): void {
  450. this._isDirty = true;
  451. if (!this._host) {
  452. return; // Not yet connected
  453. }
  454. this._host.markAsDirty();
  455. }
  456. public _markAllAsDirty(): void {
  457. this._markAsDirty();
  458. if (this._font) {
  459. this._prepareFont();
  460. }
  461. }
  462. public _link(root: Nullable<Container>, host: AdvancedDynamicTexture): void {
  463. this._root = root;
  464. this._host = host;
  465. }
  466. protected _transform(context: CanvasRenderingContext2D): void {
  467. if (!this._isMatrixDirty && this._scaleX === 1 && this._scaleY === 1 && this._rotation === 0) {
  468. return;
  469. }
  470. // postTranslate
  471. var offsetX = this._currentMeasure.width * this._transformCenterX + this._currentMeasure.left;
  472. var offsetY = this._currentMeasure.height * this._transformCenterY + this._currentMeasure.top;
  473. context.translate(offsetX, offsetY);
  474. // rotate
  475. context.rotate(this._rotation);
  476. // scale
  477. context.scale(this._scaleX, this._scaleY);
  478. // preTranslate
  479. context.translate(-offsetX, -offsetY);
  480. // Need to update matrices?
  481. if (this._isMatrixDirty || this._cachedOffsetX !== offsetX || this._cachedOffsetY !== offsetY) {
  482. this._cachedOffsetX = offsetX;
  483. this._cachedOffsetY = offsetY;
  484. this._isMatrixDirty = false;
  485. Matrix2D.ComposeToRef(-offsetX, -offsetY, this._rotation, this._scaleX, this._scaleY, this._root ? this._root._transformMatrix : null, this._transformMatrix);
  486. this._transformMatrix.invertToRef(this._invertTransformMatrix);
  487. }
  488. }
  489. protected _applyStates(context: CanvasRenderingContext2D): void {
  490. if (this._fontSet) {
  491. this._prepareFont();
  492. this._fontSet = false;
  493. }
  494. if (this._font) {
  495. context.font = this._font;
  496. }
  497. if (this._color) {
  498. context.fillStyle = this._color;
  499. }
  500. if (this._alphaSet) {
  501. context.globalAlpha = this._alpha;
  502. }
  503. }
  504. protected _processMeasures(parentMeasure: Measure, context: CanvasRenderingContext2D): boolean {
  505. if (this._isDirty || !this._cachedParentMeasure.isEqualsTo(parentMeasure)) {
  506. this._isDirty = false;
  507. this._currentMeasure.copyFrom(parentMeasure);
  508. // Let children take some pre-measurement actions
  509. this._preMeasure(parentMeasure, context);
  510. this._measure();
  511. this._computeAlignment(parentMeasure, context);
  512. // Convert to int values
  513. this._currentMeasure.left = this._currentMeasure.left | 0;
  514. this._currentMeasure.top = this._currentMeasure.top | 0;
  515. this._currentMeasure.width = this._currentMeasure.width | 0;
  516. this._currentMeasure.height = this._currentMeasure.height | 0;
  517. // Let children add more features
  518. this._additionalProcessing(parentMeasure, context);
  519. this._cachedParentMeasure.copyFrom(parentMeasure);
  520. if (this.onDirtyObservable.hasObservers()) {
  521. this.onDirtyObservable.notifyObservers(this);
  522. }
  523. }
  524. if (this._currentMeasure.left > parentMeasure.left + parentMeasure.width) {
  525. return false;
  526. }
  527. if (this._currentMeasure.left + this._currentMeasure.width < parentMeasure.left) {
  528. return false;
  529. }
  530. if (this._currentMeasure.top > parentMeasure.top + parentMeasure.height) {
  531. return false;
  532. }
  533. if (this._currentMeasure.top + this._currentMeasure.height < parentMeasure.top) {
  534. return false;
  535. }
  536. // Transform
  537. this._transform(context);
  538. // Clip
  539. this._clip(context);
  540. context.clip();
  541. return true;
  542. }
  543. protected _clip(context: CanvasRenderingContext2D) {
  544. context.beginPath();
  545. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  546. var shadowOffsetX = this.shadowOffsetX;
  547. var shadowOffsetY = this.shadowOffsetY;
  548. var shadowBlur = this.shadowBlur;
  549. var leftShadowOffset = Math.min(Math.min(shadowOffsetX, 0) - shadowBlur * 2, 0);
  550. var rightShadowOffset = Math.max(Math.max(shadowOffsetX, 0) + shadowBlur * 2, 0);
  551. var topShadowOffset = Math.min(Math.min(shadowOffsetY, 0) - shadowBlur * 2, 0);
  552. var bottomShadowOffset = Math.max(Math.max(shadowOffsetY, 0) + shadowBlur * 2, 0);
  553. context.rect(this._currentMeasure.left + leftShadowOffset,
  554. this._currentMeasure.top + topShadowOffset,
  555. this._currentMeasure.width + rightShadowOffset - leftShadowOffset,
  556. this._currentMeasure.height + bottomShadowOffset - topShadowOffset);
  557. } else {
  558. context.rect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  559. }
  560. }
  561. public _measure(): void {
  562. // Width / Height
  563. if (this._width.isPixel) {
  564. this._currentMeasure.width = this._width.getValue(this._host);
  565. } else {
  566. this._currentMeasure.width *= this._width.getValue(this._host);
  567. }
  568. if (this._height.isPixel) {
  569. this._currentMeasure.height = this._height.getValue(this._host);
  570. } else {
  571. this._currentMeasure.height *= this._height.getValue(this._host);
  572. }
  573. }
  574. protected _computeAlignment(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  575. var width = this._currentMeasure.width;
  576. var height = this._currentMeasure.height;
  577. var parentWidth = parentMeasure.width;
  578. var parentHeight = parentMeasure.height;
  579. // Left / top
  580. var x = 0;
  581. var y = 0;
  582. switch (this.horizontalAlignment) {
  583. case Control.HORIZONTAL_ALIGNMENT_LEFT:
  584. x = 0
  585. break;
  586. case Control.HORIZONTAL_ALIGNMENT_RIGHT:
  587. x = parentWidth - width;
  588. break;
  589. case Control.HORIZONTAL_ALIGNMENT_CENTER:
  590. x = (parentWidth - width) / 2;
  591. break;
  592. }
  593. switch (this.verticalAlignment) {
  594. case Control.VERTICAL_ALIGNMENT_TOP:
  595. y = 0;
  596. break;
  597. case Control.VERTICAL_ALIGNMENT_BOTTOM:
  598. y = parentHeight - height;
  599. break;
  600. case Control.VERTICAL_ALIGNMENT_CENTER:
  601. y = (parentHeight - height) / 2;
  602. break;
  603. }
  604. if (this._paddingLeft.isPixel) {
  605. this._currentMeasure.left += this._paddingLeft.getValue(this._host);
  606. this._currentMeasure.width -= this._paddingLeft.getValue(this._host);
  607. } else {
  608. this._currentMeasure.left += parentWidth * this._paddingLeft.getValue(this._host);
  609. this._currentMeasure.width -= parentWidth * this._paddingLeft.getValue(this._host);
  610. }
  611. if (this._paddingRight.isPixel) {
  612. this._currentMeasure.width -= this._paddingRight.getValue(this._host);
  613. } else {
  614. this._currentMeasure.width -= parentWidth * this._paddingRight.getValue(this._host);
  615. }
  616. if (this._paddingTop.isPixel) {
  617. this._currentMeasure.top += this._paddingTop.getValue(this._host);
  618. this._currentMeasure.height -= this._paddingTop.getValue(this._host);
  619. } else {
  620. this._currentMeasure.top += parentHeight * this._paddingTop.getValue(this._host);
  621. this._currentMeasure.height -= parentHeight * this._paddingTop.getValue(this._host);
  622. }
  623. if (this._paddingBottom.isPixel) {
  624. this._currentMeasure.height -= this._paddingBottom.getValue(this._host);
  625. } else {
  626. this._currentMeasure.height -= parentHeight * this._paddingBottom.getValue(this._host);
  627. }
  628. if (this._left.isPixel) {
  629. this._currentMeasure.left += this._left.getValue(this._host);
  630. } else {
  631. this._currentMeasure.left += parentWidth * this._left.getValue(this._host);
  632. }
  633. if (this._top.isPixel) {
  634. this._currentMeasure.top += this._top.getValue(this._host);
  635. } else {
  636. this._currentMeasure.top += parentHeight * this._top.getValue(this._host);
  637. }
  638. this._currentMeasure.left += x;
  639. this._currentMeasure.top += y;
  640. }
  641. protected _preMeasure(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  642. // Do nothing
  643. }
  644. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  645. // Do nothing
  646. }
  647. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  648. // Do nothing
  649. }
  650. public contains(x: number, y: number): boolean {
  651. // Invert transform
  652. this._invertTransformMatrix.transformCoordinates(x, y, this._transformedPosition);
  653. x = this._transformedPosition.x;
  654. y = this._transformedPosition.y;
  655. // Check
  656. if (x < this._currentMeasure.left) {
  657. return false;
  658. }
  659. if (x > this._currentMeasure.left + this._currentMeasure.width) {
  660. return false;
  661. }
  662. if (y < this._currentMeasure.top) {
  663. return false;
  664. }
  665. if (y > this._currentMeasure.top + this._currentMeasure.height) {
  666. return false;
  667. }
  668. if (this.isPointerBlocker) {
  669. this._host._shouldBlockPointer = true;
  670. }
  671. return true;
  672. }
  673. public _processPicking(x: number, y: number, type: number, buttonIndex: number): boolean {
  674. if (!this.isHitTestVisible || !this.isVisible || this._doNotRender) {
  675. return false;
  676. }
  677. if (!this.contains(x, y)) {
  678. return false;
  679. }
  680. this._processObservables(type, x, y, buttonIndex);
  681. return true;
  682. }
  683. public _onPointerMove(target: Control, coordinates: Vector2): void {
  684. var canNotify: boolean = this.onPointerMoveObservable.notifyObservers(coordinates, -1, target, this);
  685. if (canNotify && this.parent != null) this.parent._onPointerMove(target, coordinates);
  686. }
  687. public _onPointerEnter(target: Control): boolean {
  688. if (this._enterCount !== 0) {
  689. return false;
  690. }
  691. this._enterCount++;
  692. var canNotify: boolean = this.onPointerEnterObservable.notifyObservers(this, -1, target, this);
  693. if (canNotify && this.parent != null) this.parent._onPointerEnter(target);
  694. return true;
  695. }
  696. public _onPointerOut(target: Control): void {
  697. this._enterCount = 0;
  698. var canNotify: boolean = this.onPointerOutObservable.notifyObservers(this, -1, target, this);
  699. if (canNotify && this.parent != null) this.parent._onPointerOut(target);
  700. }
  701. public _onPointerDown(target: Control, coordinates: Vector2, buttonIndex: number): boolean {
  702. if (this._downCount !== 0) {
  703. return false;
  704. }
  705. this._downCount++;
  706. var canNotify: boolean = this.onPointerDownObservable.notifyObservers(new Vector2WithInfo(coordinates, buttonIndex), -1, target, this);
  707. if (canNotify && this.parent != null) this.parent._onPointerDown(target, coordinates, buttonIndex);
  708. return true;
  709. }
  710. public _onPointerUp(target: Control, coordinates: Vector2, buttonIndex: number): void {
  711. this._downCount = 0;
  712. var canNotify: boolean = this.onPointerUpObservable.notifyObservers(new Vector2WithInfo(coordinates, buttonIndex), -1, target, this);
  713. if (canNotify && this.parent != null) this.parent._onPointerUp(target, coordinates, buttonIndex);
  714. }
  715. public forcePointerUp() {
  716. this._onPointerUp(this, Vector2.Zero(), 0);
  717. }
  718. public _processObservables(type: number, x: number, y: number, buttonIndex: number): boolean {
  719. this._dummyVector2.copyFromFloats(x, y);
  720. if (type === BABYLON.PointerEventTypes.POINTERMOVE) {
  721. this._onPointerMove(this, this._dummyVector2);
  722. var previousControlOver = this._host._lastControlOver;
  723. if (previousControlOver && previousControlOver !== this) {
  724. previousControlOver._onPointerOut(this);
  725. }
  726. if (previousControlOver !== this) {
  727. this._onPointerEnter(this);
  728. }
  729. this._host._lastControlOver = this;
  730. return true;
  731. }
  732. if (type === BABYLON.PointerEventTypes.POINTERDOWN) {
  733. this._onPointerDown(this, this._dummyVector2, buttonIndex);
  734. this._host._lastControlDown = this;
  735. this._host._lastPickedControl = this;
  736. return true;
  737. }
  738. if (type === BABYLON.PointerEventTypes.POINTERUP) {
  739. if (this._host._lastControlDown) {
  740. this._host._lastControlDown._onPointerUp(this, this._dummyVector2, buttonIndex);
  741. }
  742. this._host._lastControlDown = null;
  743. return true;
  744. }
  745. return false;
  746. }
  747. private _prepareFont() {
  748. if (!this._font && !this._fontSet) {
  749. return;
  750. }
  751. this._font = this._fontStyle + " " + this._fontSize.getValue(this._host) + "px " + this._fontFamily;
  752. this._fontOffset = Control._GetFontOffset(this._font);
  753. }
  754. public dispose() {
  755. this.onDirtyObservable.clear();
  756. this.onAfterDrawObservable.clear();
  757. this.onPointerDownObservable.clear();
  758. this.onPointerEnterObservable.clear();
  759. this.onPointerMoveObservable.clear();
  760. this.onPointerOutObservable.clear();
  761. this.onPointerUpObservable.clear();
  762. if (this._root) {
  763. this._root.removeControl(this);
  764. this._root = null;
  765. }
  766. var index = this._host._linkedControls.indexOf(this);
  767. if (index > -1) {
  768. this.linkWithMesh(null);
  769. }
  770. }
  771. // Statics
  772. private static _HORIZONTAL_ALIGNMENT_LEFT = 0;
  773. private static _HORIZONTAL_ALIGNMENT_RIGHT = 1;
  774. private static _HORIZONTAL_ALIGNMENT_CENTER = 2;
  775. private static _VERTICAL_ALIGNMENT_TOP = 0;
  776. private static _VERTICAL_ALIGNMENT_BOTTOM = 1;
  777. private static _VERTICAL_ALIGNMENT_CENTER = 2;
  778. public static get HORIZONTAL_ALIGNMENT_LEFT(): number {
  779. return Control._HORIZONTAL_ALIGNMENT_LEFT;
  780. }
  781. public static get HORIZONTAL_ALIGNMENT_RIGHT(): number {
  782. return Control._HORIZONTAL_ALIGNMENT_RIGHT;
  783. }
  784. public static get HORIZONTAL_ALIGNMENT_CENTER(): number {
  785. return Control._HORIZONTAL_ALIGNMENT_CENTER;
  786. }
  787. public static get VERTICAL_ALIGNMENT_TOP(): number {
  788. return Control._VERTICAL_ALIGNMENT_TOP;
  789. }
  790. public static get VERTICAL_ALIGNMENT_BOTTOM(): number {
  791. return Control._VERTICAL_ALIGNMENT_BOTTOM;
  792. }
  793. public static get VERTICAL_ALIGNMENT_CENTER(): number {
  794. return Control._VERTICAL_ALIGNMENT_CENTER;
  795. }
  796. private static _FontHeightSizes: { [key: string]: { ascent: number, height: number, descent: number } } = {};
  797. public static _GetFontOffset(font: string): { ascent: number, height: number, descent: number } {
  798. if (Control._FontHeightSizes[font]) {
  799. return Control._FontHeightSizes[font];
  800. }
  801. var text = document.createElement("span");
  802. text.innerHTML = "Hg";
  803. text.style.font = font;
  804. var block = document.createElement("div");
  805. block.style.display = "inline-block";
  806. block.style.width = "1px";
  807. block.style.height = "0px";
  808. block.style.verticalAlign = "bottom";
  809. var div = document.createElement("div");
  810. div.appendChild(text);
  811. div.appendChild(block);
  812. document.body.appendChild(div);
  813. var fontAscent = 0;
  814. var fontHeight = 0;
  815. try {
  816. fontHeight = block.getBoundingClientRect().top - text.getBoundingClientRect().top;
  817. block.style.verticalAlign = "baseline";
  818. fontAscent = block.getBoundingClientRect().top - text.getBoundingClientRect().top;
  819. } finally {
  820. document.body.removeChild(div);
  821. }
  822. var result = { ascent: fontAscent, height: fontHeight, descent: fontHeight - fontAscent };
  823. Control._FontHeightSizes[font] = result;
  824. return result;
  825. };
  826. public static AddHeader(control: Control, text: string, size: string | number, options: { isHorizontal: boolean, controlFirst: boolean }): StackPanel {
  827. let panel = new BABYLON.GUI.StackPanel("panel");
  828. let isHorizontal = options ? options.isHorizontal : true;
  829. let controlFirst = options ? options.controlFirst : true;
  830. panel.isVertical = !isHorizontal;
  831. let header = new BABYLON.GUI.TextBlock("header");
  832. header.text = text;
  833. header.textHorizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  834. if (isHorizontal) {
  835. header.width = size;
  836. } else {
  837. header.height = size;
  838. }
  839. if (controlFirst) {
  840. panel.addControl(control);
  841. panel.addControl(header);
  842. header.paddingLeft = "5px";
  843. } else {
  844. panel.addControl(header);
  845. panel.addControl(control);
  846. header.paddingRight = "5px";
  847. }
  848. header.shadowBlur = control.shadowBlur;
  849. header.shadowColor = control.shadowColor;
  850. header.shadowOffsetX = control.shadowOffsetX;
  851. header.shadowOffsetY = control.shadowOffsetY;
  852. return panel;
  853. }
  854. protected static drawEllipse(x: number, y: number, width: number, height: number, context: CanvasRenderingContext2D): void {
  855. context.translate(x, y);
  856. context.scale(width, height);
  857. context.beginPath();
  858. context.arc(0, 0, 1, 0, 2 * Math.PI);
  859. context.closePath();
  860. context.scale(1 / width, 1 / height);
  861. context.translate(-x, -y);
  862. }
  863. }
  864. }