line.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class Line extends Control {
  4. private _lineWidth = 1;
  5. private _x1 = new ValueAndUnit(0);
  6. private _y1 = new ValueAndUnit(0);
  7. private _x2 = new ValueAndUnit(0);
  8. private _y2 = new ValueAndUnit(0);
  9. private _dash = new Array<number>();
  10. private _connectedControl: Control;
  11. private _connectedControlDirtyObserver: Nullable<Observer<Control>>;
  12. public get dash(): Array<number> {
  13. return this._dash;
  14. }
  15. public set dash(value: Array<number>) {
  16. if (this._dash === value) {
  17. return;
  18. }
  19. this._dash = value;
  20. this._markAsDirty();
  21. }
  22. public get connectedControl(): Control {
  23. return this._connectedControl;
  24. }
  25. public set connectedControl(value: Control) {
  26. if (this._connectedControl === value) {
  27. return;
  28. }
  29. if (this._connectedControlDirtyObserver && this._connectedControl) {
  30. this._connectedControl.onDirtyObservable.remove(this._connectedControlDirtyObserver);
  31. this._connectedControlDirtyObserver = null;
  32. }
  33. if (value) {
  34. this._connectedControlDirtyObserver = value.onDirtyObservable.add(() => this._markAsDirty());
  35. }
  36. this._connectedControl = value;
  37. this._markAsDirty();
  38. }
  39. public get x1(): string | number {
  40. return this._x1.toString(this._host);
  41. }
  42. public set x1(value: string | number ) {
  43. if (this._x1.toString(this._host) === value) {
  44. return;
  45. }
  46. if (this._x1.fromString(value)) {
  47. this._markAsDirty();
  48. }
  49. }
  50. public get y1(): string | number {
  51. return this._y1.toString(this._host);
  52. }
  53. public set y1(value: string | number ) {
  54. if (this._y1.toString(this._host) === value) {
  55. return;
  56. }
  57. if (this._y1.fromString(value)) {
  58. this._markAsDirty();
  59. }
  60. }
  61. public get x2(): string | number {
  62. return this._x2.toString(this._host);
  63. }
  64. public set x2(value: string | number ) {
  65. if (this._x2.toString(this._host) === value) {
  66. return;
  67. }
  68. if (this._x2.fromString(value)) {
  69. this._markAsDirty();
  70. }
  71. }
  72. public get y2(): string | number {
  73. return this._y2.toString(this._host);
  74. }
  75. public set y2(value: string | number ) {
  76. if (this._y2.toString(this._host) === value) {
  77. return;
  78. }
  79. if (this._y2.fromString(value)) {
  80. this._markAsDirty();
  81. }
  82. }
  83. public get lineWidth(): number {
  84. return this._lineWidth;
  85. }
  86. public set lineWidth(value: number) {
  87. if (this._lineWidth === value) {
  88. return;
  89. }
  90. this._lineWidth = value;
  91. this._markAsDirty();
  92. }
  93. public set horizontalAlignment(value: number) {
  94. return;
  95. }
  96. public set verticalAlignment(value: number) {
  97. return;
  98. }
  99. private get _effectiveX2(): number {
  100. return (this._connectedControl ? this._connectedControl.centerX : 0) + this._x2.getValue(this._host);
  101. }
  102. private get _effectiveY2(): number {
  103. return (this._connectedControl ? this._connectedControl.centerY : 0) + this._y2.getValue(this._host);
  104. }
  105. constructor(public name?: string) {
  106. super(name);
  107. this.isHitTestVisible = false;
  108. this._horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  109. this._verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  110. }
  111. protected _getTypeName(): string {
  112. return "Line";
  113. }
  114. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  115. context.save();
  116. if(this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY){
  117. context.shadowColor = this.shadowColor;
  118. context.shadowBlur = this.shadowBlur;
  119. context.shadowOffsetX = this.shadowOffsetX;
  120. context.shadowOffsetY = this.shadowOffsetY;
  121. }
  122. this._applyStates(context);
  123. if (this._processMeasures(parentMeasure, context)) {
  124. context.strokeStyle = this.color;
  125. context.lineWidth = this._lineWidth;
  126. context.setLineDash(this._dash);
  127. context.beginPath();
  128. context.moveTo(this._x1.getValue(this._host), this._y1.getValue(this._host));
  129. context.lineTo(this._effectiveX2, this._effectiveY2);
  130. context.stroke();
  131. }
  132. context.restore();
  133. }
  134. public _measure(): void {
  135. // Width / Height
  136. this._currentMeasure.width = Math.abs(this._x1.getValue(this._host) - this._effectiveX2) + this._lineWidth;
  137. this._currentMeasure.height = Math.abs(this._y1.getValue(this._host) - this._effectiveY2) + this._lineWidth;
  138. }
  139. protected _computeAlignment(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  140. this._currentMeasure.left = Math.min(this._x1.getValue(this._host), this._effectiveX2) - this._lineWidth / 2;
  141. this._currentMeasure.top = Math.min(this._y1.getValue(this._host), this._effectiveY2) - this._lineWidth / 2;
  142. }
  143. /**
  144. * Move one end of the line given 3D cartesian coordinates.
  145. * @param position Targeted world position
  146. * @param scene Scene
  147. * @param end (opt) Set to true to assign x2 and y2 coordinates of the line. Default assign to x1 and y1.
  148. */
  149. public moveToVector3(position: Vector3, scene: Scene, end: boolean = false): void {
  150. if (!this._host || this._root !== this._host._rootContainer) {
  151. Tools.Error("Cannot move a control to a vector3 if the control is not at root level");
  152. return;
  153. }
  154. var globalViewport = this._host._getGlobalViewport(scene);
  155. var projectedPosition = Vector3.Project(position, Matrix.Identity(), scene.getTransformMatrix(), globalViewport);
  156. this._moveToProjectedPosition(projectedPosition, end)
  157. if (projectedPosition.z < 0 || projectedPosition.z > 1) {
  158. this.notRenderable = true;
  159. return;
  160. }
  161. this.notRenderable = false;
  162. }
  163. /**
  164. * Move one end of the line to a position in screen absolute space.
  165. * @param projectedPosition Position in screen absolute space (X, Y)
  166. * @param end (opt) Set to true to assign x2 and y2 coordinates of the line. Default assign to x1 and y1.
  167. */
  168. public _moveToProjectedPosition(projectedPosition: Vector3, end: boolean = false): void {
  169. let x: string = (projectedPosition.x + this._linkOffsetX.getValue(this._host)) + "px";
  170. let y: string = (projectedPosition.y + this._linkOffsetY.getValue(this._host)) + "px";
  171. if (end) {
  172. this.x2 = x;
  173. this.y2 = y;
  174. this._x2.ignoreAdaptiveScaling = true;
  175. this._y2.ignoreAdaptiveScaling = true;
  176. } else {
  177. this.x1 = x;
  178. this.y1 = y;
  179. this._x1.ignoreAdaptiveScaling = true;
  180. this._y1.ignoreAdaptiveScaling = true;
  181. }
  182. }
  183. }
  184. }