Refactored conditional statements and variable assignments for correct functionality

This commit is contained in:
Adrian 2024-08-17 21:02:08 -04:00
parent 4dcfa357a7
commit 84d61d8dca
2 changed files with 18 additions and 15 deletions

View File

@ -51,6 +51,7 @@ export default class MenuUiHandler extends MessageUiHandler {
public bgmBar: BgmBar; public bgmBar: BgmBar;
constructor(scene: BattleScene, mode: Mode | null = null) { constructor(scene: BattleScene, mode: Mode | null = null) {
super(scene, mode); super(scene, mode);
@ -519,7 +520,7 @@ export default class MenuUiHandler extends MessageUiHandler {
const messageText = this.message; const messageText = this.message;
const messageBox = this.messageBoxBg; const messageBox = this.messageBoxBg;
this.adjustText(text,messageText,messageBox.getBounds().width,{ this.adjustText(text,messageText,messageBox.getBounds().width,{
padding: messageText.x / 2, padding: messageText.x,
ignoreTextBalance: "all" ignoreTextBalance: "all"
}); });

View File

@ -211,6 +211,7 @@ export default abstract class MessageUiHandler extends AwaitableUiHandler {
@argument ignoreBalanceText ignore Text Balance for some languages or for all. @argument ignoreBalanceText ignore Text Balance for some languages or for all.
@argument padding default 0. @argument padding default 0.
*/ */
adjustText(text:string,textObject:Phaser.GameObjects.Text,maxWidth:number,opts:argsAjustText={}): void { adjustText(text:string,textObject:Phaser.GameObjects.Text,maxWidth:number,opts:argsAjustText={}): void {
const currentLanguage = i18next.resolvedLanguage!; const currentLanguage = i18next.resolvedLanguage!;
if (opts.ignoreLanguages && opts.ignoreLanguages[0] && !opts.ignoreLanguages.some(localKey=>localKey === currentLanguage)) { if (opts.ignoreLanguages && opts.ignoreLanguages[0] && !opts.ignoreLanguages.some(localKey=>localKey === currentLanguage)) {
@ -220,33 +221,34 @@ export default abstract class MessageUiHandler extends AwaitableUiHandler {
const fontSizeToNumber = (FS: number|string):number => { const fontSizeToNumber = (FS: number|string):number => {
return parseInt(FS.toString().replace("px","")); return parseInt(FS.toString().replace("px",""));
}; };
const fontSize = fontSizeToNumber(textObject.getData("originalFontSize") ?? fontSizeToNumber(textObject.style.fontSize));
// If fontSize was modified before, revert to original
const fontSize = textObject.getData("originalFontSize") ?? fontSizeToNumber(textObject.style.fontSize);
textObject.setData("originalFontSize", textObject.getData("originalFontSize") ?? fontSize); textObject.setData("originalFontSize", textObject.getData("originalFontSize") ?? fontSize);
textObject.setFontSize(fontSize); textObject.setFontSize(fontSize);
const textWrapped = textObject.getWrappedText(text);
const textWrapped = ()=>textObject.getWrappedText(text);
const textSize = ()=>Phaser.GameObjects.GetTextSize(textObject,textObject.style.getTextMetrics(),textWrapped());
const balanceText = typeof opts.ignoreTextBalance === "string" ? opts.ignoreTextBalance === "all" : (opts.ignoreTextBalance && opts.ignoreTextBalance[0] && opts.ignoreTextBalance.some(localKey=> localKey === currentLanguage)); const balanceText = typeof opts.ignoreTextBalance === "string" ? opts.ignoreTextBalance === "all" : (opts.ignoreTextBalance && opts.ignoreTextBalance[0] && opts.ignoreTextBalance.some(localKey=> localKey === currentLanguage));
// Text Balance // Text Balance
if (!balanceText && textWrapped[1] && textWrapped.length <= textObject.style.maxLines && textWrapped[0].replace(" ","").length * 0.25 > textWrapped[1].replace(" ","").length) { if (!balanceText && textWrapped()[1] && textWrapped().length <= textObject.style.maxLines && textWrapped()[0].length * 0.25 > textWrapped()[1].length) {
textObject.setWordWrapWidth(maxWidth * 0.65); textObject.setWordWrapWidth(maxWidth * 0.65);
} }
const padding = opts.padding ?? 0;
const textSize = Phaser.GameObjects.GetTextSize(textObject,textObject.style.getTextMetrics(),textObject.getWrappedText(text));
// If is very near to border add "padding", not need if border container appareance is nice // If is very near to border add "padding", not need if border container appareance is nice
const textWidth = (textSize.width + padding * 17); const padding = opts.padding ?? 0;
// Text ajust // Text ajust
if (textWrapped.length > textObject.style.maxLines || textWidth > maxWidth) { if (textWrapped().length > textObject.style.maxLines || (textSize().width + padding) > maxWidth) {
let fontDecrement = fontSizeToNumber(textObject.style.fontSize);
while (textObject.getWrappedText(text).length > textObject.style.maxLines || (Phaser.GameObjects.GetTextSize(textObject,textObject.style.getTextMetrics(),textObject.getWrappedText(text)).width + padding * 17) > maxWidth) { let fontDecrement = fontSize;
while (textWrapped().length > textObject.style.maxLines || (textSize().width + padding) > maxWidth) {
fontDecrement -= 1; fontDecrement -= 1;
textObject.setFontSize(fontDecrement); textObject.setFontSize(fontDecrement);
} }
textObject.setFontSize(fontDecrement - padding); textObject.setFontSize(fontDecrement - padding / 2);
} else {
textObject.setFontSize(fontSize);
} }
} }
} }