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

Add Content Fitting Mode Option on Fancy Button #202

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
70 changes: 65 additions & 5 deletions src/FancyButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ type ViewsInput = BasicViewsInput & {
icon?: ButtonView;
};

type ContentFittingMode =
// Fits the text/icon content inside the button.
| 'default'
// Fill the button with the text/icon content, scaling it up to fill the view space with padding accounted for.
| 'fill'
// Only apply the default scaling and anchoring, without constraining to the button view's dimensions.
| 'none';

export type ButtonOptions = ViewsInput & {
padding?: number;
scale?: number;
Expand All @@ -68,6 +76,9 @@ export type ButtonOptions = ViewsInput & {
defaultIconAnchor?: Pos | number;
animations?: StateAnimations;
nineSliceSprite?: [number, number, number, number];
contentFittingMode?: ContentFittingMode;

/** @deprecated refer to contentFittingMode instead */
ignoreRefitting?: boolean;
};

Expand Down Expand Up @@ -413,12 +424,29 @@ export class FancyButton extends ButtonContainer

if (activeView)
{
if (!this.options?.ignoreRefitting)
if (!this.options.ignoreRefitting)
{
this._views.textView.scale.set(this._defaultTextScale.x, this._defaultTextScale.y);
}

fitToView(activeView, this._views.textView, this.padding, false);
if (this.contentFittingMode === 'default')
{
fitToView(activeView, this._views.textView, this.padding, false);
}

if (this.contentFittingMode === 'fill')
{
// reset to base dimensions for calculations
this._views.textView.scale.set(1);

const availableWidth = activeView.width - (this.padding * 2);
const availableHeight = activeView.height - (this.padding * 2);
const targetScaleX = availableWidth / this._views.textView.width;
const targetScaleY = availableHeight / this._views.textView.height;
const scale = Math.min(targetScaleX, targetScaleY);

this._views.textView.scale.set(scale * this._defaultTextScale.x, scale * this._defaultTextScale.y);
}

this._views.textView.x = activeView.x + (activeView.width / 2);
this._views.textView.y = activeView.y + (activeView.height / 2);
Expand Down Expand Up @@ -447,14 +475,31 @@ export class FancyButton extends ButtonContainer
return;
}

if (!this.options?.ignoreRefitting)
if (!this.options.ignoreRefitting)
{
this._views.iconView.scale.set(this._defaultIconScale.x, this._defaultIconScale.y);
}

const { x: anchorX, y: anchorY } = this._defaultIconAnchor;
if (this.contentFittingMode === 'default')
{
fitToView(activeView, this._views.iconView, this.padding, false);
}

if (this.contentFittingMode === 'fill')
{
// reset to base dimensions for calculations
this._views.iconView.scale.set(1);

const availableWidth = activeView.width - (this.padding * 2);
const availableHeight = activeView.height - (this.padding * 2);
const targetScaleX = availableWidth / this._views.iconView.width;
const targetScaleY = availableHeight / this._views.iconView.height;
const scale = Math.min(targetScaleX, targetScaleY);

fitToView(activeView, this._views.iconView, this.padding, false);
this._views.iconView.scale.set(scale * this._defaultIconScale.x, scale * this._defaultIconScale.y);
}

const { x: anchorX, y: anchorY } = this._defaultIconAnchor;

if ('anchor' in this._views.iconView)
{
Expand Down Expand Up @@ -509,6 +554,21 @@ export class FancyButton extends ButtonContainer
this.adjustTextView(this.state);
}

/**
* Sets the fitting mode for the button's content.
* @param {ContentFittingMode} mode - fitting mode type.
*/
set contentFittingMode(mode: ContentFittingMode)
{
this.options.contentFittingMode = mode;
}

/** Returns the fitting mode for the button's content, defaulting to 'default'. */
get contentFittingMode(): ContentFittingMode
{
return this.options.contentFittingMode ?? 'default';
}

/**
* Sets the default view of the button.
* @param { string | Container } view - string (path to the image) or a Container-based view
Expand Down
101 changes: 51 additions & 50 deletions src/stories/button/ButtonContainerSprite.stories.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,60 @@
import { Container, Graphics, Text } from "pixi.js";
import { PixiStory, StoryFn } from "@pixi/storybook-renderer";
import { ButtonContainer } from "../../Button";
import { centerElement } from "../../utils/helpers/resize";
import { argTypes, getDefaultArgs } from "../utils/argTypes";
import { action } from "@storybook/addon-actions";
import { Container, Graphics, Text } from 'pixi.js';
import { PixiStory, StoryFn } from '@pixi/storybook-renderer';
import { ButtonContainer } from '../../Button';
import { centerElement } from '../../utils/helpers/resize';
import { argTypes, getDefaultArgs } from '../utils/argTypes';
import { action } from '@storybook/addon-actions';

const args = {
color: "#A5E24D",
size: 150,
radius: 150,
disabled: false,
action: action("Button"),
color: '#A5E24D',
size: 150,
radius: 150,
disabled: false,
action: action('Button'),
};

export const ButtonContainerSprite: StoryFn<typeof args> = (
{ size, color, disabled, radius, action },
context,
) =>
new PixiStory<typeof args>({
{ size, color, disabled, radius, action },
context,
init(view) {
// Component usage !!!
const button = new ButtonContainer();

button.enabled = !disabled;

button.onPress.connect(() => action("onPress"));
button.onDown.connect(() => action("onDown"));
button.onUp.connect(() => action("onUp"));
button.onHover.connect(() => action("onHover"));
button.onOut.connect(() => action("onOut"));
button.onUpOut.connect(() => action("onUpOut"));

const buttonView = new Container();
const buttonBg = new Graphics()
.roundRect(0, 0, size, size, radius)
.fill(color);
const text = new Text({ text: "🤙", style: { fontSize: 70 } });

text.anchor.set(0.5);
text.x = buttonBg.width / 2;
text.y = buttonBg.height / 2;

buttonView.addChild(buttonBg, text);

button.addChild(buttonView);
view.addChild(button);

centerElement(button);
},
resize: (view) => centerElement(view.children[0]),
});
) =>
new PixiStory<typeof args>({
context,
init(view)
{
// Component usage !!!
const button = new ButtonContainer();

button.enabled = !disabled;

button.onPress.connect(() => action('onPress'));
button.onDown.connect(() => action('onDown'));
button.onUp.connect(() => action('onUp'));
button.onHover.connect(() => action('onHover'));
button.onOut.connect(() => action('onOut'));
button.onUpOut.connect(() => action('onUpOut'));

const buttonView = new Container();
const buttonBg = new Graphics()
.roundRect(0, 0, size, size, radius)
.fill(color);
const text = new Text({ text: '🤙', style: { fontSize: 70 } });

text.anchor.set(0.5);
text.x = buttonBg.width / 2;
text.y = buttonBg.height / 2;

buttonView.addChild(buttonBg, text);

button.addChild(buttonView);
view.addChild(button);

centerElement(button);
},
resize: (view) => centerElement(view.children[0]),
});

export default {
title: "Components/Button/Button Container Sprite",
argTypes: argTypes(args),
args: getDefaultArgs(args),
title: 'Components/Button/Button Container Sprite',
argTypes: argTypes(args),
args: getDefaultArgs(args),
};
97 changes: 49 additions & 48 deletions src/stories/button/ButtonGraphics.stories.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,58 @@
import { Container, Graphics, Text } from "pixi.js";
import { PixiStory, StoryFn } from "@pixi/storybook-renderer";
import { Button } from "../../Button";
import { centerElement } from "../../utils/helpers/resize";
import { argTypes, getDefaultArgs } from "../utils/argTypes";
import { action } from "@storybook/addon-actions";
import { Container, Graphics, Text } from 'pixi.js';
import { PixiStory, StoryFn } from '@pixi/storybook-renderer';
import { Button } from '../../Button';
import { centerElement } from '../../utils/helpers/resize';
import { argTypes, getDefaultArgs } from '../utils/argTypes';
import { action } from '@storybook/addon-actions';

const args = {
color: "#A5E24D",
size: 150,
radius: 150,
disabled: false,
action: action("Button"),
color: '#A5E24D',
size: 150,
radius: 150,
disabled: false,
action: action('Button'),
};

export const UseGraphics: StoryFn<typeof args> = (
{ size, color, disabled, radius, action },
context,
) =>
new PixiStory<typeof args>({
{ size, color, disabled, radius, action },
context,
init: (view) => {
const buttonView = new Container();
const buttonBg = new Graphics()
.roundRect(0, 0, size, size, radius)
.fill(color);
const text = new Text({ text: "🤙", style: { fontSize: 70 } });

text.anchor.set(0.5);
text.x = buttonBg.width / 2;
text.y = buttonBg.height / 2;

buttonView.addChild(buttonBg, text);

// Component usage !!!
const button = new Button(buttonView);

button.enabled = !disabled;

button.onPress.connect(() => action("onPress"));
button.onDown.connect(() => action("onDown"));
button.onUp.connect(() => action("onUp"));
button.onHover.connect(() => action("onHover"));
button.onOut.connect(() => action("onOut"));
button.onUpOut.connect(() => action("onUpOut"));
view.addChild(buttonView);

centerElement(buttonView);
},
resize: (view) => centerElement(view.children[0]),
});
) =>
new PixiStory<typeof args>({
context,
init: (view) =>
{
const buttonView = new Container();
const buttonBg = new Graphics()
.roundRect(0, 0, size, size, radius)
.fill(color);
const text = new Text({ text: '🤙', style: { fontSize: 70 } });

text.anchor.set(0.5);
text.x = buttonBg.width / 2;
text.y = buttonBg.height / 2;

buttonView.addChild(buttonBg, text);

// Component usage !!!
const button = new Button(buttonView);

button.enabled = !disabled;

button.onPress.connect(() => action('onPress'));
button.onDown.connect(() => action('onDown'));
button.onUp.connect(() => action('onUp'));
button.onHover.connect(() => action('onHover'));
button.onOut.connect(() => action('onOut'));
button.onUpOut.connect(() => action('onUpOut'));
view.addChild(buttonView);

centerElement(buttonView);
},
resize: (view) => centerElement(view.children[0]),
});

export default {
title: "Components/Button/Use Graphics",
argTypes: argTypes(args),
args: getDefaultArgs(args),
title: 'Components/Button/Use Graphics',
argTypes: argTypes(args),
args: getDefaultArgs(args),
};
Loading
Loading