imageBasedSlider.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { BaseSlider } from "./baseslider";
  2. import { Measure } from "../measure";
  3. import { Image } from "./image";
  4. /**
  5. * Class used to create slider controls based on images
  6. */
  7. export class ImageBasedSlider extends BaseSlider {
  8. private _backgroundImage: Image;
  9. private _thumbImage: Image;
  10. private _valueBarImage: Image;
  11. private _tempMeasure = new Measure(0, 0, 0, 0);
  12. /**
  13. * Gets or sets the image used to render the background
  14. */
  15. public get backgroundImage(): Image {
  16. return this._backgroundImage;
  17. }
  18. public set backgroundImage(value: Image) {
  19. if (this._backgroundImage === value) {
  20. return;
  21. }
  22. this._backgroundImage = value;
  23. if (value && !value.isLoaded) {
  24. value.onImageLoadedObservable.addOnce(() => this._markAsDirty());
  25. }
  26. this._markAsDirty();
  27. }
  28. /**
  29. * Gets or sets the image used to render the thumb
  30. */
  31. public get thumbImage(): Image {
  32. return this._thumbImage;
  33. }
  34. public set thumbImage(value: Image) {
  35. if (this._thumbImage === value) {
  36. return;
  37. }
  38. this._thumbImage = value;
  39. if (value && !value.isLoaded) {
  40. value.onImageLoadedObservable.addOnce(() => this._markAsDirty());
  41. }
  42. this._markAsDirty();
  43. }
  44. /**
  45. * Creates a new ImageBasedSlider
  46. * @param name defines the control name
  47. */
  48. constructor(public name?: string) {
  49. super(name);
  50. }
  51. protected _getTypeName(): string {
  52. return "ImageBasedSlider";
  53. }
  54. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  55. context.save();
  56. this._applyStates(context);
  57. if (this._processMeasures(parentMeasure, context)) {
  58. this._prepareRenderingData("rectangle");
  59. const thumbPosition = this._getThumbPosition();
  60. var left = this._renderLeft;
  61. var top = this._renderTop;
  62. var width = this._renderWidth;
  63. var height = this._renderHeight;
  64. // Background
  65. if (this._backgroundImage) {
  66. this._tempMeasure.copyFromFloats(left, top, width, height);
  67. if (this.isThumbClamped) {
  68. if (this.isVertical) {
  69. this._tempMeasure.height += this._effectiveThumbThickness;
  70. } else {
  71. this._tempMeasure.width += this._effectiveThumbThickness;
  72. }
  73. }
  74. this._backgroundImage._draw(this._tempMeasure, context);
  75. }
  76. // Bar
  77. if (this._valueBarImage) {
  78. }
  79. // Thumb
  80. if (this._thumbImage) {
  81. if (this.isVertical) {
  82. this._tempMeasure.copyFromFloats(left - this._effectiveBarOffset, this._currentMeasure.top + thumbPosition, this._currentMeasure.width, this._effectiveThumbThickness);
  83. } else {
  84. this._tempMeasure.copyFromFloats(this._currentMeasure.left + thumbPosition, this._currentMeasure.top, this._effectiveThumbThickness, this._currentMeasure.height);
  85. }
  86. this._thumbImage._draw(this._tempMeasure, context);
  87. }
  88. }
  89. context.restore();
  90. }
  91. }