partey_workadventure/front/src/Stores/LayoutManagerStore.ts

57 lines
1.8 KiB
TypeScript
Raw Normal View History

import { derived, writable } from "svelte/store";
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();
export const layoutManagerVisibilityStore = derived(layoutManagerActionStore, ($layoutManagerActionStore) => {
return !!$layoutManagerActionStore.length;
});