multiLinePoint.ts 4.5 KB

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