Skip to content

Commit

Permalink
add tags field so .g.jres can have gallery tags (#9411)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwunderl authored Mar 9, 2023
1 parent c2bb4dd commit c71da46
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions localtypings/pxtpackage.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ declare namespace pxt {
displayName?: string;
tilemapTile?: boolean;
tileset?: string[];
tags?: string[];
}

type SnippetOutputType = 'blocks'
Expand Down
3 changes: 2 additions & 1 deletion pxtlib/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,8 @@ namespace pxt {
mimeType,
tilemapTile: v.tilemapTile,
displayName: v.displayName,
tileset: v.tileset
tileset: v.tileset,
tags: v.tags
}
}

Expand Down
42 changes: 31 additions & 11 deletions pxtlib/tilemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,8 @@ namespace pxt {
type: type,
id: entry.id,
meta: {
displayName: entry.displayName
displayName: entry.displayName,
tags: entry.tags,
},
jresData: entry.data,
bitmap: pxt.sprite.getBitmapFromJResURL(`data:${IMAGE_MIME_TYPE};base64,${entry.data}`).data()
Expand All @@ -1179,7 +1180,8 @@ namespace pxt {
type: AssetType.Song,
id: entry.id,
meta: {
displayName: entry.displayName
displayName: entry.displayName,
tags: entry.tags
},
song: pxt.assets.music.decodeSongFromHex(entry.data)
}
Expand All @@ -1200,7 +1202,8 @@ namespace pxt {
internalID: this.getNewInternalId(),
type: AssetType.Animation,
meta: {
displayName: entry.displayName
displayName: entry.displayName,
tags: entry.tags
},
id: entry.id,
frames: [],
Expand Down Expand Up @@ -1509,25 +1512,33 @@ namespace pxt {
function addAssetToJRes(asset: Asset, allJRes: pxt.Map<Partial<JRes> | string>): void {
// Get the last part of the fully qualified name
const id = asset.id.substr(asset.id.lastIndexOf(".") + 1);
const tags = asset.meta.tags;

switch (asset.type) {
case AssetType.Image:
allJRes[id] = asset.jresData;
if (asset.meta.displayName) {
allJRes[id] = {
if (asset.meta.displayName || tags?.length) {
const imgJres: Partial<JRes> = {
data: asset.jresData,
mimeType: IMAGE_MIME_TYPE,
displayName: asset.meta.displayName
}
if (asset.meta.displayName)
imgJres.displayName = asset.meta.displayName;
if (tags?.length)
imgJres.tags = tags;
allJRes[id] = imgJres;
}
break;
case AssetType.Tile:
allJRes[id] = {
const tileJres: Partial<JRes> = {
data: asset.jresData,
mimeType: IMAGE_MIME_TYPE,
tilemapTile: true,
displayName: asset.meta.displayName
};
if (tags?.length)
tileJres.tags = tags;
allJRes[id] = tileJres;
break;
case AssetType.Tilemap:
// we include the full ID for tilemaps
Expand All @@ -1539,13 +1550,18 @@ namespace pxt {
allJRes[id] = serializeAnimation(asset);
break;
case AssetType.Song:
allJRes[id] = {
const songJres: Partial<JRes> = {
data: pxt.assets.music.encodeSongToHex(asset.song),
mimeType: SONG_MIME_TYPE,
displayName: asset.meta.displayName
};
if (tags?.length)
songJres.tags = tags;

allJRes[id] = songJres;
break;
}

}

export function assetEquals(a: Asset, b: Asset) {
Expand Down Expand Up @@ -1728,13 +1744,16 @@ namespace pxt {
}

function serializeAnimation(asset: Animation): JRes {
return {
const animationJres: JRes = {
namespace: asset.id.substr(0, asset.id.lastIndexOf(".")),
id: asset.id.substr(asset.id.lastIndexOf(".") + 1),
mimeType: ANIMATION_MIME_TYPE,
data: pxt.sprite.encodeAnimationString(asset.frames, asset.interval),
displayName: asset.meta.displayName
displayName: asset.meta.displayName,
}
if (asset.meta.tags?.length)
animationJres.tags = asset.meta.tags;
return animationJres;
}

function decodeAnimation(jres: JRes): Animation {
Expand Down Expand Up @@ -1778,7 +1797,8 @@ namespace pxt {
interval,
frames: decodedFrames,
meta: {
displayName: jres.displayName
displayName: jres.displayName,
tags: jres.tags
}
}
}
Expand Down

0 comments on commit c71da46

Please sign in to comment.