control.ts 34 KB

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