Camera FocusTarget width and height are now optional

This commit is contained in:
Hanusiak Piotr 2022-01-13 16:43:58 +01:00
parent d99930a67e
commit a164cedf3f
6 changed files with 31 additions and 25 deletions

View File

@ -4,8 +4,8 @@ export const isCameraSetViewportEvent = new tg.IsInterface()
.withProperties({ .withProperties({
x: tg.isNumber, x: tg.isNumber,
y: tg.isNumber, y: tg.isNumber,
width: tg.isNumber, width: tg.isOptional(tg.isNumber),
height: tg.isNumber, height: tg.isOptional(tg.isNumber),
lock: tg.isBoolean, lock: tg.isBoolean,
smooth: tg.isBoolean, smooth: tg.isBoolean,
}) })

View File

@ -211,6 +211,7 @@ class IframeListener {
} else if (payload.type === "setProperty" && isSetPropertyEvent(payload.data)) { } else if (payload.type === "setProperty" && isSetPropertyEvent(payload.data)) {
this._setPropertyStream.next(payload.data); this._setPropertyStream.next(payload.data);
} else if (payload.type === "cameraSetViewport" && isCameraSetViewportEvent(payload.data)) { } else if (payload.type === "cameraSetViewport" && isCameraSetViewportEvent(payload.data)) {
console.log(payload.data);
this._cameraSetViewportStream.next(payload.data); this._cameraSetViewportStream.next(payload.data);
} else if (payload.type === "cameraFollowPlayer" && isCameraFollowPlayerEvent(payload.data)) { } else if (payload.type === "cameraFollowPlayer" && isCameraFollowPlayerEvent(payload.data)) {
this._cameraFollowPlayerStream.next(payload.data); this._cameraFollowPlayerStream.next(payload.data);

View File

@ -20,11 +20,12 @@ export class WorkAdventureCameraCommands extends IframeApiContribution<WorkAdven
public setViewport( public setViewport(
x: number, x: number,
y: number, y: number,
width: number, width?: number,
height: number, height?: number,
lock: boolean = false, lock: boolean = false,
smooth: boolean = false smooth: boolean = false
): void { ): void {
console.log({ x, y, width, height, lock, smooth });
sendToWorkadventure({ sendToWorkadventure({
type: "cameraSetViewport", type: "cameraSetViewport",
data: { x, y, width, height, lock, smooth }, data: { x, y, width, height, lock, smooth },

View File

@ -2,7 +2,7 @@ import { Easing } from "../../types";
import { HtmlUtils } from "../../WebRtc/HtmlUtils"; import { HtmlUtils } from "../../WebRtc/HtmlUtils";
import type { Box } from "../../WebRtc/LayoutManager"; import type { Box } from "../../WebRtc/LayoutManager";
import { hasMovedEventName, Player } from "../Player/Player"; import { hasMovedEventName, Player } from "../Player/Player";
import type { WaScaleManager } from "../Services/WaScaleManager"; import { WaScaleManager, WaScaleManagerEvent, WaScaleManagerFocusTarget } from "../Services/WaScaleManager";
import type { GameScene } from "./GameScene"; import type { GameScene } from "./GameScene";
export enum CameraMode { export enum CameraMode {
@ -62,7 +62,7 @@ export class CameraManager extends Phaser.Events.EventEmitter {
} }
public destroy(): void { public destroy(): void {
this.scene.game.events.off("wa-scale-manager:refresh-focus-on-target"); this.scene.game.events.off(WaScaleManagerEvent.RefreshFocusOnTarget);
super.destroy(); super.destroy();
} }
@ -75,18 +75,19 @@ export class CameraManager extends Phaser.Events.EventEmitter {
* @param setTo Viewport on which the camera should set the position * @param setTo Viewport on which the camera should set the position
* @param duration Time for the transition im MS. If set to 0, transition will occur immediately * @param duration Time for the transition im MS. If set to 0, transition will occur immediately
*/ */
public setPosition(setTo: { x: number; y: number; width: number; height: number }, duration: number = 1000): void { public setPosition(setTo: WaScaleManagerFocusTarget, duration: number = 1000): void {
if (this.cameraMode === CameraMode.Focus) { if (this.cameraMode === CameraMode.Focus) {
return; return;
} }
this.setCameraMode(CameraMode.Positioned); this.setCameraMode(CameraMode.Positioned);
this.waScaleManager.saveZoom(); this.waScaleManager.saveZoom();
const currentZoomModifier = this.waScaleManager.zoomModifier; const currentZoomModifier = this.waScaleManager.zoomModifier;
const zoomModifierChange = this.getZoomModifierChange(setTo.width, setTo.height); const zoomModifierChange =
setTo.width && setTo.height ? this.getZoomModifierChange(setTo.width, setTo.height) : 0;
this.camera.stopFollow(); this.camera.stopFollow();
this.camera.pan( this.camera.pan(
setTo.x + setTo.width * 0.5, setTo.x + (setTo.width ?? 0) * 0.5,
setTo.y + setTo.height * 0.5, setTo.y + (setTo.height ?? 0) * 0.5,
duration, duration,
Easing.SineEaseOut, Easing.SineEaseOut,
true, true,
@ -117,11 +118,7 @@ export class CameraManager extends Phaser.Events.EventEmitter {
* @param setTo Viewport on which the camera should focus on * @param setTo Viewport on which the camera should focus on
* @param duration Time for the transition im MS. If set to 0, transition will occur immediately * @param duration Time for the transition im MS. If set to 0, transition will occur immediately
*/ */
public enterFocusMode( public enterFocusMode(focusOn: WaScaleManagerFocusTarget, margin: number = 0, duration: number = 1000): void {
focusOn: { x: number; y: number; width: number; height: number },
margin: number = 0,
duration: number = 1000
): void {
this.setCameraMode(CameraMode.Focus); this.setCameraMode(CameraMode.Focus);
this.waScaleManager.saveZoom(); this.waScaleManager.saveZoom();
this.waScaleManager.setFocusTarget(focusOn); this.waScaleManager.setFocusTarget(focusOn);
@ -132,12 +129,15 @@ export class CameraManager extends Phaser.Events.EventEmitter {
this.startFollowTween?.stop(); this.startFollowTween?.stop();
const marginMult = 1 + margin; const marginMult = 1 + margin;
const currentZoomModifier = this.waScaleManager.zoomModifier; const currentZoomModifier = this.waScaleManager.zoomModifier;
const zoomModifierChange = this.getZoomModifierChange(focusOn.width * marginMult, focusOn.height * marginMult); const zoomModifierChange =
focusOn.width && focusOn.height
? this.getZoomModifierChange(focusOn.width * marginMult, focusOn.height * marginMult)
: 0;
this.camera.stopFollow(); this.camera.stopFollow();
this.playerToFollow = undefined; this.playerToFollow = undefined;
this.camera.pan( this.camera.pan(
focusOn.x + focusOn.width * 0.5 * marginMult, focusOn.x + (focusOn.width ?? 0) * 0.5 * marginMult,
focusOn.y + focusOn.height * 0.5 * marginMult, focusOn.y + (focusOn.height ?? 0) * 0.5 * marginMult,
duration, duration,
Easing.SineEaseOut, Easing.SineEaseOut,
true, true,
@ -244,7 +244,7 @@ export class CameraManager extends Phaser.Events.EventEmitter {
private bindEventHandlers(): void { private bindEventHandlers(): void {
this.scene.game.events.on( this.scene.game.events.on(
"wa-scale-manager:refresh-focus-on-target", WaScaleManagerEvent.RefreshFocusOnTarget,
(focusOn: { x: number; y: number; width: number; height: number }) => { (focusOn: { x: number; y: number; width: number; height: number }) => {
if (!focusOn) { if (!focusOn) {
return; return;

View File

@ -9,6 +9,8 @@ export enum WaScaleManagerEvent {
RefreshFocusOnTarget = "wa-scale-manager:refresh-focus-on-target", RefreshFocusOnTarget = "wa-scale-manager:refresh-focus-on-target",
} }
export type WaScaleManagerFocusTarget = { x: number; y: number; width?: number; height?: number };
export class WaScaleManager { export class WaScaleManager {
private hdpiManager: HdpiManager; private hdpiManager: HdpiManager;
private scaleManager!: ScaleManager; private scaleManager!: ScaleManager;
@ -16,7 +18,7 @@ export class WaScaleManager {
private actualZoom: number = 1; private actualZoom: number = 1;
private _saveZoom: number = 1; private _saveZoom: number = 1;
private focusTarget?: { x: number; y: number; width: number; height: number }; private focusTarget?: WaScaleManagerFocusTarget;
public constructor(private minGamePixelsNumber: number, private absoluteMinPixelNumber: number) { public constructor(private minGamePixelsNumber: number, private absoluteMinPixelNumber: number) {
this.hdpiManager = new HdpiManager(minGamePixelsNumber, absoluteMinPixelNumber); this.hdpiManager = new HdpiManager(minGamePixelsNumber, absoluteMinPixelNumber);
@ -72,11 +74,13 @@ export class WaScaleManager {
if (!this.focusTarget) { if (!this.focusTarget) {
return; return;
} }
this.zoomModifier = this.getTargetZoomModifierFor(this.focusTarget.width, this.focusTarget.height); if (this.focusTarget.width && this.focusTarget.height) {
this.zoomModifier = this.getTargetZoomModifierFor(this.focusTarget.width, this.focusTarget.height);
}
this.game.events.emit(WaScaleManagerEvent.RefreshFocusOnTarget, this.focusTarget); this.game.events.emit(WaScaleManagerEvent.RefreshFocusOnTarget, this.focusTarget);
} }
public setFocusTarget(targetDimensions?: { x: number; y: number; width: number; height: number }): void { public setFocusTarget(targetDimensions?: WaScaleManagerFocusTarget): void {
this.focusTarget = targetDimensions; this.focusTarget = targetDimensions;
} }
@ -109,7 +113,7 @@ export class WaScaleManager {
} }
} }
public getFocusTarget(): { x: number; y: number; width: number; height: number } | undefined { public getFocusTarget(): WaScaleManagerFocusTarget | undefined {
return this.focusTarget; return this.focusTarget;
} }

View File

@ -21,8 +21,8 @@
WA.camera.setViewport( WA.camera.setViewport(
parseInt(xField.value), parseInt(xField.value),
parseInt(yField.value), parseInt(yField.value),
parseInt(widthField.value), widthField.value ? parseInt(widthField.value) : undefined,
parseInt(heightField.value), heightField.value ? parseInt(heightField.value) : undefined,
lockField.checked, lockField.checked,
smoothField.checked, smoothField.checked,
); );