imageBasedSlider.ts 5.1 KB

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