2022-03-07 08:35:32 +01:00
|
|
|
import { MouseEventType, TouchEventType } from '@nitrots/nitro-renderer';
|
2021-11-26 04:32:51 +01:00
|
|
|
import { NitroSoundEvent } from '@nitrots/nitro-renderer/src/nitro/events/NitroSoundEvent';
|
2022-03-03 10:11:31 +01:00
|
|
|
import { DispatchMainEvent } from '../../hooks';
|
2021-11-26 04:32:51 +01:00
|
|
|
|
2022-03-07 08:35:32 +01:00
|
|
|
let canPlaySound = false;
|
|
|
|
|
|
|
|
export const PlaySound = (sampleCode: string) =>
|
2021-11-26 04:32:51 +01:00
|
|
|
{
|
2022-03-07 08:35:32 +01:00
|
|
|
if(!canPlaySound) return;
|
|
|
|
|
2022-03-03 10:11:31 +01:00
|
|
|
DispatchMainEvent(new NitroSoundEvent(NitroSoundEvent.PLAY_SOUND, sampleCode));
|
2021-11-26 04:32:51 +01:00
|
|
|
}
|
2022-03-07 08:35:32 +01:00
|
|
|
|
|
|
|
const eventTypes = [ MouseEventType.MOUSE_CLICK, MouseEventType.MOUSE_MOVE, MouseEventType.MOUSE_MOVE, TouchEventType.TOUCH_MOVE, 'focus' ];
|
|
|
|
|
|
|
|
const startListening = () =>
|
|
|
|
{
|
|
|
|
const stopListening = () => eventTypes.forEach(type => window.removeEventListener(type, onEvent));
|
|
|
|
|
|
|
|
const onEvent = (event: Event) => ((canPlaySound = true) && stopListening());
|
|
|
|
|
|
|
|
eventTypes.forEach(type => window.addEventListener(type, onEvent));
|
|
|
|
}
|
|
|
|
|
|
|
|
startListening();
|