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();
}
});
}
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);
}
}
}
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);
}
}
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;
}
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);
}
Aggregations