From fb58077841a118639a1c21557e6ff90588f07c37 Mon Sep 17 00:00:00 2001 From: Kim Ying <15070078+kimprice@users.noreply.github.com> Date: Thu, 7 Dec 2023 13:41:24 -0800 Subject: [PATCH] Check for qualified name in sprite editor (#9789) * check for qualified name in sprite editor * check qName for sprite when converting back to js --- pxtblocks/fields/field_asset.ts | 2 ++ pxtblocks/fields/field_sprite.ts | 33 ++++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/pxtblocks/fields/field_asset.ts b/pxtblocks/fields/field_asset.ts index ad04772c0578..dc8d7dbcf4be 100644 --- a/pxtblocks/fields/field_asset.ts +++ b/pxtblocks/fields/field_asset.ts @@ -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; diff --git a/pxtblocks/fields/field_sprite.ts b/pxtblocks/fields/field_sprite.ts index b063d1b93286..8a3c619e8bfd 100644 --- a/pxtblocks/fields/field_sprite.ts +++ b/pxtblocks/fields/field_sprite.ts @@ -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, @@ -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); } @@ -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; } }