2020-10-21 15:29:25 +03:00
|
|
|
import React, { FunctionComponent, useEffect, useRef } from 'react';
|
2020-10-19 22:25:01 +03:00
|
|
|
import dis from '../../../../dispatcher/dispatcher';
|
|
|
|
import ICanvasEffect from './ICanvasEffect.js';
|
|
|
|
|
2020-10-21 15:48:11 +03:00
|
|
|
export type EffectsOverlayProps = {
|
2020-10-19 22:25:01 +03:00
|
|
|
roomWidth: number;
|
|
|
|
}
|
|
|
|
|
2020-10-21 15:29:25 +03:00
|
|
|
const EffectsOverlay: FunctionComponent<EffectsOverlayProps> = ({ roomWidth }) => {
|
2020-10-19 22:25:01 +03:00
|
|
|
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
|
|
const effectsRef = useRef<Map<String, ICanvasEffect>>(new Map<String, ICanvasEffect>());
|
|
|
|
|
|
|
|
const lazyLoadEffectModule = async (name: string): Promise<ICanvasEffect> => {
|
2020-10-21 15:29:25 +03:00
|
|
|
if (!name) return null;
|
2020-10-19 22:25:01 +03:00
|
|
|
let effect = effectsRef.current[name] ?? null;
|
2020-10-21 15:29:25 +03:00
|
|
|
if (effect === null) {
|
2020-10-19 22:25:01 +03:00
|
|
|
try {
|
2020-10-21 15:43:09 +03:00
|
|
|
const { default: Effect } = await import(`./${name}`);
|
2020-10-19 22:25:01 +03:00
|
|
|
effect = new Effect();
|
|
|
|
effectsRef.current[name] = effect;
|
|
|
|
} catch (err) {
|
|
|
|
console.warn('Unable to load effect module at \'./${name}\'.', err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return effect;
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-10-21 15:43:09 +03:00
|
|
|
const resize = () => {
|
|
|
|
canvasRef.current.height = window.innerHeight;
|
|
|
|
};
|
|
|
|
const onAction = (payload: { action: string }) => {
|
|
|
|
const actionPrefix = 'effects.';
|
|
|
|
if (payload.action.indexOf(actionPrefix) === 0) {
|
|
|
|
const effect = payload.action.substr(actionPrefix.length);
|
|
|
|
lazyLoadEffectModule(effect).then((module) => module?.start(canvasRef.current));
|
|
|
|
}
|
|
|
|
}
|
2020-10-19 22:25:01 +03:00
|
|
|
const dispatcherRef = dis.register(onAction);
|
|
|
|
const canvas = canvasRef.current;
|
|
|
|
canvas.height = window.innerHeight;
|
|
|
|
window.addEventListener('resize', resize, true);
|
2020-10-21 15:29:25 +03:00
|
|
|
|
|
|
|
return () => {
|
2020-10-19 22:25:01 +03:00
|
|
|
dis.unregister(dispatcherRef);
|
2020-10-21 16:15:26 +03:00
|
|
|
window.removeEventListener('resize', resize);
|
2020-10-21 15:56:04 +03:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
const currentEffects = effectsRef.current; // this is not a react node ref, warning can be safely ignored
|
2020-10-21 15:43:09 +03:00
|
|
|
for (const effect in currentEffects) {
|
|
|
|
const effectModule: ICanvasEffect = currentEffects[effect];
|
2020-10-21 15:56:04 +03:00
|
|
|
if (effectModule && effectModule.isRunning) {
|
2020-10-21 15:43:09 +03:00
|
|
|
effectModule.stop();
|
|
|
|
}
|
2020-10-19 22:25:01 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<canvas
|
|
|
|
ref={canvasRef}
|
2020-10-21 15:43:09 +03:00
|
|
|
width={roomWidth}
|
2020-10-19 22:25:01 +03:00
|
|
|
style={{
|
|
|
|
display: 'block',
|
|
|
|
zIndex: 999999,
|
|
|
|
pointerEvents: 'none',
|
|
|
|
position: 'fixed',
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-21 15:43:09 +03:00
|
|
|
export default EffectsOverlay;
|