[Mobile] add OtherImageLoader for otherCache

This commit is contained in:
Anthony Calosa
2021-04-05 17:04:40 +08:00
parent 1b6a0188f3
commit e57d0a4d0e
2 changed files with 51 additions and 21 deletions

View File

@@ -83,7 +83,7 @@ public class ImageCache {
}
})
.build(new ImageLoader());
private static final LoadingCache<String, Texture> otherCache = CacheBuilder.newBuilder().build(new ImageLoader());
private static final LoadingCache<String, Texture> otherCache = CacheBuilder.newBuilder().build(new OtherImageLoader());
public static final Texture defaultImage;
public static FImage BlackBorder = FSkinImage.IMG_BORDER_BLACK;
public static FImage WhiteBorder = FSkinImage.IMG_BORDER_WHITE;
@@ -119,10 +119,11 @@ public class ImageCache {
public static Texture getImage(InventoryItem ii) {
String imageKey = ii.getImageKey(false);
boolean isCardImage = imageKey.startsWith(ImageKeys.CARD_PREFIX) || imageKey.startsWith(ImageKeys.TOKEN_PREFIX);
if (isCardImage)
getImage(ii.getImageKey(false), true);
return getOtherImages(ii.getImageKey(false));
if (imageKey != null) {
if(imageKey.startsWith(ImageKeys.CARD_PREFIX) || imageKey.startsWith(ImageKeys.TOKEN_PREFIX))
return getImage(ii.getImageKey(false), true, false);
}
return getImage(ii.getImageKey(false), true, true);
}
/**
@@ -132,7 +133,7 @@ public class ImageCache {
public static FImage getIcon(IHasIcon ihi) {
String imageKey = ihi.getIconImageKey();
final Texture icon;
if (missingIconKeys.contains(imageKey) || (icon = getOtherImages(ihi.getIconImageKey())) == null) {
if (missingIconKeys.contains(imageKey) || (icon = getImage(ihi.getIconImageKey(), false, true)) == null) {
missingIconKeys.add(imageKey);
return FSkinImage.UNKNOWN;
}
@@ -181,6 +182,9 @@ public class ImageCache {
* </p>
*/
public static Texture getImage(String imageKey, boolean useDefaultIfNotFound) {
return getImage(imageKey, useDefaultIfNotFound, false);
}
public static Texture getImage(String imageKey, boolean useDefaultIfNotFound, boolean useOtherCache) {
if (StringUtils.isEmpty(imageKey)) {
return null;
}
@@ -199,7 +203,7 @@ public class ImageCache {
Texture image;
if (useDefaultIfNotFound) {
// Load from file and add to cache if not found in cache initially.
image = cache.getIfPresent(imageKey);
image = useOtherCache ? otherCache.getIfPresent(imageKey) : cache.getIfPresent(imageKey);
if (image != null) { return image; }
@@ -214,7 +218,7 @@ public class ImageCache {
imageLoaded = true;
}
try { image = cache.get(imageKey); }
try { image = useOtherCache ? otherCache.get(imageKey) : cache.get(imageKey); }
catch (final Exception ex) {
image = null;
}
@@ -224,25 +228,16 @@ public class ImageCache {
if (image == null) {
if (useDefaultIfNotFound) {
image = defaultImage;
cache.put(imageKey, defaultImage);
if (useOtherCache)
otherCache.put(imageKey, defaultImage);
else
cache.put(imageKey, defaultImage);
if (imageBorder.get(image.toString()) == null)
imageBorder.put(image.toString(), Pair.of(Color.valueOf("#171717").toString(), false)); //black border
}
}
return image;
}
public static Texture getOtherImages(String imageKey) {
if (StringUtils.isEmpty(imageKey)) {
return null;
}
Texture image;
try { image = otherCache.get(imageKey); }
catch (final Exception ex) {
image = null;
}
return image;
}
public static void preloadCache(Iterable<String> keys) {
if (FModel.getPreferences().getPrefBoolean(ForgePreferences.FPref.UI_DISABLE_CARD_IMAGES))
return;

View File

@@ -0,0 +1,35 @@
package forge.assets;
import java.io.File;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.google.common.cache.CacheLoader;
import forge.Forge;
import forge.ImageKeys;
final class OtherImageLoader extends CacheLoader<String, Texture> {
@Override
public Texture load(String key) {
File file = ImageKeys.getImageFile(key);
if (file != null) {
FileHandle fh = new FileHandle(file);
try {
if (Forge.isTextureFilteringEnabled()) {
Texture t = new Texture(fh, true);
t.setFilter(Texture.TextureFilter.MipMapLinearLinear, Texture.TextureFilter.Linear);
return t;
} else {
return new Texture(fh);
}
}
catch (Exception ex) {
Forge.log("Could not read image file " + fh.path() + "\n\nException:\n" + ex.toString());
return null;
}
}
return null;
}
}