2024-05-03 17:59:10 +01:00
|
|
|
import BattleScene from "../battle-scene";
|
2023-11-14 03:29:03 +00:00
|
|
|
import { TextStyle, addTextObject } from "./text";
|
|
|
|
import { Mode } from "./ui";
|
2023-12-21 00:19:23 +00:00
|
|
|
import UiHandler from "./ui-handler";
|
2024-04-01 02:14:35 +01:00
|
|
|
import { addWindow } from "./ui-theme";
|
2024-04-04 20:22:05 +01:00
|
|
|
import * as Utils from "../utils";
|
2024-04-13 23:59:58 +01:00
|
|
|
import { argbFromRgba } from "@material/material-color-utilities";
|
2024-05-03 17:59:10 +01:00
|
|
|
import {Button} from "#app/inputs-controller";
|
2023-11-14 03:29:03 +00:00
|
|
|
|
2024-01-11 17:26:32 +00:00
|
|
|
export interface OptionSelectConfig {
|
|
|
|
xOffset?: number;
|
2024-02-07 04:11:00 +00:00
|
|
|
yOffset?: number;
|
2024-01-11 17:26:32 +00:00
|
|
|
options: OptionSelectItem[];
|
2024-02-21 20:47:44 +00:00
|
|
|
maxOptions?: integer;
|
2024-04-04 20:22:05 +01:00
|
|
|
delay?: integer;
|
2024-03-15 19:13:32 +00:00
|
|
|
noCancel?: boolean;
|
2024-01-11 17:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface OptionSelectItem {
|
|
|
|
label: string;
|
2024-04-13 23:59:58 +01:00
|
|
|
handler: () => boolean;
|
2024-01-11 17:26:32 +00:00
|
|
|
keepOpen?: boolean;
|
|
|
|
overrideSound?: boolean;
|
2024-04-13 23:59:58 +01:00
|
|
|
item?: string;
|
|
|
|
itemArgs?: any[]
|
2024-01-11 17:26:32 +00:00
|
|
|
}
|
2023-11-14 03:29:03 +00:00
|
|
|
|
2024-02-21 20:47:44 +00:00
|
|
|
const scrollUpLabel = '↑';
|
|
|
|
const scrollDownLabel = '↓';
|
|
|
|
|
2024-01-11 17:26:32 +00:00
|
|
|
export default abstract class AbstractOptionSelectUiHandler extends UiHandler {
|
2023-11-14 03:29:03 +00:00
|
|
|
protected optionSelectContainer: Phaser.GameObjects.Container;
|
|
|
|
protected optionSelectBg: Phaser.GameObjects.NineSlice;
|
|
|
|
protected optionSelectText: Phaser.GameObjects.Text;
|
2024-04-13 23:59:58 +01:00
|
|
|
protected optionSelectIcons: Phaser.GameObjects.Sprite[];
|
2023-11-14 03:29:03 +00:00
|
|
|
|
2024-01-11 17:26:32 +00:00
|
|
|
protected config: OptionSelectConfig;
|
|
|
|
|
2024-04-04 20:22:05 +01:00
|
|
|
protected blockInput: boolean;
|
|
|
|
|
2024-02-21 20:47:44 +00:00
|
|
|
protected scrollCursor: integer = 0;
|
|
|
|
|
2023-11-14 03:29:03 +00:00
|
|
|
private cursorObj: Phaser.GameObjects.Image;
|
|
|
|
|
|
|
|
constructor(scene: BattleScene, mode?: Mode) {
|
|
|
|
super(scene, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract getWindowWidth(): integer;
|
|
|
|
|
2024-01-11 17:26:32 +00:00
|
|
|
getWindowHeight(): integer {
|
2024-02-21 20:47:44 +00:00
|
|
|
return (Math.min((this.config?.options || []).length, this.config?.maxOptions || 99) + 1) * 16;
|
2024-01-11 17:26:32 +00:00
|
|
|
}
|
2023-11-14 03:29:03 +00:00
|
|
|
|
|
|
|
setup() {
|
|
|
|
const ui = this.getUi();
|
|
|
|
|
|
|
|
this.optionSelectContainer = this.scene.add.container((this.scene.game.canvas.width / 6) - 1, -48);
|
|
|
|
this.optionSelectContainer.setVisible(false);
|
|
|
|
ui.add(this.optionSelectContainer);
|
|
|
|
|
2023-12-21 03:22:15 +00:00
|
|
|
this.optionSelectBg = addWindow(this.scene, 0, 0, this.getWindowWidth(), this.getWindowHeight());
|
2023-11-14 03:29:03 +00:00
|
|
|
this.optionSelectBg.setOrigin(1, 1);
|
|
|
|
this.optionSelectContainer.add(this.optionSelectBg);
|
|
|
|
|
2024-04-13 23:59:58 +01:00
|
|
|
this.optionSelectIcons = [];
|
|
|
|
|
2023-11-14 03:29:03 +00:00
|
|
|
this.setCursor(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected setupOptions() {
|
2024-01-11 17:26:32 +00:00
|
|
|
const options = this.config?.options || [];
|
2023-11-14 03:29:03 +00:00
|
|
|
|
|
|
|
if (this.optionSelectText)
|
|
|
|
this.optionSelectText.destroy();
|
2024-04-13 23:59:58 +01:00
|
|
|
if (this.optionSelectIcons?.length) {
|
|
|
|
this.optionSelectIcons.map(i => i.destroy());
|
|
|
|
this.optionSelectIcons.splice(0, this.optionSelectIcons.length);
|
|
|
|
}
|
2023-11-14 03:29:03 +00:00
|
|
|
|
2024-04-13 23:59:58 +01:00
|
|
|
this.optionSelectText = addTextObject(this.scene, 0, 0, options.map(o => o.item ? ` ${o.label}` : o.label).join('\n'), TextStyle.WINDOW, { maxLines: options.length });
|
2023-11-14 03:29:03 +00:00
|
|
|
this.optionSelectText.setLineSpacing(12);
|
|
|
|
this.optionSelectContainer.add(this.optionSelectText);
|
2024-02-07 04:11:00 +00:00
|
|
|
this.optionSelectContainer.setPosition((this.scene.game.canvas.width / 6) - 1 - (this.config?.xOffset || 0), -48 + (this.config?.yOffset || 0));
|
2023-11-14 03:29:03 +00:00
|
|
|
|
|
|
|
this.optionSelectBg.width = Math.max(this.optionSelectText.displayWidth + 24, this.getWindowWidth());
|
2024-02-21 20:47:44 +00:00
|
|
|
|
|
|
|
if (this.config?.options.length > this.config?.maxOptions)
|
|
|
|
this.optionSelectText.setText(this.getOptionsWithScroll().map(o => o.label).join('\n'));
|
|
|
|
|
2023-11-14 03:29:03 +00:00
|
|
|
this.optionSelectBg.height = this.getWindowHeight();
|
|
|
|
|
|
|
|
this.optionSelectText.setPositionRelative(this.optionSelectBg, 16, 9);
|
2024-04-13 23:59:58 +01:00
|
|
|
|
|
|
|
options.forEach((option: OptionSelectItem, i: integer) => {
|
|
|
|
if (option.item) {
|
|
|
|
const itemIcon = this.scene.add.sprite(0, 0, 'items', option.item);
|
|
|
|
itemIcon.setScale(0.5);
|
|
|
|
this.optionSelectIcons.push(itemIcon);
|
|
|
|
|
|
|
|
this.optionSelectContainer.add(itemIcon);
|
|
|
|
|
|
|
|
itemIcon.setPositionRelative(this.optionSelectText, 6, 7 + 16 * i);
|
|
|
|
|
|
|
|
if (option.item === 'candy') {
|
|
|
|
const itemOverlayIcon = this.scene.add.sprite(0, 0, 'items', 'candy_overlay');
|
|
|
|
itemOverlayIcon.setScale(0.5);
|
|
|
|
this.optionSelectIcons.push(itemOverlayIcon);
|
|
|
|
|
|
|
|
this.optionSelectContainer.add(itemOverlayIcon);
|
|
|
|
|
|
|
|
itemOverlayIcon.setPositionRelative(this.optionSelectText, 6, 7 + 16 * i);
|
|
|
|
|
|
|
|
itemIcon.setTint(argbFromRgba(Utils.rgbHexToRgba(option.itemArgs[0])));
|
|
|
|
itemOverlayIcon.setTint(argbFromRgba(Utils.rgbHexToRgba(option.itemArgs[1])));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2023-11-14 03:29:03 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:41:25 +00:00
|
|
|
show(args: any[]): boolean {
|
2024-01-11 17:26:32 +00:00
|
|
|
if (!args.length || !args[0].hasOwnProperty('options') || !args[0].options.length)
|
|
|
|
return false;
|
2023-11-14 03:29:03 +00:00
|
|
|
|
2024-01-11 17:26:32 +00:00
|
|
|
super.show(args);
|
2023-11-14 03:29:03 +00:00
|
|
|
|
2024-01-11 17:26:32 +00:00
|
|
|
this.config = args[0] as OptionSelectConfig;
|
|
|
|
this.setupOptions();
|
2023-12-30 23:41:25 +00:00
|
|
|
|
2024-01-11 17:26:32 +00:00
|
|
|
this.scene.ui.bringToTop(this.optionSelectContainer);
|
2023-12-30 23:41:25 +00:00
|
|
|
|
2024-01-11 17:26:32 +00:00
|
|
|
this.optionSelectContainer.setVisible(true);
|
2024-02-21 20:47:44 +00:00
|
|
|
this.scrollCursor = 0;
|
2024-01-11 17:26:32 +00:00
|
|
|
this.setCursor(0);
|
|
|
|
|
2024-04-04 20:22:05 +01:00
|
|
|
if (this.config.delay) {
|
|
|
|
this.blockInput = true;
|
|
|
|
this.optionSelectText.setAlpha(0.5);
|
2024-04-07 15:28:23 +01:00
|
|
|
this.scene.time.delayedCall(Utils.fixedInt(this.config.delay), () => this.unblockInput());
|
2024-04-04 20:22:05 +01:00
|
|
|
}
|
|
|
|
|
2024-01-11 17:26:32 +00:00
|
|
|
return true;
|
2023-11-14 03:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
processInput(button: Button): boolean {
|
|
|
|
const ui = this.getUi();
|
|
|
|
|
|
|
|
let success = false;
|
|
|
|
|
2024-02-21 20:47:44 +00:00
|
|
|
const options = this.getOptionsWithScroll();
|
2024-01-11 17:26:32 +00:00
|
|
|
|
|
|
|
let playSound = true;
|
2023-11-14 03:29:03 +00:00
|
|
|
|
|
|
|
if (button === Button.ACTION || button === Button.CANCEL) {
|
2024-04-07 15:28:23 +01:00
|
|
|
if (this.blockInput) {
|
|
|
|
ui.playError();
|
2024-04-04 23:07:24 +01:00
|
|
|
return false;
|
2024-04-07 15:28:23 +01:00
|
|
|
}
|
2024-04-04 23:07:24 +01:00
|
|
|
|
2023-11-14 03:29:03 +00:00
|
|
|
success = true;
|
2024-02-21 20:47:44 +00:00
|
|
|
if (button === Button.CANCEL) {
|
|
|
|
if (this.config?.maxOptions && this.config.options.length > this.config.maxOptions) {
|
|
|
|
this.scrollCursor = (this.config.options.length - this.config.maxOptions) + 1;
|
|
|
|
this.cursor = options.length - 1;
|
2024-03-15 19:13:32 +00:00
|
|
|
} else if (!this.config?.noCancel)
|
2024-02-21 20:47:44 +00:00
|
|
|
this.setCursor(options.length - 1);
|
2024-03-15 19:13:32 +00:00
|
|
|
else
|
|
|
|
return false;
|
2024-02-21 20:47:44 +00:00
|
|
|
}
|
|
|
|
const option = this.config.options[this.cursor + (this.scrollCursor - (this.scrollCursor ? 1 : 0))];
|
2024-04-13 23:59:58 +01:00
|
|
|
if (option.handler()) {
|
|
|
|
if (!option.keepOpen)
|
|
|
|
this.clear();
|
|
|
|
playSound = !option.overrideSound;
|
|
|
|
} else
|
|
|
|
ui.playError();
|
2023-11-14 03:29:03 +00:00
|
|
|
} else {
|
|
|
|
switch (button) {
|
|
|
|
case Button.UP:
|
|
|
|
if (this.cursor)
|
|
|
|
success = this.setCursor(this.cursor - 1);
|
|
|
|
break;
|
|
|
|
case Button.DOWN:
|
|
|
|
if (this.cursor < options.length - 1)
|
|
|
|
success = this.setCursor(this.cursor + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-11 17:26:32 +00:00
|
|
|
if (success && playSound)
|
2023-11-14 03:29:03 +00:00
|
|
|
ui.playSelect();
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2024-04-07 15:28:23 +01:00
|
|
|
unblockInput(): void {
|
|
|
|
if (!this.blockInput)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.blockInput = false;
|
|
|
|
this.optionSelectText.setAlpha(1);
|
|
|
|
}
|
|
|
|
|
2024-02-21 20:47:44 +00:00
|
|
|
getOptionsWithScroll(): OptionSelectItem[] {
|
|
|
|
if (!this.config)
|
|
|
|
return [];
|
|
|
|
|
|
|
|
const options = this.config.options.slice(0);
|
|
|
|
|
|
|
|
if (!this.config.maxOptions || this.config.options.length < this.config.maxOptions)
|
|
|
|
return options;
|
|
|
|
|
|
|
|
const optionsScrollTotal = options.length;
|
|
|
|
let optionStartIndex = this.scrollCursor;
|
|
|
|
let optionEndIndex = Math.min(optionsScrollTotal, optionStartIndex + (!optionStartIndex || this.scrollCursor + (this.config.maxOptions - 1) >= optionsScrollTotal ? this.config.maxOptions - 1 : this.config.maxOptions - 2));
|
|
|
|
|
|
|
|
if (this.config?.maxOptions && options.length > this.config.maxOptions) {
|
|
|
|
options.splice(optionEndIndex, optionsScrollTotal);
|
|
|
|
options.splice(0, optionStartIndex);
|
|
|
|
if (optionStartIndex)
|
|
|
|
options.unshift({
|
|
|
|
label: scrollUpLabel,
|
2024-04-13 23:59:58 +01:00
|
|
|
handler: () => true
|
2024-02-21 20:47:44 +00:00
|
|
|
});
|
|
|
|
if (optionEndIndex < optionsScrollTotal)
|
|
|
|
options.push({
|
|
|
|
label: scrollDownLabel,
|
2024-04-13 23:59:58 +01:00
|
|
|
handler: () => true
|
2024-02-21 20:47:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2023-11-14 03:29:03 +00:00
|
|
|
setCursor(cursor: integer): boolean {
|
2024-02-21 20:47:44 +00:00
|
|
|
const changed = this.cursor !== cursor;
|
|
|
|
|
|
|
|
let isScroll = false;
|
|
|
|
const options = this.getOptionsWithScroll();
|
|
|
|
if (changed && this.config?.maxOptions && this.config.options.length > this.config.maxOptions) {
|
|
|
|
const optionsScrollTotal = options.length;
|
|
|
|
if (Math.abs(cursor - this.cursor) === options.length - 1) {
|
|
|
|
this.scrollCursor = cursor ? optionsScrollTotal - (this.config.maxOptions - 1) : 0;
|
|
|
|
this.setupOptions();
|
|
|
|
} else {
|
|
|
|
const isDown = cursor && cursor > this.cursor;
|
|
|
|
if (isDown) {
|
|
|
|
if (options[cursor].label === scrollDownLabel) {
|
|
|
|
isScroll = true;
|
|
|
|
this.scrollCursor++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!cursor && this.scrollCursor) {
|
|
|
|
isScroll = true;
|
|
|
|
this.scrollCursor--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isScroll && this.scrollCursor === 1)
|
|
|
|
this.scrollCursor += isDown ? 1 : -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isScroll)
|
|
|
|
this.setupOptions();
|
|
|
|
else
|
|
|
|
this.cursor = cursor;
|
2023-11-14 03:29:03 +00:00
|
|
|
|
|
|
|
if (!this.cursorObj) {
|
|
|
|
this.cursorObj = this.scene.add.image(0, 0, 'cursor');
|
|
|
|
this.optionSelectContainer.add(this.cursorObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.cursorObj.setPositionRelative(this.optionSelectBg, 12, 17 + this.cursor * 16);
|
|
|
|
|
2024-02-21 20:47:44 +00:00
|
|
|
return changed;
|
2023-11-14 03:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
super.clear();
|
2024-01-11 17:26:32 +00:00
|
|
|
this.config = null;
|
2023-11-14 03:29:03 +00:00
|
|
|
this.optionSelectContainer.setVisible(false);
|
|
|
|
this.eraseCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
eraseCursor() {
|
|
|
|
if (this.cursorObj)
|
|
|
|
this.cursorObj.destroy();
|
|
|
|
this.cursorObj = null;
|
|
|
|
}
|
|
|
|
}
|