control.ts 36 KB

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