scrollBar.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { Vector2 } from "babylonjs/Maths/math.vector";
  2. import { BaseSlider } from "./baseSlider";
  3. import { Control } from "../control";
  4. import { Measure } from "../../measure";
  5. /**
  6. * Class used to create slider controls
  7. */
  8. export class ScrollBar extends BaseSlider {
  9. private _background = "black";
  10. private _borderColor = "white";
  11. private _tempMeasure = new Measure(0, 0, 0, 0);
  12. /** Gets or sets border color */
  13. public get borderColor(): string {
  14. return this._borderColor;
  15. }
  16. public set borderColor(value: string) {
  17. if (this._borderColor === value) {
  18. return;
  19. }
  20. this._borderColor = value;
  21. this._markAsDirty();
  22. }
  23. /** Gets or sets background color */
  24. public get background(): string {
  25. return this._background;
  26. }
  27. public set background(value: string) {
  28. if (this._background === value) {
  29. return;
  30. }
  31. this._background = value;
  32. this._markAsDirty();
  33. }
  34. /**
  35. * Creates a new Slider
  36. * @param name defines the control name
  37. */
  38. constructor(public name?: string) {
  39. super(name);
  40. }
  41. protected _getTypeName(): string {
  42. return "Scrollbar";
  43. }
  44. protected _getThumbThickness(): number {
  45. var thumbThickness = 0;
  46. if (this._thumbWidth.isPixel) {
  47. thumbThickness = this._thumbWidth.getValue(this._host);
  48. }
  49. else {
  50. thumbThickness = this._backgroundBoxThickness * this._thumbWidth.getValue(this._host);
  51. }
  52. return thumbThickness;
  53. }
  54. public _draw(context: CanvasRenderingContext2D): void {
  55. context.save();
  56. this._applyStates(context);
  57. this._prepareRenderingData("rectangle");
  58. var left = this._renderLeft;
  59. const thumbPosition = this._getThumbPosition();
  60. context.fillStyle = this._background;
  61. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  62. // Value bar
  63. context.fillStyle = this.color;
  64. // Thumb
  65. if (this.isVertical) {
  66. this._tempMeasure.left = left - this._effectiveBarOffset;
  67. this._tempMeasure.top = this._currentMeasure.top + thumbPosition;
  68. this._tempMeasure.width = this._currentMeasure.width;
  69. this._tempMeasure.height = this._effectiveThumbThickness;
  70. }
  71. else {
  72. this._tempMeasure.left = this._currentMeasure.left + thumbPosition;
  73. this._tempMeasure.top = this._currentMeasure.top;
  74. this._tempMeasure.width = this._effectiveThumbThickness;
  75. this._tempMeasure.height = this._currentMeasure.height;
  76. }
  77. context.fillRect(this._tempMeasure.left, this._tempMeasure.top, this._tempMeasure.width, this._tempMeasure.height);
  78. context.restore();
  79. }
  80. private _first: boolean;
  81. private _originX: number;
  82. private _originY: number;
  83. /** @hidden */
  84. protected _updateValueFromPointer(x: number, y: number): void {
  85. if (this.rotation != 0) {
  86. this._invertTransformMatrix.transformCoordinates(x, y, this._transformedPosition);
  87. x = this._transformedPosition.x;
  88. y = this._transformedPosition.y;
  89. }
  90. if (this._first) {
  91. this._first = false;
  92. this._originX = x;
  93. this._originY = y;
  94. // Check if move is required
  95. if (x < this._tempMeasure.left || x > this._tempMeasure.left + this._tempMeasure.width || y < this._tempMeasure.top || y > this._tempMeasure.top + this._tempMeasure.height) {
  96. if (this.isVertical) {
  97. this.value = this.minimum + (1 - ((y - this._currentMeasure.top) / this._currentMeasure.height)) * (this.maximum - this.minimum);
  98. }
  99. else {
  100. this.value = this.minimum + ((x - this._currentMeasure.left) / this._currentMeasure.width) * (this.maximum - this.minimum);
  101. }
  102. }
  103. }
  104. // Delta mode
  105. let delta = 0;
  106. if (this.isVertical) {
  107. delta = -((y - this._originY) / (this._currentMeasure.height - this._effectiveThumbThickness));
  108. }
  109. else {
  110. delta = (x - this._originX) / (this._currentMeasure.width - this._effectiveThumbThickness);
  111. }
  112. this.value += delta * (this.maximum - this.minimum);
  113. this._originX = x;
  114. this._originY = y;
  115. }
  116. public _onPointerDown(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number): boolean {
  117. this._first = true;
  118. return super._onPointerDown(target, coordinates, pointerId, buttonIndex);
  119. }
  120. }