2021-08-04 19:31:17 +02:00
|
|
|
import { isButtonClickedEvent } from "../Events/ButtonClickedEvent";
|
|
|
|
import { isMenuItemClickedEvent } from "../Events/ui/MenuItemClickedEvent";
|
|
|
|
import { IframeApiContribution, sendToWorkadventure } from "./IframeApiContribution";
|
|
|
|
import { apiCallback } from "./registeredCallbacks";
|
|
|
|
import type { ButtonClickedCallback, ButtonDescriptor } from "./Ui/ButtonDescriptor";
|
|
|
|
import { Popup } from "./Ui/Popup";
|
|
|
|
import { ActionMessage } from "./Ui/ActionMessage";
|
2021-06-21 11:48:39 +02:00
|
|
|
|
2021-05-28 00:24:08 +02:00
|
|
|
let popupId = 0;
|
|
|
|
const popups: Map<number, Popup> = new Map<number, Popup>();
|
2021-06-30 10:15:55 +02:00
|
|
|
const popupCallbacks: Map<number, Map<number, ButtonClickedCallback>> = new Map<
|
|
|
|
number,
|
|
|
|
Map<number, ButtonClickedCallback>
|
|
|
|
>();
|
2021-05-28 00:24:08 +02:00
|
|
|
|
2021-06-30 10:15:55 +02:00
|
|
|
const menuCallbacks: Map<string, (command: string) => void> = new Map();
|
2021-06-21 18:22:31 +02:00
|
|
|
|
2021-05-28 00:24:08 +02:00
|
|
|
interface ZonedPopupOptions {
|
2021-06-30 10:15:55 +02:00
|
|
|
zone: string;
|
|
|
|
objectLayerName?: string;
|
|
|
|
popupText: string;
|
|
|
|
delay?: number;
|
|
|
|
popupOptions: Array<ButtonDescriptor>;
|
2021-05-28 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
2021-06-30 10:15:55 +02:00
|
|
|
export class WorkAdventureUiCommands extends IframeApiContribution<WorkAdventureUiCommands> {
|
2021-06-23 17:32:32 +02:00
|
|
|
callbacks = [
|
|
|
|
apiCallback({
|
2021-08-04 19:31:17 +02:00
|
|
|
type: "buttonClickedEvent",
|
2021-06-23 17:32:32 +02:00
|
|
|
typeChecker: isButtonClickedEvent,
|
|
|
|
callback: (payloadData) => {
|
|
|
|
const callback = popupCallbacks.get(payloadData.popupId)?.get(payloadData.buttonId);
|
|
|
|
const popup = popups.get(payloadData.popupId);
|
|
|
|
if (popup === undefined) {
|
|
|
|
throw new Error('Could not find popup with ID "' + payloadData.popupId + '"');
|
|
|
|
}
|
|
|
|
if (callback) {
|
|
|
|
callback(popup);
|
|
|
|
}
|
2021-06-30 10:15:55 +02:00
|
|
|
},
|
2021-06-23 17:32:32 +02:00
|
|
|
}),
|
|
|
|
apiCallback({
|
2021-08-04 19:31:17 +02:00
|
|
|
type: "menuItemClicked",
|
2021-06-23 17:32:32 +02:00
|
|
|
typeChecker: isMenuItemClickedEvent,
|
2021-06-30 10:15:55 +02:00
|
|
|
callback: (event) => {
|
2021-06-23 17:32:32 +02:00
|
|
|
const callback = menuCallbacks.get(event.menuItem);
|
|
|
|
if (callback) {
|
2021-06-30 10:15:55 +02:00
|
|
|
callback(event.menuItem);
|
2021-06-23 17:32:32 +02:00
|
|
|
}
|
2021-06-30 10:15:55 +02:00
|
|
|
},
|
2021-06-23 17:32:32 +02:00
|
|
|
}),
|
|
|
|
];
|
2021-05-28 00:24:08 +02:00
|
|
|
|
|
|
|
openPopup(targetObject: string, message: string, buttons: ButtonDescriptor[]): Popup {
|
|
|
|
popupId++;
|
|
|
|
const popup = new Popup(popupId);
|
|
|
|
const btnMap = new Map<number, () => void>();
|
|
|
|
popupCallbacks.set(popupId, btnMap);
|
|
|
|
let id = 0;
|
|
|
|
for (const button of buttons) {
|
|
|
|
const callback = button.callback;
|
|
|
|
if (callback) {
|
|
|
|
btnMap.set(id, () => {
|
|
|
|
callback(popup);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendToWorkadventure({
|
2021-08-04 19:31:17 +02:00
|
|
|
type: "openPopup",
|
2021-06-30 10:15:55 +02:00
|
|
|
data: {
|
2021-05-28 00:24:08 +02:00
|
|
|
popupId,
|
|
|
|
targetObject,
|
|
|
|
message,
|
|
|
|
buttons: buttons.map((button) => {
|
|
|
|
return {
|
|
|
|
label: button.label,
|
2021-06-30 10:15:55 +02:00
|
|
|
className: button.className,
|
2021-05-28 00:24:08 +02:00
|
|
|
};
|
2021-06-30 10:15:55 +02:00
|
|
|
}),
|
|
|
|
},
|
2021-05-28 00:24:08 +02:00
|
|
|
});
|
|
|
|
|
2021-06-30 10:15:55 +02:00
|
|
|
popups.set(popupId, popup);
|
2021-05-28 00:24:08 +02:00
|
|
|
return popup;
|
|
|
|
}
|
2021-06-18 17:22:56 +02:00
|
|
|
|
2021-06-21 18:22:31 +02:00
|
|
|
registerMenuCommand(commandDescriptor: string, callback: (commandDescriptor: string) => void) {
|
|
|
|
menuCallbacks.set(commandDescriptor, callback);
|
|
|
|
sendToWorkadventure({
|
2021-08-04 19:31:17 +02:00
|
|
|
type: "registerMenuCommand",
|
2021-06-30 10:15:55 +02:00
|
|
|
data: {
|
|
|
|
menutItem: commandDescriptor,
|
|
|
|
},
|
2021-06-21 18:22:31 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:22:56 +02:00
|
|
|
displayBubble(): void {
|
2021-08-04 19:31:17 +02:00
|
|
|
sendToWorkadventure({ type: "displayBubble", data: null });
|
2021-06-18 17:22:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
removeBubble(): void {
|
2021-08-04 19:31:17 +02:00
|
|
|
sendToWorkadventure({ type: "removeBubble", data: null });
|
2021-06-18 17:22:56 +02:00
|
|
|
}
|
2021-07-02 18:49:22 +02:00
|
|
|
|
2021-08-04 19:31:17 +02:00
|
|
|
displayActionMessage(message: string, callback: () => void): ActionMessage {
|
|
|
|
return new ActionMessage(message, callback);
|
2021-06-23 17:32:32 +02:00
|
|
|
}
|
2021-05-28 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:48:39 +02:00
|
|
|
export default new WorkAdventureUiCommands();
|