control.ts 33 KB

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