2021-05-04 11:29:37 +02:00
|
|
|
import {HdpiManager} from "./HdpiManager";
|
|
|
|
import ScaleManager = Phaser.Scale.ScaleManager;
|
|
|
|
import {coWebsiteManager} from "../../WebRtc/CoWebsiteManager";
|
2021-05-18 09:20:38 +02:00
|
|
|
import type {Game} from "../Game/Game";
|
2021-05-31 10:20:30 +02:00
|
|
|
import {ResizableScene} from "../Login/ResizableScene";
|
2021-05-04 11:29:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class WaScaleManager {
|
|
|
|
private hdpiManager: HdpiManager;
|
|
|
|
private scaleManager!: ScaleManager;
|
2021-05-18 09:20:38 +02:00
|
|
|
private game!: Game;
|
2021-05-19 18:08:53 +02:00
|
|
|
private actualZoom: number = 1;
|
2021-05-04 11:29:37 +02:00
|
|
|
|
2021-05-04 14:08:40 +02:00
|
|
|
public constructor(private minGamePixelsNumber: number, private absoluteMinPixelNumber: number) {
|
|
|
|
this.hdpiManager = new HdpiManager(minGamePixelsNumber, absoluteMinPixelNumber);
|
2021-05-04 11:29:37 +02:00
|
|
|
}
|
|
|
|
|
2021-05-18 09:20:38 +02:00
|
|
|
public setGame(game: Game): void {
|
|
|
|
this.scaleManager = game.scale;
|
|
|
|
this.game = game;
|
2021-05-04 11:29:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public applyNewSize() {
|
|
|
|
const {width, height} = coWebsiteManager.getGameSize();
|
|
|
|
|
|
|
|
let devicePixelRatio = 1;
|
|
|
|
if (window.devicePixelRatio) {
|
|
|
|
devicePixelRatio = window.devicePixelRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { game: gameSize, real: realSize } = this.hdpiManager.getOptimalGameSize({width: width * devicePixelRatio, height: height * devicePixelRatio});
|
|
|
|
|
2021-05-19 18:08:53 +02:00
|
|
|
this.actualZoom = realSize.width / gameSize.width / devicePixelRatio;
|
2021-05-31 10:20:30 +02:00
|
|
|
this.scaleManager.setZoom(realSize.width / gameSize.width / devicePixelRatio)
|
2021-05-04 11:29:37 +02:00
|
|
|
this.scaleManager.resize(gameSize.width, gameSize.height);
|
|
|
|
|
|
|
|
// Override bug in canvas resizing in Phaser. Let's resize the canvas ourselves
|
|
|
|
const style = this.scaleManager.canvas.style;
|
|
|
|
style.width = Math.ceil(realSize.width / devicePixelRatio) + 'px';
|
|
|
|
style.height = Math.ceil(realSize.height / devicePixelRatio) + 'px';
|
2021-05-31 10:20:30 +02:00
|
|
|
// Note: onResize will be called twice (once here and once is Game.ts), but we have no better way.
|
|
|
|
for (const scene of this.game.scene.getScenes(true)) {
|
|
|
|
if (scene instanceof ResizableScene) {
|
|
|
|
scene.onResize();
|
|
|
|
}
|
|
|
|
}
|
2021-05-18 09:20:38 +02:00
|
|
|
|
|
|
|
this.game.markDirty();
|
2021-05-05 13:14:00 +02:00
|
|
|
}
|
2021-05-04 11:29:37 +02:00
|
|
|
|
|
|
|
public get zoomModifier(): number {
|
|
|
|
return this.hdpiManager.zoomModifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
public set zoomModifier(zoomModifier: number) {
|
|
|
|
this.hdpiManager.zoomModifier = zoomModifier;
|
|
|
|
this.applyNewSize();
|
|
|
|
}
|
2021-05-18 09:20:38 +02:00
|
|
|
|
2021-05-19 18:08:53 +02:00
|
|
|
/**
|
|
|
|
* This is used to scale back the ui components to counter-act the zoom.
|
|
|
|
*/
|
|
|
|
public get uiScalingFactor(): number {
|
2021-05-21 16:25:12 +02:00
|
|
|
return this.actualZoom > 1 ? 1 : 1.2;
|
2021-05-19 18:08:53 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 11:29:37 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 14:08:40 +02:00
|
|
|
export const waScaleManager = new WaScaleManager(640*480, 196*196);
|