imageBasedSlider.ts 5.3 KB

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