Search in sources :

Example 11 with Spritesheet

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

the class AssetPanel method loadSprites.

public void loadSprites(List<SpriteSheetInfo> infos) {
    this.load(infos, () -> {
        Collections.sort(infos);
        for (SpriteSheetInfo info : infos) {
            Icon icon;
            Spritesheet sprite = Spritesheet.find(info.getName());
            if (sprite == null || sprite.getSprite(0) == null) {
                icon = null;
            } else {
                icon = new ImageIcon(ImageProcessing.scaleImage(sprite.getSprite(0), 64, 64, true));
            }
            AssetPanelItem panelItem = new AssetPanelItem(icon, info.getName(), info);
            this.add(panelItem);
            panelItem.validate();
        }
    });
}
Also used : ImageIcon(javax.swing.ImageIcon) SpriteSheetInfo(de.gurkenlabs.litiengine.SpriteSheetInfo) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) Icon(javax.swing.Icon) ImageIcon(javax.swing.ImageIcon)

Example 12 with Spritesheet

use of de.gurkenlabs.litiengine.graphics.Spritesheet 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)

Example 13 with Spritesheet

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

the class EditorScreen method importSpriteFile.

public void importSpriteFile() {
    JFileChooser chooser;
    try {
        chooser = new JFileChooser(new File(this.getProjectPath()).getCanonicalPath());
        FileFilter filter = new FileNameExtensionFilter(SPRITE_FILE_NAME, SpriteSheetInfo.PLAIN_TEXT_FILE_EXTENSION);
        chooser.setFileFilter(filter);
        chooser.addChoosableFileFilter(filter);
        if (chooser.showOpenDialog(Game.getScreenManager().getRenderComponent()) == JFileChooser.APPROVE_OPTION) {
            File spriteFile = chooser.getSelectedFile();
            if (spriteFile == null) {
                return;
            }
            List<Spritesheet> loaded = Spritesheet.load(spriteFile.toString());
            List<SpriteSheetInfo> infos = new ArrayList<>();
            for (Spritesheet sprite : loaded) {
                SpriteSheetInfo info = new SpriteSheetInfo(sprite);
                infos.add(info);
                this.getGameFile().getSpriteSheets().removeIf(x -> x.getName().equals(info.getName()));
                this.getGameFile().getSpriteSheets().add(info);
            }
            this.loadSpriteSheets(infos, true);
        }
    } catch (IOException e) {
        log.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
}
Also used : SpriteSheetInfo(de.gurkenlabs.litiengine.SpriteSheetInfo) JFileChooser(javax.swing.JFileChooser) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileFilter(javax.swing.filechooser.FileFilter) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) File(java.io.File)

Example 14 with Spritesheet

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

the class PropPanel method loadAvailableProps.

private void loadAvailableProps() {
    if (this.propsLoaded) {
        return;
    }
    Map<String, String> m = new TreeMap<>();
    for (Spritesheet s : Spritesheet.getSpritesheets()) {
        String spriteName = s.getName();
        String propName = getNameBySpriteName(spriteName);
        if (propName == null) {
            continue;
        }
        if (!m.containsKey(propName)) {
            m.put(propName, spriteName);
        }
    }
    this.comboBoxSpriteSheets.removeAllItems();
    for (String key : m.keySet()) {
        JLabel label = new JLabel();
        label.setText(key);
        String value = m.get(key);
        Spritesheet sprite = Spritesheet.find(value);
        if (sprite != null && sprite.getTotalNumberOfSprites() > 0) {
            BufferedImage img = sprite.getSprite(0);
            BufferedImage scaled;
            String cacheKey = "iconx24" + sprite.getName();
            if (ImageCache.SPRITES.containsKey(cacheKey)) {
                scaled = ImageCache.SPRITES.get(cacheKey);
            } else {
                if (img != null) {
                    scaled = ImageProcessing.scaleImage(img, 24, 24, true);
                } else {
                    scaled = ImageProcessing.getCompatibleImage(24, 24);
                }
                ImageCache.SPRITES.put(cacheKey, scaled);
            }
            if (scaled != null) {
                label.setIcon(new ImageIcon(scaled));
            }
        }
        this.comboBoxSpriteSheets.addItem(label);
    }
    this.propsLoaded = true;
}
Also used : ImageIcon(javax.swing.ImageIcon) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) JLabel(javax.swing.JLabel) TreeMap(java.util.TreeMap) BufferedImage(java.awt.image.BufferedImage)

Example 15 with Spritesheet

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

the class IconTreeListRenderer method getIcon.

private static Icon getIcon(Prop prop) {
    String cacheKey = Game.getEnvironment().getMap().getName() + "-" + prop.getSpritesheetName().toLowerCase() + "-tree";
    BufferedImage propImag;
    if (ImageCache.IMAGES.containsKey(cacheKey)) {
        propImag = ImageCache.IMAGES.get(cacheKey);
    } else {
        final String name = Program.PROP_SPRITE_PREFIX + prop.getSpritesheetName().toLowerCase() + "-" + PropState.INTACT.toString().toLowerCase();
        final String fallbackName = Program.PROP_SPRITE_PREFIX + prop.getSpritesheetName().toLowerCase();
        Spritesheet sprite = Spritesheet.find(name);
        if (sprite == null) {
            sprite = Spritesheet.find(fallbackName);
        }
        if (sprite == null || sprite.getSprite(0) == null) {
            return null;
        }
        propImag = ImageProcessing.scaleImage(sprite.getSprite(0), 16, 16, true);
        ImageCache.IMAGES.put(cacheKey, propImag);
    }
    return new ImageIcon(propImag);
}
Also used : ImageIcon(javax.swing.ImageIcon) Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) BufferedImage(java.awt.image.BufferedImage)

Aggregations

Spritesheet (de.gurkenlabs.litiengine.graphics.Spritesheet)17 BufferedImage (java.awt.image.BufferedImage)9 SpriteSheetInfo (de.gurkenlabs.litiengine.SpriteSheetInfo)6 ImageIcon (javax.swing.ImageIcon)4 ITileset (de.gurkenlabs.litiengine.environment.tilemap.ITileset)3 Blueprint (de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint)3 ImageFormat (de.gurkenlabs.litiengine.graphics.ImageFormat)3 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 JFileChooser (javax.swing.JFileChooser)3 FileFilter (javax.swing.filechooser.FileFilter)3 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)3 Game (de.gurkenlabs.litiengine.Game)2 Resources (de.gurkenlabs.litiengine.Resources)2 IImageLayer (de.gurkenlabs.litiengine.environment.tilemap.IImageLayer)2 IMap (de.gurkenlabs.litiengine.environment.tilemap.IMap)2 Map (de.gurkenlabs.litiengine.environment.tilemap.xml.Map)2 MapObject (de.gurkenlabs.litiengine.environment.tilemap.xml.MapObject)2 Tileset (de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset)2