imageBasedSlider.ts 5.2 KB

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