Search in sources :

Example 1 with ImageFormat

use of de.gurkenlabs.litiengine.graphics.ImageFormat 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 2 with ImageFormat

use of de.gurkenlabs.litiengine.graphics.ImageFormat in project litiengine by gurkenlabs.

the class AssetPanelItem method exportSpritesheet.

private void exportSpritesheet() {
    if (this.getOrigin() instanceof SpriteSheetInfo) {
        SpriteSheetInfo spriteSheetInfo = (SpriteSheetInfo) this.getOrigin();
        Spritesheet sprite = Spritesheet.find(spriteSheetInfo.getName());
        if (sprite == null) {
            return;
        }
        ImageFormat format = sprite.getImageFormat() != ImageFormat.UNDEFINED ? sprite.getImageFormat() : ImageFormat.PNG;
        Object[] options = { ".xml", format.toExtension() };
        int answer = JOptionPane.showOptionDialog(Game.getScreenManager().getRenderComponent(), "Select an export format:", "Export Spritesheet", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
        try {
            JFileChooser chooser;
            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 Spritesheet");
            if (answer == 0) {
                XmlExportDialog.export(spriteSheetInfo, "Spritesheet", spriteSheetInfo.getName());
            } else if (answer == 1) {
                FileFilter filter = new FileNameExtensionFilter(format.toString() + " - Image", format.toString());
                chooser.setFileFilter(filter);
                chooser.addChoosableFileFilter(filter);
                chooser.setSelectedFile(new File(spriteSheetInfo.getName() + format.toExtension()));
                int result = chooser.showSaveDialog(Game.getScreenManager().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 : SpriteSheetInfo(de.gurkenlabs.litiengine.SpriteSheetInfo) JFileChooser(javax.swing.JFileChooser) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) MapObject(de.gurkenlabs.litiengine.environment.tilemap.xml.MapObject) 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.graphics.ImageFormat)

Aggregations

Blueprint (de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint)2 ImageFormat (de.gurkenlabs.litiengine.graphics.ImageFormat)2 File (java.io.File)2 IOException (java.io.IOException)2 JFileChooser (javax.swing.JFileChooser)2 FileFilter (javax.swing.filechooser.FileFilter)2 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)2 SpriteSheetInfo (de.gurkenlabs.litiengine.SpriteSheetInfo)1 ITileset (de.gurkenlabs.litiengine.environment.tilemap.ITileset)1 MapObject (de.gurkenlabs.litiengine.environment.tilemap.xml.MapObject)1 Tileset (de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset)1 Spritesheet (de.gurkenlabs.litiengine.graphics.Spritesheet)1