Skip to content
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
4 changes: 2 additions & 2 deletions pxtblocks/fields/field_animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class FieldAnimationEditor extends FieldAssetEditor<FieldAnimationOptions
const frames = parseImageArrayString(text, this.params.taggedTemplate);

if (frames && frames.length) {
const id = this.temporaryAssetId();
const id = this.sourceBlock_.id;

const newAnimation: pxt.Animation = {
internalID: -1,
Expand All @@ -97,7 +97,7 @@ export class FieldAnimationEditor extends FieldAssetEditor<FieldAnimationOptions
if (asset) return asset;
}

const id = this.temporaryAssetId();
const id = this.sourceBlock_.id;
const bitmap = new pxt.sprite.Bitmap(this.params.initWidth, this.params.initHeight).data()

const newAnimation: pxt.Animation = {
Expand Down
17 changes: 9 additions & 8 deletions pxtblocks/fields/field_asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export abstract class FieldAssetEditor<U extends FieldAssetEditorOptions, V exte
}

protected onFieldEditorHide(fv: pxt.react.FieldEditorView<pxt.Asset>) {
let result = fv.getResult();
const result = fv.getResult();
const project = pxt.react.getTilemapProject();

if (this.asset.type === pxt.AssetType.Song) {
Expand All @@ -275,10 +275,15 @@ export abstract class FieldAssetEditor<U extends FieldAssetEditorOptions, V exte
const old = this.getValue();
if (pxt.assetEquals(this.asset, result)) return;

result = pxt.patchTemporaryAsset(this.asset, result, project);

const oldId = isTemporaryAsset(this.asset) ? null : this.asset.id;
const newId = isTemporaryAsset(result) ? null : result.id;
let newId = isTemporaryAsset(result) ? null : result.id;

if (!oldId && newId === this.sourceBlock_.id) {
// The temporary assets we create just use the block id as the id; give it something
// a little nicer
result.id = project.generateNewID(result.type);
newId = result.id;
}

this.pendingEdit = true;

Expand Down Expand Up @@ -547,10 +552,6 @@ export abstract class FieldAssetEditor<U extends FieldAssetEditorOptions, V exte
protected isFullscreen() {
return true;
}

protected temporaryAssetId() {
return this.sourceBlock_.id + "_" + this.name;
}
}

function isTemporaryAsset(asset: pxt.Asset) {
Expand Down
2 changes: 1 addition & 1 deletion pxtblocks/fields/field_musiceditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class FieldMusicEditor extends FieldAssetEditor<FieldMusicEditorOptions,

const newAsset: pxt.Song = {
internalID: -1,
id: this.temporaryAssetId(),
id: this.sourceBlock_.id,
type: pxt.AssetType.Song,
meta: {
},
Expand Down
2 changes: 1 addition & 1 deletion pxtblocks/fields/field_sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class FieldSpriteEditor extends FieldAssetEditor<FieldSpriteEditorOptions

const newAsset: pxt.ProjectImage = {
internalID: -1,
id: this.temporaryAssetId(),
id: this.sourceBlock_.id,
type: pxt.AssetType.Image,
jresData: pxt.sprite.base64EncodeBitmap(data),
meta: {
Expand Down
18 changes: 4 additions & 14 deletions pxteditor/monaco-fields/field_musiceditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ export class MonacoSongEditor extends MonacoReactFieldEditor<pxt.Song> {
protected isPython: boolean;
protected isAsset: boolean;
protected text: string;
protected editing: pxt.Asset;

protected textToValue(text: string): pxt.Song {
this.isPython = text.indexOf("`") === -1
this.text = text;

const match = pxt.parseAssetTSReference(text);
if (match) {
const { name: matchedName } = match;
const { type, name: matchedName } = match;
const name = matchedName.trim();
const project = pxt.react.getTilemapProject();
this.isAsset = true;
const asset = project.lookupAssetByName(pxt.AssetType.Song, name);
if (asset) {
this.editing = asset;
return asset;
}
else {
Expand All @@ -31,8 +29,6 @@ export class MonacoSongEditor extends MonacoReactFieldEditor<pxt.Song> {
newAsset.meta.displayName = name;
}

this.editing = newAsset;

return newAsset;
}
}
Expand All @@ -43,24 +39,18 @@ export class MonacoSongEditor extends MonacoReactFieldEditor<pxt.Song> {
const contents = hexLiteralMatch[1].trim();

if (contents) {
this.editing = createFakeAsset(pxt.assets.music.decodeSongFromHex(contents));
}
else {
this.editing = createFakeAsset(pxt.assets.music.getEmptySong(2));
return createFakeAsset(pxt.assets.music.decodeSongFromHex(contents));
}

return this.editing;
return createFakeAsset(pxt.assets.music.getEmptySong(2));
}

return undefined; // never
}

protected resultToText(result: pxt.Song): string {
const project = pxt.react.getTilemapProject();
project.pushUndo();

result = pxt.patchTemporaryAsset(this.editing, result, project) as pxt.Song;
if (result.meta?.displayName) {
const project = pxt.react.getTilemapProject();
if (this.isAsset || project.lookupAsset(result.type, result.id)) {
result = project.updateAsset(result)
} else {
Expand Down
15 changes: 3 additions & 12 deletions pxteditor/monaco-fields/field_sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ export class MonacoSpriteEditor extends MonacoReactFieldEditor<pxt.ProjectImage>
protected isPython: boolean;
protected isAsset: boolean;
protected template: string;
protected editing: pxt.Asset;

protected textToValue(text: string): pxt.ProjectImage {
this.isPython = text.indexOf("`") === -1
this.template = text.startsWith("bmp") ? "bmp" : "img"

const match = pxt.parseAssetTSReference(text);
if (match) {
const { name: matchedName } = match;
const { type, name: matchedName } = match;
const name = matchedName.trim();
const project = pxt.react.getTilemapProject();
this.isAsset = true;
const asset = project.lookupAssetByName(pxt.AssetType.Image, name);
if (asset) {
this.editing = asset;
return asset;
}
else {
Expand All @@ -31,23 +29,16 @@ export class MonacoSpriteEditor extends MonacoReactFieldEditor<pxt.ProjectImage>
newAsset.meta.displayName = name;
}

this.editing = newAsset;

return newAsset;
}
}

this.editing = createFakeAsset(pxt.sprite.imageLiteralToBitmap(text, this.template));

return this.editing;
return createFakeAsset(pxt.sprite.imageLiteralToBitmap(text, this.template));
}

protected resultToText(result: pxt.ProjectImage): string {
const project = pxt.react.getTilemapProject();
project.pushUndo();
result = pxt.patchTemporaryAsset(this.editing, result, project) as pxt.ProjectImage;

if (result.meta?.displayName) {
const project = pxt.react.getTilemapProject();
if (this.isAsset || project.lookupAsset(result.type, result.id)) {
result = project.updateAsset(result)
} else {
Expand Down
6 changes: 2 additions & 4 deletions pxteditor/monaco-fields/field_tilemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ const fieldEditorId = "tilemap-editor";
export class MonacoTilemapEditor extends MonacoReactFieldEditor<pxt.ProjectTilemap> {
protected isTilemapLiteral: boolean;
protected tilemapLiteral: string;
protected editing: pxt.Asset;

protected textToValue(text: string): pxt.ProjectTilemap {
const tm = this.readTilemap(text);

const project = pxt.react.getTilemapProject();
pxt.sprite.addMissingTilemapTilesAndReferences(project, tm);

this.editing = tm;
return tm;
}

Expand All @@ -33,7 +31,7 @@ export class MonacoTilemapEditor extends MonacoReactFieldEditor<pxt.ProjectTilem
// If the user is still typing, they might try to open the editor on an incomplete tilemap
}
return null;
}
}
}

this.isTilemapLiteral = true;
Expand Down Expand Up @@ -73,7 +71,7 @@ export class MonacoTilemapEditor extends MonacoReactFieldEditor<pxt.ProjectTilem
protected resultToText(asset: pxt.ProjectTilemap): string {
const project = pxt.react.getTilemapProject();
project.pushUndo();
asset = pxt.patchTemporaryAsset(this.editing, asset, project) as pxt.ProjectTilemap;

pxt.sprite.updateTilemapReferencesFromResult(project, asset);

if (this.isTilemapLiteral) {
Expand Down
18 changes: 4 additions & 14 deletions pxtlib/spriteutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ namespace pxt.sprite {
}
}

dataLength() {
protected dataLength() {
return Math.ceil(this.width * this.height / 2);
}
}
Expand Down Expand Up @@ -179,7 +179,7 @@ namespace pxt.sprite {
this.buf[index] = value;
}

dataLength() {
protected dataLength() {
return this.width * this.height;
}
}
Expand All @@ -196,7 +196,7 @@ namespace pxt.sprite {

constructor(public tilemap: Tilemap, public tileset: TileSet, public layers: BitmapData) {}

cloneData(includeEditorData = false) {
cloneData() {
const tm = this.tilemap.copy();
const tileset: TileSet = {
tileWidth: this.tileset.tileWidth,
Expand All @@ -207,17 +207,7 @@ namespace pxt.sprite {
}
const layers = Bitmap.fromData(this.layers).copy().data();

const result = new TilemapData(tm, tileset, layers);

if (includeEditorData) {
result.nextId = this.nextId;
result.projectReferences = this.projectReferences?.slice();
result.tileOrder = this.tileOrder?.slice();
result.editedTiles = this.editedTiles?.slice();
result.deletedTiles = this.deletedTiles?.slice();
}

return result;
return new TilemapData(tm, tileset, layers);
}

equals(other: TilemapData) {
Expand Down
Loading