use of de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset in project litiengine by gurkenlabs.
the class TmxMapLoader method loadMap.
@Override
public IMap loadMap(final String path) {
final Map map = XmlUtilities.readFromFile(Map.class, path);
if (map == null) {
return null;
}
String basePath = FileUtilities.getParentDirPath(path);
for (Tileset tilesets : map.getRawTileSets()) {
tilesets.loadFromSource(basePath);
}
// by default the map is named by the source file
String name = path;
final int pos = name.lastIndexOf('.');
if (pos > 0) {
name = name.substring(0, pos);
}
int lastBackslash = name.lastIndexOf('/');
if (lastBackslash != -1) {
name = name.substring(lastBackslash + 1, name.length());
} else {
int lastForwardSlash = name.lastIndexOf('\\');
if (lastForwardSlash != -1) {
name = name.substring(lastForwardSlash + 1, name.length());
}
}
map.setFileName(name);
map.setPath(path);
map.updateTileTerrain();
return map;
}
use of de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset 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.xml.Tileset 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.xml.Tileset 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.xml.Tileset in project litiengine by gurkenlabs.
the class GameData method beforeMarshal.
void beforeMarshal(Marshaller m) {
List<SpriteSheetInfo> distinctList = new ArrayList<>();
for (SpriteSheetInfo sprite : this.getSpriteSheets()) {
if (distinctList.stream().anyMatch(x -> x.getName().equals(sprite.getName()) && x.getImage().equals(sprite.getImage()))) {
continue;
}
distinctList.add(sprite);
}
this.spriteSheets = distinctList;
List<Tileset> distinctTilesets = new ArrayList<>();
for (Tileset tileset : this.getTilesets()) {
if (distinctTilesets.stream().anyMatch(x -> x.getName().equals(tileset.getName()))) {
continue;
}
distinctTilesets.add(tileset);
}
this.tilesets = distinctTilesets;
if (this.version == 0) {
this.version = CURRENT_VERSION;
}
}
Aggregations