fix: saving snapshots, pngs and play time working

This commit is contained in:
lantzelot-swe 2021-11-05 21:51:19 +01:00
parent b43b80a1b9
commit 4cdc207d1a
5 changed files with 230 additions and 128 deletions

View File

@ -168,7 +168,7 @@ public class GameDetailsBackgroundPanel extends JPanel
gbc_systemPanel.gridy = 0;
settingsPanel.add(getSystemSavesTabbedPane(), gbc_systemPanel);
GridBagConstraints gbc_joystickPanel = new GridBagConstraints();
gbc_joystickPanel.insets = new Insets(0, 5, 0, 0);
gbc_joystickPanel.insets = new Insets(0, 0, 0, 0);
gbc_joystickPanel.weighty = 1.0;
gbc_joystickPanel.anchor = GridBagConstraints.NORTH;
gbc_joystickPanel.fill = GridBagConstraints.BOTH;
@ -224,7 +224,8 @@ public class GameDetailsBackgroundPanel extends JPanel
if (savesBackgroundPanel == null)
{
savesBackgroundPanel = new SaveStateBackgroundPanel(model);
savesBackgroundPanel
.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
}
return savesBackgroundPanel;
}
@ -293,6 +294,8 @@ public class GameDetailsBackgroundPanel extends JPanel
{
public void actionPerformed(ActionEvent e)
{
//Make sure any edits to time for saved states are commited.
savesBackgroundPanel.commitEdits();
if (model.saveData())
{
getInfoBackgroundPanel().getScreensPanel().resetWhenSaved();

View File

@ -72,4 +72,12 @@ public class SaveStateBackgroundPanel extends JPanel
}
return saveStatePanel4;
}
void commitEdits()
{
getSaveStatePanel1().commitEdits();
getSaveStatePanel2().commitEdits();
getSaveStatePanel3().commitEdits();
getSaveStatePanel4().commitEdits();
}
}

View File

@ -33,22 +33,24 @@ import javax.swing.text.MaskFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import se.lantz.model.InfoModel;
import se.lantz.model.MainViewModel;
import se.lantz.model.SavedStatesModel;
import se.lantz.model.SavedStatesModel.SAVESTATE;
import se.lantz.util.ExceptionHandler;
import se.lantz.util.FileDrop;
import se.lantz.util.FileManager;
import se.lantz.util.SavedStatesManager;
public class SaveStatePanel extends JPanel
{
private static final String SNAPSHOT_DIR_PROPERTY = "snapshotDir";
private static final Logger logger = LoggerFactory.getLogger(SaveStatePanel.class);
private JLabel screenshotLabel;
private JTextField snapshotTextField;
private MainViewModel model;
private SavedStatesModel stateModel;
private JButton snapshotButton;
private BufferedImage currentScreen1Image = null;
private BufferedImage currentScreenImage = null;
private String currentGameFile = "";
private ImageIcon missingSceenshotIcon = null;
private JLabel timeLabel;
@ -60,6 +62,9 @@ public class SaveStatePanel extends JPanel
private JSeparator separator;
private JButton screenshotButton;
private FileNameExtensionFilter imagefilter =
new FileNameExtensionFilter("png, gif, jpeg, bmp", "png", "gif", "jpg", "jpeg", "bmp");
public SaveStatePanel(MainViewModel model, SAVESTATE saveState)
{
this.model = model;
@ -108,12 +113,14 @@ public class SaveStatePanel extends JPanel
gbc_timeLabel.gridy = 0;
add(getTimeLabel(), gbc_timeLabel);
GridBagConstraints gbc_snapshotLabel = new GridBagConstraints();
gbc_snapshotLabel.weightx = 1.0;
gbc_snapshotLabel.insets = new Insets(5, 0, 0, 5);
gbc_snapshotLabel.anchor = GridBagConstraints.NORTHWEST;
gbc_snapshotLabel.gridx = 1;
gbc_snapshotLabel.gridy = 0;
add(getSnapshotLabel(), gbc_snapshotLabel);
GridBagConstraints gbc_screenshotButton = new GridBagConstraints();
gbc_screenshotButton.weightx = 1.0;
gbc_screenshotButton.anchor = GridBagConstraints.SOUTHWEST;
gbc_screenshotButton.insets = new Insets(0, 0, 0, 5);
gbc_screenshotButton.gridx = 1;
@ -181,36 +188,11 @@ public class SaveStatePanel extends JPanel
}
// Read from model
getSnapshotTextField().setText(getSnapshotFileName());
getTimeField().setText(getPlayTime());
reloadScreen();
getRunButton().setEnabled(!getSnapshotTextField().getText().isEmpty());
getTimeField().setEnabled(!getSnapshotTextField().getText().isEmpty());
}
private String getPlayTime()
{
String returnValue = "";
switch (saveState)
{
case Save0:
returnValue = stateModel.getState1time();
break;
case Save1:
returnValue = stateModel.getState2time();
break;
case Save2:
returnValue = stateModel.getState3time();
break;
case Save3:
returnValue = stateModel.getState4time();
break;
default:
break;
}
return returnValue;
}
private String getSnapshotFileName()
{
String returnValue = "";
@ -261,11 +243,12 @@ public class SaveStatePanel extends JPanel
}
if (screen1Image != null)
{
if (!screen1Image.equals(currentScreen1Image))
if (!screen1Image.equals(currentScreenImage))
{
logger.debug("SETTING SCREEN 1 IMAGE");
getScreenshotLabel().setIcon(new ImageIcon(FileManager.scaleImageTo320x200x32bit(screen1Image)));
currentScreen1Image = screen1Image;
Image newImage = screen1Image.getScaledInstance(130, 82, Image.SCALE_SMOOTH);
getScreenshotLabel().setIcon(new ImageIcon(newImage));
currentScreenImage = screen1Image;
}
}
else
@ -295,7 +278,7 @@ public class SaveStatePanel extends JPanel
}
else if (!model.getInfoModel().getGamesFile().equals(currentGameFile))
{
currentScreen1Image = loadScreen(modelScreenFile, getScreenshotLabel());
currentScreenImage = loadScreen(modelScreenFile, getScreenshotLabel());
}
//Keep track of which game is selected
currentGameFile = model.getInfoModel().getGamesFile();
@ -360,28 +343,7 @@ public class SaveStatePanel extends JPanel
{
public void filesDropped(java.io.File[] files)
{
//TODO
// if (files.length > 0)
// {
// gamesFileUpdated = true;
// switch (saveState)
// {
// case Save0:
// stateModel.setState1Path(files[0].toPath());
// break;
// case Save1:
// stateModel.setState2Path(files[0].toPath());
// break;
// case Save2:
// stateModel.setState3Path(files[0].toPath());
// break;
// case Save3:
// stateModel.setState4Path(files[0].toPath());
// break;
// default:
// break;
// }
// }
setScreenshotFileToModel(files);
}
});
}
@ -394,7 +356,7 @@ public class SaveStatePanel extends JPanel
{
snapshotTextField = new JTextField();
snapshotTextField.setEditable(false);
snapshotTextField.setPreferredSize(new Dimension(50, 20));
snapshotTextField.setPreferredSize(new Dimension(30, 20));
new FileDrop(snapshotTextField, new FileDrop.Listener()
{
public void filesDropped(java.io.File[] files)
@ -402,22 +364,9 @@ public class SaveStatePanel extends JPanel
if (files.length > 0)
{
gamesFileUpdated = true;
switch (saveState)
if (files[0].getName().toLowerCase().endsWith(".vsf"))
{
case Save0:
stateModel.setState1Path(files[0].toPath());
break;
case Save1:
stateModel.setState2Path(files[0].toPath());
break;
case Save2:
stateModel.setState3Path(files[0].toPath());
break;
case Save3:
stateModel.setState4Path(files[0].toPath());
break;
default:
break;
setSnapshotFileToModel(files[0]);
}
}
}
@ -447,9 +396,9 @@ public class SaveStatePanel extends JPanel
private void selectSnapshotFile()
{
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Select a valid game file for " + model.getInfoModel().getTitle());
fileChooser.setDialogTitle("Select a Vice snapsot file for " + model.getInfoModel().getTitle());
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
String gameDir = null; //FileManager.getConfiguredProperties().getProperty(GAME_DIR_PROPERTY);
String gameDir = FileManager.getConfiguredProperties().getProperty(SNAPSHOT_DIR_PROPERTY);
if (gameDir == null)
{
gameDir = ".";
@ -463,27 +412,36 @@ public class SaveStatePanel extends JPanel
if (value == JFileChooser.APPROVE_OPTION)
{
File selectedFile = fileChooser.getSelectedFile();
// FileManager.getConfiguredProperties().put(GAME_DIR_PROPERTY, selectedFile.toPath().getParent().toString());
FileManager.getConfiguredProperties().put(SNAPSHOT_DIR_PROPERTY, selectedFile.toPath().getParent().toString());
gamesFileUpdated = true;
setSnapshotFileToModel(selectedFile);
}
}
private void setSnapshotFileToModel(File file)
{
switch (saveState)
{
case Save0:
stateModel.setState1Path(selectedFile.toPath());
stateModel.setState1File(SavedStatesManager.VSZ0);
stateModel.setState1Path(file.toPath());
break;
case Save1:
stateModel.setState2Path(selectedFile.toPath());
stateModel.setState2File(SavedStatesManager.VSZ1);
stateModel.setState2Path(file.toPath());
break;
case Save2:
stateModel.setState3Path(selectedFile.toPath());
stateModel.setState3File(SavedStatesManager.VSZ2);
stateModel.setState3Path(file.toPath());
break;
case Save3:
stateModel.setState4Path(selectedFile.toPath());
stateModel.setState4File(SavedStatesManager.VSZ3);
stateModel.setState4Path(file.toPath());
break;
default:
break;
}
}
}
private JLabel getTimeLabel()
{
@ -544,31 +502,37 @@ public class SaveStatePanel extends JPanel
}
catch (ParseException e1)
{
e1.printStackTrace();
ExceptionHandler.handleException(e1, "");
}
setTimeToModel();
}
});
timeField.setPreferredSize(new Dimension(55, 20));
timeField.setMinimumSize(new Dimension(55, 20));
}
return timeField;
}
private void setTimeToModel()
{
switch (saveState)
{
case Save0:
stateModel.setState1time(timeField.getValue().toString());
stateModel.setState1time(timeField.getText().toString());
break;
case Save1:
stateModel.setState2time(timeField.getValue().toString());
stateModel.setState2time(timeField.getText().toString());
break;
case Save2:
stateModel.setState3time(timeField.getValue().toString());
stateModel.setState3time(timeField.getText().toString());
break;
case Save3:
stateModel.setState4time(timeField.getValue().toString());
stateModel.setState4time(timeField.getText().toString());
break;
default:
break;
}
}
});
timeField.setPreferredSize(new Dimension(55, 20));
}
return timeField;
}
private JButton getRunButton()
{
@ -604,11 +568,87 @@ public class SaveStatePanel extends JPanel
{
public void actionPerformed(ActionEvent e)
{
//TODO
selectScreenshotFile();
}
});
screenshotButton.setMargin(new Insets(1, 3, 1, 3));
}
return screenshotButton;
}
private void selectScreenshotFile()
{
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Select screenshot for " + model.getInfoModel().getTitle() + " snapshot");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
String screensDir = FileManager.getConfiguredProperties().getProperty(SNAPSHOT_DIR_PROPERTY);
if (screensDir == null)
{
screensDir = ".";
}
fileChooser.setCurrentDirectory(new File(screensDir));
fileChooser.addChoosableFileFilter(imagefilter);
fileChooser.setFileFilter(imagefilter);
int value = fileChooser.showOpenDialog(MainWindow.getInstance());
if (value == JFileChooser.APPROVE_OPTION)
{
File selectedFile = fileChooser.getSelectedFile();
FileManager.getConfiguredProperties().put(SNAPSHOT_DIR_PROPERTY, selectedFile.toPath().getParent().toString());
setScreenshotFileToModel(new File[] {selectedFile});
}
}
private void setScreenshotFileToModel(File[] files)
{
switch (saveState)
{
case Save0:
stateModel.setState1PngImage(handleScreenFileDrop(files, getScreenshotLabel()));
break;
case Save1:
stateModel.setState2PngImage(handleScreenFileDrop(files, getScreenshotLabel()));
break;
case Save2:
stateModel.setState3PngImage(handleScreenFileDrop(files, getScreenshotLabel()));
break;
case Save3:
stateModel.setState4PngImage(handleScreenFileDrop(files, getScreenshotLabel()));
break;
default:
break;
}
}
private BufferedImage handleScreenFileDrop(File[] files, JLabel imageLabel)
{
BufferedImage returnImage = null;
if (files.length > 0)
{
try
{
returnImage = ImageIO.read(files[0]);
Image newImage = returnImage.getScaledInstance(130, 82, Image.SCALE_SMOOTH);
imageLabel.setIcon(new ImageIcon(newImage));
}
catch (IOException e)
{
logger.error("can't read file: " + files[0].getName(), e);
imageLabel.setIcon(getMissingScreenshotImageIcon());
}
}
return returnImage;
}
void commitEdits()
{
try
{
timeField.commitEdit();
}
catch (ParseException e1)
{
ExceptionHandler.handleException(e1, "");
}
setTimeToModel();
}
}

View File

@ -1,16 +1,20 @@
package se.lantz.util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import se.lantz.model.MainViewModel;
import se.lantz.model.SavedStatesModel;
@ -28,10 +32,10 @@ public class SavedStatesManager
private static final String PNG2 = "2.png";
private static final String PNG3 = "3.png";
private static final String VSZ0 = "0.vsz";
private static final String VSZ1 = "1.vsz";
private static final String VSZ2 = "2.vsz";
private static final String VSZ3 = "3.vsz";
public static final String VSZ0 = "0.vsz";
public static final String VSZ1 = "1.vsz";
public static final String VSZ2 = "2.vsz";
public static final String VSZ3 = "3.vsz";
private SavedStatesModel savedStatesModel;
private MainViewModel model;
@ -52,28 +56,40 @@ public class SavedStatesManager
{
//Check which ones are available
Path mta0Path = saveFolder.resolve(MTA0);
if (Files.exists(mta0Path))
Path vsz0Path = saveFolder.resolve(VSZ0);
Path png0Path = saveFolder.resolve(PNG0);
if (Files.exists(mta0Path) || savedStatesModel.getState1Path() != null)
{
storePlayTime(mta0Path, savedStatesModel.getState1time());
//TODO: screen and vsz
copyVsfFile(vsz0Path, savedStatesModel.getState1Path());
copyPngFile(png0Path, savedStatesModel.getState1PngImage());
}
Path mta1Path = saveFolder.resolve(MTA1);
if (Files.exists(mta1Path))
Path vsz1Path = saveFolder.resolve(VSZ1);
Path png1Path = saveFolder.resolve(PNG1);
if (Files.exists(mta1Path) || savedStatesModel.getState2Path() != null)
{
storePlayTime(mta1Path, savedStatesModel.getState2time());
//TODO: screen and vsz
copyVsfFile(vsz1Path, savedStatesModel.getState2Path());
copyPngFile(png1Path, savedStatesModel.getState2PngImage());
}
Path mta2Path = saveFolder.resolve(MTA2);
if (Files.exists(mta2Path))
Path vsz2Path = saveFolder.resolve(VSZ2);
Path png2Path = saveFolder.resolve(PNG2);
if (Files.exists(mta2Path) || savedStatesModel.getState3Path() != null)
{
storePlayTime(mta2Path, savedStatesModel.getState3time());
//TODO: screen and vsz
copyVsfFile(vsz2Path, savedStatesModel.getState3Path());
copyPngFile(png2Path, savedStatesModel.getState3PngImage());
}
Path mta3Path = saveFolder.resolve(MTA3);
if (Files.exists(mta3Path))
Path vsz3Path = saveFolder.resolve(VSZ3);
Path png3Path = saveFolder.resolve(PNG3);
if (Files.exists(mta3Path) || savedStatesModel.getState4Path() != null)
{
storePlayTime(mta3Path, savedStatesModel.getState4time());
//TODO: screen and vsz
copyVsfFile(vsz3Path, savedStatesModel.getState4Path());
copyPngFile(png3Path, savedStatesModel.getState4PngImage());
}
}
}
@ -153,6 +169,11 @@ public class SavedStatesManager
{
try
{
if (!mtaFilePath.toFile().exists())
{
FileUtils.copyInputStreamToFile(getClass().getResourceAsStream("/se/lantz/template.mta"), mtaFilePath.toFile());
}
String[] timeparts = playTime.split(":");
int millis = (Integer.parseInt(timeparts[0])*3600 + Integer.parseInt(timeparts[1])*60 + Integer.parseInt(timeparts[2]))*1000;
byte[] fileContent = Files.readAllBytes(mtaFilePath);
@ -183,4 +204,34 @@ public class SavedStatesManager
return time;
}
private void copyVsfFile(Path target, Path source)
{
if (source != null)
{
try
{
FileManager.compressGzip(source, target);
}
catch (IOException e)
{
ExceptionHandler.handleException(e, "Could not compress vsf file");
}
}
}
private void copyPngFile(Path target, BufferedImage image)
{
if (image != null)
{
try
{
ImageIO.write(image, "png", target.toFile());
}
catch (IOException e1)
{
ExceptionHandler.logException(e1, "Could not store screenshot");
}
}
}
}

Binary file not shown.