control.ts 36 KB

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