Update group badge creator

This commit is contained in:
Bill 2022-03-02 19:04:34 -05:00
parent 43cc4d7222
commit 09915dc80a
3 changed files with 50 additions and 87 deletions

View File

@ -1,4 +1,5 @@
import { Resource, Texture } from '@pixi/core';
import { NitroContainer, NitroTexture } from '../../..';
import { IAssetManager } from '../../../core/asset/IAssetManager';
import { IMessageEvent } from '../../../core/communication/messages/IMessageEvent';
import { NitroSprite } from '../../../core/utils/proxy/NitroSprite';
@ -100,41 +101,38 @@ export class BadgeImageManager implements IDisposable
{
const url = this.getBadgeUrl(badgeName, type);
if(!url || !url.length) return null;
const existing = this._assets.getTexture(url);
if(existing) return existing.clone();
if(this._requestedBadges.get(badgeName)) return null;
if(url)
if(type === BadgeImageManager.NORMAL_BADGE)
{
if(this._requestedBadges.get(badgeName)) return null;
this._requestedBadges.set(badgeName, true);
if(type === BadgeImageManager.NORMAL_BADGE)
this._assets.downloadAsset(url, (flag: boolean) =>
{
this._assets.downloadAsset(url, (flag: boolean) =>
if(flag)
{
if(flag)
{
this._requestedBadges.delete(badgeName);
this._requestedBadges.delete(badgeName);
const texture = this._assets.getTexture(url);
const texture = this._assets.getTexture(url);
if(texture && this._sessionDataManager) this._sessionDataManager.events.dispatchEvent(new BadgeImageReadyEvent(badgeName, texture.clone()));
}
});
}
else
{
if(!this._readyToGenerateGroupBadges)
{
if(!this._groupBadgesQueue.get(badgeName)) this._groupBadgesQueue.set(badgeName, true);
if(texture && this._sessionDataManager) this._sessionDataManager.events.dispatchEvent(new BadgeImageReadyEvent(badgeName, texture.clone()));
}
else
{
this.loadGroupBadge(badgeName);
}
}
});
}
else if(type === BadgeImageManager.GROUP_BADGE)
{
if(this._groupBadgesQueue.get(badgeName)) return;
this._groupBadgesQueue.set(badgeName, true);
if(this._readyToGenerateGroupBadges) this.loadGroupBadge(badgeName);
}
return null;
@ -170,80 +168,65 @@ export class BadgeImageManager implements IDisposable
private loadGroupBadge(badgeCode: string): void
{
const groupBadge = new GroupBadge(badgeCode);
const imagePath = Nitro.instance.getConfiguration<string>('badge.asset.grouparts.url');
const urlsToLoad: string[] = [];
const partMatches = [...badgeCode.matchAll(/[b|s][0-9]{5,6}/g)];
for(const partMatch of partMatches)
{
const partCode = partMatch[0];
const shortMethod = (partCode.length === 6);
const partType = partCode[0];
const partId = parseInt(partCode.slice(1, shortMethod ? 3 : 4));
const partColor = parseInt(partCode.slice(shortMethod ? 3 : 4, shortMethod ? 5 : 6));
const partPosition = parseInt(partCode.slice(shortMethod ? 5 : 6, shortMethod ? 6 : 7));
const part = new GroupBadgePart(partType, partId, partColor, partPosition);
groupBadge.parts.push(part);
const isBase = (partType === 'b');
const requiredAssets = isBase ? this._groupBases.get(partId) : this._groupSymbols.get(partId);
if(!requiredAssets) continue;
for(const requiredAsset of requiredAssets)
{
if(requiredAsset.length > 0)
{
const url = imagePath.replace('%part%', requiredAsset);
part.urls.push(url);
if(!this._assets.getAsset(requiredAsset)) urlsToLoad.push(url);
}
}
}
if(urlsToLoad.length === 0) return this.renderGroupBadge(groupBadge);
this._assets.downloadAssets(urlsToLoad, (flag: boolean) =>
{
this.renderGroupBadge(groupBadge);
});
this.renderGroupBadge(groupBadge);
}
private renderGroupBadge(groupBadge: GroupBadge): void
{
const container = new NitroContainer();
const tempSprite = new NitroSprite(NitroTexture.EMPTY);
tempSprite.width = GroupBadgePart.IMAGE_WIDTH;
tempSprite.height = GroupBadgePart.IMAGE_HEIGHT;
container.addChild(tempSprite);
for(const part of groupBadge.parts)
{
let isFirst = true;
for(const partUrl of part.urls)
const partNames = ((part.type === 'b') ? this._groupBases.get(part.key) : this._groupSymbols.get(part.key));
for(const partName of partNames)
{
const texture = this._assets.getTexture(partUrl);
if(!partName || !partName.length) continue;
if(!texture) continue; //Generate with what we got
const texture = this._assets.getTexture(`badgepart_${ partName }`);
const pos = part.calculatePosition(texture);
if(!texture) continue;
const { x, y } = part.calculatePosition(texture);
const sprite = new NitroSprite(texture);
sprite.x = pos.x;
sprite.y = pos.y;
sprite.position.set(x, y);
if(isFirst) sprite.tint = parseInt(this._groupPartColors.get(part.color), 16);
isFirst = false;
groupBadge.container.addChild(sprite);
container.addChild(sprite);
}
}
this._requestedBadges.delete(groupBadge.code);
this._groupBadgesQueue.delete(groupBadge.code);
const texture = TextureUtils.generateTexture(groupBadge.container);
const texture = TextureUtils.generateTexture(container);
this._assets.setTexture(groupBadge.code, texture);
if(this._sessionDataManager) this._sessionDataManager.events.dispatchEvent(new BadgeImageReadyEvent(groupBadge.code, texture));
@ -257,23 +240,14 @@ export class BadgeImageManager implements IDisposable
if(!data) return;
data.bases.forEach( (names, id) =>
{
this._groupBases.set(id, names.map( val => val.replace('.png', '').replace('.gif', '')));
});
data.bases.forEach((names, id) => this._groupBases.set(id, names.map( val => val.replace('.png', '').replace('.gif', ''))));
data.symbols.forEach( (names, id) =>
{
this._groupSymbols.set(id, names.map( val => val.replace('.png', '').replace('.gif', '')));
});
data.symbols.forEach( (names, id) => this._groupSymbols.set(id, names.map( val => val.replace('.png', '').replace('.gif', ''))));
this._groupPartColors = data.partColors;
this._readyToGenerateGroupBadges = true;
this._groupBadgesQueue.forEach((_, badgeCode) =>
{
this.loadGroupBadge(badgeCode);
});
for(const badgeCode of this._groupBadgesQueue.keys()) this.loadGroupBadge(badgeCode);
}
public get disposed(): boolean

View File

@ -1,17 +1,14 @@
import { NitroContainer } from '../../..';
import { GroupBadgePart } from './GroupBadgePart';
export class GroupBadge
{
private _code: string;
private _parts: GroupBadgePart[];
private _container: NitroContainer;
constructor(code: string)
{
this._code = code;
this._parts = [];
this._container = new NitroContainer();
}
public get code(): string
@ -23,9 +20,4 @@ export class GroupBadge
{
return this._parts;
}
public get container(): NitroContainer
{
return this._container;
}
}

View File

@ -5,7 +5,6 @@ export class GroupBadgePart
public static BASE: string = 'b';
public static SYMBOL: string = 's';
public static SYMBOL_ALT: string = 't';
public static BASE_PART: number = 0;
public static LAYER_PART: number = 1;
public static IMAGE_WIDTH: number = 39;
@ -17,15 +16,13 @@ export class GroupBadgePart
public key: number;
public color: number;
public position: number;
public urls: string[];
constructor(type: string, key?: number, color?: number, position?: number)
constructor(type: string, key: number = 0, color: number = 0, position: number = 0)
{
this.type = type;
this.key = key ? key : 0;
this.color = color ? color : 0;
this.position = position ? position : 4;
this.urls = [];
this.key = key;
this.color = color;
this.position = position;
}
public get code(): string