fix: wip saved states icons

This commit is contained in:
mikael.lantz 2021-11-18 16:36:10 +01:00
parent dd656cba5a
commit 7ea98793f3
6 changed files with 143 additions and 26 deletions

View File

@ -220,7 +220,7 @@ public class DbConnector
//Construct SQL //Construct SQL
StringBuilder sqlBuilder = new StringBuilder(); StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append("SELECT title, rowid, favorite FROM gameinfo "); sqlBuilder.append("SELECT title, gamefile, rowid, favorite FROM gameinfo ");
sqlBuilder.append(view.getSqlQuery()); sqlBuilder.append(view.getSqlQuery());
sqlBuilder.append(" ORDER BY title COLLATE NOCASE ASC"); sqlBuilder.append(" ORDER BY title COLLATE NOCASE ASC");
@ -232,7 +232,7 @@ public class DbConnector
while (rs.next()) while (rs.next())
{ {
GameListData data = GameListData data =
new GameListData(rs.getString("Title"), Integer.toString(rs.getInt("rowid")), rs.getInt("Favorite")); new GameListData(rs.getString("Title"), rs.getString("GameFile"), Integer.toString(rs.getInt("rowid")), rs.getInt("Favorite"));
returnList.add(data); returnList.add(data);
} }
} }

View File

@ -11,6 +11,7 @@ import javax.swing.ImageIcon;
import javax.swing.JList; import javax.swing.JList;
import javax.swing.SwingConstants; import javax.swing.SwingConstants;
import se.lantz.manager.SavedStatesManager;
import se.lantz.model.data.GameListData; import se.lantz.model.data.GameListData;
import se.lantz.model.data.GameView; import se.lantz.model.data.GameView;
@ -33,9 +34,11 @@ public class GameListDataRenderer extends DefaultListCellRenderer
private final Font bold; private final Font bold;
private final Font boldItalic; private final Font boldItalic;
private SavedStatesManager savedStatesManager;
public GameListDataRenderer() public GameListDataRenderer(SavedStatesManager savedStatesManager)
{ {
this.savedStatesManager = savedStatesManager;
this.boldItalic = getFont().deriveFont(Font.BOLD + Font.ITALIC); this.boldItalic = getFont().deriveFont(Font.BOLD + Font.ITALIC);
this.bold = getFont().deriveFont(Font.BOLD); this.bold = getFont().deriveFont(Font.BOLD);
this.setHorizontalTextPosition(SwingConstants.LEADING); this.setHorizontalTextPosition(SwingConstants.LEADING);
@ -64,7 +67,6 @@ public class GameListDataRenderer extends DefaultListCellRenderer
private void handleGameListData(Object value, boolean isSelected) private void handleGameListData(Object value, boolean isSelected)
{ {
this.setIcon(null);
GameListData listData = (GameListData) value; GameListData listData = (GameListData) value;
if (listData.isFavorite()) if (listData.isFavorite())
{ {
@ -74,7 +76,6 @@ public class GameListDataRenderer extends DefaultListCellRenderer
{ {
case 1: case 1:
this.setForeground(isSelected ? fav1ColorSelected : fav1Color); this.setForeground(isSelected ? fav1ColorSelected : fav1Color);
this.setIcon(new ImageIcon(this.getClass().getResource("/se/lantz/16x16SaveIcon-1.png")));
break; break;
case 2: case 2:
this.setForeground(isSelected ? fav2ColorSelected : fav2Color); this.setForeground(isSelected ? fav2ColorSelected : fav2Color);
@ -112,18 +113,40 @@ public class GameListDataRenderer extends DefaultListCellRenderer
break; break;
} }
} }
//Decide which icon to use
int numberOfSavedStates = savedStatesManager.getNumberOfSavedStatesForGame(listData.getGameFileName());
if (numberOfSavedStates == 1)
{
this.setIcon(new ImageIcon(this.getClass().getResource("/se/lantz/16x16SaveIcon-1.png")));
}
else if (numberOfSavedStates == 2)
{
this.setIcon(new ImageIcon(this.getClass().getResource("/se/lantz/16x16SaveIcon-2.png")));
}
else if (numberOfSavedStates == 3)
{
this.setIcon(new ImageIcon(this.getClass().getResource("/se/lantz/16x16SaveIcon-3.png")));
}
else if (numberOfSavedStates == 4)
{
this.setIcon(new ImageIcon(this.getClass().getResource("/se/lantz/16x16SaveIcon-4.png")));
}
else
{
this.setIcon(null);
}
} }
private void handleGameListView(Object value, boolean isSelected, int index) private void handleGameListView(Object value, boolean isSelected, int index)
{ {
this.setIcon(null);
this.setBorder(null); this.setBorder(null);
GameView view = (GameView) value; GameView view = (GameView) value;
if (view.getGameViewId() == GameView.FAVORITES_ID) if (view.getGameViewId() == GameView.FAVORITES_ID)
{ {
this.setFont(bold); this.setFont(bold);
this.setForeground(isSelected ? fav1ColorSelected : fav1Color); this.setForeground(isSelected ? fav1ColorSelected : fav1Color);
if(index > -1) if (index > -1)
{ {
this.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.LIGHT_GRAY)); this.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.LIGHT_GRAY));
} }
@ -172,24 +195,28 @@ public class GameListDataRenderer extends DefaultListCellRenderer
{ {
this.setFont(boldItalic); this.setFont(boldItalic);
this.setForeground(isSelected ? fav5ColorSelected : fav5Color); this.setForeground(isSelected ? fav5ColorSelected : fav5Color);
if(index > -1) if (index > -1)
{ {
this.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY)); this.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY));
} }
} }
} }
@Override @Override
public void setBounds(int x, int y, int width, int height) { public void setBounds(int x, int y, int width, int height)
super.setBounds(x, y, width, height); {
if (getIcon() != null) { super.setBounds(x, y, width, height);
int padding = 1; if (getIcon() != null)
int textWidth = getFontMetrics(getFont()).stringWidth(getText()); {
Insets insets = getInsets(); int padding = 1;
int iconTextGap = width - textWidth - getIcon().getIconWidth() - insets.left - insets.right - padding; int textWidth = getFontMetrics(getFont()).stringWidth(getText());
setIconTextGap(iconTextGap); Insets insets = getInsets();
} else { int iconTextGap = width - textWidth - getIcon().getIconWidth() - insets.left - insets.right - padding;
setIconTextGap(0); setIconTextGap(iconTextGap);
} }
else
{
setIconTextGap(0);
}
} }
} }

View File

@ -172,7 +172,7 @@ public class ListPanel extends JPanel
} }
}); });
listViewComboBox.setModel(uiModel.getGameViewModel()); listViewComboBox.setModel(uiModel.getGameViewModel());
listViewComboBox.setRenderer(new GameListDataRenderer()); listViewComboBox.setRenderer(new GameListDataRenderer(uiModel.getSavedStatesManager()));
listViewComboBox.addPopupMenuListener(new PopupMenuListener() listViewComboBox.addPopupMenuListener(new PopupMenuListener()
{ {
@Override @Override
@ -434,7 +434,7 @@ public class ListPanel extends JPanel
} }
}); });
list.setModel(uiModel.getGameListModel()); list.setModel(uiModel.getGameListModel());
list.setCellRenderer(new GameListDataRenderer()); list.setCellRenderer(new GameListDataRenderer(uiModel.getSavedStatesManager()));
//Remove from tootlipManager to avoid throwing a nullpointer for CTRL+F1 //Remove from tootlipManager to avoid throwing a nullpointer for CTRL+F1
ToolTipManager.sharedInstance().unregisterComponent(list); ToolTipManager.sharedInstance().unregisterComponent(list);
} }

View File

@ -12,6 +12,8 @@ import java.nio.file.PathMatcher;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -54,10 +56,17 @@ public class SavedStatesManager
private int noFilesCopied = 0; private int noFilesCopied = 0;
/**
* Map holding available saved states with fileName (subfolder) as key and number of saved states available as value
* (1-4).
*/
private Map<String, Integer> savedStatesMap = new HashMap<>();
public SavedStatesManager(MainViewModel model) public SavedStatesManager(MainViewModel model)
{ {
this.model = model; this.model = model;
this.savedStatesModel = model.getSavedStatesModel(); this.savedStatesModel = model.getSavedStatesModel();
readSavedStatesAndUpdateMap();
} }
public void saveSavedStates() public void saveSavedStates()
@ -76,6 +85,7 @@ public class SavedStatesManager
String fileName = model.getInfoModel().getGamesFile(); String fileName = model.getInfoModel().getGamesFile();
Path saveFolder = new File(SAVES + fileName).toPath(); Path saveFolder = new File(SAVES + fileName).toPath();
int numberofSaves = 0;
//Check which ones are available //Check which ones are available
Path mta0Path = saveFolder.resolve(MTA0); Path mta0Path = saveFolder.resolve(MTA0);
Path vsz0Path = saveFolder.resolve(VSZ0); Path vsz0Path = saveFolder.resolve(VSZ0);
@ -85,6 +95,7 @@ public class SavedStatesManager
storePlayTime(mta0Path, savedStatesModel.getState1time()); storePlayTime(mta0Path, savedStatesModel.getState1time());
copyVsfFile(vsz0Path, savedStatesModel.getState1Path()); copyVsfFile(vsz0Path, savedStatesModel.getState1Path());
copyPngFile(png0Path, savedStatesModel.getState1PngImage()); copyPngFile(png0Path, savedStatesModel.getState1PngImage());
numberofSaves++;
} }
Path mta1Path = saveFolder.resolve(MTA1); Path mta1Path = saveFolder.resolve(MTA1);
Path vsz1Path = saveFolder.resolve(VSZ1); Path vsz1Path = saveFolder.resolve(VSZ1);
@ -94,6 +105,7 @@ public class SavedStatesManager
storePlayTime(mta1Path, savedStatesModel.getState2time()); storePlayTime(mta1Path, savedStatesModel.getState2time());
copyVsfFile(vsz1Path, savedStatesModel.getState2Path()); copyVsfFile(vsz1Path, savedStatesModel.getState2Path());
copyPngFile(png1Path, savedStatesModel.getState2PngImage()); copyPngFile(png1Path, savedStatesModel.getState2PngImage());
numberofSaves++;
} }
Path mta2Path = saveFolder.resolve(MTA2); Path mta2Path = saveFolder.resolve(MTA2);
Path vsz2Path = saveFolder.resolve(VSZ2); Path vsz2Path = saveFolder.resolve(VSZ2);
@ -103,6 +115,7 @@ public class SavedStatesManager
storePlayTime(mta2Path, savedStatesModel.getState3time()); storePlayTime(mta2Path, savedStatesModel.getState3time());
copyVsfFile(vsz2Path, savedStatesModel.getState3Path()); copyVsfFile(vsz2Path, savedStatesModel.getState3Path());
copyPngFile(png2Path, savedStatesModel.getState3PngImage()); copyPngFile(png2Path, savedStatesModel.getState3PngImage());
numberofSaves++;
} }
Path mta3Path = saveFolder.resolve(MTA3); Path mta3Path = saveFolder.resolve(MTA3);
Path vsz3Path = saveFolder.resolve(VSZ3); Path vsz3Path = saveFolder.resolve(VSZ3);
@ -112,7 +125,10 @@ public class SavedStatesManager
storePlayTime(mta3Path, savedStatesModel.getState4time()); storePlayTime(mta3Path, savedStatesModel.getState4time());
copyVsfFile(vsz3Path, savedStatesModel.getState4Path()); copyVsfFile(vsz3Path, savedStatesModel.getState4Path());
copyPngFile(png3Path, savedStatesModel.getState4PngImage()); copyPngFile(png3Path, savedStatesModel.getState4PngImage());
numberofSaves++;
} }
//Update current map also
savedStatesMap.put(fileName, numberofSaves);
} }
public void readSavedStates() public void readSavedStates()
@ -403,4 +419,61 @@ public class SavedStatesManager
{ {
return noFilesCopied; return noFilesCopied;
} }
private void readSavedStatesAndUpdateMap()
{
//Read all files in the saves folder
File saveFolder = new File(SAVES);
try (Stream<Path> stream = Files.walk(saveFolder.toPath().toAbsolutePath(), 1))
{
stream.forEachOrdered(sourcePath -> {
try
{
//Ignore first folder
if (sourcePath.equals(saveFolder.toPath().toAbsolutePath()))
{
return;
}
//Check which files are available
Path save1 = sourcePath.resolve(MTA0);
Path save2 = sourcePath.resolve(MTA1);
Path save3 = sourcePath.resolve(MTA2);
Path save4 = sourcePath.resolve(MTA3);
int noSavesAvailable = 0;
if (save1.toFile().exists())
{
noSavesAvailable++;
}
if (save2.toFile().exists())
{
noSavesAvailable++;
}
if (save3.toFile().exists())
{
noSavesAvailable++;
}
if (save4.toFile().exists())
{
noSavesAvailable++;
}
//Add to map
savedStatesMap.put(sourcePath.toFile().getName(), noSavesAvailable);
}
catch (Exception e)
{
ExceptionHandler.logException(e, "Could not check available saved states for " + sourcePath.toString());
}
});
}
catch (IOException e1)
{
ExceptionHandler.handleException(e1, "Could not construct savedStates Map");
}
}
public int getNumberOfSavedStatesForGame(String gameFileName)
{
return savedStatesMap.get(gameFileName) != null ? savedStatesMap.get(gameFileName) : 0;
}
} }

View File

@ -77,6 +77,11 @@ public class MainViewModel extends AbstractModel
{ {
this.stateManager = savedStatesManager; this.stateManager = savedStatesManager;
} }
public SavedStatesManager getSavedStatesManager()
{
return this.stateManager;
}
public void initialize() public void initialize()
{ {
@ -688,7 +693,7 @@ public class MainViewModel extends AbstractModel
public void addNewGameListData() public void addNewGameListData()
{ {
gameListModel.addElement(new GameListData("New Game", "", 0)); gameListModel.addElement(new GameListData("New Game", "", "", 0));
selectedGameView.setGameCount(gameListModel.getSize()); selectedGameView.setGameCount(gameListModel.getSize());
//Update all games count //Update all games count
allGamesCount++; allGamesCount++;

View File

@ -3,13 +3,15 @@ package se.lantz.model.data;
public class GameListData implements Comparable public class GameListData implements Comparable
{ {
private String title; private String title;
private String gameFileName;
private String gameId; private String gameId;
private int favorite; private int favorite;
public GameListData(String title, String gameId, int favorite) public GameListData(String title, String gameFileName, String gameId, int favorite)
{ {
super(); super();
this.title = title; this.title = title;
this.gameFileName = gameFileName;
this.gameId = gameId; this.gameId = gameId;
this.favorite = favorite; this.favorite = favorite;
} }
@ -59,6 +61,16 @@ public class GameListData implements Comparable
this.favorite = favorite; this.favorite = favorite;
} }
public String getGameFileName()
{
return gameFileName;
}
public void setGameFileName(String gameFileName)
{
this.gameFileName = gameFileName;
}
@Override @Override
public String toString() public String toString()
{ {