From 620bd1ab2c31eaa31348f4fd712fd2142cadb386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Mon, 21 Jun 2021 12:26:12 +0200 Subject: [PATCH] Splitting classes in separated files --- front/src/Api/iframe/Sound/Sound.ts | 39 +++++++++++++++++ front/src/Api/iframe/Ui/ButtonDescriptor.ts | 18 ++++++++ front/src/Api/iframe/Ui/Popup.ts | 19 +++++++++ front/src/Api/iframe/sound.ts | 46 +-------------------- front/src/Api/iframe/ui.ts | 37 +---------------- front/src/iframe_api.ts | 8 ++-- 6 files changed, 85 insertions(+), 82 deletions(-) create mode 100644 front/src/Api/iframe/Sound/Sound.ts create mode 100644 front/src/Api/iframe/Ui/ButtonDescriptor.ts create mode 100644 front/src/Api/iframe/Ui/Popup.ts diff --git a/front/src/Api/iframe/Sound/Sound.ts b/front/src/Api/iframe/Sound/Sound.ts new file mode 100644 index 00000000..3bb3251a --- /dev/null +++ b/front/src/Api/iframe/Sound/Sound.ts @@ -0,0 +1,39 @@ +import {sendToWorkadventure} from "../IframeApiContribution"; +import type {LoadSoundEvent} from "../../Events/LoadSoundEvent"; +import type {PlaySoundEvent} from "../../Events/PlaySoundEvent"; +import type {StopSoundEvent} from "../../Events/StopSoundEvent"; +import SoundConfig = Phaser.Types.Sound.SoundConfig; + +export class Sound { + constructor(private url: string) { + sendToWorkadventure({ + "type": 'loadSound', + "data": { + url: this.url, + } as LoadSoundEvent + + }); + } + + public play(config: SoundConfig) { + sendToWorkadventure({ + "type": 'playSound', + "data": { + url: this.url, + config + } as PlaySoundEvent + + }); + return this.url; + } + public stop() { + sendToWorkadventure({ + "type": 'stopSound', + "data": { + url: this.url, + } as StopSoundEvent + + }); + return this.url; + } +} diff --git a/front/src/Api/iframe/Ui/ButtonDescriptor.ts b/front/src/Api/iframe/Ui/ButtonDescriptor.ts new file mode 100644 index 00000000..119daf5c --- /dev/null +++ b/front/src/Api/iframe/Ui/ButtonDescriptor.ts @@ -0,0 +1,18 @@ +import type {Popup} from "./Popup"; + +export type ButtonClickedCallback = (popup: Popup) => void; + +export interface ButtonDescriptor { + /** + * 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, +} diff --git a/front/src/Api/iframe/Ui/Popup.ts b/front/src/Api/iframe/Ui/Popup.ts new file mode 100644 index 00000000..37dea922 --- /dev/null +++ b/front/src/Api/iframe/Ui/Popup.ts @@ -0,0 +1,19 @@ +import {sendToWorkadventure} from "../IframeApiContribution"; +import type {ClosePopupEvent} from "../../Events/ClosePopupEvent"; + +export class Popup { + constructor(private id: number) { + } + + /** + * Closes the popup + */ + public close(): void { + sendToWorkadventure({ + 'type': 'closePopup', + 'data': { + 'popupId': this.id, + } as ClosePopupEvent + }); + } +} diff --git a/front/src/Api/iframe/sound.ts b/front/src/Api/iframe/sound.ts index 194f937b..70430b46 100644 --- a/front/src/Api/iframe/sound.ts +++ b/front/src/Api/iframe/sound.ts @@ -2,53 +2,11 @@ import type { LoadSoundEvent } from '../Events/LoadSoundEvent'; import type { PlaySoundEvent } from '../Events/PlaySoundEvent'; import type { StopSoundEvent } from '../Events/StopSoundEvent'; import { IframeApiContribution, sendToWorkadventure } from './IframeApiContribution'; -import SoundConfig = Phaser.Types.Sound.SoundConfig; - -export class Sound { - constructor(private url: string) { - sendToWorkadventure({ - "type": 'loadSound', - "data": { - url: this.url, - } as LoadSoundEvent - - }); - } - - public play(config: SoundConfig) { - sendToWorkadventure({ - "type": 'playSound', - "data": { - url: this.url, - config - } as PlaySoundEvent - - }); - return this.url; - } - public stop() { - sendToWorkadventure({ - "type": 'stopSound', - "data": { - url: this.url, - } as StopSoundEvent - - }); - return this.url; - } - -} - - +import {Sound} from "./Sound/Sound"; class WorkadventureSoundCommands extends IframeApiContribution { - - readonly subObjectIdentifier = "sound" - - readonly addMethodsAtRoot = true callbacks = [] - loadSound(url: string): Sound { return new Sound(url); } @@ -56,4 +14,4 @@ class WorkadventureSoundCommands extends IframeApiContribution void; -export interface ButtonDescriptor { - /** - * 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 = new Map(); const popupCallbacks: Map> = new Map>(); @@ -50,9 +20,6 @@ interface ZonedPopupOptions { class WorkAdventureUiCommands extends IframeApiContribution { - readonly subObjectIdentifier = "ui" - - readonly addMethodsAtRoot = true callbacks = [apiCallback({ type: "buttonClickedEvent", typeChecker: isButtonClickedEvent, diff --git a/front/src/iframe_api.ts b/front/src/iframe_api.ts index 9371d50a..ae5321cf 100644 --- a/front/src/iframe_api.ts +++ b/front/src/iframe_api.ts @@ -9,10 +9,12 @@ import chat from "./Api/iframe/chat"; import type {IframeCallback} from './Api/iframe/IframeApiContribution'; import nav from "./Api/iframe/nav"; import controls from "./Api/iframe/controls"; -import ui, {ButtonDescriptor, Popup} from "./Api/iframe/ui"; -import sound, {Sound} from "./Api/iframe/sound"; +import ui from "./Api/iframe/ui"; +import sound from "./Api/iframe/sound"; import room from "./Api/iframe/room"; - +import type {ButtonDescriptor} from "./Api/iframe/Ui/ButtonDescriptor"; +import type {Popup} from "./Api/iframe/Ui/Popup"; +import type {Sound} from "./Api/iframe/Sound/Sound"; const wa = { ui,