control.ts 40 KB

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