diff --git a/src/api/nitro/room/widgets/events/RoomWidgetDoorbellEvent.ts b/src/api/nitro/room/widgets/events/RoomWidgetDoorbellEvent.ts deleted file mode 100644 index 9bad7fa4..00000000 --- a/src/api/nitro/room/widgets/events/RoomWidgetDoorbellEvent.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { RoomWidgetUpdateEvent } from './RoomWidgetUpdateEvent'; - -export class RoomWidgetDoorbellEvent extends RoomWidgetUpdateEvent -{ - public static RINGING: string = 'RWDE_RINGING'; - public static REJECTED: string = 'RWDE_REJECTED'; - public static ACCEPTED: string = 'RWDE_ACCEPTED'; - - private _userName: string = ''; - - constructor(type: string, userName: string) - { - super(type); - - this._userName = userName; - } - - public get userName(): string - { - return this._userName; - } -} diff --git a/src/api/nitro/room/widgets/events/index.ts b/src/api/nitro/room/widgets/events/index.ts index cd7d85f6..58abd356 100644 --- a/src/api/nitro/room/widgets/events/index.ts +++ b/src/api/nitro/room/widgets/events/index.ts @@ -3,7 +3,6 @@ export * from './RoomDimmerPreset'; export * from './RoomObjectItem'; export * from './RoomWidgetAvatarInfoEvent'; export * from './RoomWidgetChooserContentEvent'; -export * from './RoomWidgetDoorbellEvent'; export * from './RoomWidgetFloodControlEvent'; export * from './RoomWidgetObjectNameEvent'; export * from './RoomWidgetPollUpdateEvent'; diff --git a/src/api/nitro/room/widgets/handlers/DoorbellWidgetHandler.ts b/src/api/nitro/room/widgets/handlers/DoorbellWidgetHandler.ts deleted file mode 100644 index b2b69ee9..00000000 --- a/src/api/nitro/room/widgets/handlers/DoorbellWidgetHandler.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { NitroEvent, RoomSessionDoorbellEvent, RoomWidgetEnum } from '@nitrots/nitro-renderer'; -import { RoomWidgetDoorbellEvent, RoomWidgetUpdateEvent } from '../events'; -import { RoomWidgetLetUserInMessage, RoomWidgetMessage } from '../messages'; -import { RoomWidgetHandler } from './RoomWidgetHandler'; - -export class DoorbellWidgetHandler extends RoomWidgetHandler -{ - public processEvent(event: NitroEvent): void - { - const doorbellEvent = (event as RoomSessionDoorbellEvent); - - switch(event.type) - { - case RoomSessionDoorbellEvent.DOORBELL: - this.container.eventDispatcher.dispatchEvent(new RoomWidgetDoorbellEvent(RoomWidgetDoorbellEvent.RINGING, doorbellEvent.userName)); - return; - case RoomSessionDoorbellEvent.RSDE_REJECTED: - this.container.eventDispatcher.dispatchEvent(new RoomWidgetDoorbellEvent(RoomWidgetDoorbellEvent.REJECTED, doorbellEvent.userName)); - return; - case RoomSessionDoorbellEvent.RSDE_ACCEPTED: - this.container.eventDispatcher.dispatchEvent(new RoomWidgetDoorbellEvent(RoomWidgetDoorbellEvent.ACCEPTED, doorbellEvent.userName)); - return; - } - } - - public processWidgetMessage(message: RoomWidgetMessage): RoomWidgetUpdateEvent - { - switch(message.type) - { - case RoomWidgetLetUserInMessage.LET_USER_IN: - const letUserInMessage = (message as RoomWidgetLetUserInMessage); - - this.container.roomSession.sendDoorbellApprovalMessage(letUserInMessage.userName, letUserInMessage.canEnter); - break; - } - - return null; - } - - public get type(): string - { - return RoomWidgetEnum.DOORBELL; - } - - public get eventTypes(): string[] - { - return [ - RoomSessionDoorbellEvent.DOORBELL, - RoomSessionDoorbellEvent.RSDE_REJECTED, - RoomSessionDoorbellEvent.RSDE_ACCEPTED - ]; - } - - public get messageTypes(): string[] - { - return [ - RoomWidgetLetUserInMessage.LET_USER_IN - ]; - } -} diff --git a/src/api/nitro/room/widgets/handlers/index.ts b/src/api/nitro/room/widgets/handlers/index.ts index 61153aed..d0fed191 100644 --- a/src/api/nitro/room/widgets/handlers/index.ts +++ b/src/api/nitro/room/widgets/handlers/index.ts @@ -1,4 +1,3 @@ -export * from './DoorbellWidgetHandler'; export * from './FurniChooserWidgetHandler'; export * from './FurnitureContextMenuWidgetHandler'; export * from './FurnitureCreditWidgetHandler'; diff --git a/src/api/nitro/room/widgets/messages/RoomWidgetLetUserInMessage.ts b/src/api/nitro/room/widgets/messages/RoomWidgetLetUserInMessage.ts deleted file mode 100644 index 9001fe2a..00000000 --- a/src/api/nitro/room/widgets/messages/RoomWidgetLetUserInMessage.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { RoomWidgetMessage } from './RoomWidgetMessage'; - -export class RoomWidgetLetUserInMessage extends RoomWidgetMessage -{ - public static LET_USER_IN: string = 'RWLUIM_LET_USER_IN'; - - private _userName: string; - private _canEnter: boolean; - - constructor(userName: string, canEnter: boolean) - { - super(RoomWidgetLetUserInMessage.LET_USER_IN); - - this._userName = userName; - this._canEnter = canEnter; - } - - public get userName(): string - { - return this._userName; - } - - public get canEnter(): boolean - { - return this._canEnter; - } -} diff --git a/src/api/nitro/room/widgets/messages/index.ts b/src/api/nitro/room/widgets/messages/index.ts index bf9ae219..75b9cae3 100644 --- a/src/api/nitro/room/widgets/messages/index.ts +++ b/src/api/nitro/room/widgets/messages/index.ts @@ -11,7 +11,6 @@ export * from './RoomWidgetDimmerPreviewMessage'; export * from './RoomWidgetDimmerSavePresetMessage'; export * from './RoomWidgetFurniActionMessage'; export * from './RoomWidgetFurniToWidgetMessage'; -export * from './RoomWidgetLetUserInMessage'; export * from './RoomWidgetMessage'; export * from './RoomWidgetPollMessage'; export * from './RoomWidgetPresentOpenMessage'; diff --git a/src/components/room/RoomView.tsx b/src/components/room/RoomView.tsx index 01aa2c3c..5b044dd8 100644 --- a/src/components/room/RoomView.tsx +++ b/src/components/room/RoomView.tsx @@ -1,6 +1,6 @@ import { EventDispatcher, IRoomSession, NitroRectangle, RoomGeometry, RoomVariableEnum, Vector3d } from '@nitrots/nitro-renderer'; import { FC, useEffect, useRef, useState } from 'react'; -import { DispatchMouseEvent, DispatchTouchEvent, DoorbellWidgetHandler, FurniChooserWidgetHandler, FurnitureContextMenuWidgetHandler, FurnitureCreditWidgetHandler, FurnitureCustomStackHeightWidgetHandler, FurnitureDimmerWidgetHandler, FurnitureExternalImageWidgetHandler, FurnitureInternalLinkHandler, FurnitureMannequinWidgetHandler, FurniturePresentWidgetHandler, FurnitureRoomLinkHandler, FurnitureYoutubeDisplayWidgetHandler, GetNitroInstance, GetRoomEngine, InitializeRoomInstanceRenderingCanvas, IRoomWidgetHandlerManager, PollWidgetHandler, RoomWidgetAvatarInfoHandler, RoomWidgetChatHandler, RoomWidgetChatInputHandler, RoomWidgetHandlerManager, RoomWidgetInfostandHandler, RoomWidgetRoomToolsHandler, RoomWidgetUpdateRoomViewEvent, UserChooserWidgetHandler, WordQuizWidgetHandler } from '../../api'; +import { DispatchMouseEvent, DispatchTouchEvent, FurniChooserWidgetHandler, FurnitureContextMenuWidgetHandler, FurnitureCreditWidgetHandler, FurnitureCustomStackHeightWidgetHandler, FurnitureDimmerWidgetHandler, FurnitureExternalImageWidgetHandler, FurnitureInternalLinkHandler, FurnitureMannequinWidgetHandler, FurniturePresentWidgetHandler, FurnitureRoomLinkHandler, FurnitureYoutubeDisplayWidgetHandler, GetNitroInstance, GetRoomEngine, InitializeRoomInstanceRenderingCanvas, IRoomWidgetHandlerManager, PollWidgetHandler, RoomWidgetAvatarInfoHandler, RoomWidgetChatHandler, RoomWidgetChatInputHandler, RoomWidgetHandlerManager, RoomWidgetInfostandHandler, RoomWidgetRoomToolsHandler, RoomWidgetUpdateRoomViewEvent, UserChooserWidgetHandler, WordQuizWidgetHandler } from '../../api'; import { Base } from '../../common'; import { RoomColorView } from './RoomColorView'; import { RoomContextProvider } from './RoomContext'; @@ -38,7 +38,6 @@ export const RoomView: FC = props => widgetHandlerManager.registerHandler(new RoomWidgetChatInputHandler()); widgetHandlerManager.registerHandler(new RoomWidgetChatHandler()); widgetHandlerManager.registerHandler(new UserChooserWidgetHandler()); - widgetHandlerManager.registerHandler(new DoorbellWidgetHandler()); widgetHandlerManager.registerHandler(new WordQuizWidgetHandler()); widgetHandlerManager.registerHandler(new PollWidgetHandler()); diff --git a/src/components/room/widgets/doorbell/DoorbellWidgetView.tsx b/src/components/room/widgets/doorbell/DoorbellWidgetView.tsx index cf88c433..c7d1d216 100644 --- a/src/components/room/widgets/doorbell/DoorbellWidgetView.tsx +++ b/src/components/room/widgets/doorbell/DoorbellWidgetView.tsx @@ -1,63 +1,18 @@ -import { FC, useCallback, useState } from 'react'; -import { LocalizeText, RoomWidgetDoorbellEvent, RoomWidgetLetUserInMessage } from '../../../../api'; +import { FC, useEffect, useState } from 'react'; +import { LocalizeText } from '../../../../api'; import { Base, Button, Column, Flex, Grid, NitroCardContentView, NitroCardHeaderView, NitroCardView } from '../../../../common'; -import { UseEventDispatcherHook } from '../../../../hooks'; -import { useRoomContext } from '../../RoomContext'; +import { useDoorbellWidget } from '../../../../hooks'; export const DoorbellWidgetView: FC<{}> = props => { const [ isVisible, setIsVisible ] = useState(false); - const [ users, setUsers ] = useState([]); - const { eventDispatcher = null, widgetHandler = null } = useRoomContext(); + const { users = [], answer = null } = useDoorbellWidget(); - const addUser = useCallback((userName: string) => + useEffect(() => { - if(users.indexOf(userName) >= 0) return; - - setUsers([ ...users, userName ]); - setIsVisible(true); + setIsVisible(!!users.length); }, [ users ]); - const removeUser = useCallback((userName: string) => - { - const index = users.indexOf(userName); - - if(index === -1) return; - - const newUsers = [ ...users ]; - - newUsers.splice(index, 1); - - setUsers(newUsers); - - if(!newUsers.length) setIsVisible(false); - }, [ users ]); - - const onRoomWidgetDoorbellEvent = useCallback((event: RoomWidgetDoorbellEvent) => - { - switch(event.type) - { - case RoomWidgetDoorbellEvent.RINGING: - addUser(event.userName); - return; - case RoomWidgetDoorbellEvent.REJECTED: - case RoomWidgetDoorbellEvent.ACCEPTED: - removeUser(event.userName); - return; - } - }, [ addUser, removeUser ]); - - UseEventDispatcherHook(RoomWidgetDoorbellEvent.RINGING, eventDispatcher, onRoomWidgetDoorbellEvent); - UseEventDispatcherHook(RoomWidgetDoorbellEvent.REJECTED, eventDispatcher, onRoomWidgetDoorbellEvent); - UseEventDispatcherHook(RoomWidgetDoorbellEvent.ACCEPTED, eventDispatcher, onRoomWidgetDoorbellEvent); - - const answer = useCallback((userName: string, flag: boolean) => - { - widgetHandler.processWidgetMessage(new RoomWidgetLetUserInMessage(userName, flag)); - - removeUser(userName); - }, [ widgetHandler, removeUser ]); - if(!isVisible) return null; return ( @@ -66,7 +21,7 @@ export const DoorbellWidgetView: FC<{}> = props => - Username + { LocalizeText('generic.username') } diff --git a/src/hooks/index.ts b/src/hooks/index.ts index a78a216d..8713e5db 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -8,6 +8,7 @@ export * from './inventory'; export * from './messages'; export * from './navigator'; export * from './purse'; +export * from './rooms'; export * from './session'; export * from './UseMountEffect'; export * from './useSharedVisibility'; diff --git a/src/hooks/rooms/index.ts b/src/hooks/rooms/index.ts new file mode 100644 index 00000000..e7e7a361 --- /dev/null +++ b/src/hooks/rooms/index.ts @@ -0,0 +1 @@ +export * from './widgets'; diff --git a/src/hooks/rooms/widgets/index.ts b/src/hooks/rooms/widgets/index.ts new file mode 100644 index 00000000..d70c93df --- /dev/null +++ b/src/hooks/rooms/widgets/index.ts @@ -0,0 +1 @@ +export * from './useDoorbellWidget'; diff --git a/src/hooks/rooms/widgets/useDoorbellWidget.ts b/src/hooks/rooms/widgets/useDoorbellWidget.ts new file mode 100644 index 00000000..ea1f7cf3 --- /dev/null +++ b/src/hooks/rooms/widgets/useDoorbellWidget.ts @@ -0,0 +1,58 @@ +import { RoomSessionDoorbellEvent } from '@nitrots/nitro-renderer'; +import { useCallback, useState } from 'react'; +import { GetRoomSession } from '../../../api'; +import { UseRoomSessionManagerEvent } from '../../events'; + +const useDoorbellWidgetState = () => +{ + const [ users, setUsers ] = useState([]); + + const addUser = useCallback((userName: string) => + { + if(users.indexOf(userName) >= 0) return; + + setUsers([ ...users, userName ]); + }, [ users ]); + + const removeUser = useCallback((userName: string) => + { + const index = users.indexOf(userName); + + if(index === -1) return; + + const newUsers = [ ...users ]; + + newUsers.splice(index, 1); + + setUsers(newUsers); + }, [ users ]); + + const answer = useCallback((userName: string, flag: boolean) => + { + GetRoomSession().sendDoorbellApprovalMessage(userName, flag); + + removeUser(userName); + }, [ removeUser ]); + + const onRoomSessionDoorbellEvent = useCallback((event: RoomSessionDoorbellEvent) => + { + switch(event.type) + { + case RoomSessionDoorbellEvent.DOORBELL: + addUser(event.userName); + return; + case RoomSessionDoorbellEvent.RSDE_REJECTED: + case RoomSessionDoorbellEvent.RSDE_ACCEPTED: + removeUser(event.userName); + return; + } + }, [ addUser, removeUser ]); + + UseRoomSessionManagerEvent(RoomSessionDoorbellEvent.DOORBELL, onRoomSessionDoorbellEvent); + UseRoomSessionManagerEvent(RoomSessionDoorbellEvent.RSDE_REJECTED, onRoomSessionDoorbellEvent); + UseRoomSessionManagerEvent(RoomSessionDoorbellEvent.RSDE_ACCEPTED, onRoomSessionDoorbellEvent); + + return { users, addUser, removeUser, answer }; +} + +export const useDoorbellWidget = useDoorbellWidgetState;