Skip to content

Commit

Permalink
Fixed various bugs & optim issues (#1008)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepnight committed Jan 15, 2024
1 parent 9f34fd0 commit 15f68b9
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 24 deletions.
87 changes: 67 additions & 20 deletions src/electron.renderer/data/Project.hx
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,40 @@ class Project {
}


public function getOrLoadImage(relPath:String, x=-1, y=-1, w=-1, h=-1) : Null<data.DataTypes.CachedImage> {
public function getOrLoadEmbedImageSub(id:ldtk.Json.EmbedAtlas, x:Int, y:Int, w:Int, h:Int) : Null<data.DataTypes.CachedImage> {
try {
var thumbnailPath = id.getName() + "_" + Std.string(x) + "_" + Std.string(y) + "_" + Std.string(w) + "_" + Std.string(h);
if( !imageCache.exists(thumbnailPath) ) {
// Load original image
var cachedImg = getOrLoadEmbedImage(id);
if( cachedImg==null )
return null;

// Create sub tile cache
var subPixels = cachedImg.pixels.sub(x, y, w, h);
var pngBytes = subPixels.clone().toPNG();
var tex = h3d.mat.Texture.fromPixels(subPixels);
var b64 = haxe.crypto.Base64.encode(pngBytes);
imageCache.set( thumbnailPath, {
fileName: cachedImg.fileName,
relPath: cachedImg.relPath,
bytes: pngBytes,
base64: b64,
pixels: subPixels,
tex: tex,
});
}

return imageCache.get(thumbnailPath);
}
catch( e:Dynamic ) {
App.LOG.error(e);
return null;
}
}


public function getOrLoadImage(relPath:String) : Null<data.DataTypes.CachedImage> {
try {
if( !imageCache.exists(relPath) ) {
// Load it from the disk
Expand All @@ -946,25 +979,6 @@ class Project {
pixels: pixels,
tex: texture,
});

// Store thumbnail to cache
var thumbnailPath = "";
if( x!=-1 && y!=-1 && w!=-1 && h!=-1 )
thumbnailPath = relPath + "_" + Std.string(x) + "_" + Std.string(y) + "_" + Std.string(w) + "_" + Std.string(h);

if( thumbnailPath!="" ) {
var subPixels = pixels.sub(x, y, w, h);
var b64 = haxe.crypto.Base64.encode( subPixels.toPNG() );
imageCache.set( thumbnailPath, {
fileName: dn.FilePath.extractFileWithExt(relPath),
relPath: thumbnailPath,
bytes: null,
base64: b64,
pixels: subPixels,
tex: null,
});
relPath = thumbnailPath;
}
}

return imageCache.get(relPath);
Expand All @@ -976,6 +990,39 @@ class Project {
}


public function getOrLoadImageSub(relPath:String, x:Int, y:Int, w:Int, h:Int) : Null<data.DataTypes.CachedImage> {
try {
var thumbnailPath = relPath + "_" + Std.string(x) + "_" + Std.string(y) + "_" + Std.string(w) + "_" + Std.string(h);
if( !imageCache.exists(thumbnailPath) ) {
// Load original image
var cachedImg = getOrLoadImage(relPath);
if( cachedImg==null )
return null;

// Create sub tile cache
var subPixels = cachedImg.pixels.sub(x, y, w, h);
var pngBytes = subPixels.clone().toPNG();
var tex = h3d.mat.Texture.fromPixels(subPixels);
var b64 = haxe.crypto.Base64.encode(pngBytes);
imageCache.set( thumbnailPath, {
fileName: cachedImg.fileName,
relPath: cachedImg.relPath,
bytes: pngBytes,
base64: b64,
pixels: subPixels,
tex: tex,
});
}

return imageCache.get(thumbnailPath);
}
catch( e:Dynamic ) {
App.LOG.error(e);
return null;
}
}


public function getAllCachedImages() {
var arr = Lambda.array(imageCache);
arr.sort( (a,b)->Reflect.compare( a.fileName.toLowerCase(), b.fileName.toLowerCase() ) );
Expand Down
17 changes: 13 additions & 4 deletions src/electron.renderer/data/def/TilesetDef.hx
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,20 @@ class TilesetDef {

public inline function isUsingEmbedAtlas() return embedAtlas!=null;

function getOrLoadTilesetImage(x = -1, y = -1, w = -1, h = -1) {
function getOrLoadTilesetImage() {
if( !hasAtlasPointer() )
return null;
else
return embedAtlas!=null ? _project.getOrLoadEmbedImage(embedAtlas) : _project.getOrLoadImage(relPath, x, y, w, h);
return embedAtlas!=null ? _project.getOrLoadEmbedImage(embedAtlas) : _project.getOrLoadImage(relPath);
}

function getOrLoadTilesetImageSub(x:Int, y:Int, w:Int, h:Int) {
if( !hasAtlasPointer() )
return null;
else if( embedAtlas!=null )
return _project.getOrLoadEmbedImageSub(embedAtlas, x,y,w,h);
else
return _project.getOrLoadImageSub(relPath, x,y,w,h);
}

@:allow(data.Project)
Expand Down Expand Up @@ -889,8 +898,8 @@ class TilesetDef {
public function createTileHtmlImageFromRect(r:ldtk.Json.TilesetRect, ?imgWid:Int, ?imgHei:Int) : js.jquery.JQuery {
var jImg =
if( isAtlasLoaded() && isTileRectInBounds(r) ) {
var imgData = getOrLoadTilesetImage(r.x, r.y, r.w, r.h);
var subPixels = imgData.pixels;
var imgData = getOrLoadTilesetImageSub(r.x, r.y, r.w, r.h);
var subPixels = imgData.pixels;
var b64 = imgData.base64;
var img = new js.html.Image(subPixels.width, subPixels.height);
img.src = 'data:image/png;base64,$b64';
Expand Down

0 comments on commit 15f68b9

Please sign in to comment.