nitro-renderer/packages/camera/src/RoomCameraWidgetManager.ts

104 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-03-20 02:53:17 +01:00
import { IRoomCameraWidgetEffect, IRoomCameraWidgetManager, IRoomCameraWidgetSelectedEffect } from '@nitrots/api';
2024-03-31 04:53:11 +02:00
import { GetAssetManager } from '@nitrots/assets';
2024-03-20 02:53:17 +01:00
import { GetConfiguration } from '@nitrots/configuration';
import { GetEventDispatcher, RoomCameraWidgetManagerEvent } from '@nitrots/events';
import { TextureUtils } from '@nitrots/utils';
import { BLEND_MODES, ColorMatrix, ColorMatrixFilter, Container, Sprite, Texture } from 'pixi.js';
2021-06-15 22:52:08 +02:00
import { RoomCameraWidgetEffect } from './RoomCameraWidgetEffect';
export class RoomCameraWidgetManager implements IRoomCameraWidgetManager
{
private _effects: Map<string, IRoomCameraWidgetEffect>;
private _isLoaded: boolean;
constructor()
{
2022-03-03 01:21:30 +01:00
this._effects = new Map();
this._isLoaded = false;
2021-06-15 22:52:08 +02:00
}
2024-03-31 04:53:11 +02:00
public async init(): Promise<void>
2021-06-15 22:52:08 +02:00
{
2022-11-08 22:50:16 +01:00
if(this._isLoaded) return;
2021-06-15 22:52:08 +02:00
this._isLoaded = true;
2024-03-20 02:53:17 +01:00
const imagesUrl = GetConfiguration().getValue<string>('image.library.url') + 'Habbo-Stories/';
const effects = GetConfiguration().getValue<{ name: string, colorMatrix?: ColorMatrix, minLevel: number, blendMode?: BLEND_MODES, enabled: boolean }[]>('camera.available.effects');
2021-06-15 22:52:08 +02:00
2022-11-08 22:50:16 +01:00
for(const effect of effects)
2021-06-15 22:52:08 +02:00
{
2022-11-08 22:50:16 +01:00
if(!effect.enabled) continue;
2021-06-15 22:52:08 +02:00
const cameraEffect = new RoomCameraWidgetEffect(effect.name, effect.minLevel);
2022-11-08 22:50:16 +01:00
if(effect.colorMatrix.length)
2021-06-15 22:52:08 +02:00
{
cameraEffect.colorMatrix = effect.colorMatrix;
}
else
{
2024-03-31 04:53:11 +02:00
const url = `${ imagesUrl }${ effect.name }.png`;
await GetAssetManager().downloadAsset(url);
cameraEffect.texture = GetAssetManager().getTexture(url);
2022-03-03 01:21:30 +01:00
cameraEffect.blendMode = effect.blendMode;
2021-06-15 22:52:08 +02:00
}
this._effects.set(cameraEffect.name, cameraEffect);
}
2024-03-20 02:53:17 +01:00
GetEventDispatcher().dispatchEvent(new RoomCameraWidgetManagerEvent(RoomCameraWidgetManagerEvent.INITIALIZED));
2021-06-15 22:52:08 +02:00
}
2023-07-12 03:55:42 +02:00
public async applyEffects(texture: Texture, selectedEffects: IRoomCameraWidgetSelectedEffect[], isZoomed: boolean): Promise<HTMLImageElement>
2021-06-15 22:52:08 +02:00
{
2023-07-23 22:10:37 +02:00
const container = new Container();
2024-03-20 02:53:17 +01:00
const sprite = new Sprite(texture);
2021-06-15 22:52:08 +02:00
container.addChild(sprite);
2022-11-08 22:50:16 +01:00
if(isZoomed) sprite.scale.set(2);
2022-10-30 02:32:38 +01:00
2022-11-08 22:50:16 +01:00
for(const selectedEffect of selectedEffects)
2021-06-15 22:52:08 +02:00
{
const effect = selectedEffect.effect;
2022-11-08 22:50:16 +01:00
if(!effect) continue;
2021-06-15 22:52:08 +02:00
2022-11-08 22:50:16 +01:00
if(effect.colorMatrix)
2021-06-15 22:52:08 +02:00
{
2021-08-03 05:50:15 +02:00
const filter = new ColorMatrixFilter();
2021-06-15 22:52:08 +02:00
2022-03-03 01:21:30 +01:00
filter.matrix = effect.colorMatrix;
filter.alpha = selectedEffect.alpha;
2021-06-15 22:52:08 +02:00
2024-03-20 02:53:17 +01:00
if(!Array.isArray(sprite.filters)) sprite.filters = [];
2021-06-15 22:55:22 +02:00
sprite.filters.push(filter);
2021-06-15 22:52:08 +02:00
}
else
{
2024-03-20 02:53:17 +01:00
const effectSprite = new Sprite(effect.texture);
2022-03-03 01:21:30 +01:00
effectSprite.alpha = selectedEffect.alpha;
effectSprite.blendMode = effect.blendMode;
2021-06-15 22:52:08 +02:00
container.addChild(effectSprite);
}
}
2023-07-12 03:55:42 +02:00
return await TextureUtils.generateImage(container);
2021-06-15 22:52:08 +02:00
}
public get effects(): Map<string, IRoomCameraWidgetEffect>
{
return this._effects;
}
public get isLoaded(): boolean
{
return this._isLoaded;
}
}