Skip to content

Commit

Permalink
Keep large image thumbnails from getting stuck (#1008)
Browse files Browse the repository at this point in the history
Keep large image thumbnails from getting stuck
  • Loading branch information
hnhhzy authored Jan 15, 2024
1 parent aa8d4c9 commit 1a97ef9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
21 changes: 20 additions & 1 deletion src/electron.renderer/data/Project.hx
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,13 @@ class Project {
}


public function getOrLoadImage(relPath:String) : Null<data.DataTypes.CachedImage> {
public function getOrLoadImage(relPath:String,x = -1, y = -1, w = -1, h = -1) : Null<data.DataTypes.CachedImage> {
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...');
Expand All @@ -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 ) {
Expand Down
10 changes: 5 additions & 5 deletions src/electron.renderer/data/def/TilesetDef.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1a97ef9

Please sign in to comment.