control.ts 36 KB

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