diff --git a/src/api/nitro/room/widgets/events/RoomWidgetUpdateCreditFurniEvent.ts b/src/api/nitro/room/widgets/events/RoomWidgetUpdateCreditFurniEvent.ts new file mode 100644 index 00000000..be70172a --- /dev/null +++ b/src/api/nitro/room/widgets/events/RoomWidgetUpdateCreditFurniEvent.ts @@ -0,0 +1,34 @@ +import { RoomWidgetUpdateEvent } from './RoomWidgetUpdateEvent'; + +export class RoomWidgetUpdateCreditFurniEvent extends RoomWidgetUpdateEvent +{ + public static CREDIT_FURNI_UPDATE: string = 'RWUCFE_CREDIT_FURNI_UPDATE'; + + private _objectId: number; + private _value: number; + private _furniType: string; + + constructor(type: string, objectId: number, value: number, furniType: string) + { + super(type); + + this._objectId = objectId; + this._value = value; + this._furniType = furniType; + } + + public get objectId(): number + { + return this._objectId; + } + + public get value(): number + { + return this._value; + } + + public get furniType(): string + { + return this._furniType; + } +} diff --git a/src/api/nitro/room/widgets/events/index.ts b/src/api/nitro/room/widgets/events/index.ts index 1c214b61..f612d85b 100644 --- a/src/api/nitro/room/widgets/events/index.ts +++ b/src/api/nitro/room/widgets/events/index.ts @@ -11,6 +11,7 @@ export * from './RoomWidgetRoomObjectUpdateEvent'; export * from './RoomWidgetUpdateBackgroundColorPreviewEvent'; export * from './RoomWidgetUpdateChatEvent'; export * from './RoomWidgetUpdateChatInputContentEvent'; +export * from './RoomWidgetUpdateCreditFurniEvent'; export * from './RoomWidgetUpdateCustomStackHeightEvent'; export * from './RoomWidgetUpdateDanceStatusEvent'; export * from './RoomWidgetUpdateDimmerEvent'; diff --git a/src/api/nitro/room/widgets/handlers/FurnitureCreditWidgetHandler.ts b/src/api/nitro/room/widgets/handlers/FurnitureCreditWidgetHandler.ts new file mode 100644 index 00000000..7ab62fbb --- /dev/null +++ b/src/api/nitro/room/widgets/handlers/FurnitureCreditWidgetHandler.ts @@ -0,0 +1,59 @@ +import { FurnitureExchangeComposer, NitroEvent, RoomObjectVariable, RoomWidgetEnum } from '@nitrots/nitro-renderer'; +import { RoomWidgetCreditFurniRedeemMessage, RoomWidgetUpdateCreditFurniEvent, RoomWidgetUpdateEvent } from '..'; +import { GetRoomEngine } from '../..'; +import { IsOwnerOfFurniture } from '../../..'; +import { RoomWidgetFurniToWidgetMessage, RoomWidgetMessage } from '../messages'; +import { RoomWidgetHandler } from './RoomWidgetHandler'; + +export class FurnitureCreditWidgetHandler extends RoomWidgetHandler +{ + public processEvent(event: NitroEvent): void + { + return; + } + + public processWidgetMessage(message: RoomWidgetMessage): RoomWidgetUpdateEvent + { + switch(message.type) + { + case RoomWidgetFurniToWidgetMessage.REQUEST_CREDITFURNI: { + const creditMessage = (message as RoomWidgetFurniToWidgetMessage); + + const roomObject = GetRoomEngine().getRoomObject(creditMessage.roomId, creditMessage.objectId, creditMessage.category); + + if(!roomObject || !IsOwnerOfFurniture(roomObject)) return; + + this.container.eventDispatcher.dispatchEvent(new RoomWidgetUpdateCreditFurniEvent(RoomWidgetUpdateCreditFurniEvent.CREDIT_FURNI_UPDATE, creditMessage.objectId, roomObject.model.getValue(RoomObjectVariable.FURNITURE_CREDIT_VALUE), (roomObject.model.getValue(RoomObjectVariable.FURNITURE_TYPE_ID) + '_' + creditMessage.type + '_' + creditMessage.objectId))); + + break; + } + case RoomWidgetCreditFurniRedeemMessage.REDEEM: { + const redeemMessage = (message as RoomWidgetCreditFurniRedeemMessage); + + this.container.roomSession.connection.send(new FurnitureExchangeComposer(redeemMessage.objectId)); + + break; + } + } + + return null; + } + + public get type(): string + { + return RoomWidgetEnum.FURNI_CREDIT_WIDGET; + } + + public get eventTypes(): string[] + { + return []; + } + + public get messageTypes(): string[] + { + return [ + RoomWidgetFurniToWidgetMessage.REQUEST_CREDITFURNI, + RoomWidgetCreditFurniRedeemMessage.REDEEM + ]; + } +} diff --git a/src/api/nitro/room/widgets/handlers/index.ts b/src/api/nitro/room/widgets/handlers/index.ts index ebd2591f..f2a12569 100644 --- a/src/api/nitro/room/widgets/handlers/index.ts +++ b/src/api/nitro/room/widgets/handlers/index.ts @@ -1,6 +1,7 @@ export * from './DoorbellWidgetHandler'; export * from './FurniChooserWidgetHandler'; export * from './FurnitureContextMenuWidgetHandler'; +export * from './FurnitureCreditWidgetHandler'; export * from './FurnitureCustomStackHeightWidgetHandler'; export * from './FurnitureDimmerWidgetHandler'; export * from './FurnitureExternalImageWidgetHandler'; diff --git a/src/api/nitro/room/widgets/messages/RoomWidgetCreditFurniRedeemMessage.ts b/src/api/nitro/room/widgets/messages/RoomWidgetCreditFurniRedeemMessage.ts new file mode 100644 index 00000000..7b77fdaa --- /dev/null +++ b/src/api/nitro/room/widgets/messages/RoomWidgetCreditFurniRedeemMessage.ts @@ -0,0 +1,20 @@ +import { RoomWidgetMessage } from './RoomWidgetMessage'; + +export class RoomWidgetCreditFurniRedeemMessage extends RoomWidgetMessage +{ + public static REDEEM: string = 'RWCFRM_REDEEM'; + + private _objectId: number; + + constructor(type: string, objectId: number) + { + super(type); + + this._objectId = objectId; + } + + public get objectId(): number + { + return this._objectId; + } +} diff --git a/src/api/nitro/room/widgets/messages/index.ts b/src/api/nitro/room/widgets/messages/index.ts index 65309aad..287a3aa6 100644 --- a/src/api/nitro/room/widgets/messages/index.ts +++ b/src/api/nitro/room/widgets/messages/index.ts @@ -4,6 +4,7 @@ export * from './RoomWidgetChangePostureMessage'; export * from './RoomWidgetChatMessage'; export * from './RoomWidgetChatSelectAvatarMessage'; export * from './RoomWidgetChatTypingMessage'; +export * from './RoomWidgetCreditFurniRedeemMessage'; export * from './RoomWidgetDanceMessage'; export * from './RoomWidgetDimmerChangeStateMessage'; export * from './RoomWidgetDimmerPreviewMessage'; diff --git a/src/views/room/RoomView.tsx b/src/views/room/RoomView.tsx index 9ac948a8..89b96ef0 100644 --- a/src/views/room/RoomView.tsx +++ b/src/views/room/RoomView.tsx @@ -1,6 +1,6 @@ import { EventDispatcher, NitroRectangle, RoomGeometry, RoomVariableEnum, Vector3d } from '@nitrots/nitro-renderer'; import { FC, useEffect, useRef, useState } from 'react'; -import { DispatchMouseEvent, DispatchTouchEvent, DoorbellWidgetHandler, FurniChooserWidgetHandler, FurnitureContextMenuWidgetHandler, FurnitureCustomStackHeightWidgetHandler, FurnitureDimmerWidgetHandler, FurnitureExternalImageWidgetHandler, FurniturePresentWidgetHandler, GetNitroInstance, GetRoomEngine, InitializeRoomInstanceRenderingCanvas, IRoomWidgetHandlerManager, RoomWidgetAvatarInfoHandler, RoomWidgetChatHandler, RoomWidgetChatInputHandler, RoomWidgetHandlerManager, RoomWidgetInfostandHandler, RoomWidgetRoomToolsHandler, RoomWidgetUpdateRoomViewEvent, UserChooserWidgetHandler } from '../../api'; +import { DispatchMouseEvent, DispatchTouchEvent, DoorbellWidgetHandler, FurniChooserWidgetHandler, FurnitureContextMenuWidgetHandler, FurnitureCreditWidgetHandler, FurnitureCustomStackHeightWidgetHandler, FurnitureDimmerWidgetHandler, FurnitureExternalImageWidgetHandler, FurniturePresentWidgetHandler, GetNitroInstance, GetRoomEngine, InitializeRoomInstanceRenderingCanvas, IRoomWidgetHandlerManager, RoomWidgetAvatarInfoHandler, RoomWidgetChatHandler, RoomWidgetChatInputHandler, RoomWidgetHandlerManager, RoomWidgetInfostandHandler, RoomWidgetRoomToolsHandler, RoomWidgetUpdateRoomViewEvent, UserChooserWidgetHandler } from '../../api'; import { RoomContextProvider } from './context/RoomContext'; import { RoomColorView } from './RoomColorView'; import { RoomViewProps } from './RoomView.types'; @@ -39,6 +39,7 @@ export const RoomView: FC = props => widgetHandlerManager.registerHandler(new FurniChooserWidgetHandler()); widgetHandlerManager.registerHandler(new FurnitureContextMenuWidgetHandler()); + widgetHandlerManager.registerHandler(new FurnitureCreditWidgetHandler()); widgetHandlerManager.registerHandler(new FurnitureCustomStackHeightWidgetHandler()); widgetHandlerManager.registerHandler(new FurnitureExternalImageWidgetHandler()); widgetHandlerManager.registerHandler(new FurniturePresentWidgetHandler()); diff --git a/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditData.ts b/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditData.ts deleted file mode 100644 index eeb128b3..00000000 --- a/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditData.ts +++ /dev/null @@ -1,7 +0,0 @@ -export class FurnitureExchangeCreditData -{ - constructor( - public objectId: number, - public category: number, - public value: number) {} -} diff --git a/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditView.tsx b/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditView.tsx index 43204da6..e8f6387b 100644 --- a/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditView.tsx +++ b/src/views/room/widgets/furniture/exchange-credit/FurnitureExchangeCreditView.tsx @@ -1,73 +1,51 @@ -import { FurnitureExchangeComposer, NitroEvent, RoomEngineTriggerWidgetEvent, RoomObjectVariable } from '@nitrots/nitro-renderer'; import { FC, useCallback, useState } from 'react'; -import { GetRoomEngine, GetRoomSession, IsOwnerOfFurniture, LocalizeText, RoomWidgetRoomObjectUpdateEvent } from '../../../../../api'; +import { LocalizeText, RoomWidgetCreditFurniRedeemMessage, RoomWidgetUpdateCreditFurniEvent } from '../../../../../api'; +import { BatchUpdates } from '../../../../../hooks'; import { CreateEventDispatcherHook } from '../../../../../hooks/events/event-dispatcher.base'; -import { useRoomEngineEvent } from '../../../../../hooks/events/nitro/room/room-engine-event'; import { NitroCardContentView, NitroCardHeaderView, NitroCardView } from '../../../../../layout'; import { useRoomContext } from '../../../context/RoomContext'; -import { FurnitureExchangeCreditData } from './FurnitureExchangeCreditData'; export const FurnitureExchangeCreditView: FC<{}> = props => { + const [ objectId, setObjectId ] = useState(-1); + const [ value, setValue ] = useState(0); const { eventDispatcher = null, widgetHandler = null } = useRoomContext(); - const [ exchangeCreditData, setExchangeCreditData ] = useState(null); - const onNitroEvent = useCallback((event: NitroEvent) => + const onRoomWidgetUpdateCreditFurniEvent = useCallback((event: RoomWidgetUpdateCreditFurniEvent) => { - switch(event.type) - { - case RoomEngineTriggerWidgetEvent.REQUEST_CREDITFURNI: { - const widgetEvent = (event as RoomEngineTriggerWidgetEvent); - - const roomObject = GetRoomEngine().getRoomObject(widgetEvent.roomId, widgetEvent.objectId, widgetEvent.category); - - if(!roomObject || !IsOwnerOfFurniture(roomObject)) return; - - const value = roomObject.model.getValue(RoomObjectVariable.FURNITURE_CREDIT_VALUE); - - setExchangeCreditData(new FurnitureExchangeCreditData(widgetEvent.objectId, widgetEvent.category, value)); - return; - } - case RoomWidgetRoomObjectUpdateEvent.FURNI_REMOVED: { - const widgetEvent = (event as RoomWidgetRoomObjectUpdateEvent); - - setExchangeCreditData(prevState => - { - if(!prevState || (widgetEvent.id !== prevState.objectId) || (widgetEvent.category !== prevState.category)) return prevState; - - return null; - }); - return; - } - } + setObjectId(event.objectId); + setValue(event.value); }, []); - useRoomEngineEvent(RoomEngineTriggerWidgetEvent.REQUEST_CREDITFURNI, onNitroEvent); - CreateEventDispatcherHook(RoomWidgetRoomObjectUpdateEvent.FURNI_REMOVED, eventDispatcher, onNitroEvent); + CreateEventDispatcherHook(RoomWidgetUpdateCreditFurniEvent.CREDIT_FURNI_UPDATE, eventDispatcher, onRoomWidgetUpdateCreditFurniEvent); const processAction = useCallback((type: string, value: string = null) => { switch(type) { case 'close': - setExchangeCreditData(null); + BatchUpdates(() => + { + setObjectId(-1); + setValue(0); + }); return; case 'redeem': - if(!exchangeCreditData) return null; - - GetRoomSession().connection.send(new FurnitureExchangeComposer(exchangeCreditData.objectId)); + widgetHandler.processWidgetMessage(new RoomWidgetCreditFurniRedeemMessage(RoomWidgetCreditFurniRedeemMessage.REDEEM, objectId)); + + processAction('close'); return; } - }, [exchangeCreditData]); + }, [ widgetHandler, objectId ]); - if(!exchangeCreditData) return null; + if(objectId === -1) return null; return ( processAction('close') } />
- { LocalizeText('widgets.furniture.credit.redeem.value', [ 'value' ], [ exchangeCreditData.value.toString() ]) } + { LocalizeText('widgets.furniture.credit.redeem.value', [ 'value' ], [ value.toString() ]) }