Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable8.5] Cherry pick: Check for qualified name in sprite editor (#9789) #9792

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading