control.ts 36 KB

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