use of de.gurkenlabs.litiengine.environment.tilemap.ITileset in project litiengine by gurkenlabs.
the class EditorScreen method loadSpriteSheets.
private void loadSpriteSheets(Map map) {
List<SpriteSheetInfo> infos = new ArrayList<>();
int cnt = 0;
for (ITileset tileSet : map.getTilesets()) {
if (tileSet.getImage() == null || Spritesheet.find(tileSet.getName()) != null) {
continue;
}
Spritesheet sprite = Spritesheet.find(tileSet.getImage().getSource());
if (sprite == null) {
sprite = Spritesheet.load(tileSet);
if (sprite == null) {
continue;
}
}
infos.add(new SpriteSheetInfo(sprite));
cnt++;
}
for (IImageLayer imageLayer : map.getImageLayers()) {
Spritesheet sprite = Spritesheet.find(imageLayer.getImage().getSource());
if (sprite == null) {
BufferedImage img = Resources.getImage(imageLayer.getImage().getAbsoluteSourcePath(), true);
sprite = Spritesheet.load(img, imageLayer.getImage().getSource(), img.getWidth(), img.getHeight());
if (sprite == null) {
continue;
}
}
SpriteSheetInfo info = new SpriteSheetInfo(sprite);
infos.add(info);
this.getGameFile().getSpriteSheets().removeIf(x -> x.getName().equals(info.getName()));
this.getGameFile().getSpriteSheets().add(info);
cnt++;
}
this.loadSpriteSheets(infos, false);
for (SpriteSheetInfo info : infos) {
if (!this.getGameFile().getSpriteSheets().stream().anyMatch(x -> x.getName().equals(info.getName()))) {
this.getGameFile().getSpriteSheets().add(info);
}
}
if (cnt > 0) {
log.log(Level.INFO, "{0} tilesets loaded from {1}", new Object[] { cnt, map.getFileName() });
}
}
use of de.gurkenlabs.litiengine.environment.tilemap.ITileset in project litiengine by gurkenlabs.
the class MapComponent method importMap.
public void importMap() {
if (this.getMaps() == null) {
return;
}
final IMapLoader tmxLoader = new TmxMapLoader();
XmlImportDialog.importXml("Tilemap", Map.FILE_EXTENSION, files -> {
for (File file : files) {
String mapPath = file.toString();
Map map = (Map) tmxLoader.loadMap(mapPath);
if (map == null) {
log.log(Level.WARNING, "could not load map from file {0}", new Object[] { mapPath });
return;
}
if (map.getMapObjectLayers().isEmpty()) {
// make sure there's a map object layer on the map because we need one
// to add any kind of entities
MapObjectLayer layer = new MapObjectLayer();
layer.setName(DEFAULT_MAPOBJECTLAYER_NAME);
map.addMapObjectLayer(layer);
}
Optional<Map> current = this.maps.stream().filter(x -> x.getFileName().equals(map.getFileName())).findFirst();
if (current.isPresent()) {
int n = JOptionPane.showConfirmDialog(Game.getScreenManager().getRenderComponent(), Resources.get("input_replace_map", map.getFileName()), Resources.get("input_replace_map_title"), JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
this.getMaps().remove(current.get());
} else {
return;
}
}
this.getMaps().add(map);
Collections.sort(this.getMaps());
for (IImageLayer imageLayer : map.getImageLayers()) {
BufferedImage img = Resources.getImage(imageLayer.getImage().getAbsoluteSourcePath(), true);
Spritesheet sprite = Spritesheet.load(img, imageLayer.getImage().getSource(), img.getWidth(), img.getHeight());
this.screen.getGameFile().getSpriteSheets().add(new SpriteSheetInfo(sprite));
}
// remove old spritesheets
for (ITileset tileSet : map.getTilesets()) {
this.loadTileset(tileSet, true);
}
// remove old tilesets
for (ITileset tileset : map.getExternalTilesets()) {
this.loadTileset(tileset, false);
}
EditorScreen.instance().updateGameFileMaps();
ImageCache.clearAll();
if (this.environments.containsKey(map.getFileName())) {
this.environments.remove(map.getFileName());
}
EditorScreen.instance().getMapSelectionPanel().bind(this.getMaps(), true);
this.loadEnvironment(map);
log.log(Level.INFO, "imported map {0}", new Object[] { map.getFileName() });
}
});
}
use of de.gurkenlabs.litiengine.environment.tilemap.ITileset in project litiengine by gurkenlabs.
the class MapComponent method exportMap.
public void exportMap(Map map) {
// TODO: replace by XmlExportDialog call
JFileChooser chooser;
try {
String source = EditorScreen.instance().getProjectPath();
chooser = new JFileChooser(source != null ? source : new File(".").getCanonicalPath());
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setDialogTitle("Export Map");
FileFilter filter = new FileNameExtensionFilter("tmx - Tilemap XML", Map.FILE_EXTENSION);
chooser.setFileFilter(filter);
chooser.addChoosableFileFilter(filter);
chooser.setSelectedFile(new File(map.getFileName() + "." + Map.FILE_EXTENSION));
int result = chooser.showSaveDialog(Game.getScreenManager().getRenderComponent());
if (result == JFileChooser.APPROVE_OPTION) {
String newFile = XmlUtilities.save(map, chooser.getSelectedFile().toString(), Map.FILE_EXTENSION);
// save all tilesets manually because a map has a relative reference to
// the tilesets
String dir = FileUtilities.getParentDirPath(newFile);
for (ITileset tileSet : map.getTilesets()) {
ImageFormat format = ImageFormat.get(FileUtilities.getExtension(tileSet.getImage().getSource()));
ImageSerializer.saveImage(Paths.get(dir, tileSet.getImage().getSource()).toString(), Spritesheet.find(tileSet.getImage().getSource()).getImage(), format);
Tileset tile = (Tileset) tileSet;
if (tile.isExternal()) {
tile.saveSource(dir);
}
}
log.log(Level.INFO, "exported {0} to {1}", new Object[] { map.getFileName(), newFile });
}
} catch (IOException e) {
log.log(Level.SEVERE, e.getMessage(), e);
}
}
use of de.gurkenlabs.litiengine.environment.tilemap.ITileset in project litiengine by gurkenlabs.
the class AssetTree method loadAssetsOfCurrentSelection.
private void loadAssetsOfCurrentSelection(TreePath selectedPath) {
if (selectedPath == null) {
return;
}
final TreePath spritePath = new TreePath(this.nodeSpritesheets.getPath());
final TreePath propPath = new TreePath(this.nodeSpriteProps.getPath());
final TreePath creaturePath = new TreePath(this.nodeCreatures.getPath());
final TreePath miscPath = new TreePath(this.nodeSpriteMisc.getPath());
final TreePath tilesetPath = new TreePath(this.nodeTileSets.getPath());
final TreePath emitterPath = new TreePath(this.nodeEmitters.getPath());
final TreePath blueprintPath = new TreePath(this.nodeBlueprints.getPath());
final GameData gameFile = EditorScreen.instance().getGameFile();
if (selectedPath.equals(spritePath)) {
Program.getAssetPanel().loadSprites(gameFile.getSpriteSheets().stream().collect(Collectors.toList()));
} else if (this.getSelectionPath().equals(propPath)) {
Program.getAssetPanel().loadSprites(gameFile.getSpriteSheets().stream().filter(x -> x.getName() != null && x.getName().contains(Program.PROP_SPRITE_PREFIX)).collect(Collectors.toList()));
} else if (this.getSelectionPath().equals(creaturePath)) {
Program.getAssetPanel().loadSprites(gameFile.getSpriteSheets().stream().filter(x -> x.getName() != null && CreaturePanel.getCreatureSpriteName(x.getName()) != null).collect(Collectors.toList()));
} else if (selectedPath.equals(miscPath)) {
Program.getAssetPanel().loadSprites(gameFile.getSpriteSheets().stream().filter(x -> x.getName() != null && !x.getName().contains(Program.PROP_SPRITE_PREFIX) && CreaturePanel.getCreatureSpriteName(x.getName()) == null).collect(Collectors.toList()));
} else if (selectedPath.equals(tilesetPath)) {
ArrayList<Tileset> allTilesets = new ArrayList<>();
allTilesets.addAll(gameFile.getTilesets().stream().filter(x -> x.getName() != null).collect(Collectors.toList()));
for (Map map : gameFile.getMaps()) {
for (ITileset tileset : map.getTilesets()) {
if (allTilesets.stream().anyMatch(x -> x.getName() != null && x.getName().equals(tileset.getName()))) {
continue;
}
allTilesets.add((Tileset) tileset);
}
}
Program.getAssetPanel().loadTilesets(allTilesets);
} else if (selectedPath.equals(emitterPath)) {
Program.getAssetPanel().loadEmitters(gameFile.getEmitters());
} else if (selectedPath.equals(blueprintPath)) {
Program.getAssetPanel().loadBlueprints(gameFile.getBluePrints());
}
}
use of de.gurkenlabs.litiengine.environment.tilemap.ITileset in project litiengine by gurkenlabs.
the class Game method load.
public static void load(final String gameResourceFile) {
final GameData file = GameData.load(gameResourceFile);
if (file == null) {
return;
}
int mapCnt = 0;
for (final IMap m : file.getMaps()) {
if (getMaps().stream().anyMatch(x -> x.getFileName().equals(m.getFileName()))) {
continue;
}
getMaps().add(m);
mapCnt++;
}
log.log(Level.INFO, "{0} maps loaded from {1}", new Object[] { mapCnt, gameResourceFile });
int tileCnt = 0;
for (final ITileset tileset : file.getTilesets()) {
if (getTilesets().stream().anyMatch(x -> x.getName().equals(tileset.getName()))) {
continue;
}
getTilesets().add(tileset);
tileCnt++;
}
log.log(Level.INFO, "{0} tilesets loaded from {1}", new Object[] { tileCnt, gameResourceFile });
final List<Spritesheet> loadedSprites = new ArrayList<>();
for (final SpriteSheetInfo tileset : file.getSpriteSheets()) {
final Spritesheet sprite = Spritesheet.load(tileset);
loadedSprites.add(sprite);
}
log.log(Level.INFO, "{0} spritesheets loaded from {1}", new Object[] { loadedSprites.size(), gameResourceFile });
int spriteload = 0;
for (final Spritesheet s : loadedSprites) {
for (int i = 0; i < s.getRows() * s.getColumns(); i++) {
BufferedImage sprite = s.getSprite(i);
if (sprite != null) {
spriteload++;
}
}
}
log.log(Level.INFO, "{0} sprites loaded to memory", new Object[] { spriteload });
}
Aggregations