Skip to content

Commit

Permalink
check qName for sprite when converting back to js
Browse files Browse the repository at this point in the history
  • Loading branch information
kimprice committed Dec 6, 2023
1 parent 2a8490a commit c9ad930
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions pxtblocks/fields/field_asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace pxtblockly {
protected pendingEdit = false;
protected isEmpty = false;

protected qName?: string;

// If input is invalid, the subclass can set this to be true. The field will instead
// render as a grey block and preserve the decompiled code
public isGreyBlock: boolean;
Expand Down
24 changes: 20 additions & 4 deletions pxtblocks/fields/field_sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@ namespace pxtblockly {

if (!bmp) {
// check for qualified name
const images = project.getGalleryAssets(pxt.AssetType.Image).filter(asset => asset.id === text);
const img = images.length && images[0];
if (!img) {
data = qNameToBitmapData(text);
if (!data) {
this.isGreyBlock = true;
this.valueText = text;
return undefined;
} else {
data = img.bitmap;
this.qName = text;
}
}

Expand All @@ -80,6 +79,12 @@ namespace pxtblockly {
protected getValueText(): string {
if (this.asset && !this.isTemporaryAsset()) {
return pxt.getTSReferenceForAsset(this.asset);
} else if (this.qName) {
// check if image has been edited
const data = qNameToBitmapData(this.qName);
if (data && pxt.sprite.bitmapEquals(data, (this.asset as pxt.ProjectImage).bitmap)) {
return this.qName;
}
}
return pxt.sprite.bitmapToImageLiteral(this.asset && pxt.sprite.Bitmap.fromData((this.asset as pxt.ProjectImage).bitmap), pxt.editor.FileType.TypeScript);
}
Expand Down Expand Up @@ -157,5 +162,16 @@ namespace pxtblockly {
}
return res;
}

}

function qNameToBitmapData(qName: string): pxt.sprite.BitmapData {
const project = pxt.react.getTilemapProject();
const images = project.getGalleryAssets(pxt.AssetType.Image).filter(asset => asset.id === qName);
const img = images.length && images[0];
if (img) {
return img.bitmap;
}
return undefined;
}
}

0 comments on commit c9ad930

Please sign in to comment.