imageBasedSlider.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 value bar
  30. */
  31. public get valueBarImage(): Image {
  32. return this._valueBarImage;
  33. }
  34. public set valueBarImage(value: Image) {
  35. if (this._valueBarImage === value) {
  36. return;
  37. }
  38. this._valueBarImage = value;
  39. if (value && !value.isLoaded) {
  40. value.onImageLoadedObservable.addOnce(() => this._markAsDirty());
  41. }
  42. this._markAsDirty();
  43. }
  44. /**
  45. * Gets or sets the image used to render the thumb
  46. */
  47. public get thumbImage(): Image {
  48. return this._thumbImage;
  49. }
  50. public set thumbImage(value: Image) {
  51. if (this._thumbImage === value) {
  52. return;
  53. }
  54. this._thumbImage = value;
  55. if (value && !value.isLoaded) {
  56. value.onImageLoadedObservable.addOnce(() => this._markAsDirty());
  57. }
  58. this._markAsDirty();
  59. }
  60. /**
  61. * Creates a new ImageBasedSlider
  62. * @param name defines the control name
  63. */
  64. constructor(public name?: string) {
  65. super(name);
  66. }
  67. protected _getTypeName(): string {
  68. return "ImageBasedSlider";
  69. }
  70. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  71. context.save();
  72. this._applyStates(context);
  73. if (this._processMeasures(parentMeasure, context)) {
  74. this._prepareRenderingData("rectangle");
  75. const thumbPosition = this._getThumbPosition();
  76. var left = this._renderLeft;
  77. var top = this._renderTop;
  78. var width = this._renderWidth;
  79. var height = this._renderHeight;
  80. // Background
  81. if (this._backgroundImage) {
  82. this._tempMeasure.copyFromFloats(left, top, width, height);
  83. if (this.isThumbClamped) {
  84. if (this.isVertical) {
  85. this._tempMeasure.height += this._effectiveThumbThickness;
  86. } else {
  87. this._tempMeasure.width += this._effectiveThumbThickness;
  88. }
  89. }
  90. this._backgroundImage._draw(this._tempMeasure, context);
  91. }
  92. // Bar
  93. if (this._valueBarImage) {
  94. if (this.isVertical) {
  95. this._tempMeasure.copyFromFloats(left, top + thumbPosition, width, height - thumbPosition);
  96. if (this.isThumbClamped) {
  97. this._tempMeasure.copyFromFloats(left, top + thumbPosition, width, this._currentMeasure.height - thumbPosition);
  98. } else {
  99. this._tempMeasure.copyFromFloats(left, top + thumbPosition, width, height - thumbPosition);
  100. }
  101. } else {
  102. this._tempMeasure.copyFromFloats(left, top, thumbPosition, height);
  103. }
  104. this._valueBarImage._draw(this._tempMeasure, context);
  105. }
  106. // Thumb
  107. if (this._thumbImage) {
  108. if (this.isVertical) {
  109. this._tempMeasure.copyFromFloats(left - this._effectiveBarOffset, this._currentMeasure.top + thumbPosition, this._currentMeasure.width, this._effectiveThumbThickness);
  110. } else {
  111. this._tempMeasure.copyFromFloats(this._currentMeasure.left + thumbPosition, this._currentMeasure.top, this._effectiveThumbThickness, this._currentMeasure.height);
  112. }
  113. this._thumbImage._draw(this._tempMeasure, context);
  114. }
  115. }
  116. context.restore();
  117. }
  118. }