control.ts 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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 moveToVector3(position: Vector3, scene: Scene): void {
  325. if (!this._host || this._root !== this._host._rootContainer) {
  326. Tools.Error("Cannot move a control to a vector3 if the control is not at root level");
  327. return;
  328. }
  329. this.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  330. this.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
  331. var engine = scene.getEngine();
  332. var globalViewport = this._host._getGlobalViewport(scene);
  333. var projectedPosition = Vector3.Project(position, Matrix.Identity(), scene.getTransformMatrix(), globalViewport);
  334. this._moveToProjectedPosition(projectedPosition);
  335. if (projectedPosition.z < 0 || projectedPosition.z > 1) {
  336. this.isVisible = false;
  337. return;
  338. }
  339. this.isVisible = true;
  340. }
  341. public linkWithMesh(mesh: AbstractMesh): void {
  342. if (!this._host || this._root !== this._host._rootContainer) {
  343. Tools.Error("Cannot link a control to a mesh if the control is not at root level");
  344. return;
  345. }
  346. if (this._host._linkedControls.indexOf(this) !== -1) {
  347. return;
  348. }
  349. this.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  350. this.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
  351. this._linkedMesh = mesh;
  352. this._host._linkedControls.push(this);
  353. }
  354. public _moveToProjectedPosition(projectedPosition: Vector3): void {
  355. this.left = ((projectedPosition.x + this._linkOffsetX.getValue(this._host)) - this._currentMeasure.width / 2) + "px";
  356. this.top = ((projectedPosition.y + this._linkOffsetY.getValue(this._host)) - this._currentMeasure.height / 2) + "px";
  357. this._left.ignoreAdaptiveScaling = true;
  358. this._top.ignoreAdaptiveScaling = true;
  359. }
  360. public _markMatrixAsDirty(): void {
  361. this._isMatrixDirty = true;
  362. this._markAsDirty();
  363. }
  364. public _markAsDirty(): void {
  365. this._isDirty = true;
  366. if (!this._host) {
  367. return; // Not yet connected
  368. }
  369. this._host.markAsDirty();
  370. }
  371. public _markAllAsDirty(): void {
  372. this._markAsDirty();
  373. if (this._font) {
  374. this._prepareFont();
  375. }
  376. }
  377. public _link(root: Container, host: AdvancedDynamicTexture): void {
  378. this._root = root;
  379. this._host = host;
  380. }
  381. protected _transform(context: CanvasRenderingContext2D): void {
  382. if (!this._isMatrixDirty && this._scaleX === 1 && this._scaleY ===1 && this._rotation === 0) {
  383. return;
  384. }
  385. // postTranslate
  386. var offsetX = this._currentMeasure.width * this._transformCenterX + this._currentMeasure.left;
  387. var offsetY = this._currentMeasure.height * this._transformCenterY + this._currentMeasure.top;
  388. context.translate(offsetX, offsetY);
  389. // rotate
  390. context.rotate(this._rotation);
  391. // scale
  392. context.scale(this._scaleX, this._scaleY);
  393. // preTranslate
  394. context.translate(-offsetX, -offsetY);
  395. // Need to update matrices?
  396. if (this._isMatrixDirty || this._cachedOffsetX !== offsetX || this._cachedOffsetY !== offsetY) {
  397. this._cachedOffsetX = offsetX;
  398. this._cachedOffsetY = offsetY;
  399. this._isMatrixDirty = false;
  400. Matrix2D.ComposeToRef(-offsetX, -offsetY, this._rotation, this._scaleX, this._scaleY, this._root ? this._root._transformMatrix : null, this._transformMatrix);
  401. this._transformMatrix.invertToRef(this._invertTransformMatrix);
  402. }
  403. }
  404. protected _applyStates(context: CanvasRenderingContext2D): void {
  405. if (this._fontSet) {
  406. this._fontSet = false;
  407. this._prepareFont();
  408. }
  409. if (this._font) {
  410. context.font = this._font;
  411. }
  412. if (this._color) {
  413. context.fillStyle = this._color;
  414. }
  415. if (this._alphaSet) {
  416. context.globalAlpha = this._alpha;
  417. }
  418. }
  419. protected _processMeasures(parentMeasure: Measure, context: CanvasRenderingContext2D): boolean {
  420. if (this._isDirty || !this._cachedParentMeasure.isEqualsTo(parentMeasure)) {
  421. this._isDirty = false;
  422. this._currentMeasure.copyFrom(parentMeasure);
  423. // Let children take some pre-measurement actions
  424. this._preMeasure(parentMeasure, context);
  425. this._measure();
  426. this._computeAlignment(parentMeasure, context);
  427. // Convert to int values
  428. this._currentMeasure.left = this._currentMeasure.left | 0;
  429. this._currentMeasure.top = this._currentMeasure.top | 0;
  430. this._currentMeasure.width = this._currentMeasure.width | 0;
  431. this._currentMeasure.height = this._currentMeasure.height | 0;
  432. // Let children add more features
  433. this._additionalProcessing(parentMeasure, context);
  434. this._cachedParentMeasure.copyFrom(parentMeasure);
  435. if (this.onDirtyObservable.hasObservers()) {
  436. this.onDirtyObservable.notifyObservers(this);
  437. }
  438. }
  439. if (this._currentMeasure.left > parentMeasure.left + parentMeasure.width) {
  440. return false;
  441. }
  442. if (this._currentMeasure.left + this._currentMeasure.width < parentMeasure.left) {
  443. return false;
  444. }
  445. if (this._currentMeasure.top > parentMeasure.top + parentMeasure.height) {
  446. return false;
  447. }
  448. if (this._currentMeasure.top + this._currentMeasure.height < parentMeasure.top) {
  449. return false;
  450. }
  451. // Transform
  452. this._transform(context);
  453. // Clip
  454. this._clip(context);
  455. context.clip();
  456. return true;
  457. }
  458. protected _clip( context: CanvasRenderingContext2D) {
  459. context.beginPath();
  460. context.rect(this._currentMeasure.left ,this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  461. }
  462. public _measure(): void {
  463. // Width / Height
  464. if (this._width.isPixel) {
  465. this._currentMeasure.width = this._width.getValue(this._host);
  466. } else {
  467. this._currentMeasure.width *= this._width.getValue(this._host);
  468. }
  469. if (this._height.isPixel) {
  470. this._currentMeasure.height = this._height.getValue(this._host);
  471. } else {
  472. this._currentMeasure.height *= this._height.getValue(this._host);
  473. }
  474. }
  475. protected _computeAlignment(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  476. var width = this._currentMeasure.width;
  477. var height = this._currentMeasure.height;
  478. var parentWidth = parentMeasure.width;
  479. var parentHeight = parentMeasure.height;
  480. // Left / top
  481. var x = 0;
  482. var y = 0;
  483. switch (this.horizontalAlignment) {
  484. case Control.HORIZONTAL_ALIGNMENT_LEFT:
  485. x = 0
  486. break;
  487. case Control.HORIZONTAL_ALIGNMENT_RIGHT:
  488. x = parentWidth - width;
  489. break;
  490. case Control.HORIZONTAL_ALIGNMENT_CENTER:
  491. x = (parentWidth - width) / 2;
  492. break;
  493. }
  494. switch (this.verticalAlignment) {
  495. case Control.VERTICAL_ALIGNMENT_TOP:
  496. y = 0;
  497. break;
  498. case Control.VERTICAL_ALIGNMENT_BOTTOM:
  499. y = parentHeight - height;
  500. break;
  501. case Control.VERTICAL_ALIGNMENT_CENTER:
  502. y = (parentHeight - height) / 2;
  503. break;
  504. }
  505. if (this._paddingLeft.isPixel) {
  506. this._currentMeasure.left += this._paddingLeft.getValue(this._host);
  507. this._currentMeasure.width -= this._paddingLeft.getValue(this._host);
  508. } else {
  509. this._currentMeasure.left += parentWidth * this._paddingLeft.getValue(this._host);
  510. this._currentMeasure.width -= parentWidth * this._paddingLeft.getValue(this._host);
  511. }
  512. if (this._paddingRight.isPixel) {
  513. this._currentMeasure.width -= this._paddingRight.getValue(this._host);
  514. } else {
  515. this._currentMeasure.width -= parentWidth * this._paddingRight.getValue(this._host);
  516. }
  517. if (this._paddingTop.isPixel) {
  518. this._currentMeasure.top += this._paddingTop.getValue(this._host);
  519. this._currentMeasure.height -= this._paddingTop.getValue(this._host);
  520. } else {
  521. this._currentMeasure.top += parentHeight * this._paddingTop.getValue(this._host);
  522. this._currentMeasure.height -= parentHeight * this._paddingTop.getValue(this._host);
  523. }
  524. if (this._paddingBottom.isPixel) {
  525. this._currentMeasure.height -= this._paddingBottom.getValue(this._host);
  526. } else {
  527. this._currentMeasure.height -= parentHeight * this._paddingBottom.getValue(this._host);
  528. }
  529. if (this._left.isPixel) {
  530. this._currentMeasure.left += this._left.getValue(this._host);
  531. } else {
  532. this._currentMeasure.left += parentWidth * this._left.getValue(this._host);
  533. }
  534. if (this._top.isPixel) {
  535. this._currentMeasure.top += this._top.getValue(this._host);
  536. } else {
  537. this._currentMeasure.top += parentHeight * this._top.getValue(this._host);
  538. }
  539. this._currentMeasure.left += x;
  540. this._currentMeasure.top += y;
  541. }
  542. protected _preMeasure(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  543. // Do nothing
  544. }
  545. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  546. // Do nothing
  547. }
  548. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  549. // Do nothing
  550. }
  551. public contains(x: number, y: number) : boolean {
  552. // Invert transform
  553. this._invertTransformMatrix.transformCoordinates(x, y, this._transformedPosition);
  554. x = this._transformedPosition.x;
  555. y = this._transformedPosition.y;
  556. // Check
  557. if (x < this._currentMeasure.left) {
  558. return false;
  559. }
  560. if (x > this._currentMeasure.left + this._currentMeasure.width) {
  561. return false;
  562. }
  563. if (y < this._currentMeasure.top) {
  564. return false;
  565. }
  566. if (y > this._currentMeasure.top + this._currentMeasure.height) {
  567. return false;
  568. }
  569. if (this.isPointerBlocker) {
  570. this._host._shouldBlockPointer = true;
  571. }
  572. return true;
  573. }
  574. public _processPicking(x: number, y: number, type: number): boolean {
  575. if (!this.isHitTestVisible || !this.isVisible) {
  576. return false;
  577. }
  578. if (!this.contains(x, y)) {
  579. return false;
  580. }
  581. this._processObservables(type, x, y);
  582. return true;
  583. }
  584. protected _onPointerMove(coordinates: Vector2): void {
  585. if (this.onPointerMoveObservable.hasObservers()) {
  586. this.onPointerMoveObservable.notifyObservers(coordinates);
  587. }
  588. }
  589. protected _onPointerEnter(): boolean {
  590. if (this._enterCount !== 0) {
  591. return false;
  592. }
  593. this._enterCount++;
  594. if (this.onPointerEnterObservable.hasObservers()) {
  595. this.onPointerEnterObservable.notifyObservers(this);
  596. }
  597. return true;
  598. }
  599. protected _onPointerOut(): void {
  600. this._enterCount = 0;
  601. if (this.onPointerOutObservable.hasObservers()) {
  602. this.onPointerOutObservable.notifyObservers(this);
  603. }
  604. }
  605. protected _onPointerDown(coordinates: Vector2): boolean {
  606. if (this._downCount !== 0) {
  607. return false;
  608. }
  609. this._downCount++;
  610. if (this.onPointerDownObservable.hasObservers()) {
  611. this.onPointerDownObservable.notifyObservers(coordinates);
  612. }
  613. return true;
  614. }
  615. protected _onPointerUp(coordinates: Vector2): void {
  616. this._downCount = 0;
  617. if (this.onPointerUpObservable.hasObservers()) {
  618. this.onPointerUpObservable.notifyObservers(coordinates);
  619. }
  620. }
  621. public forcePointerUp() {
  622. this._onPointerUp(Vector2.Zero());
  623. }
  624. public _processObservables(type: number, x: number, y: number): boolean {
  625. this._dummyVector2.copyFromFloats(x, y);
  626. if (type === BABYLON.PointerEventTypes.POINTERMOVE) {
  627. this._onPointerMove(this._dummyVector2);
  628. var previousControlOver = this._host._lastControlOver;
  629. if (previousControlOver && previousControlOver !== this) {
  630. previousControlOver._onPointerOut();
  631. }
  632. if (previousControlOver !== this) {
  633. this._onPointerEnter();
  634. }
  635. this._host._lastControlOver = this;
  636. return true;
  637. }
  638. if (type === BABYLON.PointerEventTypes.POINTERDOWN) {
  639. this._onPointerDown(this._dummyVector2);
  640. this._host._lastControlDown = this;
  641. return true;
  642. }
  643. if (type === BABYLON.PointerEventTypes.POINTERUP) {
  644. if (this._host._lastControlDown) {
  645. this._host._lastControlDown._onPointerUp(this._dummyVector2);
  646. }
  647. this._host._lastControlDown = null;
  648. return true;
  649. }
  650. return false;
  651. }
  652. private _prepareFont() {
  653. if (!this._fontFamily) {
  654. return;
  655. }
  656. this._font = this._fontSize.getValue(this._host) + "px " + this._fontFamily;
  657. this._fontOffset = Control._GetFontOffset(this._font);
  658. }
  659. // Statics
  660. private static _HORIZONTAL_ALIGNMENT_LEFT = 0;
  661. private static _HORIZONTAL_ALIGNMENT_RIGHT = 1;
  662. private static _HORIZONTAL_ALIGNMENT_CENTER = 2;
  663. private static _VERTICAL_ALIGNMENT_TOP = 0;
  664. private static _VERTICAL_ALIGNMENT_BOTTOM = 1;
  665. private static _VERTICAL_ALIGNMENT_CENTER = 2;
  666. public static get HORIZONTAL_ALIGNMENT_LEFT(): number {
  667. return Control._HORIZONTAL_ALIGNMENT_LEFT;
  668. }
  669. public static get HORIZONTAL_ALIGNMENT_RIGHT(): number {
  670. return Control._HORIZONTAL_ALIGNMENT_RIGHT;
  671. }
  672. public static get HORIZONTAL_ALIGNMENT_CENTER(): number {
  673. return Control._HORIZONTAL_ALIGNMENT_CENTER;
  674. }
  675. public static get VERTICAL_ALIGNMENT_TOP(): number {
  676. return Control._VERTICAL_ALIGNMENT_TOP;
  677. }
  678. public static get VERTICAL_ALIGNMENT_BOTTOM(): number {
  679. return Control._VERTICAL_ALIGNMENT_BOTTOM;
  680. }
  681. public static get VERTICAL_ALIGNMENT_CENTER(): number {
  682. return Control._VERTICAL_ALIGNMENT_CENTER;
  683. }
  684. private static _FontHeightSizes = {};
  685. public static _GetFontOffset(font: string): {ascent: number, height: number, descent: number} {
  686. if (Control._FontHeightSizes[font]) {
  687. return Control._FontHeightSizes[font];
  688. }
  689. var text = document.createElement("span");
  690. text.innerHTML = "Hg";
  691. text.style.font = font;
  692. var block = document.createElement("div");
  693. block.style.display = "inline-block";
  694. block.style.width = "1px";
  695. block.style.height = "0px";
  696. block.style.verticalAlign = "bottom";
  697. var div = document.createElement("div");
  698. div.appendChild(text);
  699. div.appendChild(block);
  700. document.body.appendChild(div);
  701. var fontAscent = 0;
  702. var fontHeight = 0;
  703. try {
  704. fontHeight = block.getBoundingClientRect().top - text.getBoundingClientRect().top;
  705. block.style.verticalAlign = "baseline";
  706. fontAscent = block.getBoundingClientRect().top - text.getBoundingClientRect().top;
  707. } finally {
  708. document.body.removeChild(div);
  709. }
  710. var result = { ascent: fontAscent, height: fontHeight, descent: fontHeight - fontAscent };
  711. Control._FontHeightSizes[font] = result;
  712. return result;
  713. };
  714. public static AddHeader(control: Control, text: string, size: string | number, options: { isHorizontal: boolean, controlFirst: boolean }): StackPanel {
  715. let panel = new BABYLON.GUI.StackPanel("panel");
  716. let isHorizontal = options ? options.isHorizontal : true;
  717. let controlFirst = options ? options.controlFirst : true;
  718. panel.isVertical = !isHorizontal;
  719. let header = new BABYLON.GUI.TextBlock("header");
  720. header.text = text;
  721. header.textHorizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  722. if (isHorizontal) {
  723. header.width = size;
  724. } else {
  725. header.height = size;
  726. }
  727. if (controlFirst) {
  728. panel.addControl(control);
  729. panel.addControl(header);
  730. header.paddingLeft = "5px";
  731. } else {
  732. panel.addControl(header);
  733. panel.addControl(control);
  734. header.paddingRight = "5px";
  735. }
  736. return panel;
  737. }
  738. protected static drawEllipse(x:number, y:number, width:number, height:number, context:CanvasRenderingContext2D):void{
  739. context.translate(x, y);
  740. context.scale(width, height);
  741. context.beginPath();
  742. context.arc(0, 0, 1, 0, 2 * Math.PI);
  743. context.closePath();
  744. context.scale(1/width, 1/height);
  745. context.translate(-x, -y);
  746. }
  747. }
  748. }