Moved effect options to configuration

pull/21833/head
Steffen Kolmer 2020-10-21 17:58:54 +02:00
parent cb79e38377
commit 3ea4560019
4 changed files with 37 additions and 8 deletions

View File

@ -1,6 +1,7 @@
import React, { FunctionComponent, useEffect, useRef } from 'react'; import React, { FunctionComponent, useEffect, useRef } from 'react';
import dis from '../../../../dispatcher/dispatcher'; import dis from '../../../../dispatcher/dispatcher';
import ICanvasEffect from './ICanvasEffect.js'; import ICanvasEffect, { ICanvasEffectConstructable } from './ICanvasEffect.js';
import effects from './index'
export type EffectsOverlayProps = { export type EffectsOverlayProps = {
roomWidth: number; roomWidth: number;
@ -8,15 +9,16 @@ export type EffectsOverlayProps = {
const EffectsOverlay: FunctionComponent<EffectsOverlayProps> = ({ roomWidth }) => { const EffectsOverlay: FunctionComponent<EffectsOverlayProps> = ({ roomWidth }) => {
const canvasRef = useRef<HTMLCanvasElement>(null); const canvasRef = useRef<HTMLCanvasElement>(null);
const effectsRef = useRef<Map<String, ICanvasEffect>>(new Map<String, ICanvasEffect>()); const effectsRef = useRef<Map<string, ICanvasEffect>>(new Map<string, ICanvasEffect>());
const lazyLoadEffectModule = async (name: string): Promise<ICanvasEffect> => { const lazyLoadEffectModule = async (name: string): Promise<ICanvasEffect> => {
if (!name) return null; if (!name) return null;
let effect = effectsRef.current[name] ?? null; let effect: ICanvasEffect | null = effectsRef.current[name] || null;
if (effect === null) { if (effect === null) {
const options = effects.find((e) => e.command === name)?.options
try { try {
const { default: Effect } = await import(`./${name}`); const { default: Effect }: { default: ICanvasEffectConstructable } = await import(`./${name}`);
effect = new Effect(); effect = new Effect(options);
effectsRef.current[name] = effect; effectsRef.current[name] = effect;
} catch (err) { } catch (err) {
console.warn('Unable to load effect module at \'./${name}\'.', err) console.warn('Unable to load effect module at \'./${name}\'.', err)

View File

@ -1,3 +1,7 @@
export interface ICanvasEffectConstructable {
new(options?: { [key: string]: any }): ICanvasEffect
}
export default interface ICanvasEffect { export default interface ICanvasEffect {
start: (canvas: HTMLCanvasElement, timeout?: number) => Promise<void>, start: (canvas: HTMLCanvasElement, timeout?: number) => Promise<void>,
stop: () => Promise<void>, stop: () => Promise<void>,

View File

@ -43,8 +43,8 @@ export const DefaultOptions: ConfettiOptions = {
export default class Confetti implements ICanvasEffect { export default class Confetti implements ICanvasEffect {
private readonly options: ConfettiOptions; private readonly options: ConfettiOptions;
constructor(options: ConfettiOptions = DefaultOptions) { constructor(options: { [key: string]: any }) {
this.options = options; this.options = {...DefaultOptions, ...options};
} }
private context: CanvasRenderingContext2D | null = null; private context: CanvasRenderingContext2D | null = null;

View File

@ -1,11 +1,22 @@
import { _t, _td } from "../../../../languageHandler"; import { _t, _td } from "../../../../languageHandler";
type Effect = { export type Effect = {
emojis: Array<string>; emojis: Array<string>;
msgType: string; msgType: string;
command: string; command: string;
description: () => string; description: () => string;
fallbackMessage: () => string; fallbackMessage: () => string;
options: {
[key: string]: any
}
}
type ConfettiOptions = {
maxCount: number,
speed: number,
frameInterval: number,
alpha: number,
gradient: boolean,
} }
const effects: Array<Effect> = [ const effects: Array<Effect> = [
@ -15,6 +26,18 @@ const effects: Array<Effect> = [
command: 'confetti', command: 'confetti',
description: () => _td("Sends the given message with confetti"), description: () => _td("Sends the given message with confetti"),
fallbackMessage: () => _t("sends confetti") + " 🎉", fallbackMessage: () => _t("sends confetti") + " 🎉",
options: {
//set max confetti count
maxCount: 150,
//syarn addet the particle animation speed
speed: 3,
//the confetti animation frame interval in milliseconds
frameInterval: 15,
//the alpha opacity of the confetti (between 0 and 1, where 1 is opaque and 0 is invisible)
alpha: 1.0,
//use gradient instead of solid particle color
gradient: false,
} as ConfettiOptions,
}, },
]; ];