multiLinePoint.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. /**
  4. * Class used to store a point for a MultiLine object.
  5. * The point can be pure 2D coordinates, a mesh or a control
  6. */
  7. export class MultiLinePoint {
  8. private _multiLine: MultiLine;
  9. private _x: ValueAndUnit;
  10. private _y: ValueAndUnit;
  11. private _control: Nullable<Control>;
  12. private _mesh: Nullable<AbstractMesh>;
  13. private _controlObserver: Nullable< Observer<Control> >;
  14. private _meshObserver: Nullable< Observer<Camera> >;
  15. /** @hidden */
  16. public _point: Vector2;
  17. /**
  18. * Creates a new MultiLinePoint
  19. * @param multiLine defines the source MultiLine object
  20. */
  21. constructor(multiLine: MultiLine) {
  22. this._multiLine = multiLine;
  23. this._x = new ValueAndUnit(0);
  24. this._y = new ValueAndUnit(0);
  25. this._point = new Vector2(0, 0);
  26. }
  27. /** Gets or sets x coordinate */
  28. public get x(): string | number {
  29. return this._x.toString(this._multiLine._host);
  30. }
  31. public set x(value: string | number) {
  32. if (this._x.toString(this._multiLine._host) === value) {
  33. return;
  34. }
  35. if (this._x.fromString(value)) {
  36. this._multiLine._markAsDirty();
  37. }
  38. }
  39. /** Gets or sets y coordinate */
  40. public get y(): string | number {
  41. return this._y.toString(this._multiLine._host);
  42. }
  43. public set y(value: string | number) {
  44. if (this._y.toString(this._multiLine._host) === value) {
  45. return;
  46. }
  47. if (this._y.fromString(value)) {
  48. this._multiLine._markAsDirty();
  49. }
  50. }
  51. /** Gets or sets the control associated with this point */
  52. public get control(): Nullable<Control> {
  53. return this._control;
  54. }
  55. public set control(value: Nullable<Control>) {
  56. if (this._control === value) {
  57. return;
  58. }
  59. if (this._control && this._controlObserver) {
  60. this._control.onDirtyObservable.remove(this._controlObserver);
  61. this._controlObserver = null;
  62. }
  63. this._control = value;
  64. if (this._control) {
  65. this._controlObserver = this._control.onDirtyObservable.add(this._multiLine.onPointUpdate);
  66. }
  67. this._multiLine._markAsDirty();
  68. }
  69. /** Gets or sets the mesh associated with this point */
  70. public get mesh(): Nullable<AbstractMesh> {
  71. return this._mesh;
  72. }
  73. public set mesh(value: Nullable<AbstractMesh>) {
  74. if (this._mesh === value) {
  75. return;
  76. }
  77. if (this._mesh && this._meshObserver) {
  78. this._mesh.getScene().onAfterCameraRenderObservable.remove(this._meshObserver);
  79. }
  80. this._mesh = value;
  81. if (this._mesh) {
  82. this._meshObserver = this._mesh.getScene().onAfterCameraRenderObservable.add(this._multiLine.onPointUpdate);
  83. }
  84. this._multiLine._markAsDirty();
  85. }
  86. /**
  87. * Gets a translation vector
  88. * @returns the translation vector
  89. */
  90. public translate(): Vector2 {
  91. this._point = this._translatePoint();
  92. return this._point;
  93. }
  94. private _translatePoint(): Vector2 {
  95. if (this._mesh != null) {
  96. return this._multiLine._host.getProjectedPosition(this._mesh.getBoundingInfo().boundingSphere.center, this._mesh.getWorldMatrix());
  97. }
  98. else if (this._control != null) {
  99. return new Vector2(this._control.centerX, this._control.centerY);
  100. }
  101. else {
  102. var host: any = this._multiLine._host as any;
  103. var xValue: number = this._x.getValueInPixel(host, Number(host._canvas.width));
  104. var yValue: number = this._y.getValueInPixel(host, Number(host._canvas.height));
  105. return new Vector2(xValue, yValue);
  106. }
  107. }
  108. /** Release associated resources */
  109. public dispose(): void {
  110. this.control = null;
  111. this.mesh = null;
  112. }
  113. }
  114. }