complexLine.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class ComplexLine extends Control {
  4. private _lineWidth: number = 1;
  5. private _dash: number[];
  6. private _trackers: Nullable<Tracker>[];
  7. private _minX: Nullable<number>;
  8. private _minY: Nullable<number>;
  9. private _maxX: Nullable<number>;
  10. private _maxY: Nullable<number>;
  11. constructor(public name?: string) {
  12. super(name);
  13. this.isHitTestVisible = false;
  14. this._horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  15. this._verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  16. this._dash = [];
  17. this._trackers = [];
  18. }
  19. public get dash(): Array<number> {
  20. return this._dash;
  21. }
  22. public set dash(value: Array<number>) {
  23. if (this._dash === value) {
  24. return;
  25. }
  26. this._dash = value;
  27. this._markAsDirty();
  28. }
  29. getX(index: number): string | number {
  30. return this.getTracker(index).x.toString(this._host);
  31. }
  32. setX(index: number, value: string | number): void {
  33. var point: Tracker = this.getTracker(index);
  34. if (point.x.toString(this._host) === value) {
  35. return;
  36. }
  37. if (point.x.fromString(value)) {
  38. this._markAsDirty();
  39. }
  40. }
  41. getY(index: number): string | number {
  42. return this.getTracker(index).y.toString(this._host);
  43. }
  44. setY(index: number, value: string | number): void {
  45. var tracker: Tracker = this.getTracker(index);
  46. if (tracker.y.toString(this._host) === value) {
  47. return;
  48. }
  49. if (tracker.y.fromString(value)) {
  50. this._markAsDirty();
  51. }
  52. }
  53. getControl(index: number): Nullable<Control> {
  54. return this.getTracker(index).control;
  55. }
  56. setControl(index: number, value: Nullable<Control> = null) {
  57. var tracker: Tracker = this.getTracker(index);
  58. if (tracker.control === value) {
  59. return;
  60. }
  61. if (tracker.control) {
  62. tracker.control.onDirtyObservable.removeCallback(this.onTrackerUpdate);
  63. }
  64. tracker.control = value;
  65. if (tracker.control) {
  66. tracker.control.onDirtyObservable.add(this.onTrackerUpdate);
  67. }
  68. this._markAsDirty();
  69. }
  70. getMesh(index: number): Nullable<BABYLON.AbstractMesh> {
  71. return this.getTracker(index).mesh;
  72. }
  73. setMesh(index: number, value: Nullable<BABYLON.AbstractMesh> = null) {
  74. var tracker: Tracker = this.getTracker(index);
  75. if (tracker.mesh === value) {
  76. return;
  77. }
  78. if (tracker.mesh) {
  79. tracker.mesh.getScene().onAfterCameraRenderObservable.removeCallback(this.onTrackerUpdate);
  80. }
  81. tracker.mesh = value;
  82. if (tracker.mesh) {
  83. tracker.mesh.getScene().onAfterCameraRenderObservable.add(this.onTrackerUpdate);
  84. }
  85. this._markAsDirty();
  86. }
  87. private getTracker(index: number): Tracker {
  88. if (!this._trackers[index]) {
  89. this._trackers[index] = new Tracker();
  90. }
  91. return this._trackers[index] as Tracker;
  92. }
  93. private updateTracker(value: Tracker): void {
  94. if (value.mesh != null) {
  95. var projection: BABYLON.Vector2 = this._host.getProjectedPosition(value.mesh.getBoundingInfo().boundingSphere.center, value.mesh.getWorldMatrix());
  96. value._point.x = projection.x;
  97. value._point.y = projection.y;
  98. }
  99. else if (value.control != null) {
  100. value._point.x = value.control.centerX;
  101. value._point.y = value.control.centerY;
  102. }
  103. else {
  104. value._point.x = value.x.getValue(this._host);
  105. value._point.y = value.y.getValue(this._host);
  106. }
  107. }
  108. private onTrackerUpdate = (): void => {
  109. this._markAsDirty();
  110. }
  111. track(items: (BABYLON.AbstractMesh | Control | BABYLON.Vector2)): void {
  112. //TODO: implement
  113. }
  114. addTracker(): number {
  115. this.getTracker(this._trackers.length);
  116. return this._trackers.length - 1;
  117. }
  118. removeTracker(index: number): void {
  119. var tracker: Nullable<Tracker> = this._trackers[index];
  120. if (!tracker) {
  121. return;
  122. }
  123. if (tracker.mesh) {
  124. tracker.mesh.onAfterWorldMatrixUpdateObservable.removeCallback(this.onTrackerUpdate);
  125. }
  126. if (tracker.control) {
  127. tracker.control.onDirtyObservable.removeCallback(this.onTrackerUpdate);
  128. }
  129. this._trackers.splice(index, 1);
  130. }
  131. public get lineWidth(): number {
  132. return this._lineWidth;
  133. }
  134. public set lineWidth(value: number) {
  135. if (this._lineWidth === value) {
  136. return;
  137. }
  138. this._lineWidth = value;
  139. this._markAsDirty();
  140. }
  141. public set horizontalAlignment(value: number) {
  142. return;
  143. }
  144. public set verticalAlignment(value: number) {
  145. return;
  146. }
  147. protected _getTypeName(): string {
  148. return "ComplexLine";
  149. }
  150. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  151. context.save();
  152. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY){
  153. context.shadowColor = this.shadowColor;
  154. context.shadowBlur = this.shadowBlur;
  155. context.shadowOffsetX = this.shadowOffsetX;
  156. context.shadowOffsetY = this.shadowOffsetY;
  157. }
  158. this._applyStates(context);
  159. if (this._processMeasures(parentMeasure, context)) {
  160. context.strokeStyle = this.color;
  161. context.lineWidth = this._lineWidth;
  162. context.setLineDash(this._dash);
  163. context.beginPath();
  164. var first: boolean = true; //first index is not necessarily 0
  165. this._trackers.forEach(tracker => {
  166. if (!tracker) {
  167. return;
  168. }
  169. if (first) {
  170. context.moveTo(tracker._point.x, tracker._point.y);
  171. first = false;
  172. }
  173. else {
  174. context.lineTo(tracker._point.x, tracker._point.y);
  175. }
  176. });
  177. context.stroke();
  178. }
  179. context.restore();
  180. }
  181. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  182. this._minX = null;
  183. this._minY = null;
  184. this._maxX = null;
  185. this._maxY = null;
  186. this._trackers.forEach((tracker, index) => {
  187. if (!tracker) {
  188. return;
  189. }
  190. this.updateTracker(tracker);
  191. if (this._minX == null || tracker._point.x < this._minX) this._minX = tracker._point.x;
  192. if (this._minY == null || tracker._point.y < this._minY) this._minY = tracker._point.y;
  193. if (this._maxX == null || tracker._point.x > this._maxX) this._maxX = tracker._point.x;
  194. if (this._maxY == null || tracker._point.y > this._maxY) this._maxY = tracker._point.y;
  195. });
  196. if (this._minX == null) this._minX = 0;
  197. if (this._minY == null) this._minY = 0;
  198. if (this._maxX == null) this._maxX = 0;
  199. if (this._maxY == null) this._maxY = 0;
  200. }
  201. public _measure(): void {
  202. if (this._minX == null || this._maxX == null || this._minY == null || this._maxY == null) {
  203. return;
  204. }
  205. this._currentMeasure.width = Math.abs(this._maxX - this._minX) + this._lineWidth;
  206. this._currentMeasure.height = Math.abs(this._maxY - this._minY) + this._lineWidth;
  207. }
  208. protected _computeAlignment(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  209. if (this._minX == null || this._minY == null) {
  210. return;
  211. }
  212. this._currentMeasure.left = this._minX - this._lineWidth / 2;
  213. this._currentMeasure.top = this._minY - this._lineWidth / 2;
  214. }
  215. dispose(): void {
  216. while (this._trackers.length > 0) {
  217. this.removeTracker(this._trackers.length - 1);
  218. }
  219. }
  220. }
  221. }