FEATURE: we now allow a global zoom level of 1.5

This commit is contained in:
kharhamel 2021-05-17 15:49:18 +02:00
parent a23e72454d
commit a66d42e158
3 changed files with 24 additions and 18 deletions

View file

@ -4,8 +4,8 @@ const PUSHER_URL = process.env.PUSHER_URL || '//pusher.workadventure.localhost';
const UPLOADER_URL = process.env.UPLOADER_URL || '//uploader.workadventure.localhost'; const UPLOADER_URL = process.env.UPLOADER_URL || '//uploader.workadventure.localhost';
const STUN_SERVER: string = process.env.STUN_SERVER || "stun:stun.l.google.com:19302"; const STUN_SERVER: string = process.env.STUN_SERVER || "stun:stun.l.google.com:19302";
const TURN_SERVER: string = process.env.TURN_SERVER || ""; const TURN_SERVER: string = process.env.TURN_SERVER || "";
const SKIP_RENDER_OPTIMIZATIONS: boolean = !!(process.env.SKIP_RENDER_OPTIMIZATIONS); const SKIP_RENDER_OPTIMIZATIONS: boolean = process.env.SKIP_RENDER_OPTIMIZATIONS == "true";
const DISABLE_NOTIFICATIONS: boolean = !!(process.env.DISABLE_NOTIFICATIONS); const DISABLE_NOTIFICATIONS: boolean = process.env.DISABLE_NOTIFICATIONS == "true";
const TURN_USER: string = process.env.TURN_USER || ''; const TURN_USER: string = process.env.TURN_USER || '';
const TURN_PASSWORD: string = process.env.TURN_PASSWORD || ''; const TURN_PASSWORD: string = process.env.TURN_PASSWORD || '';
const JITSI_URL : string|undefined = (process.env.JITSI_URL === '') ? undefined : process.env.JITSI_URL; const JITSI_URL : string|undefined = (process.env.JITSI_URL === '') ? undefined : process.env.JITSI_URL;

View file

@ -1,4 +1,3 @@
import ScaleManager = Phaser.Scale.ScaleManager;
interface Size { interface Size {
width: number; width: number;
@ -13,8 +12,7 @@ export class HdpiManager {
* @param minRecommendedGamePixelsNumber The minimum number of pixels we want to display "by default" to the user * @param minRecommendedGamePixelsNumber The minimum number of pixels we want to display "by default" to the user
* @param absoluteMinPixelNumber The very minimum of game pixels to display. Below, we forbid zooming more * @param absoluteMinPixelNumber The very minimum of game pixels to display. Below, we forbid zooming more
*/ */
public constructor(private minRecommendedGamePixelsNumber: number, private absoluteMinPixelNumber: number) { public constructor(private minRecommendedGamePixelsNumber: number, private absoluteMinPixelNumber: number) {}
}
/** /**
* Returns the optimal size in "game pixels" based on the screen size in "real pixels". * Returns the optimal size in "game pixels" based on the screen size in "real pixels".
@ -36,16 +34,12 @@ export class HdpiManager {
}; };
} }
let i = 1; const optimalZoomLevel = this.getOptimalZoomLevel(realPixelNumber);
while (realPixelNumber > this.minRecommendedGamePixelsNumber * i * i) {
i++;
}
// Has the canvas more pixels than the screen? This is forbidden // Has the canvas more pixels than the screen? This is forbidden
if ((i - 1) * this._zoomModifier < 1) { if (optimalZoomLevel * this._zoomModifier < 1) {
// Let's reset the zoom modifier (WARNING this is a SIDE EFFECT in a getter) // Let's reset the zoom modifier (WARNING this is a SIDE EFFECT in a getter)
this._zoomModifier = 1 / (i - 1); this._zoomModifier = 1 / optimalZoomLevel;
return { return {
game: { game: {
@ -59,8 +53,8 @@ export class HdpiManager {
} }
} }
const gameWidth = Math.ceil(realPixelScreenSize.width / (i - 1) / this._zoomModifier); const gameWidth = Math.ceil(realPixelScreenSize.width / optimalZoomLevel / this._zoomModifier);
const gameHeight = Math.ceil(realPixelScreenSize.height / (i - 1) / this._zoomModifier); const gameHeight = Math.ceil(realPixelScreenSize.height / optimalZoomLevel / this._zoomModifier);
// Let's ensure we display a minimum of pixels, even if crazily zoomed in. // Let's ensure we display a minimum of pixels, even if crazily zoomed in.
if (gameWidth * gameHeight < this.absoluteMinPixelNumber) { if (gameWidth * gameHeight < this.absoluteMinPixelNumber) {
@ -68,7 +62,7 @@ export class HdpiManager {
const minGameWidth = Math.sqrt(this.absoluteMinPixelNumber * realPixelScreenSize.width / realPixelScreenSize.height); const minGameWidth = Math.sqrt(this.absoluteMinPixelNumber * realPixelScreenSize.width / realPixelScreenSize.height);
// Let's reset the zoom modifier (WARNING this is a SIDE EFFECT in a getter) // Let's reset the zoom modifier (WARNING this is a SIDE EFFECT in a getter)
this._zoomModifier = realPixelScreenSize.width / minGameWidth / (i - 1); this._zoomModifier = realPixelScreenSize.width / minGameWidth / optimalZoomLevel;
return { return {
game: { game: {
@ -89,12 +83,24 @@ export class HdpiManager {
height: gameHeight, height: gameHeight,
}, },
real: { real: {
width: Math.ceil(realPixelScreenSize.width / (i - 1)) * (i - 1), width: Math.ceil(realPixelScreenSize.width / optimalZoomLevel) * optimalZoomLevel,
height: Math.ceil(realPixelScreenSize.height / (i - 1)) * (i - 1), height: Math.ceil(realPixelScreenSize.height / optimalZoomLevel) * optimalZoomLevel,
} }
} }
} }
/**
* We only accept integer but we make an exception for 1.5
*/
private getOptimalZoomLevel(realPixelNumber: number): number {
const result = Math.sqrt(realPixelNumber / this.minRecommendedGamePixelsNumber);
if (1.5 <= result && result < 2) {
return 1.5
} else {
return Math.floor(result);
}
}
public get zoomModifier(): number { public get zoomModifier(): number {
return this._zoomModifier; return this._zoomModifier;
} }

View file

@ -50,6 +50,6 @@ describe("Test HdpiManager", () => {
const result = hdpiManager.getOptimalGameSize({ width: 1280, height: 768 }); const result = hdpiManager.getOptimalGameSize({ width: 1280, height: 768 });
expect(result.game.width).toEqual(1280); expect(result.game.width).toEqual(1280);
expect(result.game.height).toEqual(768); expect(result.game.height).toEqual(768);
expect(hdpiManager.zoomModifier).toEqual(1); expect(hdpiManager.zoomModifier).toEqual(2 / 3);
}); });
}); });