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

99 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-08-03 05:50:15 +02:00
import { Texture } from '@pixi/core';
import { ColorMatrix, ColorMatrixFilter } from '@pixi/filter-color-matrix';
2023-07-19 02:28:28 +02:00
import { IRoomCameraWidgetEffect, IRoomCameraWidgetManager, IRoomCameraWidgetSelectedEffect, NitroConfiguration } from '../../api';
import { NitroEventDispatcher, RoomCameraWidgetManagerEvent } from '../../events';
2022-10-30 05:02:19 +01:00
import { NitroContainer, NitroSprite, TextureUtils } from '../../pixi-proxy';
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
}
public init(): void
{
2022-11-08 22:50:16 +01:00
if(this._isLoaded) return;
2021-06-15 22:52:08 +02:00
this._isLoaded = true;
2022-11-08 22:44:41 +01:00
const imagesUrl = NitroConfiguration.getValue<string>('image.library.url') + 'Habbo-Stories/';
const effects = NitroConfiguration.getValue<{ name: string, colorMatrix?: ColorMatrix, minLevel: number, blendMode?: number, 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
{
2022-03-03 01:21:30 +01:00
cameraEffect.texture = Texture.from(imagesUrl + effect.name + '.png');
cameraEffect.blendMode = effect.blendMode;
2021-06-15 22:52:08 +02:00
}
this._effects.set(cameraEffect.name, cameraEffect);
}
2023-07-19 02:28:28 +02:00
NitroEventDispatcher.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
{
2021-08-03 05:50:15 +02:00
const container = new NitroContainer();
2022-03-03 01:21:30 +01:00
const sprite = new NitroSprite(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
2022-11-08 22:50:16 +01:00
if(!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
{
2022-03-03 01:21:30 +01:00
const effectSprite = new NitroSprite(effect.texture);
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;
}
}