nitro-react/src/hooks/rooms/widgets/useFurniChooserWidget.ts

133 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-04-17 20:40:11 -04:00
import { RoomObjectCategory, RoomObjectVariable } from '@nitrots/nitro-renderer';
2022-07-18 20:48:54 -04:00
import { useState } from 'react';
import { GetRoomEngine, GetRoomSession, GetSessionDataManager, LocalizeText, RoomObjectItem } from '../../../api';
import { useFurniAddedEvent, useFurniRemovedEvent } from '../engine';
import { useRoom } from '../useRoom';
2022-04-17 20:40:11 -04:00
const useFurniChooserWidgetState = () =>
{
const [ items, setItems ] = useState<RoomObjectItem[]>(null);
2022-07-18 20:48:54 -04:00
const { roomSession = null } = useRoom();
2022-04-17 20:40:11 -04:00
2022-07-27 19:08:53 -04:00
const onClose = () => setItems(null);
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
const selectItem = (item: RoomObjectItem) => item && GetRoomEngine().selectRoomObject(GetRoomSession().roomId, item.id, item.category);
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
const populateChooser = () =>
2022-04-17 20:40:11 -04:00
{
const sessionDataManager = GetSessionDataManager();
const wallObjects = GetRoomEngine().getRoomObjects(roomSession.roomId, RoomObjectCategory.WALL);
const floorObjects = GetRoomEngine().getRoomObjects(roomSession.roomId, RoomObjectCategory.FLOOR);
const wallItems = wallObjects.map(roomObject =>
{
if(roomObject.id < 0) return null;
let name = roomObject.type;
if(name.startsWith('poster'))
{
name = LocalizeText(`poster_${ name.replace('poster', '') }_name`);
}
else
{
const typeId = roomObject.model.getValue<number>(RoomObjectVariable.FURNITURE_TYPE_ID);
const furniData = sessionDataManager.getWallItemData(typeId);
if(furniData && furniData.name.length) name = furniData.name;
}
return new RoomObjectItem(roomObject.id, RoomObjectCategory.WALL, name);
});
const floorItems = floorObjects.map(roomObject =>
{
if(roomObject.id < 0) return null;
let name = roomObject.type;
const typeId = roomObject.model.getValue<number>(RoomObjectVariable.FURNITURE_TYPE_ID);
const furniData = sessionDataManager.getFloorItemData(typeId);
if(furniData && furniData.name.length) name = furniData.name;
return new RoomObjectItem(roomObject.id, RoomObjectCategory.FLOOR, name);
});
setItems([ ...wallItems, ...floorItems ].sort((a, b) => ((a.name < b.name) ? -1 : 1)));
2022-07-18 20:48:54 -04:00
}
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
useFurniAddedEvent(!!items, event =>
2022-04-17 20:40:11 -04:00
{
2022-07-18 20:48:54 -04:00
if(event.id < 0) return;
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
const roomObject = GetRoomEngine().getRoomObject(GetRoomSession().roomId, event.id, event.category);
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
if(!roomObject) return;
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
let item: RoomObjectItem = null;
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
switch(event.category)
{
case RoomObjectCategory.WALL: {
let name = roomObject.type;
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
if(name.startsWith('poster'))
{
name = LocalizeText(`poster_${ name.replace('poster', '') }_name`);
2022-04-17 20:40:11 -04:00
}
2022-07-18 20:48:54 -04:00
else
{
2022-04-17 20:40:11 -04:00
const typeId = roomObject.model.getValue<number>(RoomObjectVariable.FURNITURE_TYPE_ID);
2022-07-18 20:48:54 -04:00
const furniData = GetSessionDataManager().getWallItemData(typeId);
2022-04-17 20:40:11 -04:00
if(furniData && furniData.name.length) name = furniData.name;
}
2022-07-18 20:48:54 -04:00
item = new RoomObjectItem(roomObject.id, RoomObjectCategory.WALL, name);
break;
2022-04-17 20:40:11 -04:00
}
2022-07-18 20:48:54 -04:00
case RoomObjectCategory.FLOOR: {
let name = roomObject.type;
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
const typeId = roomObject.model.getValue<number>(RoomObjectVariable.FURNITURE_TYPE_ID);
const furniData = GetSessionDataManager().getFloorItemData(typeId);
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
if(furniData && furniData.name.length) name = furniData.name;
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
item = new RoomObjectItem(roomObject.id, RoomObjectCategory.FLOOR, name);
}
2022-04-17 20:40:11 -04:00
}
2022-07-18 20:48:54 -04:00
setItems(prevValue => [ ...prevValue, item ].sort((a, b) => ((a.name < b.name) ? -1 : 1)));
});
useFurniRemovedEvent(!!items, event =>
2022-04-17 20:40:11 -04:00
{
2022-07-18 20:48:54 -04:00
if(event.id < 0) return;
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
setItems(prevValue =>
2022-04-17 20:40:11 -04:00
{
2022-07-18 20:48:54 -04:00
const newValue = [ ...prevValue ];
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
for(let i = 0; i < newValue.length; i++)
2022-04-17 20:40:11 -04:00
{
2022-07-18 20:48:54 -04:00
const existingValue = newValue[i];
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
if((existingValue.id !== event.id) || (existingValue.category !== event.category)) continue;
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
newValue.splice(i, 1);
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
break;
}
2022-04-17 20:40:11 -04:00
2022-07-18 20:48:54 -04:00
return newValue;
});
});
2022-04-17 20:40:11 -04:00
2022-07-27 19:08:53 -04:00
return { items, onClose, selectItem, populateChooser };
2022-04-17 20:40:11 -04:00
}
export const useFurniChooserWidget = useFurniChooserWidgetState;