workadventure/front/src/Stores/LayoutManagerStore.ts

58 lines
1.9 KiB
TypeScript
Raw Normal View History

import { derived, writable } from "svelte/store";
import type { ActivatablesManager } from "../Phaser/Game/ActivatablesManager";
2021-08-03 11:13:08 +02:00
import type { UserInputManager } from "../Phaser/UserInput/UserInputManager";
export interface LayoutManagerAction {
uuid: string;
type: "warning" | "message";
2021-08-03 11:13:08 +02:00
message: string | number | boolean | undefined;
callback: () => void;
userInputManager: UserInputManager | undefined;
}
function createLayoutManagerAction() {
const { subscribe, set, update } = writable<LayoutManagerAction[]>([]);
return {
subscribe,
addAction: (newAction: LayoutManagerAction): void => {
update((list: LayoutManagerAction[]) => {
let found = false;
for (const action of list) {
if (action.uuid === newAction.uuid) {
2021-08-03 11:13:08 +02:00
found = true;
}
}
if (!found) {
list.push(newAction);
newAction.userInputManager?.addSpaceEventListner(newAction.callback);
2021-08-03 11:13:08 +02:00
}
return list;
});
},
removeAction: (uuid: string): void => {
2021-08-03 11:13:08 +02:00
update((list: LayoutManagerAction[]) => {
const index = list.findIndex((action) => action.uuid === uuid);
2021-08-03 11:13:08 +02:00
if (index !== -1) {
list[index].userInputManager?.removeSpaceEventListner(list[index].callback);
2021-08-03 11:13:08 +02:00
list.splice(index, 1);
}
return list;
});
},
clearActions: (): void => {
set([]);
},
};
}
export const layoutManagerActionStore = createLayoutManagerAction();
2022-01-05 10:27:40 +01:00
export const layoutManagerActionVisibilityStore = derived(layoutManagerActionStore, ($layoutManagerActionStore) => {
return !!$layoutManagerActionStore.length;
});