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

Fixes for image editor in micro:bit #9900

Merged
merged 2 commits into from
Mar 4, 2024
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
14 changes: 10 additions & 4 deletions pxtblocks/fields/field_animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface FieldAnimationOptions {

filter?: string;
lightMode: boolean;

taggedTemplate?: string;
}

export interface ParsedFieldAnimationOptions {
Expand All @@ -21,6 +23,8 @@ export interface ParsedFieldAnimationOptions {
disableResize: boolean;
filter?: string;
lightMode: boolean;

taggedTemplate?: string;
}

// 32 is specifically chosen so that we can scale the images for the default
Expand Down Expand Up @@ -73,7 +77,7 @@ export class FieldAnimationEditor extends FieldAssetEditor<FieldAnimationOptions
const existing = pxt.lookupProjectAssetByTSReference(text, project);
if (existing) return existing;

const frames = parseImageArrayString(text);
const frames = parseImageArrayString(text, this.params.taggedTemplate);

if (frames && frames.length) {
const id = this.sourceBlock_.id;
Expand Down Expand Up @@ -117,7 +121,7 @@ export class FieldAnimationEditor extends FieldAssetEditor<FieldAnimationOptions

if (this.isTemporaryAsset()) {
return "[" + this.asset.frames.map(frame =>
pxt.sprite.bitmapToImageLiteral(pxt.sprite.Bitmap.fromData(frame), pxt.editor.FileType.TypeScript)
pxt.sprite.bitmapToImageLiteral(pxt.sprite.Bitmap.fromData(frame), pxt.editor.FileType.TypeScript, this.params.taggedTemplate)
).join(",") + "]"
}

Expand Down Expand Up @@ -239,6 +243,8 @@ function parseFieldOptions(opts: FieldAnimationOptions) {
parsed.initWidth = withDefault(opts.initWidth, parsed.initWidth);
parsed.initHeight = withDefault(opts.initHeight, parsed.initHeight);

parsed.taggedTemplate = opts.taggedTemplate;

return parsed;

function withDefault(raw: string, def: number) {
Expand All @@ -250,10 +256,10 @@ function parseFieldOptions(opts: FieldAnimationOptions) {
}
}

function parseImageArrayString(str: string): pxt.sprite.BitmapData[] {
function parseImageArrayString(str: string, templateLiteral?: string): pxt.sprite.BitmapData[] {
if (str.indexOf("[") === -1) return null;
str = str.replace(/[\[\]]/mg, "");
return str.split(",").map(s => pxt.sprite.imageLiteralToBitmap(s).data()).filter(b => b.height && b.width);
return str.split(",").map(s => pxt.sprite.imageLiteralToBitmap(s, templateLiteral).data()).filter(b => b.height && b.width);
}

function isNumberType(type: string) {
Expand Down
10 changes: 8 additions & 2 deletions pxtblocks/fields/field_sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface FieldSpriteEditorOptions {

filter?: string;
lightMode: boolean;

taggedTemplate?: string;
}

interface ParsedSpriteEditorOptions {
Expand All @@ -27,6 +29,8 @@ interface ParsedSpriteEditorOptions {
disableResize: boolean;
filter?: string;
lightMode: boolean;

taggedTemplate?: string;
}

export class FieldSpriteEditor extends FieldAssetEditor<FieldSpriteEditorOptions, ParsedSpriteEditorOptions> {
Expand All @@ -46,7 +50,7 @@ export class FieldSpriteEditor extends FieldAssetEditor<FieldSpriteEditorOptions
return project.lookupAsset(pxt.AssetType.Image, this.getBlockData());
}

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

let data: pxt.sprite.BitmapData;

Expand Down Expand Up @@ -88,7 +92,7 @@ export class FieldSpriteEditor extends FieldAssetEditor<FieldSpriteEditorOptions
return this.qName;
}
}
return pxt.sprite.bitmapToImageLiteral(this.asset && pxt.sprite.Bitmap.fromData((this.asset as pxt.ProjectImage).bitmap), pxt.editor.FileType.TypeScript);
return pxt.sprite.bitmapToImageLiteral(this.asset && pxt.sprite.Bitmap.fromData((this.asset as pxt.ProjectImage).bitmap), pxt.editor.FileType.TypeScript, this.params.taggedTemplate);
}

protected parseFieldOptions(opts: FieldSpriteEditorOptions): ParsedSpriteEditorOptions {
Expand Down Expand Up @@ -155,6 +159,8 @@ function parseFieldOptions(opts: FieldSpriteEditorOptions) {
parsed.initWidth = withDefault(opts.initWidth, parsed.initWidth);
parsed.initHeight = withDefault(opts.initHeight, parsed.initHeight);

parsed.taggedTemplate = opts.taggedTemplate;

return parsed;

function withDefault(raw: string, def: number) {
Expand Down
6 changes: 5 additions & 1 deletion pxtlib/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ namespace pxt {
return r;
}

protected patchAppTargetPalette() {
patchAppTargetPalette() {
if (this.config.palette && appTarget.runtime) {
appTarget.runtime.palette = U.clone(this.config.palette);
if (this.config.paletteNames) appTarget.runtime.paletteNames = this.config.paletteNames;
Expand Down Expand Up @@ -1294,6 +1294,10 @@ namespace pxt {
const functionOpts = pxt.appTarget.runtime && pxt.appTarget.runtime.functionsOptions;
opts.allowedArgumentTypes = functionOpts && functionOpts.extraFunctionEditorTypes && functionOpts.extraFunctionEditorTypes.map(info => info.typeName).concat("number", "boolean", "string");

for (const dep of this.sortedDeps()) {
dep.patchAppTargetPalette();
}

this.patchAppTargetPalette();
return opts;
}
Expand Down
13 changes: 7 additions & 6 deletions pxtlib/spriteutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,11 +603,12 @@ namespace pxt.sprite {
return result;
}

export function imageLiteralToBitmap(text: string): Bitmap {
export function imageLiteralToBitmap(text: string, templateLiteral = "img"): Bitmap {
// Strip the tagged template string business and the whitespace. We don't have to exhaustively
// replace encoded characters because the compiler will catch any disallowed characters and throw
// an error before the decompilation happens. 96 is backtick and 9 is tab
text = text.replace(/[ `]|(?:&#96;)|(?:&#9;)|(?:img)/g, "").trim();
text = text.replaceAll(templateLiteral, "");
text = text.replace(/^["`\(\)]*/, '').replace(/["`\(\)]*$/, '');
text = text.replace(/&#10;/g, "\n");

Expand Down Expand Up @@ -735,14 +736,14 @@ namespace pxt.sprite {
pxt.sprite.trimTilemapTileset(result);
}

function imageLiteralPrologue(fileType: "typescript" | "python"): string {
function imageLiteralPrologue(fileType: "typescript" | "python", templateLiteral = "img"): string {
let res = '';
switch (fileType) {
case "python":
res = "img(\"\"\"";
res = `${templateLiteral}("""`;
break;
default:
res = "img`";
res = `${templateLiteral}\``;
break;
}
return res;
Expand Down Expand Up @@ -778,10 +779,10 @@ namespace pxt.sprite {
return res;
}

export function bitmapToImageLiteral(bitmap: Bitmap, fileType: "typescript" | "python"): string {
export function bitmapToImageLiteral(bitmap: Bitmap, fileType: "typescript" | "python", templateLiteral = "img"): string {
if (!bitmap || bitmap.height === 0 || bitmap.width === 0) return "";

let res = imageLiteralPrologue(fileType);
let res = imageLiteralPrologue(fileType, templateLiteral);

if (bitmap) {
const paddingBetweenPixels = (bitmap.width * bitmap.height > 300) ? "" : " ";
Expand Down
3 changes: 2 additions & 1 deletion pxtlib/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"dom.iterable",
"scripthost",
"es2017",
"ES2018.Promise"
"ES2018.Promise",
"ES2021.String"
],
"types": [
"highlight.js",
Expand Down