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 #198

Merged
merged 5 commits into from
Nov 27, 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
8 changes: 4 additions & 4 deletions src/CheckBox.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Container, Text } from 'pixi.js';
import { Text } from 'pixi.js';
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
4 changes: 2 additions & 2 deletions src/DoubleSlider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Container, FederatedPointerEvent, Optional, Size } from 'pixi.js';
import { Container, FederatedPointerEvent, Optional, Size, Texture } from 'pixi.js';
import { Signal } from 'typed-signals';
import { DoubleSliderOptions, SliderBase } from './SliderBase';

Expand Down Expand Up @@ -165,7 +165,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
32 changes: 19 additions & 13 deletions src/FancyButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Group, Tween } from 'tweedle.js';
import { ButtonContainer } from './Button';
import { fitToView } from './utils/helpers/fit';
import { AnyText, getTextView, PixiText } from './utils/helpers/text';
import { getView } from './utils/helpers/view';
import { getView, type GetViewSettings } from './utils/helpers/view';

import type { Optional, Size, Sprite } from 'pixi.js';

Expand All @@ -24,8 +24,6 @@ export type Offset = Pos & PosList;

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

type ButtonView = string | Container;

type BasicButtonViews = {
[K in ButtonViewType]?: Container | NineSliceSprite;
};
Expand Down Expand Up @@ -53,12 +51,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 @@ -496,7 +494,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 @@ -509,7 +507,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') {
this._views.hoverView.visible = false;
Expand All @@ -522,7 +520,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) {
this._views.pressedView.visible = false;
Expand All @@ -535,7 +533,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) {
this._views.disabledView.visible = false;
Expand All @@ -550,9 +548,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;

this.removeView(viewType);
Expand All @@ -570,6 +568,14 @@ export class FancyButton extends ButtonContainer {
rightWidth: this.options.nineSliceSprite[2],
bottomHeight: this.options.nineSliceSprite[3],
});
} else if (view instanceof Texture) {
this._views[viewType] = new NineSliceSprite({
texture: view,
leftWidth: this.options.nineSliceSprite[0],
topHeight: this.options.nineSliceSprite[1],
rightWidth: this.options.nineSliceSprite[2],
bottomHeight: this.options.nineSliceSprite[3],
});
} else {
console.warn('NineSliceSprite can not be used with views set as Container.');
}
Expand Down Expand Up @@ -637,9 +643,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;

this.removeView('iconView');
Expand Down
12 changes: 10 additions & 2 deletions src/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { PixiText, PixiTextClass, PixiTextStyle } from './utils/helpers/text';
import { getView } from './utils/helpers/view';
import { Padding } from './utils/HelpTypes';

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

export type InputOptions = {
bg: ViewType;
Expand Down Expand Up @@ -88,7 +88,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 { PixiTextStyle } 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 @@ -210,6 +210,14 @@ export class Input extends Container {
rightWidth: this.options.nineSliceSprite[2],
bottomHeight: this.options.nineSliceSprite[3],
});
} else if (bg instanceof Texture) {
this._bg = new NineSliceSprite({
texture: bg,
leftWidth: this.options.nineSliceSprite[0],
topHeight: this.options.nineSliceSprite[1],
rightWidth: this.options.nineSliceSprite[2],
bottomHeight: this.options.nineSliceSprite[3],
});
} else {
console.warn('NineSliceSprite can not be used with views set as Container.');
}
Expand Down
42 changes: 24 additions & 18 deletions src/ProgressBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Sprite,
Texture,
} from 'pixi.js';
import { getSpriteView } from './utils/helpers/view';
import { getView, type GetViewSettings } from './utils/helpers/view';

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

export type ProgressBarViewType = Sprite | Graphics | string;
export type ProgressBarViewType = GetViewSettings;
export type NineSliceSprite = {
bg: [number, number, number, number];
fill: [number, number, number, number];
Expand Down Expand Up @@ -57,8 +57,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 @@ -116,17 +116,21 @@ export class ProgressBar extends Container {
rightWidth: this.options.nineSliceSprite.bg[2],
bottomHeight: this.options.nineSliceSprite.bg[3],
});
} else if (bg instanceof Texture) {
this.bg = new PixiNineSliceSprite({
texture: bg,
leftWidth: this.options.nineSliceSprite.bg[0],
topHeight: this.options.nineSliceSprite.bg[1],
rightWidth: this.options.nineSliceSprite.bg[2],
bottomHeight: this.options.nineSliceSprite.bg[3],
});
} else {
console.warn('NineSliceSprite 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)) {
this.bg = getSpriteView(bg);
if (!this.bg) {
this.bg = getView(bg) as Sprite | Graphics;
}

this.innerView.addChildAt(this.bg, 0);
Expand Down Expand Up @@ -158,17 +162,19 @@ export class ProgressBar extends Container {
rightWidth: this.options.nineSliceSprite.fill[2],
bottomHeight: this.options.nineSliceSprite.fill[3],
});
} else if (fill instanceof Texture) {
this.fill = new PixiNineSliceSprite({
texture: fill,
leftWidth: this.options.nineSliceSprite.fill[0],
topHeight: this.options.nineSliceSprite.fill[1],
rightWidth: this.options.nineSliceSprite.fill[2],
bottomHeight: this.options.nineSliceSprite.fill[3],
});
} else {
console.warn('NineSliceSprite can not be used with views set as Container.');
}
}

if (!this.fill) {
if (fill instanceof Graphics) {
this.fill = fill;
} else {
this.fill = getSpriteView(fill);
}
} else {
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 @@ -3,7 +3,7 @@ import { Signal } from 'typed-signals';
import { FancyButton } from './FancyButton';
import { ScrollBox, ScrollBoxOptions } from './ScrollBox';
import { PixiTextClass, PixiTextStyle } from './utils/helpers/text';
import { getView } from './utils/helpers/view';
import { getView, type GetViewSettings } from './utils/helpers/view';

const defaultVisibleItems = 5;

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

export type SelectOptions = {
closedBG: string | Container;
openBG: string | Container;
closedBG: GetViewSettings;
openBG: GetViewSettings;
textStyle?: PixiTextStyle;
TextClass?: PixiTextClass;
selected?: number;
Expand Down Expand Up @@ -125,7 +125,7 @@ export class Select extends Container {
// openButton
if (!this.openButton) {
this.openButton = new FancyButton({
defaultView: getView(closedBG),
defaultView: closedBG,
text: new TextClass({
text: items?.items ? items.items[0] : '',
style: textStyle,
Expand Down
10 changes: 5 additions & 5 deletions src/SliderBase.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Container, FederatedPointerEvent, Sprite, Text } from 'pixi.js';
import { Container, FederatedPointerEvent, Sprite, Text, Texture } from 'pixi.js';
import { ProgressBar, ProgressBarOptions, ProgressBarViewType } from './ProgressBar';
import { PixiText, PixiTextClass, PixiTextStyle } from './utils/helpers/text';
import { getView } from './utils/helpers/view';
import { getView, type GetViewSettings } from './utils/helpers/view';

import type { DragObject } from './utils/HelpTypes';

Expand Down Expand Up @@ -77,7 +77,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;

if (this._slider1) {
Expand Down Expand Up @@ -158,7 +158,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) => {
// This is needed to do proper calculations in update method calls
Expand All @@ -183,7 +183,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.js';
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 @@ -95,7 +95,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);

this.innerView.addChild(viewInstance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ProgressBar } from '../../ProgressBar';
import { centerElement } from '../../utils/helpers/resize';
import { argTypes, getDefaultArgs } from '../utils/argTypes';
import { preload } from '../utils/loader';
import { Sprite } from 'pixi.js';

const args = {
value: 50,
Expand All @@ -30,7 +31,7 @@ export const NineSliceSprite: StoryFn<typeof args> = (
preload(assets).then(() => {
// Component usage !!!
progressBar = new ProgressBar({
bg: 'slider_bg.png',
bg: Sprite.from('slider_bg.png'),
fill: 'slider_progress.png',
nineSliceSprite: {
bg: [22, 15, 22, 23],
Expand Down
3 changes: 2 additions & 1 deletion src/stories/progressBar/ProgressBarSprite.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ProgressBar } from '../../ProgressBar';
import { centerElement } from '../../utils/helpers/resize';
import { argTypes, getDefaultArgs } from '../utils/argTypes';
import { preload } from '../utils/loader';
import { Texture } from 'pixi.js';

const args = {
value: 50,
Expand All @@ -24,7 +25,7 @@ export const Sprite: StoryFn<typeof args> = ({ value, animate, vertical }, conte
preload(assets).then(() => {
// Component usage !!!
progressBar = new ProgressBar({
bg: 'slider_bg.png',
bg: Texture.from('slider_bg.png'),
fill: 'slider_progress.png',
progress: value,
fillPaddings: {
Expand Down
14 changes: 6 additions & 8 deletions src/utils/helpers/view.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Container, Sprite } from 'pixi.js';
import { Sprite, Container, Texture, Graphics } from 'pixi.js';

export function getView(view: string | Container): Container {
export type GetViewSettings = string | Texture | Container | Sprite | Graphics;

export function getView(view: GetViewSettings): Container | Sprite | Graphics {
if (typeof view === 'string') {
return Sprite.from(view);
}

return view;
}

export function getSpriteView(view: string | Sprite): Sprite {
if (typeof view === 'string') {
return Sprite.from(view);
if (view instanceof Texture) {
return new Sprite(view);
}

return view;
Expand Down
Loading