2021-06-15 19:40:02 +02:00
|
|
|
import { RoomCameraWidgetEditorEffect } from 'nitro-renderer/src/nitro/room/camera-widget/RoomCameraWidgetEditorEffect';
|
|
|
|
import { RoomCameraWidgetManagerEvent } from 'nitro-renderer/src/nitro/room/events/RoomCameraWidgetManagerEvent';
|
|
|
|
import { FC, useCallback, useState } from 'react';
|
|
|
|
import { GetRoomEngine } from '../../../../api';
|
2021-06-13 07:13:55 +02:00
|
|
|
import { RoomWidgetCameraEvent } from '../../../../events/room-widgets/camera/RoomWidgetCameraEvent';
|
2021-06-15 19:40:02 +02:00
|
|
|
import { useRoomEngineEvent } from '../../../../hooks/events/nitro/room/room-engine-event';
|
2021-06-13 07:13:55 +02:00
|
|
|
import { useUiEvent } from '../../../../hooks/events/ui/ui-event';
|
|
|
|
import { CameraWidgetViewProps } from './CameraWidgetView.types';
|
2021-06-15 19:40:02 +02:00
|
|
|
import { CameraWidgetCaptureView } from './views/capture/CameraWidgetCaptureView';
|
|
|
|
import { CameraWidgetEditorView } from './views/editor/CameraWidgetEditorView';
|
2021-06-13 07:13:55 +02:00
|
|
|
|
|
|
|
export const CameraWidgetView: FC<CameraWidgetViewProps> = props =>
|
|
|
|
{
|
2021-06-15 19:40:02 +02:00
|
|
|
const [ isCaptureVisible, setIsCaptureVisible ] = useState(false);
|
|
|
|
const [ isEditorVisible, setIsEditorVisible ] = useState(false);
|
|
|
|
const [ chosenPicture, setChosenPicture ] = useState<HTMLImageElement>(null);
|
|
|
|
const [ availableEffects, setAvailableEffects ] = useState<RoomCameraWidgetEditorEffect[]>(null);
|
2021-06-13 07:13:55 +02:00
|
|
|
|
2021-06-15 19:40:02 +02:00
|
|
|
const getAvailableEffects = useCallback(() =>
|
|
|
|
{
|
|
|
|
if(GetRoomEngine().roomCameraWidgetManager.isLoaded)
|
|
|
|
{
|
|
|
|
setAvailableEffects(Array.from(GetRoomEngine().roomCameraWidgetManager.loadedEffects.values()));
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onNitroEvent = useCallback((event: RoomWidgetCameraEvent) =>
|
2021-06-13 07:13:55 +02:00
|
|
|
{
|
|
|
|
switch(event.type)
|
|
|
|
{
|
|
|
|
case RoomWidgetCameraEvent.SHOW_CAMERA:
|
2021-06-15 19:40:02 +02:00
|
|
|
setIsCaptureVisible(true);
|
|
|
|
getAvailableEffects();
|
2021-06-13 07:13:55 +02:00
|
|
|
return;
|
|
|
|
case RoomWidgetCameraEvent.HIDE_CAMERA:
|
2021-06-15 19:40:02 +02:00
|
|
|
setIsCaptureVisible(false);
|
|
|
|
setIsEditorVisible(false);
|
2021-06-13 07:13:55 +02:00
|
|
|
return;
|
|
|
|
case RoomWidgetCameraEvent.TOGGLE_CAMERA:
|
2021-06-15 19:40:02 +02:00
|
|
|
setIsEditorVisible(false);
|
|
|
|
setIsCaptureVisible(value => !value);
|
|
|
|
getAvailableEffects();
|
|
|
|
return;
|
|
|
|
case RoomCameraWidgetManagerEvent.INITIALIZED:
|
|
|
|
getAvailableEffects();
|
2021-06-13 07:13:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2021-06-15 19:40:02 +02:00
|
|
|
useUiEvent(RoomWidgetCameraEvent.SHOW_CAMERA, onNitroEvent);
|
|
|
|
useUiEvent(RoomWidgetCameraEvent.HIDE_CAMERA, onNitroEvent);
|
|
|
|
useUiEvent(RoomWidgetCameraEvent.TOGGLE_CAMERA, onNitroEvent);
|
|
|
|
useRoomEngineEvent(RoomCameraWidgetManagerEvent.INITIALIZED, onNitroEvent);
|
2021-06-13 07:13:55 +02:00
|
|
|
|
2021-06-15 19:40:02 +02:00
|
|
|
const processAction = useCallback((type: string, value: any = null) =>
|
2021-06-13 07:13:55 +02:00
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case 'close':
|
2021-06-15 19:40:02 +02:00
|
|
|
setIsCaptureVisible(false);
|
|
|
|
setIsEditorVisible(false);
|
|
|
|
return;
|
|
|
|
case 'capture_choose_picture':
|
|
|
|
setChosenPicture(value);
|
|
|
|
setIsCaptureVisible(false);
|
|
|
|
setIsEditorVisible(true);
|
2021-06-13 07:13:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}, []);
|
2021-06-14 09:49:30 +02:00
|
|
|
|
2021-06-13 07:13:55 +02:00
|
|
|
return (
|
2021-06-15 19:40:02 +02:00
|
|
|
( isCaptureVisible && <CameraWidgetCaptureView onCloseClick={ () => processAction('close') } onChoosePicture={ (picture) => processAction('capture_choose_picture', picture) } /> ) ||
|
|
|
|
( isEditorVisible && <CameraWidgetEditorView onCloseClick={ () => processAction('close') } picture={ chosenPicture } availableEffects={ availableEffects } /> )
|
2021-06-13 07:13:55 +02:00
|
|
|
);
|
|
|
|
}
|