slider.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import { BaseSlider } from "./baseSlider";
  2. import { _TypeStore } from 'babylonjs/Misc/typeStore';
  3. import { Nullable } from 'babylonjs/types';
  4. import { Measure } from '../../measure';
  5. /**
  6. * Class used to create slider controls
  7. */
  8. export class Slider extends BaseSlider {
  9. private _background = "black";
  10. private _borderColor = "white";
  11. private _isThumbCircle = false;
  12. protected _displayValueBar = true;
  13. /** Gets or sets a boolean indicating if the value bar must be rendered */
  14. public get displayValueBar(): boolean {
  15. return this._displayValueBar;
  16. }
  17. public set displayValueBar(value: boolean) {
  18. if (this._displayValueBar === value) {
  19. return;
  20. }
  21. this._displayValueBar = value;
  22. this._markAsDirty();
  23. }
  24. /** Gets or sets border color */
  25. public get borderColor(): string {
  26. return this._borderColor;
  27. }
  28. public set borderColor(value: string) {
  29. if (this._borderColor === value) {
  30. return;
  31. }
  32. this._borderColor = value;
  33. this._markAsDirty();
  34. }
  35. /** Gets or sets background color */
  36. public get background(): string {
  37. return this._background;
  38. }
  39. public set background(value: string) {
  40. if (this._background === value) {
  41. return;
  42. }
  43. this._background = value;
  44. this._markAsDirty();
  45. }
  46. /** Gets or sets a boolean indicating if the thumb should be round or square */
  47. public get isThumbCircle(): boolean {
  48. return this._isThumbCircle;
  49. }
  50. public set isThumbCircle(value: boolean) {
  51. if (this._isThumbCircle === value) {
  52. return;
  53. }
  54. this._isThumbCircle = value;
  55. this._markAsDirty();
  56. }
  57. /**
  58. * Creates a new Slider
  59. * @param name defines the control name
  60. */
  61. constructor(public name?: string) {
  62. super(name);
  63. }
  64. protected _getTypeName(): string {
  65. return "Slider";
  66. }
  67. public _draw(context: CanvasRenderingContext2D, invalidatedRectangle?: Nullable<Measure>): void {
  68. context.save();
  69. this._applyStates(context);
  70. this._prepareRenderingData(this.isThumbCircle ? "circle" : "rectangle");
  71. var left = this._renderLeft;
  72. var top = this._renderTop;
  73. var width = this._renderWidth;
  74. var height = this._renderHeight;
  75. var radius = 0;
  76. if (this.isThumbClamped && this.isThumbCircle) {
  77. if (this.isVertical) {
  78. top += (this._effectiveThumbThickness / 2);
  79. }
  80. else {
  81. left += (this._effectiveThumbThickness / 2);
  82. }
  83. radius = this._backgroundBoxThickness / 2;
  84. }
  85. else {
  86. radius = (this._effectiveThumbThickness - this._effectiveBarOffset) / 2;
  87. }
  88. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  89. context.shadowColor = this.shadowColor;
  90. context.shadowBlur = this.shadowBlur;
  91. context.shadowOffsetX = this.shadowOffsetX;
  92. context.shadowOffsetY = this.shadowOffsetY;
  93. }
  94. const thumbPosition = this._getThumbPosition();
  95. context.fillStyle = this._background;
  96. if (this.isVertical) {
  97. if (this.isThumbClamped) {
  98. if (this.isThumbCircle) {
  99. context.beginPath();
  100. context.arc(left + this._backgroundBoxThickness / 2, top, radius, Math.PI, 2 * Math.PI);
  101. context.fill();
  102. context.fillRect(left, top, width, height);
  103. }
  104. else {
  105. context.fillRect(left, top, width, height + this._effectiveThumbThickness);
  106. }
  107. }
  108. else {
  109. context.fillRect(left, top, width, height);
  110. }
  111. }
  112. else {
  113. if (this.isThumbClamped) {
  114. if (this.isThumbCircle) {
  115. context.beginPath();
  116. context.arc(left + this._backgroundBoxLength, top + (this._backgroundBoxThickness / 2), radius, 0, 2 * Math.PI);
  117. context.fill();
  118. context.fillRect(left, top, width, height);
  119. }
  120. else {
  121. context.fillRect(left, top, width + this._effectiveThumbThickness, height);
  122. }
  123. }
  124. else {
  125. context.fillRect(left, top, width, height);
  126. }
  127. }
  128. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  129. context.shadowBlur = 0;
  130. context.shadowOffsetX = 0;
  131. context.shadowOffsetY = 0;
  132. }
  133. // Value bar
  134. context.fillStyle = this.color;
  135. if (this._displayValueBar) {
  136. if (this.isVertical) {
  137. if (this.isThumbClamped) {
  138. if (this.isThumbCircle) {
  139. context.beginPath();
  140. context.arc(left + this._backgroundBoxThickness / 2, top + this._backgroundBoxLength, radius, 0, 2 * Math.PI);
  141. context.fill();
  142. context.fillRect(left, top + thumbPosition, width, height - thumbPosition);
  143. }
  144. else {
  145. context.fillRect(left, top + thumbPosition, width, height - thumbPosition + this._effectiveThumbThickness);
  146. }
  147. }
  148. else {
  149. context.fillRect(left, top + thumbPosition, width, height - thumbPosition);
  150. }
  151. }
  152. else {
  153. if (this.isThumbClamped) {
  154. if (this.isThumbCircle) {
  155. context.beginPath();
  156. context.arc(left, top + this._backgroundBoxThickness / 2, radius, 0, 2 * Math.PI);
  157. context.fill();
  158. context.fillRect(left, top, thumbPosition, height);
  159. }
  160. else {
  161. context.fillRect(left, top, thumbPosition, height);
  162. }
  163. }
  164. else {
  165. context.fillRect(left, top, thumbPosition, height);
  166. }
  167. }
  168. }
  169. // Thumb
  170. if (this.displayThumb) {
  171. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  172. context.shadowColor = this.shadowColor;
  173. context.shadowBlur = this.shadowBlur;
  174. context.shadowOffsetX = this.shadowOffsetX;
  175. context.shadowOffsetY = this.shadowOffsetY;
  176. }
  177. if (this._isThumbCircle) {
  178. context.beginPath();
  179. if (this.isVertical) {
  180. context.arc(left + this._backgroundBoxThickness / 2, top + thumbPosition, radius, 0, 2 * Math.PI);
  181. }
  182. else {
  183. context.arc(left + thumbPosition, top + (this._backgroundBoxThickness / 2), radius, 0, 2 * Math.PI);
  184. }
  185. context.fill();
  186. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  187. context.shadowBlur = 0;
  188. context.shadowOffsetX = 0;
  189. context.shadowOffsetY = 0;
  190. }
  191. context.strokeStyle = this._borderColor;
  192. context.stroke();
  193. }
  194. else {
  195. if (this.isVertical) {
  196. context.fillRect(left - this._effectiveBarOffset, this._currentMeasure.top + thumbPosition, this._currentMeasure.width, this._effectiveThumbThickness);
  197. }
  198. else {
  199. context.fillRect(this._currentMeasure.left + thumbPosition, this._currentMeasure.top, this._effectiveThumbThickness, this._currentMeasure.height);
  200. }
  201. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  202. context.shadowBlur = 0;
  203. context.shadowOffsetX = 0;
  204. context.shadowOffsetY = 0;
  205. }
  206. context.strokeStyle = this._borderColor;
  207. if (this.isVertical) {
  208. context.strokeRect(left - this._effectiveBarOffset, this._currentMeasure.top + thumbPosition, this._currentMeasure.width, this._effectiveThumbThickness);
  209. }
  210. else {
  211. context.strokeRect(this._currentMeasure.left + thumbPosition, this._currentMeasure.top, this._effectiveThumbThickness, this._currentMeasure.height);
  212. }
  213. }
  214. }
  215. context.restore();
  216. }
  217. }
  218. _TypeStore.RegisteredTypes["BABYLON.GUI.Slider"] = Slider;