Search in sources :

Example 1 with IImageLayer

use of de.gurkenlabs.litiengine.environment.tilemap.IImageLayer 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 IImageLayer

use of de.gurkenlabs.litiengine.environment.tilemap.IImageLayer in project litiengine by gurkenlabs.

the class Editor method loadSpriteSheets.

private void loadSpriteSheets(TmxMap map) {
    List<SpritesheetResource> infos = new ArrayList<>();
    int cnt = 0;
    for (ITileset tileSet : map.getTilesets()) {
        if (tileSet.getImage() == null) {
            continue;
        }
        Spritesheet opt = Resources.spritesheets().get(tileSet.getImage().getSource());
        Spritesheet sprite = null;
        if (opt == null) {
            sprite = Resources.spritesheets().load(tileSet);
            if (sprite == null) {
                continue;
            }
        } else {
            sprite = opt;
        }
        infos.add(new SpritesheetResource(sprite));
        cnt++;
    }
    for (IImageLayer imageLayer : map.getImageLayers()) {
        Spritesheet opt = Resources.spritesheets().get(imageLayer.getImage().getSource());
        Spritesheet sprite = null;
        if (opt == null) {
            BufferedImage img = Resources.images().get(imageLayer.getImage().getAbsoluteSourcePath(), true);
            if (img == null) {
                continue;
            }
            sprite = Resources.spritesheets().load(img, imageLayer.getImage().getSource(), img.getWidth(), img.getHeight());
            if (sprite == null) {
                continue;
            }
        } else {
            sprite = opt;
        }
        SpritesheetResource info = new SpritesheetResource(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 (SpritesheetResource info : infos) {
        if (this.getGameFile().getSpriteSheets().stream().noneMatch(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.getName() });
    }
}
Also used : SpritesheetResource(de.gurkenlabs.litiengine.resources.SpritesheetResource) ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) IImageLayer(de.gurkenlabs.litiengine.environment.tilemap.IImageLayer) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint) BufferedImage(java.awt.image.BufferedImage)

Example 3 with IImageLayer

use of de.gurkenlabs.litiengine.environment.tilemap.IImageLayer in project litiengine by gurkenlabs.

the class MapComponent method importMap.

public void importMap() {
    if (this.getMaps() == null) {
        return;
    }
    XmlImportDialog.importXml("Tilemap", file -> {
        String mapPath = file.toURI().toString();
        Resources.maps().clear();
        TmxMap map = (TmxMap) Resources.maps().get(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(MapObjectLayer.DEFAULT_MAPOBJECTLAYER_NAME);
            map.addLayer(layer);
        }
        Optional<TmxMap> current = this.maps.stream().filter(x -> x.getName().equals(map.getName())).findFirst();
        if (current.isPresent()) {
            if (ConfirmDialog.show(Resources.strings().get("input_replace_map_title"), Resources.strings().get("input_replace_map", map.getName()))) {
                this.getMaps().remove(current.get());
            } else {
                return;
            }
        }
        this.getMaps().add(map);
        Collections.sort(this.getMaps());
        for (IImageLayer imageLayer : map.getImageLayers()) {
            BufferedImage img = Resources.images().get(imageLayer.getImage().getAbsoluteSourcePath(), true);
            if (img == null) {
                continue;
            }
            Spritesheet sprite = Resources.spritesheets().load(img, imageLayer.getImage().getSource(), img.getWidth(), img.getHeight());
            Editor.instance().getGameFile().getSpriteSheets().add(new SpritesheetResource(sprite));
        }
        // remove old spritesheets
        for (ITileset tileSet : map.getTilesets()) {
            Editor.instance().loadTileset(tileSet, true);
        }
        // remove old tilesets
        for (ITileset tileset : map.getExternalTilesets()) {
            Editor.instance().loadTileset(tileset, false);
        }
        Editor.instance().updateGameFileMaps();
        Resources.images().clear();
        Renderers.get(GridRenderer.class).clearCache();
        if (this.environments.containsKey(map.getName())) {
            this.environments.remove(map.getName());
        }
        UI.getMapController().bind(this.getMaps(), true);
        this.loadEnvironment(map);
        log.log(Level.INFO, "imported map {0}", new Object[] { map.getName() });
    }, TmxMap.FILE_EXTENSION);
}
Also used : Color(java.awt.Color) Point2D(java.awt.geom.Point2D) Date(java.util.Date) Rectangle2D(java.awt.geom.Rectangle2D) IntConsumer(java.util.function.IntConsumer) MapObject(de.gurkenlabs.litiengine.environment.tilemap.xml.MapObject) Zoom(de.gurkenlabs.utiliti.handlers.Zoom) MathUtilities(de.gurkenlabs.litiengine.util.MathUtilities) UndoManager(de.gurkenlabs.utiliti.UndoManager) EmitterData(de.gurkenlabs.litiengine.graphics.emitters.xml.EmitterData) FileUtilities(de.gurkenlabs.litiengine.util.io.FileUtilities) GridRenderer(de.gurkenlabs.utiliti.renderers.GridRenderer) Map(java.util.Map) MapObjectProperty(de.gurkenlabs.litiengine.environment.tilemap.MapObjectProperty) Tileset(de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset) Transform(de.gurkenlabs.utiliti.handlers.Transform) Snap(de.gurkenlabs.utiliti.handlers.Snap) IImageLayer(de.gurkenlabs.litiengine.environment.tilemap.IImageLayer) BufferedImage(java.awt.image.BufferedImage) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TransformType(de.gurkenlabs.utiliti.handlers.Transform.TransformType) KeyEvent(java.awt.event.KeyEvent) Logger(java.util.logging.Logger) Dimension(java.awt.Dimension) List(java.util.List) IMapObjectLayer(de.gurkenlabs.litiengine.environment.tilemap.IMapObjectLayer) Cursors(de.gurkenlabs.utiliti.Cursors) ComponentMouseEvent(de.gurkenlabs.litiengine.gui.ComponentMouseEvent) XmlExportDialog(de.gurkenlabs.utiliti.swing.dialogs.XmlExportDialog) Optional(java.util.Optional) 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) SpritesheetResource(de.gurkenlabs.litiengine.resources.SpritesheetResource) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) SimpleDateFormat(java.text.SimpleDateFormat) 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) Collision(de.gurkenlabs.litiengine.physics.Collision) ConfirmDialog(de.gurkenlabs.utiliti.swing.dialogs.ConfirmDialog) EmitterMapObjectLoader(de.gurkenlabs.litiengine.environment.EmitterMapObjectLoader) Graphics2D(java.awt.Graphics2D) Scroll(de.gurkenlabs.utiliti.handlers.Scroll) ImageFormat(de.gurkenlabs.litiengine.resources.ImageFormat) Renderers(de.gurkenlabs.utiliti.renderers.Renderers) MapObjectLayer(de.gurkenlabs.litiengine.environment.tilemap.xml.MapObjectLayer) XmlImportDialog(de.gurkenlabs.utiliti.swing.dialogs.XmlImportDialog) UI(de.gurkenlabs.utiliti.swing.UI) Emitter(de.gurkenlabs.litiengine.graphics.emitters.Emitter) TmxMap(de.gurkenlabs.litiengine.environment.tilemap.xml.TmxMap) ComponentMouseWheelEvent(de.gurkenlabs.litiengine.gui.ComponentMouseWheelEvent) Resources(de.gurkenlabs.litiengine.resources.Resources) JOptionPane(javax.swing.JOptionPane) MouseEvent(java.awt.event.MouseEvent) File(java.io.File) Input(de.gurkenlabs.litiengine.input.Input) Consumer(java.util.function.Consumer) 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) Collections(java.util.Collections) MapRenderer(de.gurkenlabs.litiengine.environment.tilemap.MapRenderer) SpritesheetResource(de.gurkenlabs.litiengine.resources.SpritesheetResource) TmxMap(de.gurkenlabs.litiengine.environment.tilemap.xml.TmxMap) ITileset(de.gurkenlabs.litiengine.environment.tilemap.ITileset) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) IImageLayer(de.gurkenlabs.litiengine.environment.tilemap.IImageLayer) GridRenderer(de.gurkenlabs.utiliti.renderers.GridRenderer) IMapObjectLayer(de.gurkenlabs.litiengine.environment.tilemap.IMapObjectLayer) MapObjectLayer(de.gurkenlabs.litiengine.environment.tilemap.xml.MapObjectLayer) BufferedImage(java.awt.image.BufferedImage)

Aggregations

IImageLayer (de.gurkenlabs.litiengine.environment.tilemap.IImageLayer)3 ITileset (de.gurkenlabs.litiengine.environment.tilemap.ITileset)3 Blueprint (de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint)3 Spritesheet (de.gurkenlabs.litiengine.graphics.Spritesheet)3 Game (de.gurkenlabs.litiengine.Game)2 Tileset (de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset)2 Input (de.gurkenlabs.litiengine.input.Input)2 SpritesheetResource (de.gurkenlabs.litiengine.resources.SpritesheetResource)2 MathUtilities (de.gurkenlabs.litiengine.util.MathUtilities)2 FileUtilities (de.gurkenlabs.litiengine.util.io.FileUtilities)2 BufferedImage (java.awt.image.BufferedImage)2 ArrayList (java.util.ArrayList)2 GameData (de.gurkenlabs.litiengine.GameData)1 Resources (de.gurkenlabs.litiengine.Resources)1 SpriteSheetInfo (de.gurkenlabs.litiengine.SpriteSheetInfo)1 ScreenInfo (de.gurkenlabs.litiengine.annotation.ScreenInfo)1 EmitterMapObjectLoader (de.gurkenlabs.litiengine.environment.EmitterMapObjectLoader)1 Environment (de.gurkenlabs.litiengine.environment.Environment)1 IMapObject (de.gurkenlabs.litiengine.environment.tilemap.IMapObject)1 IMapObjectLayer (de.gurkenlabs.litiengine.environment.tilemap.IMapObjectLayer)1