nitro-react/src/views/shared/furni-image/FurniImageView.tsx

60 lines
2.0 KiB
TypeScript
Raw Normal View History

import { IGetImageListener, ImageResult, TextureUtils, Vector3d } from '@nitrots/nitro-renderer';
2021-05-11 21:08:18 +02:00
import { FC, useCallback, useEffect, useState } from 'react';
2021-06-23 05:59:02 +02:00
import { GetRoomEngine } from '../../../api';
2021-06-23 06:03:56 +02:00
import { ProductTypeEnum } from '../../catalog/common/ProductTypeEnum';
2021-05-11 21:08:18 +02:00
import { FurniImageViewProps } from './FurniImageView.types';
export const FurniImageView: FC<FurniImageViewProps> = props =>
{
const { type = 's', spriteId = -1, direction = 0, extras = '', scale = 1 } = props;
const [ imageElement, setImageElement ] = useState<HTMLImageElement>(null);
const buildImage = useCallback(() =>
{
let imageResult: ImageResult = null;
const furniType = type.toLocaleLowerCase();
const listener: IGetImageListener = {
2021-07-16 19:17:52 +02:00
imageReady: (id, texture, image) =>
2021-05-11 21:08:18 +02:00
{
if(!image && texture)
{
image = TextureUtils.generateImage(texture);
}
image.onload = () => setImageElement(image);
},
imageFailed: null
};
switch(furniType)
{
case ProductTypeEnum.FLOOR:
imageResult = GetRoomEngine().getFurnitureFloorImage(spriteId, new Vector3d(direction), 64, listener, 0, extras);
break;
case ProductTypeEnum.WALL:
imageResult = GetRoomEngine().getFurnitureWallImage(spriteId, new Vector3d(direction), 64, listener, 0, extras);
break;
}
if(imageResult)
{
const image = imageResult.getImage();
2021-09-12 05:16:09 +02:00
2021-05-11 21:08:18 +02:00
image.onload = () => setImageElement(image);
}
}, [ type, spriteId, direction, extras ]);
useEffect(() =>
{
buildImage();
}, [ buildImage ]);
if(!imageElement) return null;
const imageUrl = `url('${ imageElement.src }')`;
return <div className={ 'furni-image scale-' + scale } style={ { backgroundImage: imageUrl, width: imageElement.width, height: imageElement.height } }></div>;
}