diff --git a/src/electron.renderer/data/Project.hx b/src/electron.renderer/data/Project.hx index c8327eb82..bd20da3c1 100644 --- a/src/electron.renderer/data/Project.hx +++ b/src/electron.renderer/data/Project.hx @@ -920,8 +920,13 @@ class Project { } - public function getOrLoadImage(relPath:String) : Null { + public function getOrLoadImage(relPath:String,x = -1, y = -1, w = -1, h = -1) : Null { try { + 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( !imageCache.exists(relPath) ) { // Load it from the disk App.LOG.add("cache", 'Caching image $relPath...'); @@ -946,7 +951,21 @@ class Project { pixels: pixels, tex: texture, }); + 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); } catch( e:Dynamic ) { diff --git a/src/electron.renderer/data/def/TilesetDef.hx b/src/electron.renderer/data/def/TilesetDef.hx index 821bbc230..0831d69fd 100644 --- a/src/electron.renderer/data/def/TilesetDef.hx +++ b/src/electron.renderer/data/def/TilesetDef.hx @@ -60,11 +60,11 @@ class TilesetDef { public inline function isUsingEmbedAtlas() return embedAtlas!=null; - function getOrLoadTilesetImage() { + function getOrLoadTilesetImage(x = -1, y = -1, w = -1, h = -1) { if( !hasAtlasPointer() ) return null; else - return embedAtlas!=null ? _project.getOrLoadEmbedImage(embedAtlas) : _project.getOrLoadImage(relPath); + return embedAtlas!=null ? _project.getOrLoadEmbedImage(embedAtlas) : _project.getOrLoadImage(relPath, x, y, w, h); } @:allow(data.Project) @@ -889,9 +889,9 @@ 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(); - var subPixels = imgData.pixels.sub(r.x, r.y, r.w, r.h); - var b64 = haxe.crypto.Base64.encode( subPixels.toPNG() ); + var imgData = getOrLoadTilesetImage(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'; new J(img);