nitro-react/src/views/room/widgets/avatar-info/AvatarInfoWidgetView.tsx

239 lines
10 KiB
TypeScript
Raw Normal View History

2021-06-24 09:58:43 +02:00
import { RoomEnterEffect, RoomObjectCategory } from 'nitro-renderer';
import { FC, useCallback, useMemo, useState } from 'react';
import { GetRoomSession, GetSessionDataManager } from '../../../../api';
2021-06-12 04:53:56 +02:00
import { CreateEventDispatcherHook } from '../../../../hooks/events/event-dispatcher.base';
2021-06-17 19:23:34 +02:00
import { useRoomContext } from '../../context/RoomContext';
2021-06-24 09:58:43 +02:00
import { RoomWidgetObjectNameEvent, RoomWidgetRoomEngineUpdateEvent, RoomWidgetRoomObjectUpdateEvent, RoomWidgetUpdateDanceStatusEvent, RoomWidgetUpdateInfostandEvent, RoomWidgetUpdateInfostandFurniEvent, RoomWidgetUpdateInfostandPetEvent, RoomWidgetUpdateInfostandRentableBotEvent, RoomWidgetUpdateInfostandUserEvent } from '../../events';
2021-06-17 19:23:34 +02:00
import { RoomWidgetRoomObjectMessage } from '../../messages';
2021-06-12 04:53:56 +02:00
import { AvatarInfoWidgetViewProps } from './AvatarInfoWidgetView.types';
2021-06-24 09:58:43 +02:00
import { AvatarInfoWidgetAvatarView } from './views/avatar/AvatarInfoWidgetAvatarView';
import { AvatarInfoWidgetDecorateView } from './views/decorate/AvatarInfoWidgetDecorateView';
2021-06-13 10:13:46 +02:00
import { AvatarInfoWidgetNameView } from './views/name/AvatarInfoWidgetNameView';
2021-06-24 09:58:43 +02:00
import { AvatarInfoWidgetOwnAvatarView } from './views/own-avatar/AvatarInfoWidgetOwnAvatarView';
import { AvatarInfoWidgetOwnPetView } from './views/own-pet/AvatarInfoWidgetOwnPetView';
import { AvatarInfoWidgetPetView } from './views/pet/AvatarInfoWidgetPetView';
import { AvatarInfoWidgetRentableBotView } from './views/rentable-bot/AvatarInfoWidgetRentableBotView';
2021-06-12 04:53:56 +02:00
export const AvatarInfoWidgetView: FC<AvatarInfoWidgetViewProps> = props =>
{
2021-06-17 19:23:34 +02:00
const { eventDispatcher = null, widgetHandler = null } = useRoomContext();
const [ name, setName ] = useState<RoomWidgetObjectNameEvent>(null);
const [ infoStandEvent, setInfoStandEvent ] = useState<RoomWidgetUpdateInfostandEvent>(null);
2021-06-13 10:13:46 +02:00
const [ isGameMode, setGameMode ] = useState(false);
2021-06-24 09:58:43 +02:00
const [ isDancing, setIsDancing ] = useState(false);
const [ isDecorating, setIsDecorating ] = useState(GetRoomSession().isDecorating);
2021-06-12 04:53:56 +02:00
2021-06-24 09:58:43 +02:00
const onRoomWidgetRoomEngineUpdateEvent = useCallback((event: RoomWidgetRoomEngineUpdateEvent) =>
2021-06-12 04:53:56 +02:00
{
2021-06-13 10:13:46 +02:00
switch(event.type)
2021-06-12 04:53:56 +02:00
{
2021-06-17 19:23:34 +02:00
case RoomWidgetRoomEngineUpdateEvent.NORMAL_MODE: {
2021-06-24 09:58:43 +02:00
if(isGameMode) setGameMode(false);
2021-06-17 19:23:34 +02:00
return;
}
case RoomWidgetRoomEngineUpdateEvent.GAME_MODE: {
2021-06-24 09:58:43 +02:00
if(!isGameMode) setGameMode(true);
2021-06-17 19:23:34 +02:00
return;
}
2021-06-24 09:58:43 +02:00
}
}, [ isGameMode ]);
2021-06-17 19:23:34 +02:00
2021-06-24 09:58:43 +02:00
CreateEventDispatcherHook(RoomWidgetRoomEngineUpdateEvent.NORMAL_MODE, eventDispatcher, onRoomWidgetRoomEngineUpdateEvent);
CreateEventDispatcherHook(RoomWidgetRoomEngineUpdateEvent.GAME_MODE, eventDispatcher, onRoomWidgetRoomEngineUpdateEvent);
2021-06-17 19:23:34 +02:00
2021-06-24 09:58:43 +02:00
const onRoomObjectRemoved = useCallback((event: RoomWidgetRoomObjectUpdateEvent) =>
{
if(name)
{
if(event.id === name.id) setName(null);
}
2021-06-12 04:53:56 +02:00
2021-06-24 09:58:43 +02:00
if(infoStandEvent)
{
if(infoStandEvent instanceof RoomWidgetUpdateInfostandFurniEvent)
{
if(infoStandEvent.id === event.id) setInfoStandEvent(null);
}
2021-06-12 04:53:56 +02:00
2021-06-24 09:58:43 +02:00
else if((infoStandEvent instanceof RoomWidgetUpdateInfostandUserEvent) || (infoStandEvent instanceof RoomWidgetUpdateInfostandRentableBotEvent))
{
if(infoStandEvent.roomIndex === event.id) setInfoStandEvent(null);
}
2021-06-17 19:23:34 +02:00
2021-06-24 09:58:43 +02:00
else if(infoStandEvent instanceof RoomWidgetUpdateInfostandPetEvent)
{
if(infoStandEvent.roomIndex === event.id) setInfoStandEvent(null);
2021-06-13 10:13:46 +02:00
}
2021-06-24 09:58:43 +02:00
}
}, [ name, infoStandEvent ]);
CreateEventDispatcherHook(RoomWidgetRoomObjectUpdateEvent.USER_REMOVED, eventDispatcher, onRoomObjectRemoved);
CreateEventDispatcherHook(RoomWidgetRoomObjectUpdateEvent.FURNI_REMOVED, eventDispatcher, onRoomObjectRemoved);
const onObjectRolled = useCallback((event: RoomWidgetRoomObjectUpdateEvent) =>
{
switch(event.type)
{
2021-06-17 19:23:34 +02:00
case RoomWidgetRoomObjectUpdateEvent.OBJECT_ROLL_OVER: {
const roomObjectEvent = (event as RoomWidgetRoomObjectUpdateEvent);
2021-06-24 09:58:43 +02:00
if(infoStandEvent) return;
2021-06-17 19:23:34 +02:00
widgetHandler.processWidgetMessage(new RoomWidgetRoomObjectMessage(RoomWidgetRoomObjectMessage.GET_OBJECT_NAME, roomObjectEvent.id, roomObjectEvent.category));
2021-06-24 09:58:43 +02:00
2021-06-17 19:23:34 +02:00
return;
}
case RoomWidgetRoomObjectUpdateEvent.OBJECT_ROLL_OUT: {
const roomObjectEvent = (event as RoomWidgetRoomObjectUpdateEvent);
2021-06-12 04:53:56 +02:00
2021-06-24 09:58:43 +02:00
if(!name || (name.roomIndex !== roomObjectEvent.id)) return;
2021-06-17 19:23:34 +02:00
setName(null);
2021-06-24 09:58:43 +02:00
2021-06-13 10:13:46 +02:00
return;
}
2021-06-24 09:58:43 +02:00
}
}, [ infoStandEvent, name, widgetHandler ]);
CreateEventDispatcherHook(RoomWidgetRoomObjectUpdateEvent.OBJECT_ROLL_OVER, eventDispatcher, onObjectRolled);
CreateEventDispatcherHook(RoomWidgetRoomObjectUpdateEvent.OBJECT_ROLL_OUT, eventDispatcher, onObjectRolled);
const onObjectDeselected = useCallback((event: RoomWidgetRoomObjectUpdateEvent) =>
{
if(!infoStandEvent) return;
setInfoStandEvent(null);
}, [ infoStandEvent ]);
CreateEventDispatcherHook(RoomWidgetRoomObjectUpdateEvent.OBJECT_DESELECTED, eventDispatcher, onObjectDeselected);
const onRoomWidgetObjectNameEvent = useCallback((event: RoomWidgetObjectNameEvent) =>
{
if(event.category !== RoomObjectCategory.UNIT) return;
setName(event);
}, []);
2021-06-12 04:53:56 +02:00
2021-06-24 09:58:43 +02:00
CreateEventDispatcherHook(RoomWidgetObjectNameEvent.TYPE, eventDispatcher, onRoomWidgetObjectNameEvent);
2021-06-17 19:23:34 +02:00
2021-06-24 09:58:43 +02:00
const onRoomWidgetUpdateInfostandEvent = useCallback((event: RoomWidgetUpdateInfostandEvent) =>
{
if(name) setName(null);
setInfoStandEvent(event);
}, [ name ]);
CreateEventDispatcherHook(RoomWidgetUpdateInfostandFurniEvent.FURNI, eventDispatcher, onRoomWidgetUpdateInfostandEvent);
CreateEventDispatcherHook(RoomWidgetUpdateInfostandUserEvent.OWN_USER, eventDispatcher, onRoomWidgetUpdateInfostandEvent);
CreateEventDispatcherHook(RoomWidgetUpdateInfostandUserEvent.PEER, eventDispatcher, onRoomWidgetUpdateInfostandEvent);
CreateEventDispatcherHook(RoomWidgetUpdateInfostandUserEvent.BOT, eventDispatcher, onRoomWidgetUpdateInfostandEvent);
CreateEventDispatcherHook(RoomWidgetUpdateInfostandRentableBotEvent.RENTABLE_BOT, eventDispatcher, onRoomWidgetUpdateInfostandEvent);
2021-06-24 18:47:40 +02:00
CreateEventDispatcherHook(RoomWidgetUpdateInfostandPetEvent.PET_INFO, eventDispatcher, onRoomWidgetUpdateInfostandEvent);
2021-06-24 09:58:43 +02:00
const onRoomWidgetUpdateDanceStatusEvent = useCallback((event: RoomWidgetUpdateDanceStatusEvent) =>
{
setIsDancing(event.isDancing);
}, []);
CreateEventDispatcherHook(RoomWidgetUpdateDanceStatusEvent.UPDATE_DANCE, eventDispatcher, onRoomWidgetUpdateDanceStatusEvent);
const onRoomWidgetRoomObjectUpdateEvent = useCallback((event: RoomWidgetRoomObjectUpdateEvent) =>
{
switch(event.type)
{
case RoomWidgetRoomObjectUpdateEvent.USER_ADDED: {
// bubble if friend
2021-06-17 19:23:34 +02:00
return;
}
2021-06-24 09:58:43 +02:00
case RoomWidgetRoomObjectUpdateEvent.OBJECT_SELECTED: {
// set if waiting for pet
2021-06-17 19:23:34 +02:00
return;
}
}
2021-06-24 09:58:43 +02:00
}, []);
CreateEventDispatcherHook(RoomWidgetRoomObjectUpdateEvent.USER_ADDED, eventDispatcher, onRoomWidgetRoomObjectUpdateEvent);
CreateEventDispatcherHook(RoomWidgetRoomObjectUpdateEvent.OBJECT_SELECTED, eventDispatcher, onRoomWidgetRoomObjectUpdateEvent);
const decorateView = useMemo(() =>
{
GetRoomSession().isDecorating = isDecorating;
if(!isDecorating) return null;
const userId = GetSessionDataManager().userId;
const userName = GetSessionDataManager().userName;
const roomIndex = GetRoomSession().ownRoomIndex;
2021-06-24 18:47:40 +02:00
return <AvatarInfoWidgetDecorateView userId={ userId } userName={ userName } roomIndex={ roomIndex } setIsDecorating={ setIsDecorating } />;
2021-06-24 09:58:43 +02:00
}, [ isDecorating ]);
const clearInfoStandEvent = useCallback(() =>
{
setInfoStandEvent(null);
}, []);
2021-06-25 07:42:41 +02:00
const clearName = useCallback(() =>
{
setName(null);
}, []);
2021-06-24 09:58:43 +02:00
const currentView = useMemo(() =>
{
if(isGameMode) return null;
if(decorateView) return decorateView;
2021-06-25 07:42:41 +02:00
if(name) return <AvatarInfoWidgetNameView nameData={ name } close={ clearName } />;
2021-06-24 09:58:43 +02:00
if(infoStandEvent)
{
switch(infoStandEvent.type)
{
case RoomWidgetUpdateInfostandUserEvent.OWN_USER:
case RoomWidgetUpdateInfostandUserEvent.PEER: {
const event = (infoStandEvent as RoomWidgetUpdateInfostandUserEvent);
if(event.isSpectatorMode) return null;
// if existing name bubble remove it
if(event.isOwnUser)
{
if(RoomEnterEffect.isRunning()) return null;
2021-06-24 18:47:40 +02:00
return <AvatarInfoWidgetOwnAvatarView userData={ event } isDancing={ isDancing } setIsDecorating={ setIsDecorating } close={ clearInfoStandEvent } />;
2021-06-24 09:58:43 +02:00
}
return <AvatarInfoWidgetAvatarView userData={ event } close={ clearInfoStandEvent } />;
}
case RoomWidgetUpdateInfostandPetEvent.PET_INFO: {
const event = (infoStandEvent as RoomWidgetUpdateInfostandPetEvent);
if(event.isOwner)
{
return <AvatarInfoWidgetOwnPetView petData={ event } close={ clearInfoStandEvent } />;
}
return <AvatarInfoWidgetPetView petData={ event } close={ clearInfoStandEvent } />;
}
case RoomWidgetUpdateInfostandRentableBotEvent.RENTABLE_BOT: {
return <AvatarInfoWidgetRentableBotView rentableBotData={ (infoStandEvent as RoomWidgetUpdateInfostandRentableBotEvent) } close={ clearInfoStandEvent } />
}
case RoomWidgetUpdateInfostandFurniEvent.FURNI: {
return null;
}
}
}
return null;
2021-06-24 18:47:40 +02:00
}, [ isGameMode, decorateView, name, isDancing, infoStandEvent, clearInfoStandEvent ]);
2021-06-13 10:13:46 +02:00
return (
<>
2021-06-24 09:58:43 +02:00
{ currentView }
2021-06-13 10:13:46 +02:00
</>
)
2021-06-12 04:53:56 +02:00
}