Refactoring layoutManagerActionStore. It now supports an uuid field.

layoutManagerVisibilityStore is now a derived field from layoutManagerActionStore.
This commit is contained in:
David Négrier 2021-08-05 11:19:28 +02:00
parent 92fee33b64
commit 87e4367455
4 changed files with 42 additions and 53 deletions

View File

@ -1,26 +1,5 @@
<script lang="ts">
import { layoutManagerActionStore } from "../../Stores/LayoutManagerStore";
import { onDestroy, onMount } from "svelte";
import { get } from "svelte/store";
onMount(() => {
for (const action of get(layoutManagerActionStore)) {
action.userInputManager?.addSpaceEventListner(action.callback);
if ( action.type === 'warning') {
//remove it after 10 sec
setTimeout(() => {
layoutManagerActionStore.removeAction(action);
}, 10000)
}
}
})
onDestroy(() => {
for (const action of get(layoutManagerActionStore)) {
action.userInputManager?.removeSpaceEventListner(action.callback);
}
layoutManagerActionStore.clearActions();
})
function onClick(callback: () => void) {
callback();
@ -75,4 +54,4 @@
50% {bottom: 30px;}
100% {bottom: 40px;}
}
</style>
</style>

View File

@ -86,7 +86,7 @@ import { playersStore } from "../../Stores/PlayersStore";
import { chatVisibilityStore } from "../../Stores/ChatStore";
import Tileset = Phaser.Tilemaps.Tileset;
import { userIsAdminStore } from "../../Stores/GameStore";
import { layoutManagerActionStore, layoutManagerVisibilityStore } from "../../Stores/LayoutManagerStore";
import { layoutManagerActionStore } from "../../Stores/LayoutManagerStore";
import { get } from "svelte/store";
export interface GameSceneInitInterface {
@ -792,7 +792,7 @@ export class GameScene extends DirtyScene {
});
this.gameMap.onPropertyChange("openWebsite", (newValue, oldValue, allProps) => {
if (newValue === undefined) {
layoutManagerVisibilityStore.set(false);
layoutManagerActionStore.removeAction("openWebsite");
coWebsiteManager.closeCoWebsite();
} else {
const openWebsiteFunction = () => {
@ -802,7 +802,7 @@ export class GameScene extends DirtyScene {
allProps.get("openWebsiteAllowApi") as boolean | undefined,
allProps.get("openWebsitePolicy") as string | undefined
);
layoutManagerVisibilityStore.set(false);
layoutManagerActionStore.removeAction("openWebsite");
};
const openWebsiteTriggerValue = allProps.get(TRIGGER_WEBSITE_PROPERTIES);
@ -812,12 +812,12 @@ export class GameScene extends DirtyScene {
message = "Press SPACE or touch here to open web site";
}
layoutManagerActionStore.addAction({
type: "openWebsite",
uuid: "openWebSite",
type: "message",
message: message,
callback: () => openWebsiteFunction(),
userInputManager: this.userInputManager,
});
layoutManagerVisibilityStore.set(true);
} else {
openWebsiteFunction();
}
@ -825,7 +825,7 @@ export class GameScene extends DirtyScene {
});
this.gameMap.onPropertyChange("jitsiRoom", (newValue, oldValue, allProps) => {
if (newValue === undefined) {
layoutManagerVisibilityStore.set(false);
layoutManagerActionStore.removeAction("jitsi");
this.stopJitsi();
} else {
const openJitsiRoomFunction = () => {
@ -838,7 +838,7 @@ export class GameScene extends DirtyScene {
} else {
this.startJitsi(roomName, undefined);
}
layoutManagerVisibilityStore.set(false);
layoutManagerActionStore.removeAction("jitsi");
};
const jitsiTriggerValue = allProps.get(TRIGGER_JITSI_PROPERTIES);
@ -848,12 +848,12 @@ export class GameScene extends DirtyScene {
message = "Press SPACE or touch here to enter Jitsi Meet room";
}
layoutManagerActionStore.addAction({
type: "jitsiRoom",
uuid: "jitsi",
type: "message",
message: message,
callback: () => openJitsiRoomFunction(),
userInputManager: this.userInputManager,
});
layoutManagerVisibilityStore.set(true);
} else {
openJitsiRoomFunction();
}
@ -1154,20 +1154,21 @@ ${escapedMessage}
"triggerActionMessage",
(message) =>
new Promise((resolver) => {
layoutManager.addActionButton(
message.uuid,
message.message,
() => {
layoutManager.removeActionButton(message.uuid, this.userInputManager);
layoutManagerActionStore.addAction({
uuid: message.uuid,
type: "message",
message: message.message,
callback: () => {
layoutManagerActionStore.removeAction(message.uuid);
resolver();
},
this.userInputManager
);
userInputManager: this.userInputManager,
});
})
);
iframeListener.registerAnswerer("removeActionMessage", (message) => {
layoutManager.removeActionButton(message.uuid, this.userInputManager);
layoutManagerActionStore.removeAction(message.uuid);
});
}

View File

@ -1,15 +1,14 @@
import { writable } from "svelte/store";
import { derived, writable } from "svelte/store";
import type { UserInputManager } from "../Phaser/UserInput/UserInputManager";
export interface LayoutManagerAction {
type: string;
uuid: string;
type: "warning" | "message";
message: string | number | boolean | undefined;
callback: () => void;
userInputManager: UserInputManager | undefined;
}
export const layoutManagerVisibilityStore = writable(false);
function createLayoutManagerAction() {
const { subscribe, set, update } = writable<LayoutManagerAction[]>([]);
@ -18,26 +17,26 @@ function createLayoutManagerAction() {
addAction: (newAction: LayoutManagerAction): void => {
update((list: LayoutManagerAction[]) => {
let found = false;
for (const actions of list) {
if (actions.type === newAction.type && actions.message === newAction.message) {
for (const action of list) {
if (action.uuid === newAction.uuid) {
found = true;
}
}
if (!found) {
list.push(newAction);
newAction.userInputManager?.addSpaceEventListner(newAction.callback);
}
return list;
});
},
removeAction: (oldAction: LayoutManagerAction): void => {
removeAction: (uuid: string): void => {
update((list: LayoutManagerAction[]) => {
const index = list.findIndex(
(actions) => actions.type === oldAction.type && actions.message === oldAction.message
);
const index = list.findIndex((action) => action.uuid === uuid);
if (index !== -1) {
list[index].userInputManager?.removeSpaceEventListner(list[index].callback);
list.splice(index, 1);
}
@ -51,3 +50,7 @@ function createLayoutManagerAction() {
}
export const layoutManagerActionStore = createLayoutManagerAction();
export const layoutManagerVisibilityStore = derived(layoutManagerActionStore, ($layoutManagerActionStore) => {
return !!$layoutManagerActionStore.length;
});

View File

@ -25,15 +25,18 @@ export class MediaManager {
if (result.type === "error") {
console.error(result.error);
layoutManagerActionStore.addAction({
uuid: "cameraAccessDenied",
type: "warning",
message: "Camera access denied. Click here and check your browser permissions.",
callback: () => {
helpCameraSettingsVisibleStore.set(true);
layoutManagerVisibilityStore.set(false);
},
userInputManager: this.userInputManager,
});
layoutManagerVisibilityStore.set(true);
//remove it after 10 sec
setTimeout(() => {
layoutManagerActionStore.removeAction("cameraAccessDenied");
}, 10000);
return;
}
});
@ -42,15 +45,18 @@ export class MediaManager {
if (result.type === "error") {
console.error(result.error);
layoutManagerActionStore.addAction({
uuid: "screenSharingAccessDenied",
type: "warning",
message: "Screen sharing denied. Click here and check your browser permissions.",
callback: () => {
helpCameraSettingsVisibleStore.set(true);
layoutManagerVisibilityStore.set(false);
},
userInputManager: this.userInputManager,
});
layoutManagerVisibilityStore.set(true);
//remove it after 10 sec
setTimeout(() => {
layoutManagerActionStore.removeAction("screenSharingAccessDenied");
}, 10000);
return;
}
});