2021-05-28 00:24:08 +02:00
|
|
|
import { isButtonClickedEvent } from '../Events/ButtonClickedEvent';
|
2021-05-28 00:34:40 +02:00
|
|
|
import type { ClosePopupEvent } from '../Events/ClosePopupEvent';
|
2021-06-21 11:48:39 +02:00
|
|
|
import { IframeApiContribution, sendToWorkadventure } from './IframeApiContribution';
|
|
|
|
import { apiCallback } from "./registeredCallbacks";
|
|
|
|
|
2021-06-18 17:22:56 +02:00
|
|
|
export class Popup {
|
2021-05-28 00:24:08 +02:00
|
|
|
constructor(private id: number) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the popup
|
|
|
|
*/
|
|
|
|
public close(): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
sendToWorkadventure({
|
2021-05-28 00:24:08 +02:00
|
|
|
'type': 'closePopup',
|
|
|
|
'data': {
|
|
|
|
'popupId': this.id,
|
|
|
|
} as ClosePopupEvent
|
2021-06-21 11:48:39 +02:00
|
|
|
});
|
2021-05-28 00:24:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ButtonClickedCallback = (popup: Popup) => void;
|
2021-06-18 17:22:56 +02:00
|
|
|
export interface ButtonDescriptor {
|
2021-05-28 00:24:08 +02:00
|
|
|
/**
|
|
|
|
* The label of the button
|
|
|
|
*/
|
|
|
|
label: string,
|
|
|
|
/**
|
|
|
|
* The type of the button. Can be one of "normal", "primary", "success", "warning", "error", "disabled"
|
|
|
|
*/
|
|
|
|
className?: "normal" | "primary" | "success" | "warning" | "error" | "disabled",
|
|
|
|
/**
|
|
|
|
* Callback called if the button is pressed
|
|
|
|
*/
|
|
|
|
callback: ButtonClickedCallback,
|
|
|
|
}
|
|
|
|
let popupId = 0;
|
|
|
|
const popups: Map<number, Popup> = new Map<number, Popup>();
|
|
|
|
const popupCallbacks: Map<number, Map<number, ButtonClickedCallback>> = new Map<number, Map<number, ButtonClickedCallback>>();
|
|
|
|
|
|
|
|
interface ZonedPopupOptions {
|
|
|
|
zone: string
|
|
|
|
objectLayerName?: string,
|
|
|
|
popupText: string,
|
|
|
|
delay?: number
|
|
|
|
popupOptions: Array<ButtonDescriptor>
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-18 17:22:56 +02:00
|
|
|
class WorkAdventureUiCommands extends IframeApiContribution<WorkAdventureUiCommands> {
|
2021-05-28 00:24:08 +02:00
|
|
|
|
|
|
|
readonly subObjectIdentifier = "ui"
|
|
|
|
|
|
|
|
readonly addMethodsAtRoot = true
|
|
|
|
callbacks = [apiCallback({
|
|
|
|
type: "buttonClickedEvent",
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})];
|
|
|
|
|
|
|
|
|
|
|
|
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({
|
|
|
|
'type': 'openPopup',
|
|
|
|
'data': {
|
|
|
|
popupId,
|
|
|
|
targetObject,
|
|
|
|
message,
|
|
|
|
buttons: buttons.map((button) => {
|
|
|
|
return {
|
|
|
|
label: button.label,
|
|
|
|
className: button.className
|
|
|
|
};
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
popups.set(popupId, popup)
|
|
|
|
return popup;
|
|
|
|
}
|
2021-06-18 17:22:56 +02:00
|
|
|
|
|
|
|
displayBubble(): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
sendToWorkadventure({ 'type': 'displayBubble', data: null });
|
2021-06-18 17:22:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
removeBubble(): void {
|
2021-06-21 11:48:39 +02:00
|
|
|
sendToWorkadventure({ 'type': 'removeBubble', data: null });
|
2021-06-18 17:22:56 +02:00
|
|
|
}
|
2021-05-28 00:24:08 +02:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:48:39 +02:00
|
|
|
export default new WorkAdventureUiCommands();
|