Skip to content

Commit

Permalink
Refactor SpriteRepository for updateImage()
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeLonewolf committed Oct 31, 2023
1 parent b840832 commit 363af8c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion shieldlib/src/shield.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export function storeNoShield(
export function missingIconLoader(
renderContext: ShieldRenderingContext,
routeDef: RouteDefinition,
spriteID: string
spriteID: string,
update?: boolean
): void;

export function romanizeRef(ref: string): string;
14 changes: 11 additions & 3 deletions shieldlib/src/shield_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,17 @@ class MaplibreGLSpriteRepository implements SpriteRepository {
getSprite(spriteID: string): StyleImage {
return this.map.style.getImage(spriteID);
}
putSprite(spriteID: string, image: ImageData, pixelRatio: number): void {
this.map.addImage(spriteID, image, { pixelRatio: pixelRatio });
putSprite(
spriteID: string,
image: ImageData,
pixelRatio: number,
update?: boolean
): void {
if (update) {
this.map.updateImage(spriteID, image);
} else {
this.map.addImage(spriteID, image, { pixelRatio: pixelRatio });
}
}
}

Expand Down Expand Up @@ -143,7 +152,6 @@ export class AbstractShieldRenderer {

// Loop through each previously-loaded shield and re-render it
for (let [id, routeDef] of this._preFontImageCache.entries()) {
map.removeImage(id);
missingIconLoader(this._renderContext, routeDef, id);
console.log(`Updated ${id} post font-load`); // Example action
}
Expand Down
7 changes: 6 additions & 1 deletion shieldlib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ export interface SpriteProducer {

/** Store a sprite graphic based on an ID */
export interface SpriteConsumer {
putSprite(spriteID: string, image: ImageData, pixelRatio: number): void;
putSprite(
spriteID: string,
image: ImageData,
pixelRatio: number,
update?: boolean
): void;
}

/** Respository that can store and retrieve sprite graphics */
Expand Down

0 comments on commit 363af8c

Please sign in to comment.