multiLinePoint.ts 4.3 KB

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