pokerogue/src/ui/ui-theme.ts

172 lines
6.4 KiB
TypeScript
Raw Normal View History

import { UiTheme } from "#enums/ui-theme";
import { legacyCompatibleImages } from "#app/scene-base";
import BattleScene from "../battle-scene";
export enum WindowVariant {
NORMAL,
THIN,
XTHIN
}
export function getWindowVariantSuffix(windowVariant: WindowVariant): string {
switch (windowVariant) {
case WindowVariant.THIN:
return "_thin";
case WindowVariant.XTHIN:
return "_xthin";
default:
return "";
}
}
const windowTypeControlColors = {
[UiTheme.DEFAULT]: {
0: [ "#6b5a73", "#DD5748", "#7E4955" ],
1: [ "#6b5a73", "#48DDAA", "#4D7574" ],
2: [ "#6b5a73", "#C5C5C5", "#766D7E" ],
3: [ "#6b5a73", "#EBC07C", "#836C66" ],
4: [ "#686868", "#E8E8E8", "#919191" ]
},
[UiTheme.LEGACY]: {
0: [ "#706880", "#8888c8", "#484868" ],
1: [ "#d04028", "#e0a028", "#902008" ],
2: [ "#48b840", "#88d880", "#089040" ],
3: [ "#2068d0", "#80b0e0", "#104888" ],
4: [ "#706880", "#8888c8", "#484868" ]
}
};
export function addWindow(scene: BattleScene, x: number, y: number, width: number, height: number, mergeMaskTop?: boolean, mergeMaskLeft?: boolean, maskOffsetX?: number, maskOffsetY?: number, windowVariant?: WindowVariant): Phaser.GameObjects.NineSlice {
if (windowVariant === undefined) {
windowVariant = WindowVariant.NORMAL;
}
const borderSize = scene.uiTheme ? 6 : 8;
2024-05-23 23:45:04 +00:00
[Refactor] use typescript `strict-null` (#3259) * TS: enable strict-null * fix battle-scene.ts * fix voucher.ts * adapt more files to strict-null * adapt more files to strict-null ( 2) * adapt ability.ts to strict-null * adapt `arena.ts` to strict-null * adapt TagAddedEvent constructor to strict-null * adapt phases.ts.to strict-null * adapt status-effect.ts to strict-null * adapt `account.ts` to strict-null * adapt `configHandler.ts` to strict-null * adapt `ability.ts` to strict-null * adapt `biomes.ts` to strict-null * adapt `challenge.ts` to strict-null * adapt `daily-run.ts` to strict-null * adapt `nature.ts` to strict-null * adapt `pokemon-forms.ts` to strict-null * adapt `tainer-names.ts` to strict-null * adapt `types.ts` to strict-null * adapt `weather.ts` to strict-null * adapt `egg-hatch-phase.ts` to strict-null * adapt `evolution-phase.ts` to strict-null * adapt `pokemon-sprite-sparkle-handler.ts` to strict-null * adapt `evolution-phase.ts` to strict-null * adapt `game-mode.ts` to strict-null * adapt `utils.ts` to strict-null * adapt `voucher-ui-handler.ts` to strict-null * adapt `src/ui/unavailable-modal-ui-handler.ts` to strict-null * adapt `src/ui/ui.ts` to strict-null * adapt `src/ui/ui-theme.ts` to strict-null * adapt `src/ui/title-ui-handler.ts` to strict-null * adapt `src/ui/time-of-day-widget.ts` to strict-null * adapt `src/ui/text.ts` to strict-null * adapt `src/ui/target-select-ui-handler.ts` to strict-null * adapt `src/ui/settings/settings-keyboard-ui-handler.ts` to strict-null * adapt more files to strict-null (3) * adapt more files to strict-null (4) * adapt more files (mostly tests) to strict-null (5) * adapt more files to strict-null (6) * adapt more files to strict-null (7) * Update `src/data/pokemon-evolutions.ts` for strict-null Partial update `src/data/pokemon-species.ts` for strict-null * adapt more files to strict-null (8) * adapt more files to strict-null (9) * Strict some more nulls (still a few errors remaining) * adapt rest of the files to strict-null (9) * fix tests (check for null instead of undefined) * repalce a lot of `??` with bangs And added TODO notice as usual * fix more tests * all tests pass now * fix broken game-loop after trainer battle add some console.warn for missing cases and falling back to default * remove guessed fallback from utils.rgbHexToRgba * add TODO for this.currentBattle = null * adjust getPokemonById() return to include `null` * fix compilation errors * add test for pokemon.trySetStatus * `chanceMultiplier` shouldn't be optional * allow `null` for currentPhase * adjust hasExpSprite logic for no keymatch found * reduce bang usage in account.updateUserInfo() * fix new strict-null issues after merge * fix `strict-null` issues in dropdown.ts and sand_spit.test.ts * fix egg-gacha * adapt gul_missile.test.ts to strict-null * fix move.ts strict-null * fix i18n.ts strict-null * fix strict-null issues * fix baton_pass test after accidentially breaking it * chore: fix compiler errors * revert accidential changes in baton_pass.test.ts --------- Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
2024-08-07 16:23:12 +00:00
const window = scene.add.nineslice(x, y, `window_${scene.windowType}${getWindowVariantSuffix(windowVariant)}`, undefined, width, height, borderSize, borderSize, borderSize, borderSize);
window.setOrigin(0, 0);
if (mergeMaskLeft || mergeMaskTop || maskOffsetX || maskOffsetY) {
/**
* x: left
* y: top
* width: right
* height: bottom
*/
const maskRect = new Phaser.GameObjects.Rectangle(
scene,
6*(x - (mergeMaskLeft ? 2 : 0) - (maskOffsetX || 0)),
6*(y + (mergeMaskTop ? 2 : 0) + (maskOffsetY || 0)),
width - (mergeMaskLeft ? 2 : 0),
height - (mergeMaskTop ? 2 : 0),
0xffffff
);
maskRect.setOrigin(0);
maskRect.setScale(6);
const mask = maskRect.createGeometryMask();
window.setMask(mask);
}
return window;
}
export function updateWindowType(scene: BattleScene, windowTypeIndex: integer): void {
const windowObjects: [Phaser.GameObjects.NineSlice, WindowVariant][] = [];
const themedObjects: (Phaser.GameObjects.Image | Phaser.GameObjects.NineSlice)[] = [];
const traverse = (object: any) => {
if (object.hasOwnProperty("children") && object.children instanceof Phaser.GameObjects.DisplayList) {
const children = object.children as Phaser.GameObjects.DisplayList;
for (const child of children.getAll()) {
traverse(child);
}
} else if (object instanceof Phaser.GameObjects.Container) {
for (const child of object.getAll()) {
traverse(child);
}
} else if (object instanceof Phaser.GameObjects.NineSlice) {
if (object.texture.key.startsWith("window_")) {
windowObjects.push([ object, object.texture.key.endsWith(getWindowVariantSuffix(WindowVariant.XTHIN)) ? WindowVariant.XTHIN : object.texture.key.endsWith(getWindowVariantSuffix(WindowVariant.THIN)) ? WindowVariant.THIN : WindowVariant.NORMAL ]);
} else if (object.texture?.key === "namebox") {
themedObjects.push(object);
}
2024-04-01 14:06:28 +00:00
} else if (object instanceof Phaser.GameObjects.Sprite) {
if (object.texture?.key === "bg") {
2024-04-01 14:06:28 +00:00
themedObjects.push(object);
}
2024-04-01 14:06:28 +00:00
}
};
traverse(scene);
scene.windowType = windowTypeIndex;
const rootStyle = document.documentElement.style;
[ "base", "light", "dark" ].map((k, i) => rootStyle.setProperty(`--color-${k}`, windowTypeControlColors[scene.uiTheme][windowTypeIndex - 1][i]));
const windowKey = `window_${windowTypeIndex}`;
for (const [ window, variant ] of windowObjects) {
window.setTexture(`${windowKey}${getWindowVariantSuffix(variant)}`);
}
for (const obj of themedObjects) {
2024-04-01 14:06:28 +00:00
obj.setFrame(windowTypeIndex);
}
}
export function addUiThemeOverrides(scene: BattleScene): void {
const originalAddImage = scene.add.image;
scene.add.image = function (x: number, y: number, texture: string | Phaser.Textures.Texture, frame?: string | number): Phaser.GameObjects.Image {
let legacy = false;
if (typeof texture === "string" && scene.uiTheme && legacyCompatibleImages.includes(texture)) {
legacy = true;
texture += "_legacy";
}
const ret: Phaser.GameObjects.Image = originalAddImage.apply(this, [ x, y, texture, frame ]);
if (legacy) {
const originalSetTexture = ret.setTexture;
ret.setTexture = function (key: string, frame?: string | number) {
key += "_legacy";
return originalSetTexture.apply(this, [ key, frame ]);
};
}
return ret;
};
const originalAddSprite = scene.add.sprite;
scene.add.sprite = function (x: number, y: number, texture: string | Phaser.Textures.Texture, frame?: string | number): Phaser.GameObjects.Sprite {
let legacy = false;
if (typeof texture === "string" && scene.uiTheme && legacyCompatibleImages.includes(texture)) {
legacy = true;
texture += "_legacy";
}
const ret: Phaser.GameObjects.Sprite = originalAddSprite.apply(this, [ x, y, texture, frame ]);
if (legacy) {
const originalSetTexture = ret.setTexture;
ret.setTexture = function (key: string, frame?: string | number) {
key += "_legacy";
return originalSetTexture.apply(this, [ key, frame ]);
};
}
return ret;
};
const originalAddNineslice = scene.add.nineslice;
scene.add.nineslice = function (x: number, y: number, texture: string | Phaser.Textures.Texture, frame?: string | number, width?: number, height?: number, leftWidth?: number, rightWidth?: number, topHeight?: number, bottomHeight?: number): Phaser.GameObjects.NineSlice {
let legacy = false;
if (typeof texture === "string" && scene.uiTheme && legacyCompatibleImages.includes(texture)) {
legacy = true;
texture += "_legacy";
}
const ret: Phaser.GameObjects.NineSlice = originalAddNineslice.apply(this, [ x, y, texture, frame, width, height, leftWidth, rightWidth, topHeight, bottomHeight ]);
if (legacy) {
const originalSetTexture = ret.setTexture;
ret.setTexture = function (key: string | Phaser.Textures.Texture, frame?: string | number, updateSize?: boolean, updateOrigin?: boolean) {
key += "_legacy";
return originalSetTexture.apply(this, [ key, frame, updateSize, updateOrigin ]);
};
}
return ret;
};
}