Skip to content

Commit

Permalink
Check for qualified name in sprite editor (#9789)
Browse files Browse the repository at this point in the history
* check for qualified name in sprite editor

* check qName for sprite when converting back to js
  • Loading branch information
kimprice committed Dec 8, 2023
1 parent 5b8cd3f commit cd44989
Show file tree
Hide file tree
Showing 2 changed files with 31 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
33 changes: 29 additions & 4 deletions pxtblocks/fields/field_sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,21 @@ namespace pxtblockly {

const bmp = text ? pxt.sprite.imageLiteralToBitmap(text) : new pxt.sprite.Bitmap(this.params.initWidth, this.params.initHeight);

let data: pxt.sprite.BitmapData;

if (!bmp) {
this.isGreyBlock = true;
this.valueText = text;
return undefined;
// check for qualified name
data = qNameToBitmapData(text);
if (!data) {
this.isGreyBlock = true;
this.valueText = text;
return undefined;
} else {
this.qName = text;
}
}

const data = bmp.data();
if (!data) data = bmp.data();

const newAsset: pxt.ProjectImage = {
internalID: -1,
Expand All @@ -71,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 @@ -148,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 cd44989

Please sign in to comment.