Permalink
Browse files

Add save buttons to the background and sprite frames.

  • Loading branch information...
RobertBColton committed Nov 10, 2014
1 parent c545305 commit 337081b85d4b212c51d12f2bcf5261bef0e14224
@@ -29,6 +29,7 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
@@ -181,6 +182,36 @@ public int filterRGB(int x, int y, int rgb)
return toBufferedImage(Toolkit.getDefaultToolkit().createImage(ip));
}
private static void createImageChooser() {
imageFc = new CustomFileChooser("/org/lateralgm","LAST_IMAGE_DIR");
imageFc.setAccessory(new FileChooserImagePreview(imageFc));
String[] exts = { "jpg","bmp","tif","jpeg","wbmp","apng","png","ico","TIF","TIFF","gif","tiff" };
if (LGM.javaVersion >= 10600) {
String[] internalexts = ImageIO.getReaderFileSuffixes();
ArrayList<String> extensions = new ArrayList<String>();
for (String ext : exts) {
extensions.add(ext);
}
for (String ext : internalexts) {
if (!extensions.contains(ext)) {
extensions.add(ext);
}
}
exts = extensions.toArray(new String[extensions.size()]);
}
for (int i = 0; i < exts.length; i++)
exts[i] = "." + exts[i]; //$NON-NLS-1$
String allSpiImages = Messages.getString("Util.ALL_SPI_IMAGES"); //$NON-NLS-1$
CustomFileFilter filt = new CustomFileFilter(allSpiImages,exts);
imageFc.addChoosableFileFilter(filt);
for (String element : exts)
{
imageFc.addChoosableFileFilter(new CustomFileFilter(Messages.format("Util.FILES", //$NON-NLS-1$
element),element));
}
imageFc.setFileFilter(filt);
}
/**
* Shows a JFileChooser with file filters for all currently registered instances of
* ImageReaderSpi with multiple file selection.
@@ -190,41 +221,15 @@ public int filterRGB(int x, int y, int rgb)
public static File chooseImageFile()
{
if (imageFc == null)
{
imageFc = new CustomFileChooser("/org/lateralgm","LAST_IMAGE_DIR");
imageFc.setAccessory(new FileChooserImagePreview(imageFc));
String[] exts = { "jpg","bmp","tif","jpeg","wbmp","apng","png","ico","TIF","TIFF","gif","tiff" };
if (LGM.javaVersion >= 10600) {
String[] internalexts = ImageIO.getReaderFileSuffixes();
ArrayList<String> extensions = new ArrayList<String>();
for (String ext : exts) {
extensions.add(ext);
}
for (String ext : internalexts) {
if (!extensions.contains(ext)) {
extensions.add(ext);
}
}
exts = extensions.toArray(new String[extensions.size()]);
}
for (int i = 0; i < exts.length; i++)
exts[i] = "." + exts[i]; //$NON-NLS-1$
String allSpiImages = Messages.getString("Util.ALL_SPI_IMAGES"); //$NON-NLS-1$
CustomFileFilter filt = new CustomFileFilter(allSpiImages,exts);
imageFc.addChoosableFileFilter(filt);
for (String element : exts)
{
imageFc.addChoosableFileFilter(new CustomFileFilter(Messages.format("Util.FILES", //$NON-NLS-1$
element),element));
}
imageFc.setFileFilter(filt);
}
{
createImageChooser();
}
imageFc.setMultiSelectionEnabled(false);
if (imageFc.showOpenDialog(LGM.frame) == JFileChooser.APPROVE_OPTION)
return imageFc.getSelectedFile();
return null;
}
/**
* Shows a JFileChooser with file filters for all currently registered instances of
* ImageReaderSpi with multiple file selection.
@@ -235,40 +240,104 @@ public static File chooseImageFile()
{
if (imageFc == null)
{
imageFc = new CustomFileChooser("/org/lateralgm","LAST_IMAGE_DIR");
imageFc.setAccessory(new FileChooserImagePreview(imageFc));
String[] exts = { "jpg","bmp","tif","jpeg","wbmp","apng","png","ico","TIF","TIFF","gif","tiff" };
if (LGM.javaVersion >= 10600) {
String[] internalexts = ImageIO.getReaderFileSuffixes();
ArrayList<String> extensions = new ArrayList<String>();
for (String ext : exts) {
extensions.add(ext);
}
for (String ext : internalexts) {
if (!extensions.contains(ext)) {
extensions.add(ext);
}
}
exts = extensions.toArray(new String[extensions.size()]);
}
for (int i = 0; i < exts.length; i++)
exts[i] = "." + exts[i]; //$NON-NLS-1$
String allSpiImages = Messages.getString("Util.ALL_SPI_IMAGES"); //$NON-NLS-1$
CustomFileFilter filt = new CustomFileFilter(allSpiImages,exts);
imageFc.addChoosableFileFilter(filt);
for (String element : exts)
{
imageFc.addChoosableFileFilter(new CustomFileFilter(Messages.format("Util.FILES", //$NON-NLS-1$
element),element));
}
imageFc.setFileFilter(filt);
createImageChooser();
}
imageFc.setMultiSelectionEnabled(true);
if (imageFc.showOpenDialog(LGM.frame) == JFileChooser.APPROVE_OPTION)
return imageFc.getSelectedFiles();
return null;
}
/**
* Returns the selected file from a JFileChooser, including the extension from
* the file filter.
*/
public static File getSelectedFileWithExtension(JFileChooser c) {
File file = c.getSelectedFile();
if (c.getFileFilter() instanceof CustomFileFilter) {
String[] exts = ((CustomFileFilter)c.getFileFilter()).getExtensions();
String nameLower = file.getName().toLowerCase();
System.out.println(nameLower);
for (String ext : exts) { // check if it already has a valid extension
if (nameLower.endsWith('.' + ext.toLowerCase())) {
return file; // if yes, return as-is
}
}
// if not, append the first extension from the selected filter
file = new File(file.toString() + (exts[0].startsWith(".") ? "" : ".") + exts[0]);
}
return file;
}
private static String getFileExtension(File file) {
String name = file.getName();
int lastIndexOf = name.lastIndexOf(".");
if (lastIndexOf == -1) {
return ""; // empty extension
}
return name.substring(lastIndexOf + 1);
}
public static void saveImages(ArrayList<BufferedImage> imgs)
{
if (imageFc == null)
{
createImageChooser();
}
imageFc.setMultiSelectionEnabled(false);
if (imageFc.showSaveDialog(LGM.frame) == JFileChooser.APPROVE_OPTION) {
try
{
File f = getSelectedFileWithExtension(imageFc);
String ext = getFileExtension(f);
System.out.println(f.getName() + " : " + ext);
if (ext.equals("apng")) {
FileOutputStream os = new FileOutputStream(f);
ApngIO.imagesToApng(imgs, os);
os.close();
} else {
ImageIO.write(imgs.get(0),ext,f);
}
}
catch (IOException e)
{
LGM.showDefaultExceptionHandler(e);
}
}
}
public static void saveImage(BufferedImage img)
{
if (imageFc == null)
{
createImageChooser();
}
imageFc.setMultiSelectionEnabled(false);
if (imageFc.showSaveDialog(LGM.frame) == JFileChooser.APPROVE_OPTION) {
try
{
File f = getSelectedFileWithExtension(imageFc);
String ext = getFileExtension(f);
System.out.println(f.getName() + " : " + ext);
if (ext.equals("apng")) {
ArrayList<BufferedImage> imgs = new ArrayList<BufferedImage>(1);
imgs.add(img);
FileOutputStream os = new FileOutputStream(f);
ApngIO.imagesToApng(imgs,os);
os.close();
} else {
ImageIO.write(img,ext,f);
}
}
catch (IOException e)
{
LGM.showDefaultExceptionHandler(e);
}
}
}
public static BufferedImage getValidImage()
{
File f = chooseImageFile();
@@ -414,7 +483,9 @@ else if (disposal.equals("restoreToBackgroundColor"))
}
else if (f[i].getName().endsWith(".apng"))
{
subframes.addAll(ApngIO.apngToBufferedImages(new FileInputStream(f[i])));
FileInputStream is = new FileInputStream(f[i]);
subframes.addAll(ApngIO.apngToBufferedImages(is));
is.close();
}
else
{
@@ -179,6 +179,7 @@ SoundFrame.EDIT=restree/sound.png
SpriteFrame.LOAD=actions/open.png
SpriteFrame.LOAD_STRIP=actions/open-strip.png
SpriteFrame.LOAD_SUBIMAGE=actions/open-subimage.png
SpriteFrame.SAVE=actions/save.png
SpriteFrame.EDIT=actions/palette.png
SpriteFrame.EFFECT=actions/spraypaint.png
SpriteFrame.CUT=actions/cut.png
@@ -211,6 +212,7 @@ CursorDisplay.ZOOM_IN=actions/zoom-in.png
CursorDisplay.ZOOM_OUT=actions/zoom-out.png
BackgroundFrame.CREATE=actions/new.png
BackgroundFrame.SAVE=actions/save.png
BackgroundFrame.EFFECT=actions/spraypaint.png
BackgroundFrame.ZOOM=actions/zoom.png
BackgroundFrame.ZOOM_IN=actions/zoom-in.png
@@ -90,7 +90,7 @@ DocumentUndoManager.REDO=Redo
AboutBox.TITLE=About LateralGM
AboutBox.ABOUT=<h1 style="white-space: nowrap">Copyright &copy; 2006-2014</h1>\
<p style="white-space: nowrap">Version: 1.8.6.8<br><br>\
<p style="white-space: nowrap">Version: 1.8.6.82<br><br>\
IsmAvatar \
&lt;<a href="mailto:IsmAvatar@gmail.com">IsmAvatar@gmail.com</a>&gt;<br>\
Clam \
@@ -923,6 +923,7 @@ FontFrame.SELECTALL=Select All
SpriteFrame.NAME=Name:
SpriteFrame.LOAD=Load Subimages
SpriteFrame.SAVE=Save Subimages
SpriteFrame.LOAD_STRIP=Add Spritesheet Subimages
SpriteFrame.LOAD_SUBIMAGE=Add Subimages
SpriteFrame.ADD=Add Subimage
@@ -1108,8 +1109,9 @@ ResourceInfoFrame.SELECTALL=Select All
ResourceInfoFrame.CONFIRM=Confirm
BackgroundFrame.NAME=Name:
BackgroundFrame.LOAD=Load Image
BackgroundFrame.CREATE=New Image
BackgroundFrame.LOAD=Load Image
BackgroundFrame.SAVE=Save Image
BackgroundFrame.EFFECT=Add Effect
BackgroundFrame.MEMORY=Memory
BackgroundFrame.ZOOM=Zoom
@@ -253,8 +253,9 @@ private JToolBar makeToolBar()
tool.setAlignmentX(0);
tool.add(save);
tool.add(makeJButton("BackgroundFrame.LOAD"));
tool.add(makeJButton("BackgroundFrame.CREATE"));
tool.add(makeJButton("BackgroundFrame.LOAD"));
tool.add(makeJButton("BackgroundFrame.SAVE"));
tool.add(makeJButton("BackgroundFrame.EDIT"));
tool.add(makeJButton("BackgroundFrame.EFFECT"));
@@ -517,6 +518,15 @@ public void handleToolBar(String cmd)
}
return;
}
else if (cmd.endsWith(".SAVE"))
{
BufferedImage img = res.getBackgroundImage();
if (img != null)
{
Util.saveImage(img);
}
return;
}
else if (cmd.endsWith(".CREATE"))
{
createNewImage(true);
@@ -113,6 +113,7 @@
{
private static final long serialVersionUID = 1L;
private static final ImageIcon LOAD_ICON = LGM.getIconForKey("SpriteFrame.LOAD"); //$NON-NLS-1$
private static final ImageIcon SAVE_ICON = LGM.getIconForKey("SpriteFrame.SAVE"); //$NON-NLS-1$
private static final ImageIcon LOAD_SUBIMAGE_ICON = LGM.getIconForKey("SpriteFrame.LOAD_SUBIMAGE"); //$NON-NLS-1$
private static final ImageIcon LOAD_STRIP_ICON = LGM.getIconForKey("SpriteFrame.LOAD_STRIP"); //$NON-NLS-1$
private static final ImageIcon PLAY_ICON = LGM.getIconForKey("SpriteFrame.PLAY"); //$NON-NLS-1$
@@ -122,7 +123,7 @@
private static final ImageIcon ZOOM_OUT_ICON = LGM.getIconForKey("SpriteFrame.ZOOM_OUT"); //$NON-NLS-1$
//toolbar
public JButton load, loadSubimage, loadStrip, zoomIn, zoomOut;
public JButton load, loadSubimages, loadStrip, saveSubimages, zoomIn, zoomOut;
public JToggleButton zoomButton;
//origin
@@ -303,7 +304,7 @@ public void mouseReleased(MouseEvent ev)
updateStatusLabel();
pack();
this.setSize(750,500);
this.setSize(850,650);
previewPane.setDividerLocation(getHeight() / 2);
updateScrollBars();
}
@@ -321,15 +322,20 @@ private JToolBar makeToolBar()
load.addActionListener(this);
tool.add(load);
loadSubimage = new JButton(LOAD_SUBIMAGE_ICON);
loadSubimage.setToolTipText(Messages.getString("SpriteFrame.LOAD_SUBIMAGE")); //$NON-NLS-1$
loadSubimage.addActionListener(this);
tool.add(loadSubimage);
loadSubimages = new JButton(LOAD_SUBIMAGE_ICON);
loadSubimages.setToolTipText(Messages.getString("SpriteFrame.LOAD_SUBIMAGE")); //$NON-NLS-1$
loadSubimages.addActionListener(this);
tool.add(loadSubimages);
loadStrip = new JButton(LOAD_STRIP_ICON);
loadStrip.setToolTipText(Messages.getString("SpriteFrame.LOAD_STRIP")); //$NON-NLS-1$
loadStrip.addActionListener(this);
tool.add(loadStrip);
saveSubimages = new JButton(SAVE_ICON);
saveSubimages.setToolTipText(Messages.getString("SpriteFrame.SAVE")); //$NON-NLS-1$
saveSubimages.addActionListener(this);
tool.add(saveSubimages);
tool.addSeparator();
@@ -1340,6 +1346,21 @@ public void run()
}
}
}
public ArrayList<BufferedImage> getSelectedImages() {
int[] selected = subList.getSelectedIndices();
if (selected.length <= 0) {
return res.subImages;
} else {
if (res.subImages.getSize() <= 0) return null;
ArrayList<BufferedImage> subimages = new ArrayList<BufferedImage>(selected.length);
for (int id : selected) {
subimages.add(res.subImages.get(id));
}
return subimages;
}
}
private AnimThread animThread = null;
@@ -1362,12 +1383,20 @@ else if (e.getSource() == loadStrip)
addFromStrip(true);
return;
}
else if (e.getSource() == loadSubimage)
else if (e.getSource() == loadSubimages)
{
BufferedImage[] img = Util.getValidImages();
if (img != null) addSubimages(img,false);
return;
}
else if (e.getSource() == saveSubimages)
{
ArrayList<BufferedImage> imgs = getSelectedImages();
if (imgs != null) {
Util.saveImages(imgs);
}
return;
}
else if (e.getSource() == showBbox)
{
preview.setShowBbox(showBbox.isSelected());

0 comments on commit 337081b

Please sign in to comment.