Search in sources :

Example 6 with Tileset

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;
}
Also used : Map(de.gurkenlabs.litiengine.environment.tilemap.xml.Map) Tileset(de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset)

Example 7 with Tileset

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() });
        }
    });
}
Also used : Color(java.awt.Color) Arrays(java.util.Arrays) Point2D(java.awt.geom.Point2D) Rectangle2D(java.awt.geom.Rectangle2D) MapObject(de.gurkenlabs.litiengine.environment.tilemap.xml.MapObject) MathUtilities(de.gurkenlabs.litiengine.util.MathUtilities) SpriteSheetInfo(de.gurkenlabs.litiengine.SpriteSheetInfo) Map(de.gurkenlabs.litiengine.environment.tilemap.xml.Map) Ellipse2D(java.awt.geom.Ellipse2D) UndoManager(de.gurkenlabs.utiliti.UndoManager) IMapLoader(de.gurkenlabs.litiengine.environment.tilemap.IMapLoader) ImageCache(de.gurkenlabs.litiengine.graphics.ImageCache) FileUtilities(de.gurkenlabs.litiengine.util.io.FileUtilities) MapObjectProperty(de.gurkenlabs.litiengine.environment.tilemap.MapObjectProperty) Tileset(de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset) JFileChooser(javax.swing.JFileChooser) Resources(de.gurkenlabs.litiengine.Resources) IImageLayer(de.gurkenlabs.litiengine.environment.tilemap.IImageLayer) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) CollisionEntity(de.gurkenlabs.litiengine.entities.CollisionEntity) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KeyEvent(java.awt.event.KeyEvent) Logger(java.util.logging.Logger) IEntity(de.gurkenlabs.litiengine.entities.IEntity) Program(de.gurkenlabs.utiliti.Program) ImageFormat(de.gurkenlabs.litiengine.graphics.ImageFormat) Dimension(java.awt.Dimension) List(java.util.List) Align(de.gurkenlabs.litiengine.Align) IMapObjectLayer(de.gurkenlabs.litiengine.environment.tilemap.IMapObjectLayer) XmlUtilities(de.gurkenlabs.litiengine.util.io.XmlUtilities) Entry(java.util.Map.Entry) ComponentMouseEvent(de.gurkenlabs.litiengine.gui.ComponentMouseEvent) Optional(java.util.Optional) BasicStroke(java.awt.BasicStroke) ImageSerializer(de.gurkenlabs.litiengine.util.io.ImageSerializer) Game(de.gurkenlabs.litiengine.Game) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) GeometricUtilities(de.gurkenlabs.litiengine.util.geom.GeometricUtilities) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) EditorScreen(de.gurkenlabs.utiliti.EditorScreen) IMap(de.gurkenlabs.litiengine.environment.tilemap.IMap) MapUtilities(de.gurkenlabs.litiengine.environment.tilemap.MapUtilities) IEnvironment(de.gurkenlabs.litiengine.environment.IEnvironment) IMapObject(de.gurkenlabs.litiengine.environment.tilemap.IMapObject) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint) FocusAdapter(java.awt.event.FocusAdapter) SwingUtilities(javax.swing.SwingUtilities) Graphics2D(java.awt.Graphics2D) MapObjectLayer(de.gurkenlabs.litiengine.environment.tilemap.xml.MapObjectLayer) LightSource(de.gurkenlabs.litiengine.graphics.LightSource) Stroke(java.awt.Stroke) Line2D(java.awt.geom.Line2D) Valign(de.gurkenlabs.litiengine.Valign) IOException(java.io.IOException) ComponentMouseWheelEvent(de.gurkenlabs.litiengine.gui.ComponentMouseWheelEvent) JOptionPane(javax.swing.JOptionPane) FileFilter(javax.swing.filechooser.FileFilter) File(java.io.File) TmxMapLoader(de.gurkenlabs.litiengine.environment.tilemap.TmxMapLoader) Path2D(java.awt.geom.Path2D) Input(de.gurkenlabs.litiengine.input.Input) Consumer(java.util.function.Consumer) FontMetrics(java.awt.FontMetrics) MapObjectType(de.gurkenlabs.litiengine.environment.tilemap.MapObjectType) Paths(java.nio.file.Paths) FocusEvent(java.awt.event.FocusEvent) GuiComponent(de.gurkenlabs.litiengine.gui.GuiComponent) Environment(de.gurkenlabs.litiengine.environment.Environment) RenderEngine(de.gurkenlabs.litiengine.graphics.RenderEngine) Collections(java.util.Collections) IUpdateable(de.gurkenlabs.litiengine.IUpdateable) XmlImportDialog(de.gurkenlabs.utiliti.swing.XmlImportDialog) TmxMapLoader(de.gurkenlabs.litiengine.environment.tilemap.TmxMapLoader) ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) IMapLoader(de.gurkenlabs.litiengine.environment.tilemap.IMapLoader) IImageLayer(de.gurkenlabs.litiengine.environment.tilemap.IImageLayer) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint) BufferedImage(java.awt.image.BufferedImage) SpriteSheetInfo(de.gurkenlabs.litiengine.SpriteSheetInfo) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) File(java.io.File) Map(de.gurkenlabs.litiengine.environment.tilemap.xml.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IMap(de.gurkenlabs.litiengine.environment.tilemap.IMap) IMapObjectLayer(de.gurkenlabs.litiengine.environment.tilemap.IMapObjectLayer) MapObjectLayer(de.gurkenlabs.litiengine.environment.tilemap.xml.MapObjectLayer)

Example 8 with Tileset

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);
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) IOException(java.io.IOException) FileFilter(javax.swing.filechooser.FileFilter) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) File(java.io.File) Tileset(de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset) ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint) ImageFormat(de.gurkenlabs.litiengine.graphics.ImageFormat)

Example 9 with Tileset

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());
    }
}
Also used : ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) Resources(de.gurkenlabs.litiengine.Resources) TreePath(javax.swing.tree.TreePath) EditorScreen(de.gurkenlabs.utiliti.EditorScreen) Icons(de.gurkenlabs.utiliti.Icons) JTree(javax.swing.JTree) Collectors(java.util.stream.Collectors) Map(de.gurkenlabs.litiengine.environment.tilemap.xml.Map) CreaturePanel(de.gurkenlabs.utiliti.swing.panels.CreaturePanel) ArrayList(java.util.ArrayList) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) GameData(de.gurkenlabs.litiengine.GameData) Program(de.gurkenlabs.utiliti.Program) Dimension(java.awt.Dimension) Tileset(de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) GameData(de.gurkenlabs.litiengine.GameData) TreePath(javax.swing.tree.TreePath) ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) ArrayList(java.util.ArrayList) ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) Tileset(de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset) Map(de.gurkenlabs.litiengine.environment.tilemap.xml.Map)

Example 10 with Tileset

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;
    }
}
Also used : ArrayList(java.util.ArrayList) Tileset(de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset)

Aggregations

Tileset (de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset)10 ITileset (de.gurkenlabs.litiengine.environment.tilemap.ITileset)5 Blueprint (de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint)4 Map (de.gurkenlabs.litiengine.environment.tilemap.xml.Map)4 File (java.io.File)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Resources (de.gurkenlabs.litiengine.Resources)3 ImageFormat (de.gurkenlabs.litiengine.graphics.ImageFormat)3 Game (de.gurkenlabs.litiengine.Game)2 GameData (de.gurkenlabs.litiengine.GameData)2 SpriteSheetInfo (de.gurkenlabs.litiengine.SpriteSheetInfo)2 IImageLayer (de.gurkenlabs.litiengine.environment.tilemap.IImageLayer)2 ImageCache (de.gurkenlabs.litiengine.graphics.ImageCache)2 RenderEngine (de.gurkenlabs.litiengine.graphics.RenderEngine)2 Spritesheet (de.gurkenlabs.litiengine.graphics.Spritesheet)2 EditorScreen (de.gurkenlabs.utiliti.EditorScreen)2 Program (de.gurkenlabs.utiliti.Program)2 Dimension (java.awt.Dimension)2 Point (java.awt.Point)2