123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import { Vector2 } from "babylonjs/Maths/math.vector";
- import { BaseSlider } from "./baseSlider";
- import { Control } from "../control";
- import { Measure } from "../../measure";
- /**
- * Class used to create slider controls
- */
- export class ScrollBar extends BaseSlider {
- private _background = "black";
- private _borderColor = "white";
- private _tempMeasure = new Measure(0, 0, 0, 0);
- /** Gets or sets border color */
- public get borderColor(): string {
- return this._borderColor;
- }
- public set borderColor(value: string) {
- if (this._borderColor === value) {
- return;
- }
- this._borderColor = value;
- this._markAsDirty();
- }
- /** Gets or sets background color */
- public get background(): string {
- return this._background;
- }
- public set background(value: string) {
- if (this._background === value) {
- return;
- }
- this._background = value;
- this._markAsDirty();
- }
- /**
- * Creates a new Slider
- * @param name defines the control name
- */
- constructor(public name?: string) {
- super(name);
- }
- protected _getTypeName(): string {
- return "Scrollbar";
- }
- protected _getThumbThickness(): number {
- var thumbThickness = 0;
- if (this._thumbWidth.isPixel) {
- thumbThickness = this._thumbWidth.getValue(this._host);
- }
- else {
- thumbThickness = this._backgroundBoxThickness * this._thumbWidth.getValue(this._host);
- }
- return thumbThickness;
- }
- public _draw(context: CanvasRenderingContext2D): void {
- context.save();
- this._applyStates(context);
- this._prepareRenderingData("rectangle");
- var left = this._renderLeft;
- const thumbPosition = this._getThumbPosition();
- context.fillStyle = this._background;
- context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
- // Value bar
- context.fillStyle = this.color;
- // Thumb
- if (this.isVertical) {
- this._tempMeasure.left = left - this._effectiveBarOffset;
- this._tempMeasure.top = this._currentMeasure.top + thumbPosition;
- this._tempMeasure.width = this._currentMeasure.width;
- this._tempMeasure.height = this._effectiveThumbThickness;
- }
- else {
- this._tempMeasure.left = this._currentMeasure.left + thumbPosition;
- this._tempMeasure.top = this._currentMeasure.top;
- this._tempMeasure.width = this._effectiveThumbThickness;
- this._tempMeasure.height = this._currentMeasure.height;
- }
- context.fillRect(this._tempMeasure.left, this._tempMeasure.top, this._tempMeasure.width, this._tempMeasure.height);
- context.restore();
- }
- private _first: boolean;
- private _originX: number;
- private _originY: number;
- /** @hidden */
- protected _updateValueFromPointer(x: number, y: number): void {
- if (this.rotation != 0) {
- this._invertTransformMatrix.transformCoordinates(x, y, this._transformedPosition);
- x = this._transformedPosition.x;
- y = this._transformedPosition.y;
- }
- if (this._first) {
- this._first = false;
- this._originX = x;
- this._originY = y;
- // Check if move is required
- if (x < this._tempMeasure.left || x > this._tempMeasure.left + this._tempMeasure.width || y < this._tempMeasure.top || y > this._tempMeasure.top + this._tempMeasure.height) {
- if (this.isVertical) {
- this.value = this.minimum + (1 - ((y - this._currentMeasure.top) / this._currentMeasure.height)) * (this.maximum - this.minimum);
- }
- else {
- this.value = this.minimum + ((x - this._currentMeasure.left) / this._currentMeasure.width) * (this.maximum - this.minimum);
- }
- }
- }
- // Delta mode
- let delta = 0;
- if (this.isVertical) {
- delta = -((y - this._originY) / (this._currentMeasure.height - this._effectiveThumbThickness));
- }
- else {
- delta = (x - this._originX) / (this._currentMeasure.width - this._effectiveThumbThickness);
- }
- this.value += delta * (this.maximum - this.minimum);
- this._originX = x;
- this._originY = y;
- }
- public _onPointerDown(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number): boolean {
- this._first = true;
- return super._onPointerDown(target, coordinates, pointerId, buttonIndex);
- }
- }
|