line.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var DOMImage = Image;
  3. module BABYLON.GUI {
  4. export class Line extends Control {
  5. private _lineWidth = 1;
  6. private _background: string;
  7. private _x1 = new ValueAndUnit(0);
  8. private _y1 = new ValueAndUnit(0);
  9. private _x2 = new ValueAndUnit(0);
  10. private _y2 = new ValueAndUnit(0);
  11. private _dash = new Array<number>();
  12. private _connectedControl: Control;
  13. private _connectedControlDirtyObserver: Observer<Control>;
  14. public get dash(): Array<number> {
  15. return this._dash;
  16. }
  17. public set dash(value: Array<number>) {
  18. if (this._dash === value) {
  19. return;
  20. }
  21. this._dash = value;
  22. this._markAsDirty();
  23. }
  24. public get connectedControl(): Control {
  25. return this._connectedControl;
  26. }
  27. public set connectedControl(value: Control) {
  28. if (this._connectedControl === value) {
  29. return;
  30. }
  31. if (this._connectedControlDirtyObserver && this._connectedControl) {
  32. this._connectedControl.onDirtyObservable.remove(this._connectedControlDirtyObserver);
  33. this._connectedControlDirtyObserver = null;
  34. }
  35. if (value) {
  36. this._connectedControlDirtyObserver = value.onDirtyObservable.add(() => this._markAsDirty());
  37. }
  38. this._connectedControl = value;
  39. this._markAsDirty();
  40. }
  41. public get x1(): string | number {
  42. return this._x1.toString(this._host);
  43. }
  44. public set x1(value: string | number ) {
  45. if (this._x1.toString(this._host) === value) {
  46. return;
  47. }
  48. if (this._x1.fromString(value)) {
  49. this._markAsDirty();
  50. }
  51. }
  52. public get y1(): string | number {
  53. return this._y1.toString(this._host);
  54. }
  55. public set y1(value: string | number ) {
  56. if (this._y1.toString(this._host) === value) {
  57. return;
  58. }
  59. if (this._y1.fromString(value)) {
  60. this._markAsDirty();
  61. }
  62. }
  63. public get x2(): string | number {
  64. return this._x2.toString(this._host);
  65. }
  66. public set x2(value: string | number ) {
  67. if (this._x2.toString(this._host) === value) {
  68. return;
  69. }
  70. if (this._x2.fromString(value)) {
  71. this._markAsDirty();
  72. }
  73. }
  74. public get y2(): string | number {
  75. return this._y2.toString(this._host);
  76. }
  77. public set y2(value: string | number ) {
  78. if (this._y2.toString(this._host) === value) {
  79. return;
  80. }
  81. if (this._y2.fromString(value)) {
  82. this._markAsDirty();
  83. }
  84. }
  85. public get lineWidth(): number {
  86. return this._lineWidth;
  87. }
  88. public set lineWidth(value: number) {
  89. if (this._lineWidth === value) {
  90. return;
  91. }
  92. this._lineWidth = value;
  93. this._markAsDirty();
  94. }
  95. public set horizontalAlignment(value: number) {
  96. return;
  97. }
  98. public set verticalAlignment(value: number) {
  99. return;
  100. }
  101. private get _effectiveX2(): number {
  102. return (this._connectedControl ? this._connectedControl.centerX : 0) + this._x2.getValue(this._host);
  103. }
  104. private get _effectiveY2(): number {
  105. return (this._connectedControl ? this._connectedControl.centerY : 0) + this._y2.getValue(this._host);
  106. }
  107. constructor(public name?: string) {
  108. super(name);
  109. this.isHitTestVisible = false;
  110. this._horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  111. this._verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  112. }
  113. protected _getTypeName(): string {
  114. return "Line";
  115. }
  116. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  117. context.save();
  118. this._applyStates(context);
  119. if (this._processMeasures(parentMeasure, context)) {
  120. context.strokeStyle = this.color;
  121. context.lineWidth = this._lineWidth;
  122. context.setLineDash(this._dash);
  123. context.beginPath();
  124. context.moveTo(this._x1.getValue(this._host), this._y1.getValue(this._host));
  125. context.lineTo(this._effectiveX2, this._effectiveY2);
  126. context.stroke();
  127. }
  128. context.restore();
  129. }
  130. public _measure(): void {
  131. // Width / Height
  132. this._currentMeasure.width = Math.abs(this._x1.getValue(this._host) - this._effectiveX2) + this._lineWidth;
  133. this._currentMeasure.height = Math.abs(this._y1.getValue(this._host) - this._effectiveY2) + this._lineWidth;
  134. }
  135. protected _computeAlignment(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  136. this._currentMeasure.left = Math.min(this._x1.getValue(this._host), this._effectiveX2) - this._lineWidth / 2;
  137. this._currentMeasure.top = Math.min(this._y1.getValue(this._host), this._effectiveY2) - this._lineWidth / 2;
  138. }
  139. public _moveToProjectedPosition(projectedPosition: Vector3): void {
  140. this.x1 = (projectedPosition.x + this._linkOffsetX.getValue(this._host)) + "px";
  141. this.y1 = (projectedPosition.y + this._linkOffsetY.getValue(this._host)) + "px";
  142. this._x1.ignoreAdaptiveScaling = true;
  143. this._y1.ignoreAdaptiveScaling = true;
  144. }
  145. }
  146. }