Skip to content

Commit

Permalink
fix: Can't change default value to Slider after it has been created #124
Browse files Browse the repository at this point in the history
 (#125)
  • Loading branch information
CyberDex authored Jan 6, 2024
1 parent 5d3d220 commit deb2574
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/DoubleSlider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ export class DoubleSlider extends SliderBase

protected updateSlider1()
{
this.updateProgress(this.value1, this.value2);

this._slider1.x = (this.bg?.width / 100 * this.progressStart) - (this._slider1.width / 2);
this._slider1.y = this.bg?.height / 2;

Expand All @@ -244,6 +246,8 @@ export class DoubleSlider extends SliderBase

protected updateSlider2()
{
this.updateProgress(this.value1, this.value2);

this._slider2.x = ((this.bg?.width / 100) * this.progress) - (this._slider2.width / 2);
this._slider2.y = this.bg?.height / 2;

Expand Down
30 changes: 26 additions & 4 deletions src/Slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ export class Slider extends SliderBase
});

this.sliderOptions = options;

this.progress = ((options.value ?? this.min) - this.min) / (this.max - this.min) * 100;

this.value = options.value ?? this.min;
this.updateSlider();
}

/** Return selected value. */
Expand All @@ -72,6 +70,28 @@ export class Slider extends SliderBase
this.onUpdate?.emit(this.value);
}

override set max(value: number)
{
super.max = value;
this.updateSlider();
}

override get max(): number
{
return super.max;
}

override set min(value: number)
{
super.min = value;
this.updateSlider();
}

override get min(): number
{
return super.min;
}

/** Set slider instance ot texture. */
// eslint-disable-next-line accessor-pairs
set slider(value: Container | string)
Expand Down Expand Up @@ -100,10 +120,12 @@ export class Slider extends SliderBase

protected updateSlider()
{
this.progress = ((this.value ?? this.min) - this.min) / (this.max - this.min) * 100;

this._slider1.x = ((this.bg?.width / 100) * this.progress) - (this._slider1.width / 2);
this._slider1.y = this.bg?.height / 2;

if (this.sliderOptions.showValue)
if (this.sliderOptions?.showValue)
{
this.value1Text.text = `${Math.round(this.value)}`;

Expand Down
34 changes: 32 additions & 2 deletions src/SliderBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ export class SliderBase extends ProgressBar
protected dragging = 0;

/** Minimal value. */
min = 0;
protected _min = 0;

/** Maximal value. */
max = 100;
protected _max = 100;

protected startX!: number;
protected startUpdateValue1!: number;
Expand Down Expand Up @@ -222,4 +222,34 @@ export class SliderBase extends ProgressBar
{
// override me
}

/**
* Set max value.
* @param value
*/
set max(value: number)
{
this._max = value;
}

/** Get max value. */
get max(): number
{
return this._max;
}

/**
* Set min value.
* @param value
*/
set min(value: number)
{
this._min = value;
}

/** Get min value. */
get min(): number
{
return this._min;
}
}

0 comments on commit deb2574

Please sign in to comment.