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