/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.processors.loaders;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.tools.imagepacker.TexturePacker2;
import com.badlogic.gdx.tools.imagepacker.TexturePacker2.Settings;
import com.badlogic.gdx.utils.Array;
import java.io.File;
import transientlibs.slick2d.util.Log;
import transientlibs.bindedobjects.core.Binding;
import transientlibs.bindedobjects.core.GDXFonts;
import transientlibs.bindedobjects.core.GDXImageCatalog;
import transientlibs.bindedobjects.core.Module;
import transientlibs.bindedobjects.core.Tex;
import transientlibs.bindedobjects.core.loaders.GameCharacterLoader;
import transientlibs.bindedobjects.core.datasets.Action;
import transientlibs.bindedobjects.core.datasets.EquipmentSlots;
import transientlibs.bindedobjects.gamecontent.CardGroups;
import transientlibs.bindedobjects.gamecontent.Cards;
import transientlibs.bindedobjects.gamecontent.Creatures;
import transientlibs.bindedobjects.core.datasets.Events;
import transientlibs.bindedobjects.core.datasets.GameCharacter;
import transientlibs.bindedobjects.core.datasets.ItemGroups;
import transientlibs.bindedobjects.core.datasets.Items;
import transientlibs.bindedobjects.core.datasets.Landmarks;
import transientlibs.bindedobjects.core.datasets.Skills;
import transientlibs.bindedobjects.gamecontent.MusicTrackGDX;
import transientlibs.bindedobjects.gamecontent.Terrain;
import transientlibs.maps.container.TiledMapGDXContainer;
import transientlibs.objects.general.Initializer;
import transientlibs.processors.loaders.MapAssetLoader;
import transientlibs.processors.misc.Detonator;
/**
*
* @author kibertoad
*/
public class GenericAssetLoader {
public boolean regenerateAtlases = false;
public AssetManager manager;
public MapAssetLoader mapLoader = new MapAssetLoader();
public static final String pathToPackedGenericImages = "data/init/ui/imgpacked/";
public static final String pathToGenericImages = "data/init/ui/";
public GenericAssetLoader(String inModule) {
init(inModule);
}
public void regenerateImageList() {
//SlickImageCatalog.regenerateImagesFile();
}
public void bindGenericGDXImages() {
TextureAtlas atlas = manager.get(pathToPackedGenericImages + "packedG.atlas", TextureAtlas.class);
Array<AtlasRegion> regions = atlas.getRegions();
Log.info("Amount of images to bind: "+regions.size);
GDXImageCatalog newImage;
for (AtlasRegion ar : regions) {
newImage = new GDXImageCatalog(ar, pathToGenericImages);
GDXImageCatalog.images.add(newImage);
newImage.tags.add("generic");
}
}
public void bindGDXImages() {
TextureAtlas atlas = manager.get(Detonator.INSTANCE.gameDataDir + "/imgpacked/packed.atlas", TextureAtlas.class);
Array<AtlasRegion> regions = atlas.getRegions();
for (AtlasRegion ar : regions) {
GDXImageCatalog.images.add(new GDXImageCatalog(ar));
}
}
private void setDefaultImages() {
Detonator.INSTANCE.imageProvider.defaultHoveredButtonImage = Detonator.INSTANCE.imageProvider.getImage("button-150-45-01-black-rounded");
Detonator.INSTANCE.imageProvider.defaultButtonImage = Detonator.INSTANCE.imageProvider.getImage("button-150-45-01-black-rounded");
}
/**
* Process non-game specific assets
*/
public void processGeneric() {
Initializer.start();
Log.info("Load generic images.");
GDXFonts.loadGlobalFonts();
loadGenericImages();
bindGenericGDXImages();
}
public void process() {
//selectModule();
loadingGDX();
bindGDXImages();
setDefaultImages();
Terrain.loadTerrain();
loadingTransient();
loadGDXMaps();
Log.info("Load transient.");
}
public final void init(String moduleName) {
Detonator.INSTANCE.currentModuleID = moduleName;
manager = new AssetManager();
}
public void loadGenericImages() {
//Log.warn("Regenerate generic atlases: "+regenerateAtlases);
if (regenerateAtlases) {regenerateGenericAtlas();}
manager.load(pathToPackedGenericImages + "packedG.atlas", TextureAtlas.class);
manager.finishLoading();
}
public void regenerateGenericAtlas() {
String imageFolder = pathToGenericImages;
String packedFolder = pathToPackedGenericImages;
Settings settings = new Settings();
settings.maxWidth = Detonator.INSTANCE.maxAtlasSizeX;
settings.maxHeight = Detonator.INSTANCE.maxAtlasSizeY;
new File(packedFolder).mkdirs();
File[] files = new File(packedFolder).listFiles();
for (File file : files) {
file.delete();
}
Log.info("Pack from: " + imageFolder);
Log.info("Pack to: " + packedFolder);
TexturePacker2.process(settings, imageFolder,
packedFolder, "packedG");
}
public void loadingGDX() {
Settings settings = new Settings();
settings.maxWidth = Detonator.INSTANCE.maxAtlasSizeX;
settings.maxHeight = Detonator.INSTANCE.maxAtlasSizeY;
if (regenerateAtlases) {
new File(Detonator.INSTANCE.gameDataDir + "imgpacked").mkdirs();
File[] files = new File(Detonator.INSTANCE.gameDataDir + "imgpacked").listFiles();
for (File file : files) {
file.delete();
}
Log.info("Pack from: " + Detonator.INSTANCE.gameDataDir + "img");
TexturePacker2.process(settings, Detonator.INSTANCE.gameDataDir + "img",
Detonator.INSTANCE.gameDataDir + "imgpacked", "packed");
}
//load prepared atlas
Log.info("Load prepared atlas: "+Detonator.INSTANCE.gameDataDir + "/imgpacked/packed.atlas");
manager.load(Detonator.INSTANCE.gameDataDir + "/imgpacked/packed.atlas", TextureAtlas.class);
manager.finishLoading();
}
public void loadGDXMaps() {
manager.setLoader(TiledMap.class, new TmxMapLoader(new InternalFileHandleResolver()));
File dir = new File(Detonator.INSTANCE.currentModule.dirName + "maps");
String[] fileList = dir.list();
if (fileList == null) {
Log.warn(Detonator.INSTANCE.currentModule.dirName + "maps");
Log.warn("Specified directory does not exist!");
} else {
for (int x = 0; x < fileList.length; x++) {
if (fileList[x].endsWith(".tmx")) {
Log.info(fileList[x]);
//manager.load(fileList[x], TiledMap.class);
new TiledMapGDXContainer(fileList[x]);
Log.info("Map: " + fileList[x]);
mapLoader.loadMapCollisionInfo((TiledMapGDXContainer) Detonator.INSTANCE.mapProvider.getMap(fileList[x]));
}
//map = new TiledMapGDXContainer("map-test-aquamarine");
}
}
}
public void loadingTransient() {
//uncomment later
//ButtonEffect.sbg = game;
//Images.loadUIImages();
//GDXFonts.loadGlobalFonts();
Skills.loadSkills();
Action.loadActions();
ItemGroups.loadItemGroups();
EquipmentSlots.loadEquipmentSlots();
CardGroups.loadCardGroups();
Items.loadItems();
Cards.loadAllCards();
Landmarks.loadLandmarks();
Creatures.loadCreatures();
MusicTrackGDX.loadMusic();
GameCharacter.loadCharacters();
//Terrain.loadTerrain();
Tex.runInit();
Tex.loadTexts();
//Events.loadAllEvents();
Events.loadEvents();
//MusicTrack.loadMusic();
//Sounds.loadSounds();
}
public void selectModule() {
//Fonts.loadGlobalFonts();
Module.loadModules();
Detonator.INSTANCE.currentModule = Module.modules.get(Binding.readBinding(Binding.moduleBinding, Detonator.INSTANCE.currentModuleID));
Detonator.INSTANCE.gameDataDir = Detonator.INSTANCE.currentModule.dirName;
Log.info("Gamedatadir: " + Detonator.INSTANCE.gameDataDir);
}
}