diff --git a/src/main.ts b/src/main.ts index fe510eafb64..5d181690cd2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -31,13 +31,25 @@ window.addEventListener("unhandledrejection", event => { }); /** - * Sets this object's position relative to another object with a given offset + * Set this object's position relative to another object with a given offset. + * @param guideObject - The object to base this object's position off of; must have defined + * x/y co-ordinates, an origin and width/height + * @param x - The X-position to set, relative to `guideObject`'s `x` value + * @param y - The Y-position to set, relative to `guideObject`'s `y` value + * @returns `this` */ -const setPositionRelative = function (guideObject: Phaser.GameObjects.GameObject, x: number, y: number) { +function setPositionRelative( + this: T, + guideObject: Pick & + Pick & + Pick, + x: number, + y: number, +): T { const offsetX = guideObject.width * (-0.5 + (0.5 - guideObject.originX)); const offsetY = guideObject.height * (-0.5 + (0.5 - guideObject.originY)); return this.setPosition(guideObject.x + offsetX + x, guideObject.y + offsetY + y); -}; +} Phaser.GameObjects.Container.prototype.setPositionRelative = setPositionRelative; Phaser.GameObjects.Sprite.prototype.setPositionRelative = setPositionRelative; diff --git a/src/typings/phaser/index.d.ts b/src/typings/phaser/index.d.ts index 706ee1a35ba..275aafba353 100644 --- a/src/typings/phaser/index.d.ts +++ b/src/typings/phaser/index.d.ts @@ -1,57 +1,36 @@ import "phaser"; +/** + * Interface representing an object that can be passed to {@linkcode setPositionRelative}. + * @interface + */ +type GuideObject = Pick & + Pick & + Pick; + +type setPositionRelative = + /** + * Set this object's position relative to another object with a given offset. + * @param guideObject - The object to base this object's position off of; must have defined + * x/y co-ordinates, an origin and width/height + * @param x - The X-position to set, relative to `guideObject`'s `x` value + * @param y - The Y-position to set, relative to `guideObject`'s `y` value + * @returns `this` + */ + (this: T, guideObject: GuideObject, x: number, y: number) => T; + +interface hasSetPositionRelative { + setPositionRelative: setPositionRelative; +} + declare module "phaser" { namespace GameObjects { - interface GameObject { - width: number; - - height: number; - - originX: number; - - originY: number; - - x: number; - - y: number; - } - - interface Container { - /** - * Sets this object's position relative to another object with a given offset - */ - setPositionRelative(guideObject: any, x: number, y: number): this; - } - interface Sprite { - /** - * Sets this object's position relative to another object with a given offset - */ - setPositionRelative(guideObject: any, x: number, y: number): this; - } - interface Image { - /** - * Sets this object's position relative to another object with a given offset - */ - setPositionRelative(guideObject: any, x: number, y: number): this; - } - interface NineSlice { - /** - * Sets this object's position relative to another object with a given offset - */ - setPositionRelative(guideObject: any, x: number, y: number): this; - } - interface Text { - /** - * Sets this object's position relative to another object with a given offset - */ - setPositionRelative(guideObject: any, x: number, y: number): this; - } - interface Rectangle { - /** - * Sets this object's position relative to another object with a given offset - */ - setPositionRelative(guideObject: any, x: number, y: number): this; - } + interface Container extends hasSetPositionRelative {} + interface Sprite extends hasSetPositionRelative {} + interface Image extends hasSetPositionRelative {} + interface NineSlice extends hasSetPositionRelative {} + interface Text extends hasSetPositionRelative {} + interface Rectangle extends hasSetPositionRelative {} } namespace Math { diff --git a/src/ui/handlers/admin-ui-handler.ts b/src/ui/handlers/admin-ui-handler.ts index ce3798fec4d..8ae8c04500d 100644 --- a/src/ui/handlers/admin-ui-handler.ts +++ b/src/ui/handlers/admin-ui-handler.ts @@ -199,7 +199,9 @@ export class AdminUiHandler extends FormModalUiHandler { this.inputs[i].setText(adminResult[aR]); if (aR === "discordId" || aR === "googleId") { // this is here to add the icons for linking/unlinking of google/discord IDs - const nineSlice = this.inputContainers[i].list.find(iC => iC.type === "NineSlice"); + const nineSlice = this.inputContainers[i].list.find( + (iC): iC is Phaser.GameObjects.NineSlice => iC.type === "NineSlice", + ); const img = globalScene.add.image( this.inputContainers[i].x + nineSlice!.width + this.buttonGap, this.inputContainers[i].y + Math.floor(nineSlice!.height / 2), diff --git a/src/ui/handlers/pokedex-scan-ui-handler.ts b/src/ui/handlers/pokedex-scan-ui-handler.ts index 18afd0598c2..b046a3dab3d 100644 --- a/src/ui/handlers/pokedex-scan-ui-handler.ts +++ b/src/ui/handlers/pokedex-scan-ui-handler.ts @@ -157,6 +157,8 @@ export class PokedexScanUiHandler extends FormModalUiHandler { const inputWidth = label.width < 420 ? 200 : 200 - (label.width - 420) / 5.75; this.inputs[0].resize(inputWidth * 5.75, 116); + // @ts-expect-error: TODO Resolve + // TODO: Figure out what the type of `this.inputContainers.list` is this.inputContainers[0].list[0].width = inputWidth; if (args[1] && typeof (args[1] as PlayerPokemon).getNameToRender === "function") { this.inputs[0].text = (args[1] as PlayerPokemon).getNameToRender(); diff --git a/src/ui/handlers/test-dialogue-ui-handler.ts b/src/ui/handlers/test-dialogue-ui-handler.ts index b33e6726547..0bad8f6f0a9 100644 --- a/src/ui/handlers/test-dialogue-ui-handler.ts +++ b/src/ui/handlers/test-dialogue-ui-handler.ts @@ -133,6 +133,8 @@ export class TestDialogueUiHandler extends FormModalUiHandler { if (super.show(args)) { const config = args[0] as ModalConfig; this.inputs[0].resize(1150, 116); + // @ts-expect-error: Resolve + // TODO: Figure out what the type of `this.inputContainers.list` is this.inputContainers[0].list[0].width = 200; if (args[1] && typeof (args[1] as PlayerPokemon).getNameToRender === "function") { this.inputs[0].text = (args[1] as PlayerPokemon).getNameToRender();