Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added slider step support #184

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/Slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BaseSliderOptions, SliderBase } from './SliderBase';

export type SliderOptions = BaseSliderOptions & {
slider: Container | string;
step?: number;
value?: number;
};

Expand Down Expand Up @@ -45,6 +46,10 @@ export class Slider extends SliderBase
});

this.sliderOptions = options;

// Avoid zero value
this.step = options.step || 1;

this.value = options.value ?? this.min;
this.updateSlider();
}
Expand Down Expand Up @@ -92,6 +97,17 @@ export class Slider extends SliderBase
return super.min;
}

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

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

/** Set slider instance ot texture. */
// eslint-disable-next-line accessor-pairs
set slider(value: Container | string)
Expand All @@ -108,9 +124,11 @@ export class Slider extends SliderBase

const obj = event.currentTarget as DragObject;
const { x } = obj.parent.worldTransform.applyInverse(event.global);
const positionRatio = x / (this.bg?.width || 1);
const rawValue = this.min + (positionRatio * (this.max - this.min));

this.progress = this.validate((x / this.bg?.width) * 100);
this.value = this.min + (((this.max - this.min) / 100) * this.progress);
// Snap the raw value to the nearest step
this.value = Math.round(rawValue / this.step) * this.step;
}

protected override change()
Expand Down
22 changes: 20 additions & 2 deletions src/SliderBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export class SliderBase extends ProgressBar
/** Maximal value. */
protected _max = 100;

/** Progress value step */
protected _step = 1;

protected startX!: number;
protected startUpdateValue1!: number;
protected startUpdateValue2!: number;
Expand All @@ -70,12 +73,12 @@ export class SliderBase extends ProgressBar
{
super.init(progressBarOptions);

if (this.fill) {
if (this.fill)
{
this.fill.eventMode = 'none';
}
}


/**
* Sets Slider1 instance.
* @param value - Container or string with texture name.
Expand Down Expand Up @@ -269,4 +272,19 @@ export class SliderBase extends ProgressBar
{
return this._min;
}

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

/** Get step value. */
get step(): number
{
return this._step;
}
}
3 changes: 3 additions & 0 deletions src/stories/slider/SliderGraphics.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const args = {
fontColor: '#FFFFFF',
min: 0,
max: 100,
step: 1,
value: 50,
width: 450,
height: 35,
Expand All @@ -29,6 +30,7 @@ const args = {
export const Single: StoryFn = ({
min,
max,
step,
value,
meshColor,
borderColor,
Expand Down Expand Up @@ -78,6 +80,7 @@ export const Single: StoryFn = ({
slider,
min,
max,
step,
value,
valueTextStyle: {
fill: fontColor,
Expand Down
3 changes: 3 additions & 0 deletions src/stories/slider/SliderNineSlicePlane.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const args = {
fontColor: '#FFFFFF',
min: 0,
max: 100,
step: 1,
value: 50,
fontSize: 20,
showValue: true,
Expand All @@ -21,6 +22,7 @@ const args = {
export const Single: StoryFn = ({
min,
max,
step,
value,
fontSize,
fontColor,
Expand Down Expand Up @@ -53,6 +55,7 @@ export const Single: StoryFn = ({
},
min,
max,
step,
value,
valueTextStyle: {
fill: fontColor,
Expand Down
4 changes: 3 additions & 1 deletion src/stories/slider/SliderSprite.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ const args = {
fontColor: '#FFFFFF',
min: 0,
max: 100,
step: 1,
value: 50,
fontSize: 20,
showValue: true,
amount: 1,
onChange: action('Slider')
};

export const Single: StoryFn = ({ min, max, value, fontSize, fontColor, onChange, showValue, amount }: any) =>
export const Single: StoryFn = ({ min, max, value, fontSize, fontColor, onChange, showValue, amount, step }: any) =>
{
const view = new List({ type: 'vertical', elementsMargin: 10 });

Expand All @@ -34,6 +35,7 @@ export const Single: StoryFn = ({ min, max, value, fontSize, fontColor, onChange
slider: 'slider.png',
min,
max,
step,
value,
valueTextStyle: {
fill: fontColor,
Expand Down
Loading