control.ts 32 KB

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