Search in sources :

Example 1 with ITileset

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() });
    }
}
Also used : Color(java.awt.Color) ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) EditorComponent(de.gurkenlabs.utiliti.components.EditorComponent) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) Point(java.awt.Point) ComponentType(de.gurkenlabs.utiliti.components.EditorComponent.ComponentType) MathUtilities(de.gurkenlabs.litiengine.util.MathUtilities) SpriteSheetInfo(de.gurkenlabs.litiengine.SpriteSheetInfo) Map(de.gurkenlabs.litiengine.environment.tilemap.xml.Map) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint) MapComponent(de.gurkenlabs.utiliti.components.MapComponent) Graphics2D(java.awt.Graphics2D) ImageCache(de.gurkenlabs.litiengine.graphics.ImageCache) FileUtilities(de.gurkenlabs.litiengine.util.io.FileUtilities) 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) EmitterData(de.gurkenlabs.litiengine.graphics.particles.xml.EmitterData) Collection(java.util.Collection) IOException(java.io.IOException) Logger(java.util.logging.Logger) JOptionPane(javax.swing.JOptionPane) FileFilter(javax.swing.filechooser.FileFilter) Collectors(java.util.stream.Collectors) MapObjectPanel(de.gurkenlabs.utiliti.swing.panels.MapObjectPanel) File(java.io.File) GameData(de.gurkenlabs.litiengine.GameData) Input(de.gurkenlabs.litiengine.input.Input) ImageFormat(de.gurkenlabs.litiengine.graphics.ImageFormat) List(java.util.List) CustomEmitter(de.gurkenlabs.litiengine.graphics.particles.xml.CustomEmitter) XmlUtilities(de.gurkenlabs.litiengine.util.io.XmlUtilities) Paths(java.nio.file.Paths) Screen(de.gurkenlabs.litiengine.gui.screens.Screen) RenderEngine(de.gurkenlabs.litiengine.graphics.RenderEngine) SpritesheetImportPanel(de.gurkenlabs.utiliti.swing.dialogs.SpritesheetImportPanel) Game(de.gurkenlabs.litiengine.Game) ScreenInfo(de.gurkenlabs.litiengine.annotation.ScreenInfo) XmlImportDialog(de.gurkenlabs.utiliti.swing.XmlImportDialog) SpriteSheetInfo(de.gurkenlabs.litiengine.SpriteSheetInfo) ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) ArrayList(java.util.ArrayList) IImageLayer(de.gurkenlabs.litiengine.environment.tilemap.IImageLayer) Point(java.awt.Point) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint) BufferedImage(java.awt.image.BufferedImage)

Example 2 with ITileset

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() });
        }
    });
}
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 3 with ITileset

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);
    }
}
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 4 with ITileset

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());
    }
}
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 5 with ITileset

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 });
}
Also used : IMap(de.gurkenlabs.litiengine.environment.tilemap.IMap) ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) BufferedImage(java.awt.image.BufferedImage)

Aggregations

ITileset (de.gurkenlabs.litiengine.environment.tilemap.ITileset)5 Tileset (de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset)4 ArrayList (java.util.ArrayList)4 Resources (de.gurkenlabs.litiengine.Resources)3 Blueprint (de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint)3 Map (de.gurkenlabs.litiengine.environment.tilemap.xml.Map)3 ImageFormat (de.gurkenlabs.litiengine.graphics.ImageFormat)3 Spritesheet (de.gurkenlabs.litiengine.graphics.Spritesheet)3 BufferedImage (java.awt.image.BufferedImage)3 File (java.io.File)3 IOException (java.io.IOException)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 IMap (de.gurkenlabs.litiengine.environment.tilemap.IMap)2 ImageCache (de.gurkenlabs.litiengine.graphics.ImageCache)2 EditorScreen (de.gurkenlabs.utiliti.EditorScreen)2 Program (de.gurkenlabs.utiliti.Program)2 Dimension (java.awt.Dimension)2