Assets class

This commit is contained in:
Anthony Calosa
2022-07-06 21:32:37 +08:00
parent 42c59b773b
commit c838b60b7a
3 changed files with 57 additions and 47 deletions

View File

@@ -1,13 +1,12 @@
package forge.adventure.util; package forge.adventure.util;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.resolvers.AbsoluteFileHandleResolver;
import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Json; import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonWriter; import com.badlogic.gdx.utils.JsonWriter;
import forge.adventure.data.ConfigData; import forge.adventure.data.ConfigData;
import forge.adventure.data.SettingData; import forge.adventure.data.SettingData;
import forge.assets.Assets;
import forge.deck.Deck; import forge.deck.Deck;
import forge.gui.GuiBase; import forge.gui.GuiBase;
import forge.localinstance.properties.ForgeConstants; import forge.localinstance.properties.ForgeConstants;
@@ -27,7 +26,7 @@ public class Config {
private static Config currentConfig; private static Config currentConfig;
private final String prefix; private final String prefix;
private final HashMap<String, FileHandle> Cache = new HashMap<String, FileHandle>(); private final HashMap<String, FileHandle> Cache = new HashMap<String, FileHandle>();
private final AssetManager textureAtlasManager = new AssetManager(new AbsoluteFileHandleResolver()); private final Assets atlasAssets = new Assets();
private final ConfigData configData; private final ConfigData configData;
private final String[] adventures; private final String[] adventures;
private SettingData settingsData; private SettingData settingsData;
@@ -122,11 +121,11 @@ public class Config {
public TextureAtlas getAtlas(String spriteAtlas) { public TextureAtlas getAtlas(String spriteAtlas) {
String fileName = getFile(spriteAtlas).path(); String fileName = getFile(spriteAtlas).path();
if (!textureAtlasManager.contains(fileName, TextureAtlas.class)) { if (!atlasAssets.manager().contains(fileName, TextureAtlas.class)) {
textureAtlasManager.load(fileName, TextureAtlas.class); atlasAssets.manager().load(fileName, TextureAtlas.class);
textureAtlasManager.finishLoadingAsset(fileName); atlasAssets.manager().finishLoadingAsset(fileName);
} }
return textureAtlasManager.get(fileName); return atlasAssets.manager().get(fileName);
} }
public SettingData getSettingData() public SettingData getSettingData()
{ {
@@ -145,6 +144,6 @@ public class Config {
} }
public void disposeTextureAtlasManager() { public void disposeTextureAtlasManager() {
textureAtlasManager.dispose(); atlasAssets.dispose();
} }
} }

View File

@@ -0,0 +1,16 @@
package forge.assets;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.resolvers.AbsoluteFileHandleResolver;
import com.badlogic.gdx.utils.Disposable;
public class Assets implements Disposable {
public AssetManager manager = new AssetManager(new AbsoluteFileHandleResolver());
public AssetManager manager() {
return manager;
}
@Override
public void dispose() {
manager.dispose();
}
}

View File

@@ -20,9 +20,7 @@ package forge.assets;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.assets.loaders.TextureLoader.TextureParameter;
import com.badlogic.gdx.assets.loaders.TextureLoader;
import com.badlogic.gdx.assets.loaders.resolvers.AbsoluteFileHandleResolver;
import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.TextureData; import com.badlogic.gdx.graphics.TextureData;
@@ -73,10 +71,10 @@ public class ImageCache {
private static final ObjectSet<String> missingIconKeys = new ObjectSet<>(); private static final ObjectSet<String> missingIconKeys = new ObjectSet<>();
private static List<String> borderlessCardlistKey = FileUtil.readFile(ForgeConstants.BORDERLESS_CARD_LIST_FILE); private static List<String> borderlessCardlistKey = FileUtil.readFile(ForgeConstants.BORDERLESS_CARD_LIST_FILE);
static int maxCardCapacity = 400; //default card capacity static int maxCardCapacity = 400; //default card capacity
static AssetManager cardTextureManager = new AssetManager(new AbsoluteFileHandleResolver()); static Assets cardAssets = new Assets();
static AssetManager otherTextureManager = new AssetManager(new AbsoluteFileHandleResolver()); static Assets otherAssets = new Assets();
static TextureLoader.TextureParameter defaultParameter = new TextureLoader.TextureParameter(); static TextureParameter defaultParameter = new TextureParameter();
static TextureLoader.TextureParameter filtered = new TextureLoader.TextureParameter(); static TextureParameter filtered = new TextureParameter();
public static void initCache(int capacity) { public static void initCache(int capacity) {
//init filter //init filter
filtered.genMipMaps = true; filtered.genMipMaps = true;
@@ -117,11 +115,11 @@ public class ImageCache {
} }
public static void disposeTextures(){ public static void disposeTextures(){
CardRenderer.clearcardArtCache(); CardRenderer.clearcardArtCache();
cardTextureManager.clear(); cardAssets.manager().clear();
} }
public static void disposeTextureManager() { public static void disposeTextureManager() {
cardTextureManager.dispose(); cardAssets.dispose();
otherTextureManager.dispose(); otherAssets.dispose();
} }
public static Texture getImage(InventoryItem ii) { public static Texture getImage(InventoryItem ii) {
@@ -262,26 +260,26 @@ public class ImageCache {
return null; return null;
if (!otherCache && Forge.enableUIMask.equals("Full") && isBorderless(imageKey)) if (!otherCache && Forge.enableUIMask.equals("Full") && isBorderless(imageKey))
return generatedCards.get(imageKey); return generatedCards.get(imageKey);
return otherCache ? otherTextureManager.get(file.getPath(), Texture.class, false) : cardTextureManager.get(file.getPath(), Texture.class, false); return otherCache ? otherAssets.manager().get(file.getPath(), Texture.class, false) : cardAssets.manager().get(file.getPath(), Texture.class, false);
} }
static Texture loadAsset(String imageKey, File file, boolean otherCache) { static Texture loadAsset(String imageKey, File file, boolean otherCache) {
if (file == null) if (file == null)
return null; return null;
if (!otherCache && cardTextureManager.getLoadedAssets() > maxCardCapacity) { if (!otherCache && cardAssets.manager().getLoadedAssets() > maxCardCapacity) {
//when maxCardCapacity is reached, clear to refresh //when maxCardCapacity is reached, clear to refresh
cardTextureManager.clear(); cardAssets.manager().clear();
CardRenderer.clearcardArtCache(); CardRenderer.clearcardArtCache();
return null; return null;
} }
String fileName = file.getPath(); String fileName = file.getPath();
if (otherCache) { if (otherCache) {
otherTextureManager.load(fileName, Texture.class, Forge.isTextureFilteringEnabled() ? filtered : defaultParameter); otherAssets.manager().load(fileName, Texture.class, Forge.isTextureFilteringEnabled() ? filtered : defaultParameter);
otherTextureManager.finishLoadingAsset(fileName); otherAssets.manager().finishLoadingAsset(fileName);
return otherTextureManager.get(fileName, Texture.class, false); return otherAssets.manager().get(fileName, Texture.class, false);
} else { } else {
cardTextureManager.load(fileName, Texture.class, Forge.isTextureFilteringEnabled() ? filtered : defaultParameter); cardAssets.manager().load(fileName, Texture.class, Forge.isTextureFilteringEnabled() ? filtered : defaultParameter);
cardTextureManager.finishLoadingAsset(fileName); cardAssets.manager().finishLoadingAsset(fileName);
Texture t = cardTextureManager.get(fileName, Texture.class, false); Texture t = cardAssets.manager().get(fileName, Texture.class, false);
if (Forge.enableUIMask.equals("Full")) { if (Forge.enableUIMask.equals("Full")) {
boolean borderless = isBorderless(imageKey); boolean borderless = isBorderless(imageKey);
updateBorders(t.toString(), borderless ? Pair.of(Color.valueOf("#171717").toString(), false): isCloserToWhite(getpixelColor(t))); updateBorders(t.toString(), borderless ? Pair.of(Color.valueOf("#171717").toString(), false): isCloserToWhite(getpixelColor(t)));
@@ -390,26 +388,23 @@ public class ImageCache {
if (t == null || fh == null) if (t == null || fh == null)
return t; return t;
final Texture[] n = new Texture[1]; final Texture[] n = new Texture[1];
FThreads.invokeInEdtNowOrLater(new Runnable() { FThreads.invokeInEdtNowOrLater(() -> {
@Override Pixmap pImage = new Pixmap(fh);
public void run() { int w = pImage.getWidth();
Pixmap pImage = new Pixmap(fh); int h = pImage.getHeight();
int w = pImage.getWidth(); int radius = (h - w) / 8;
int h = pImage.getHeight(); Pixmap pMask = createRoundedRectangle(w, h, radius, Color.RED);
int radius = (h - w) / 8; drawPixelstoMask(pImage, pMask);
Pixmap pMask = createRoundedRectangle(w, h, radius, Color.RED); TextureData textureData = new PixmapTextureData(
drawPixelstoMask(pImage, pMask); pMask, //pixmap to use
TextureData textureData = new PixmapTextureData( Format.RGBA8888,
pMask, //pixmap to use textureFilter, //use mipmaps
Pixmap.Format.RGBA8888, false, true);
textureFilter, //use mipmaps n[0] = new Texture(textureData);
false, true); if (textureFilter)
n[0] = new Texture(textureData); n[0].setFilter(Texture.TextureFilter.MipMapLinearLinear, Texture.TextureFilter.Linear);
if (textureFilter) pImage.dispose();
n[0].setFilter(Texture.TextureFilter.MipMapLinearLinear, Texture.TextureFilter.Linear); pMask.dispose();
pImage.dispose();
pMask.dispose();
}
}); });
return n[0]; return n[0];
} }