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

Allow Texture as the View Options #206

Merged
merged 1 commit into from
Dec 1, 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
7 changes: 3 additions & 4 deletions src/CheckBox.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Container } from '@pixi/display';
import { Text } from '@pixi/text';
import { Signal } from 'typed-signals';
import { Switcher } from './Switcher';
import { cleanup } from './utils/helpers/cleanup';
import { PixiText, PixiTextClass, PixiTextStyle } from './utils/helpers/text';
import { getView } from './utils/helpers/view';
import { getView, type GetViewSettings } from './utils/helpers/view';

type CheckBoxStyle = {
checked: Container | string;
unchecked: Container | string;
checked: GetViewSettings;
unchecked: GetViewSettings;
text?: PixiTextStyle;
textOffset?: {
x?: number;
Expand Down
3 changes: 2 additions & 1 deletion src/DoubleSlider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Texture } from '@pixi/core';
import { FederatedPointerEvent } from '@pixi/events';
import type { DragObject } from './utils/HelpTypes';
import { DoubleSliderOptions, SliderBase } from './SliderBase';
Expand Down Expand Up @@ -192,7 +193,7 @@ export class DoubleSlider extends SliderBase
* Set Slider1 instance.
* @param value - Container or string with texture name.
*/
override set slider1(value: Container | string)
override set slider1(value: Container | Texture | string)
{
super.slider1 = value;
this.updateSlider1();
Expand Down
28 changes: 15 additions & 13 deletions src/FancyButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ObservablePoint, Ticker, Rectangle, utils, Texture } from '@pixi/core';
import { Container } from '@pixi/display';
import { Sprite } from '@pixi/sprite';
import { getView } from './utils/helpers/view';
import { getView, GetViewSettings } from './utils/helpers/view';
import { AnyText, getTextView, PixiText } from './utils/helpers/text';
import { fitToView } from './utils/helpers/fit';
import { Tween, Group } from 'tweedle.js';
Expand All @@ -17,8 +17,6 @@ export type Offset = Pos & PosList;

type ButtonViewType = 'defaultView' | 'hoverView' | 'pressedView' | 'disabledView';

type ButtonView = string | Container;

type BasicButtonViews = {
[K in ButtonViewType]?: Container | NineSlicePlane;
};
Expand Down Expand Up @@ -46,12 +44,12 @@ type StateAnimations = {
};

type BasicViewsInput = {
[K in ButtonViewType]?: ButtonView;
[K in ButtonViewType]?: GetViewSettings;
};

type ViewsInput = BasicViewsInput & {
text?: AnyText;
icon?: ButtonView;
icon?: GetViewSettings;
};

export type ButtonOptions = ViewsInput & {
Expand Down Expand Up @@ -512,7 +510,7 @@ export class FancyButton extends ButtonContainer
* Sets the default view of the button.
* @param { string | Container } view - string (path to the image) or a Container-based view
*/
set defaultView(view: ButtonView | null)
set defaultView(view: GetViewSettings | null)
{
this.updateView('defaultView', view);
}
Expand All @@ -527,7 +525,7 @@ export class FancyButton extends ButtonContainer
* Sets the hover view of the button.
* @param { string | Container } view - string (path to the image) or a Container-based view
*/
set hoverView(view: ButtonView | null)
set hoverView(view: GetViewSettings | null)
{
this.updateView('hoverView', view);
if (this._views.hoverView && this.state !== 'hover')
Expand All @@ -543,7 +541,7 @@ export class FancyButton extends ButtonContainer
}

/** Sets the pressed view of the button. */
set pressedView(view: ButtonView | null)
set pressedView(view: GetViewSettings | null)
{
this.updateView('pressedView', view);
if (this._views.pressedView)
Expand All @@ -559,7 +557,7 @@ export class FancyButton extends ButtonContainer
}

/** Sets the disabled view of the button. */
set disabledView(view: ButtonView | null)
set disabledView(view: GetViewSettings | null)
{
this.updateView('disabledView', view);
if (this._views.disabledView)
Expand All @@ -577,9 +575,9 @@ export class FancyButton extends ButtonContainer
/**
* Helper method to update or cleanup button views.
* @param { 'defaultView' | 'hoverView' | 'pressedView' | 'disabledView' } viewType - type of the view to update
* @param { string | Container | null } view - new view
* @param { string | Texture | Container | null } view - new view
*/
protected updateView(viewType: ButtonViewType, view: ButtonView | null)
protected updateView(viewType: ButtonViewType, view: GetViewSettings | null)
{
if (view === undefined) return;

Expand All @@ -596,6 +594,10 @@ export class FancyButton extends ButtonContainer
{
this._views[viewType] = new NineSlicePlane(Texture.from(view), ...this.options.nineSlicePlane);
}
else if (view instanceof Texture)
{
this._views[viewType] = new NineSlicePlane(view, ...this.options.nineSlicePlane);
}
else
{
console.warn('NineSlicePlane can not be used with views set as Container.');
Expand Down Expand Up @@ -673,9 +675,9 @@ export class FancyButton extends ButtonContainer

/**
* Sets the iconView of the button.
* @param { string | Container } view - string (path to the image) or a Container-based view
* @param { string | Texture | Container } view - string (path to the image), texture instance or a Container-based view
*/
set iconView(view: ButtonView | null)
set iconView(view: GetViewSettings | null)
{
if (view === undefined) return;

Expand Down
8 changes: 6 additions & 2 deletions src/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Padding } from './utils/HelpTypes';
import { PixiText, PixiTextClass, PixiTextStyle } from './utils/helpers/text';
import { getView } from './utils/helpers/view';

type ViewType = Sprite | Graphics | string;
type ViewType = Sprite | Graphics | Texture | string;

export type InputOptions = {
bg: ViewType;
Expand Down Expand Up @@ -79,7 +79,7 @@ export class Input extends Container
/**
* Creates an input.
* @param { number } options - Options object to use.
* @param { Sprite | Graphics | string } options.bg - Background of the Input.
* @param { Sprite | Graphics | Texture | string } options.bg - Background of the Input.
* @param { Partial<TextStyle> } options.textStyle - Text style of the Input.
* @param { string } options.placeholder - Placeholder of the Input.
* @param { string } options.value - Value of the Input.
Expand Down Expand Up @@ -215,6 +215,10 @@ export class Input extends Container
{
this._bg = new NineSlicePlane(Texture.from(bg), ...this.options.nineSlicePlane);
}
else if (bg instanceof Texture)
{
this._bg = new NineSlicePlane(bg, ...this.options.nineSlicePlane);
}
else
{
console.warn('NineSlicePlane can not be used with views set as Container.');
Expand Down
34 changes: 15 additions & 19 deletions src/ProgressBar.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Container } from '@pixi/display';
import { Texture } from '@pixi/core';
import { Sprite } from '@pixi/sprite';
import { getSpriteView } from './utils/helpers/view';
import { getView, type GetViewSettings } from './utils/helpers/view';
import { NineSlicePlane as PixiNineSlicePlane } from '@pixi/mesh-extras';
import { Graphics } from '@pixi/graphics';

Expand All @@ -12,7 +12,7 @@ type FillPaddings = {
left?: number;
};

export type ProgressBarViewType = Sprite | Graphics | string;
export type ProgressBarViewType = GetViewSettings;
export type NineSlicePlane = {
bg: [number, number, number, number],
fill: [number, number, number, number]
Expand Down Expand Up @@ -54,8 +54,8 @@ export class ProgressBar extends Container
/**
* Creates a ProgressBar.
* @param options - Options.
* @param { Sprite | Graphics | string } options.bg - Background of the ProgressBar.
* @param { Sprite | Graphics | string } options.fill - Fill of the ProgressBar.
* @param { Sprite | Graphics | Texture | string } options.bg - Background of the ProgressBar.
* @param { Sprite | Graphics | Texture | string } options.fill - Fill of the ProgressBar.
* @param { FillPaddings } options.fillPaddings - Fill offsets.
* @param { number } options.fillPaddings.top - Fill top offset.
* @param { number } options.fillPaddings.right - Fill right offset.
Expand Down Expand Up @@ -115,20 +115,19 @@ export class ProgressBar extends Container
{
this.bg = new PixiNineSlicePlane(Texture.from(bg), ...this.options.nineSlicePlane.bg);
}
else if (bg instanceof Texture)
{
this.bg = new PixiNineSlicePlane(bg, ...this.options.nineSlicePlane.bg);
}
else
{
console.warn('NineSlicePlane can not be used with views set as Container.');
}
}

if (bg instanceof Graphics)
{
this.bg = bg;
}

if (!this.bg && (typeof bg === 'string' || bg instanceof Sprite))
if (!this.bg)
{
this.bg = getSpriteView(bg);
this.bg = getView(bg) as Sprite | Graphics;
}

this.innerView.addChildAt(this.bg, 0);
Expand Down Expand Up @@ -160,6 +159,10 @@ export class ProgressBar extends Container
{
this.fill = new PixiNineSlicePlane(Texture.from(fill), ...this.options.nineSlicePlane.fill);
}
else if (fill instanceof Texture)
{
this.fill = new PixiNineSlicePlane(fill, ...this.options.nineSlicePlane.fill);
}
else
{
console.warn('NineSlicePlane can not be used with views set as Container.');
Expand All @@ -168,14 +171,7 @@ export class ProgressBar extends Container

if (!this.fill)
{
if (fill instanceof Graphics)
{
this.fill = fill;
}
else
{
this.fill = getSpriteView(fill);
}
this.fill = getView(fill) as Sprite | Graphics;
}

this.innerView.addChildAt(this.fill, 1);
Expand Down
8 changes: 4 additions & 4 deletions src/Select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Signal } from 'typed-signals';
import { FancyButton } from './FancyButton';
import { ScrollBox, ScrollBoxOptions } from './ScrollBox';
import { PixiText, PixiTextStyle } from './utils/helpers/text';
import { getView } from './utils/helpers/view';
import { getView, type GetViewSettings } from './utils/helpers/view';

const defaultVisibleItems = 5;

Expand All @@ -26,8 +26,8 @@ export type SelectItemsOptions = {
};

export type SelectOptions = {
closedBG: string | Container;
openBG: string | Container;
closedBG: GetViewSettings;
openBG: GetViewSettings;
textStyle?: PixiTextStyle;
TextClass?: new (...args: any[]) => PixiText;
selected?: number;
Expand Down Expand Up @@ -123,7 +123,7 @@ export class Select extends Container
if (!this.openButton)
{
this.openButton = new FancyButton({
defaultView: getView(closedBG),
defaultView: closedBG,
text: new TextClass(items?.items ? items.items[0] : '', textStyle),
textOffset: selectedTextOffset
});
Expand Down
9 changes: 5 additions & 4 deletions src/SliderBase.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Texture } from '@pixi/core';
import { Container } from '@pixi/display';
import { FederatedPointerEvent } from '@pixi/events';
import { Sprite } from '@pixi/sprite';
import { Text } from '@pixi/text';
import { ProgressBar, ProgressBarOptions, ProgressBarViewType } from './ProgressBar';
import type { DragObject } from './utils/HelpTypes';
import { PixiText, PixiTextClass, PixiTextStyle } from './utils/helpers/text';
import { getView } from './utils/helpers/view';
import { getView, type GetViewSettings } from './utils/helpers/view';

export type BaseSliderOptions = ProgressBarOptions & {
min?: number;
Expand Down Expand Up @@ -83,7 +84,7 @@ export class SliderBase extends ProgressBar
* Sets Slider1 instance.
* @param value - Container or string with texture name.
*/
set slider1(value: Container | string)
set slider1(value: Container | Texture | string)
{
if (!value) return;

Expand Down Expand Up @@ -169,7 +170,7 @@ export class SliderBase extends ProgressBar
.on('pointerupoutside', this.endUpdate, this);
}

protected createSlider(sliderData: Container | string): Container
protected createSlider(sliderData: GetViewSettings): Container
{
const slider = getView(sliderData);
const onPointerDown = (event: FederatedPointerEvent) =>
Expand Down Expand Up @@ -197,7 +198,7 @@ export class SliderBase extends ProgressBar
slider.anchor.set(0.5);
}

container.y = this.bg?.height / 2 ?? 0;
container.y = this.bg?.height ? this.bg?.height / 2 : 0;

this.addChild(container);

Expand Down
4 changes: 2 additions & 2 deletions src/Switcher.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Container } from '@pixi/display';
import { Signal } from 'typed-signals';
import { getView } from './utils/helpers/view';
import { getView, type GetViewSettings } from './utils/helpers/view';
import { ButtonEvent } from './utils/HelpTypes';

/**
Expand Down Expand Up @@ -100,7 +100,7 @@ export class Switcher extends Container
* Adds view instance to a switching list.
* @param view
*/
add(view: Container | string): void
add(view: GetViewSettings): void
{
const viewInstance = getView(view);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { argTypes, getDefaultArgs } from '../utils/argTypes';
import { Sprite } from '@pixi/sprite';
import type { StoryFn } from '@storybook/types';
import { List } from '../../List';
import { ProgressBar } from '../../ProgressBar';
import { centerElement } from '../../utils/helpers/resize';
import { argTypes, getDefaultArgs } from '../utils/argTypes';
import { preload } from '../utils/loader';
import type { StoryFn } from '@storybook/types';
import { List } from '../../List';

const args = {
value: 50,
Expand All @@ -25,7 +26,7 @@ export const NineSlicePlane: StoryFn = ({ value, animate, vertical, width, heigh
{
// Component usage !!!
progressBar = new ProgressBar({
bg: 'slider_bg.png',
bg: Sprite.from('slider_bg.png'),
fill: 'slider_progress.png',
nineSlicePlane: {
bg: [22, 15, 22, 23],
Expand Down
9 changes: 5 additions & 4 deletions src/stories/progressBar/ProgressBarSprite.stories.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { argTypes, getDefaultArgs } from '../utils/argTypes';
import { Texture } from '@pixi/core';
import type { StoryFn } from '@storybook/types';
import { List } from '../../List';
import { ProgressBar } from '../../ProgressBar';
import { centerElement } from '../../utils/helpers/resize';
import { argTypes, getDefaultArgs } from '../utils/argTypes';
import { preload } from '../utils/loader';
import type { StoryFn } from '@storybook/types';
import { List } from '../../List';

const args = {
value: 50,
Expand All @@ -23,7 +24,7 @@ export const Sprite: StoryFn = ({ value, animate, vertical }: any) =>
{
// Component usage !!!
progressBar = new ProgressBar({
bg: 'slider_bg.png',
bg: Texture.from('slider_bg.png'),
fill: 'slider_progress.png',
progress: value,
fillPaddings: {
Expand Down
Loading