control.ts 32 KB

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