Search in sources :

Example 6 with SpritesheetResource

use of de.gurkenlabs.litiengine.resources.SpritesheetResource 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)

Example 7 with SpritesheetResource

use of de.gurkenlabs.litiengine.resources.SpritesheetResource in project litiengine by gurkenlabs.

the class AssetPanelItem method addEntity.

private boolean addEntity() {
    if (Game.world().environment() == null || Game.world().camera() == null) {
        return false;
    }
    if (this.getOrigin() instanceof SpritesheetResource) {
        SpritesheetResource info = (SpritesheetResource) this.getOrigin();
        MapObject mo = new MapObject();
        String propName = PropPanel.getIdentifierBySpriteName(info.getName());
        String creatureName = CreaturePanel.getCreatureSpriteName(info.getName());
        if (propName != null) {
            mo.setType(MapObjectType.PROP.name());
            mo.setValue(MapObjectProperty.SPRITESHEETNAME, propName);
        } else if (creatureName != null) {
            mo.setType(MapObjectType.CREATURE.name());
            mo.setValue(MapObjectProperty.SPRITESHEETNAME, creatureName);
        } else {
            return false;
        }
        mo.setX((int) Game.world().camera().getFocus().getX() - info.getWidth() / 2f);
        mo.setY((int) Game.world().camera().getFocus().getY() - info.getHeight() / 2f);
        mo.setWidth(info.getWidth());
        mo.setHeight(info.getHeight());
        mo.setId(Game.world().environment().getNextMapId());
        mo.setName("");
        mo.setValue(MapObjectProperty.COLLISIONBOX_WIDTH, info.getWidth() * 0.4);
        mo.setValue(MapObjectProperty.COLLISIONBOX_HEIGHT, info.getHeight() * 0.4);
        mo.setValue(MapObjectProperty.COLLISION, true);
        mo.setValue(MapObjectProperty.COMBAT_INDESTRUCTIBLE, false);
        mo.setValue(MapObjectProperty.PROP_ADDSHADOW, true);
        Editor.instance().getMapComponent().add(mo);
        return true;
    } else if (this.getOrigin() instanceof EmitterData) {
        MapObject newEmitter = (MapObject) EmitterMapObjectLoader.createMapObject((EmitterData) this.getOrigin());
        newEmitter.setX((int) (Game.world().camera().getFocus().getX() - newEmitter.getWidth()));
        newEmitter.setY((int) (Game.world().camera().getFocus().getY() - newEmitter.getHeight()));
        newEmitter.setId(Game.world().environment().getNextMapId());
        Editor.instance().getMapComponent().add(newEmitter);
    } else if (this.getOrigin() instanceof Blueprint) {
        Blueprint blueprint = (Blueprint) this.getOrigin();
        UndoManager.instance().beginOperation();
        try {
            List<IMapObject> newObjects = blueprint.build((int) Game.world().camera().getFocus().getX() - blueprint.getWidth() / 2, (int) Game.world().camera().getFocus().getY() - blueprint.getHeight() / 2);
            for (IMapObject newMapObject : newObjects) {
                Editor.instance().getMapComponent().add(newMapObject);
            }
            // gets added
            for (IMapObject newMapObject : newObjects) {
                Editor.instance().getMapComponent().setSelection(newMapObject, false);
            }
        } finally {
            UndoManager.instance().endOperation();
        }
    }
    return false;
}
Also used : SpritesheetResource(de.gurkenlabs.litiengine.resources.SpritesheetResource) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint) IMapObject(de.gurkenlabs.litiengine.environment.tilemap.IMapObject) MapObject(de.gurkenlabs.litiengine.environment.tilemap.xml.MapObject) IMapObject(de.gurkenlabs.litiengine.environment.tilemap.IMapObject) EmitterData(de.gurkenlabs.litiengine.graphics.emitters.xml.EmitterData)

Example 8 with SpritesheetResource

use of de.gurkenlabs.litiengine.resources.SpritesheetResource in project litiengine by gurkenlabs.

the class AssetPanelItem method deleteAsset.

private void deleteAsset() {
    if (getOrigin() instanceof SpritesheetResource) {
        SpritesheetResource info = (SpritesheetResource) getOrigin();
        int n = getDeleteDialog("spritesheet", info.getName());
        if (n == JOptionPane.OK_OPTION) {
            Editor.instance().getGameFile().getSpriteSheets().remove(getOrigin());
            Resources.images().clear();
            Resources.spritesheets().remove(info.getName());
            Editor.instance().getMapComponent().reloadEnvironment();
            UI.getAssetController().refresh();
        }
    } else if (getOrigin() instanceof EmitterData) {
        EmitterData emitter = (EmitterData) getOrigin();
        int n = getDeleteDialog("emitter", emitter.getName());
        if (n == JOptionPane.OK_OPTION) {
            Editor.instance().getGameFile().getEmitters().remove(getOrigin());
            Editor.instance().getMapComponent().reloadEnvironment();
            UI.getAssetController().refresh();
        }
    } else if (getOrigin() instanceof Blueprint) {
        Blueprint blueprint = (Blueprint) getOrigin();
        int n = getDeleteDialog("blueprint", blueprint.getName());
        if (n == JOptionPane.OK_OPTION) {
            Editor.instance().getGameFile().getBluePrints().remove(getOrigin());
            UI.getAssetController().refresh();
        }
    } else if (getOrigin() instanceof SoundResource) {
        SoundResource sound = (SoundResource) getOrigin();
        int n = getDeleteDialog("sound", sound.getName());
        if (n == JOptionPane.OK_OPTION) {
            Editor.instance().getGameFile().getSounds().remove(getOrigin());
            UI.getAssetController().refresh();
        }
    }
}
Also used : SpritesheetResource(de.gurkenlabs.litiengine.resources.SpritesheetResource) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint) SoundResource(de.gurkenlabs.litiengine.resources.SoundResource) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint) EmitterData(de.gurkenlabs.litiengine.graphics.emitters.xml.EmitterData)

Example 9 with SpritesheetResource

use of de.gurkenlabs.litiengine.resources.SpritesheetResource in project litiengine by gurkenlabs.

the class AssetPanelItem method exportSpritesheet.

private void exportSpritesheet() {
    if (this.getOrigin() instanceof SpritesheetResource) {
        SpritesheetResource spriteSheetInfo = (SpritesheetResource) this.getOrigin();
        Spritesheet sprite = Resources.spritesheets().get(spriteSheetInfo.getName());
        if (sprite == null) {
            return;
        }
        ImageFormat format = sprite.getImageFormat() != ImageFormat.UNSUPPORTED ? sprite.getImageFormat() : ImageFormat.PNG;
        Object[] options = { ".xml", format.toFileExtension() };
        int answer = JOptionPane.showOptionDialog(Game.window().getRenderComponent(), "Select an export format:", "Export Spritesheet", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
        try {
            JFileChooser chooser;
            String source = Editor.instance().getProjectPath();
            chooser = new JFileChooser(source != null ? source : new File(".").getCanonicalPath());
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            chooser.setDialogType(JFileChooser.SAVE_DIALOG);
            chooser.setDialogTitle("Export Spritesheet");
            if (answer == 0) {
                XmlExportDialog.export(spriteSheetInfo, "Spritesheet", spriteSheetInfo.getName());
            } else if (answer == 1) {
                FileFilter filter = new FileNameExtensionFilter(format + " - Image", format.toString());
                chooser.setFileFilter(filter);
                chooser.addChoosableFileFilter(filter);
                chooser.setSelectedFile(new File(spriteSheetInfo.getName() + format.toFileExtension()));
                int result = chooser.showSaveDialog(Game.window().getRenderComponent());
                if (result == JFileChooser.APPROVE_OPTION) {
                    ImageSerializer.saveImage(chooser.getSelectedFile().toString(), sprite.getImage(), format);
                    log.log(Level.INFO, "exported spritesheet {0} to {1}", new Object[] { spriteSheetInfo.getName(), chooser.getSelectedFile() });
                }
            }
        } catch (IOException e) {
            log.log(Level.SEVERE, e.getMessage(), e);
        }
    }
}
Also used : SpritesheetResource(de.gurkenlabs.litiengine.resources.SpritesheetResource) JFileChooser(javax.swing.JFileChooser) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) MapObject(de.gurkenlabs.litiengine.environment.tilemap.xml.MapObject) IMapObject(de.gurkenlabs.litiengine.environment.tilemap.IMapObject) IOException(java.io.IOException) FileFilter(javax.swing.filechooser.FileFilter) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) File(java.io.File) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint) ImageFormat(de.gurkenlabs.litiengine.resources.ImageFormat)

Example 10 with SpritesheetResource

use of de.gurkenlabs.litiengine.resources.SpritesheetResource in project litiengine by gurkenlabs.

the class AssetPanelItem method editAsset.

private void editAsset() {
    if (!(this.getOrigin() instanceof SpritesheetResource)) {
        return;
    }
    SpritesheetImportPanel spritePanel = new SpritesheetImportPanel((SpritesheetResource) this.getOrigin());
    int option = JOptionPane.showConfirmDialog(Game.window().getRenderComponent(), spritePanel, Resources.strings().get("menu_assets_editSprite"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    if (option != JOptionPane.OK_OPTION) {
        return;
    }
    final Collection<SpritesheetResource> sprites = spritePanel.getSpriteSheets();
    for (SpritesheetResource spriteFile : sprites) {
        int index = -1;
        Optional<SpritesheetResource> old = Editor.instance().getGameFile().getSpriteSheets().stream().filter((x -> x.getName().equals(spriteFile.getName()))).findFirst();
        if (old.isPresent()) {
            index = Editor.instance().getGameFile().getSpriteSheets().indexOf(old.get());
            Editor.instance().getGameFile().getSpriteSheets().remove(index);
        }
        Editor.instance().getGameFile().getSpriteSheets().removeIf(x -> x.getName().equals(spriteFile.getName()));
        if (index != -1) {
            Editor.instance().getGameFile().getSpriteSheets().add(index, spriteFile);
        } else {
            Editor.instance().getGameFile().getSpriteSheets().add(spriteFile);
        }
    }
    Editor.instance().loadSpriteSheets(Editor.instance().getGameFile().getSpriteSheets(), true);
}
Also used : SpritesheetResource(de.gurkenlabs.litiengine.resources.SpritesheetResource) SpritesheetImportPanel(de.gurkenlabs.utiliti.swing.dialogs.SpritesheetImportPanel) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint)

Aggregations

SpritesheetResource (de.gurkenlabs.litiengine.resources.SpritesheetResource)12 Blueprint (de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint)7 Spritesheet (de.gurkenlabs.litiengine.graphics.Spritesheet)6 IMapObject (de.gurkenlabs.litiengine.environment.tilemap.IMapObject)4 MapObject (de.gurkenlabs.litiengine.environment.tilemap.xml.MapObject)4 EmitterData (de.gurkenlabs.litiengine.graphics.emitters.xml.EmitterData)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 IImageLayer (de.gurkenlabs.litiengine.environment.tilemap.IImageLayer)2 ITileset (de.gurkenlabs.litiengine.environment.tilemap.ITileset)2 ImageFormat (de.gurkenlabs.litiengine.resources.ImageFormat)2 Game (de.gurkenlabs.litiengine.Game)1 EmitterMapObjectLoader (de.gurkenlabs.litiengine.environment.EmitterMapObjectLoader)1 Environment (de.gurkenlabs.litiengine.environment.Environment)1 IMapObjectLayer (de.gurkenlabs.litiengine.environment.tilemap.IMapObjectLayer)1 MapObjectProperty (de.gurkenlabs.litiengine.environment.tilemap.MapObjectProperty)1 MapObjectType (de.gurkenlabs.litiengine.environment.tilemap.MapObjectType)1 MapRenderer (de.gurkenlabs.litiengine.environment.tilemap.MapRenderer)1 MapObjectLayer (de.gurkenlabs.litiengine.environment.tilemap.xml.MapObjectLayer)1