slider.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class Slider extends Control {
  4. private _thumbWidth = new ValueAndUnit(30, ValueAndUnit.UNITMODE_PIXEL, false);
  5. private _minimum = 0;
  6. private _maximum = 100;
  7. private _value = 50;
  8. private _background = "black";
  9. private _borderColor = "white";
  10. private _barOffset = new ValueAndUnit(5, ValueAndUnit.UNITMODE_PIXEL, false);
  11. private _isThumbCircle = false;
  12. private _isThumbClamped = false;
  13. public onValueChangedObservable = new Observable<number>();
  14. public get borderColor(): string {
  15. return this._borderColor;
  16. }
  17. public set borderColor(value: string) {
  18. if (this._borderColor === value) {
  19. return;
  20. }
  21. this._borderColor = value;
  22. this._markAsDirty();
  23. }
  24. public get background(): string {
  25. return this._background;
  26. }
  27. public set background(value: string) {
  28. if (this._background === value) {
  29. return;
  30. }
  31. this._background = value;
  32. this._markAsDirty();
  33. }
  34. public get barOffset(): string | number {
  35. return this._barOffset.toString(this._host);
  36. }
  37. public get barOffsetInPixels(): number {
  38. return this._barOffset.getValueInPixel(this._host, this._cachedParentMeasure.width);
  39. }
  40. public set barOffset(value: string | number) {
  41. if (this._barOffset.toString(this._host) === value) {
  42. return;
  43. }
  44. if (this._barOffset.fromString(value)) {
  45. this._markAsDirty();
  46. }
  47. }
  48. public get thumbWidth(): string | number {
  49. return this._thumbWidth.toString(this._host);
  50. }
  51. public get thumbWidthInPixels(): number {
  52. return this._thumbWidth.getValueInPixel(this._host, this._cachedParentMeasure.width);
  53. }
  54. public set thumbWidth(value: string | number) {
  55. if (this._thumbWidth.toString(this._host) === value) {
  56. return;
  57. }
  58. if (this._thumbWidth.fromString(value)) {
  59. this._markAsDirty();
  60. }
  61. }
  62. public get minimum(): number {
  63. return this._minimum;
  64. }
  65. public set minimum(value: number) {
  66. if (this._minimum === value) {
  67. return;
  68. }
  69. this._minimum = value;
  70. this._markAsDirty();
  71. this.value = Math.max(Math.min(this.value, this._maximum), this._minimum);
  72. }
  73. public get maximum(): number {
  74. return this._maximum;
  75. }
  76. public set maximum(value: number) {
  77. if (this._maximum === value) {
  78. return;
  79. }
  80. this._maximum = value;
  81. this._markAsDirty();
  82. this.value = Math.max(Math.min(this.value, this._maximum), this._minimum);
  83. }
  84. public get value(): number {
  85. return this._value;
  86. }
  87. public set value(value: number) {
  88. value = Math.max(Math.min(value, this._maximum), this._minimum);
  89. if (this._value === value) {
  90. return;
  91. }
  92. this._value = value;
  93. this._markAsDirty();
  94. this.onValueChangedObservable.notifyObservers(this._value);
  95. }
  96. public get isThumbCircle(): boolean {
  97. return this._isThumbCircle;
  98. }
  99. public set isThumbCircle(value: boolean) {
  100. if (this._isThumbCircle === value) {
  101. return;
  102. }
  103. this._isThumbCircle = value;
  104. this._markAsDirty();
  105. }
  106. public get isThumbClamped(): boolean {
  107. return this._isThumbClamped;
  108. }
  109. public set isThumbClamped(value: boolean) {
  110. if (this._isThumbClamped === value) {
  111. return;
  112. }
  113. this._isThumbClamped = value;
  114. this._markAsDirty();
  115. }
  116. constructor(public name?: string) {
  117. super(name);
  118. this.isPointerBlocker = true;
  119. }
  120. protected _getTypeName(): string {
  121. return "Slider";
  122. }
  123. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  124. context.save();
  125. this._applyStates(context);
  126. if (this._processMeasures(parentMeasure, context)) {
  127. // Main bar
  128. var effectiveThumbWidth;
  129. var effectiveBarOffset;
  130. if (this._thumbWidth.isPixel) {
  131. effectiveThumbWidth = Math.min(this._thumbWidth.getValue(this._host), this._currentMeasure.width);
  132. }
  133. else {
  134. effectiveThumbWidth = this._currentMeasure.width * this._thumbWidth.getValue(this._host);
  135. }
  136. if (this._barOffset.isPixel) {
  137. effectiveBarOffset = Math.min(this._barOffset.getValue(this._host), this._currentMeasure.height);
  138. }
  139. else {
  140. effectiveBarOffset = this._currentMeasure.height * this._barOffset.getValue(this._host);
  141. }
  142. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  143. context.shadowColor = this.shadowColor;
  144. context.shadowBlur = this.shadowBlur;
  145. context.shadowOffsetX = this.shadowOffsetX;
  146. context.shadowOffsetY = this.shadowOffsetY;
  147. }
  148. var left = this._currentMeasure.left;
  149. var width = this._currentMeasure.width - effectiveThumbWidth;
  150. var thumbPosition = ((this._value - this._minimum) / (this._maximum - this._minimum)) * width;
  151. context.fillStyle = this._background;
  152. if (this.isThumbClamped) {
  153. context.fillRect(left, this._currentMeasure.top + effectiveBarOffset, width + effectiveThumbWidth, this._currentMeasure.height - effectiveBarOffset * 2);
  154. }
  155. else {
  156. context.fillRect(left + (effectiveThumbWidth / 2), this._currentMeasure.top + effectiveBarOffset, width, this._currentMeasure.height - effectiveBarOffset * 2);
  157. }
  158. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  159. context.shadowBlur = 0;
  160. context.shadowOffsetX = 0;
  161. context.shadowOffsetY = 0;
  162. }
  163. context.fillStyle = this.color;
  164. if (this.isThumbClamped) {
  165. context.fillRect(left, this._currentMeasure.top + effectiveBarOffset, thumbPosition, this._currentMeasure.height - effectiveBarOffset * 2);
  166. }
  167. else {
  168. context.fillRect(left + (effectiveThumbWidth / 2), this._currentMeasure.top + effectiveBarOffset, thumbPosition, this._currentMeasure.height - effectiveBarOffset * 2);
  169. }
  170. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  171. context.shadowColor = this.shadowColor;
  172. context.shadowBlur = this.shadowBlur;
  173. context.shadowOffsetX = this.shadowOffsetX;
  174. context.shadowOffsetY = this.shadowOffsetY;
  175. }
  176. if (this._isThumbCircle) {
  177. context.beginPath();
  178. context.arc(left + thumbPosition + (effectiveThumbWidth / 2), this._currentMeasure.top + this._currentMeasure.height / 2, effectiveThumbWidth / 2, 0, 2 * Math.PI);
  179. context.fill();
  180. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  181. context.shadowBlur = 0;
  182. context.shadowOffsetX = 0;
  183. context.shadowOffsetY = 0;
  184. }
  185. context.strokeStyle = this._borderColor;
  186. context.stroke();
  187. }
  188. else {
  189. context.fillRect(left + thumbPosition, this._currentMeasure.top, effectiveThumbWidth, this._currentMeasure.height);
  190. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  191. context.shadowBlur = 0;
  192. context.shadowOffsetX = 0;
  193. context.shadowOffsetY = 0;
  194. }
  195. context.strokeStyle = this._borderColor;
  196. context.strokeRect(left + thumbPosition, this._currentMeasure.top, effectiveThumbWidth, this._currentMeasure.height);
  197. }
  198. }
  199. context.restore();
  200. }
  201. // Events
  202. private _pointerIsDown = false;
  203. private _updateValueFromPointer(x: number, y: number): void {
  204. if (this.rotation != 0) {
  205. this._invertTransformMatrix.transformCoordinates(x, y, this._transformedPosition);
  206. x = this._transformedPosition.x;
  207. }
  208. this.value = this._minimum + ((x - this._currentMeasure.left) / this._currentMeasure.width) * (this._maximum - this._minimum);
  209. }
  210. public _onPointerDown(target: Control, coordinates: Vector2, pointerId:number, buttonIndex: number): boolean {
  211. if (!super._onPointerDown(target, coordinates, pointerId, buttonIndex)) {
  212. return false;
  213. }
  214. this._pointerIsDown = true;
  215. this._updateValueFromPointer(coordinates.x, coordinates.y);
  216. this._host._capturingControl[pointerId] = this;
  217. return true;
  218. }
  219. public _onPointerMove(target: Control, coordinates: Vector2): void {
  220. if (this._pointerIsDown) {
  221. this._updateValueFromPointer(coordinates.x, coordinates.y);
  222. }
  223. super._onPointerMove(target, coordinates);
  224. }
  225. public _onPointerUp(target: Control, coordinates: Vector2, pointerId:number, buttonIndex: number, notifyClick: boolean): void {
  226. this._pointerIsDown = false;
  227. delete this._host._capturingControl[pointerId];
  228. super._onPointerUp(target, coordinates, pointerId, buttonIndex, notifyClick);
  229. }
  230. }
  231. }