feat: generate infoslot screenshots based on games in game view
If the game view has more than 15 games the screenshots will be created based on the available games.
This commit is contained in:
parent
f75aaa624d
commit
9eedbce8cd
|
@ -13,6 +13,8 @@ import java.util.Collections;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -826,6 +828,54 @@ public class DbConnector
|
|||
return returnValue;
|
||||
}
|
||||
|
||||
public List<String> getScreenShotNames(List<String> gameIds)
|
||||
{
|
||||
//Returns 32 screenshot file names used for creating info slot screens.
|
||||
List<String> returnList = new ArrayList<>();
|
||||
Random screenIndexRandom = new Random();
|
||||
String gameIdsString = gameIds.stream().collect(Collectors.joining(","));
|
||||
|
||||
String sql = "SELECT Screen1File, Screen2File FROM gameinfo WHERE rowid IN (" + gameIdsString + ");";
|
||||
logger.debug("Generated SELECT String:\n{}", sql);
|
||||
|
||||
try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql))
|
||||
{
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
while (rs.next())
|
||||
{
|
||||
if (gameIds.size() < 32)
|
||||
{
|
||||
//Add both screens
|
||||
returnList.add(rs.getString(DbConstants.SCREEN1));
|
||||
returnList.add(rs.getString(DbConstants.SCREEN2));
|
||||
}
|
||||
else
|
||||
{
|
||||
//Randomize which screen to pick
|
||||
if (screenIndexRandom.nextInt(2) == 0)
|
||||
{
|
||||
returnList.add(rs.getString(DbConstants.SCREEN1));
|
||||
}
|
||||
else
|
||||
{
|
||||
returnList.add(rs.getString(DbConstants.SCREEN2));
|
||||
}
|
||||
}
|
||||
}
|
||||
logger.debug("SELECT Executed successfully");
|
||||
if (returnList.size() > 32)
|
||||
{
|
||||
Collections.shuffle(returnList);
|
||||
returnList = returnList.subList(0, 31);
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
ExceptionHandler.handleException(e, "Could not fetch game details.");
|
||||
}
|
||||
return returnList;
|
||||
}
|
||||
|
||||
public int getGameDuplicateIndexToUse(String title, String gameId)
|
||||
{
|
||||
//This is only called when the title changes for an existing game or when adding a new game
|
||||
|
|
|
@ -1015,15 +1015,45 @@ public class MainViewModel extends AbstractModel
|
|||
|
||||
infoModel.resetOldFileNames();
|
||||
//Cover image
|
||||
infoModel.setCoverImage(FileManager.getInfoSlotCover(this.selectedGameView.getGameViewId(), this.selectedGameView.getName()));
|
||||
infoModel.setCoverImage(FileManager.getInfoSlotCover(this.selectedGameView.getGameViewId(),
|
||||
this.selectedGameView.getName()));
|
||||
//Screen images
|
||||
BufferedImage screenImage1 = FileManager.getInfoSlotScreenImage(true, this.selectedGameView.getName());
|
||||
writeGameViewTextOnScreen(screenImage1, Color.yellow);
|
||||
infoModel.setScreen1Image(screenImage1);
|
||||
List<String> screenShotFileNames = new ArrayList<>();
|
||||
if (this.selectedGameView.getGameCount() > 15)
|
||||
{
|
||||
ArrayList<Integer> indexList = new ArrayList<Integer>();
|
||||
for (int i = 0; i < selectedGameView.getGameCount(); i++)
|
||||
{
|
||||
indexList.add(i);
|
||||
}
|
||||
|
||||
BufferedImage screenImage2 = FileManager.getInfoSlotScreenImage(false, this.selectedGameView.getName());
|
||||
writeGameViewTextOnScreen(screenImage2, Color.red);
|
||||
infoModel.setScreen2Image(screenImage2);
|
||||
Collections.shuffle(indexList);
|
||||
|
||||
List<String> gameIdList = new ArrayList<>();
|
||||
int index = 0;
|
||||
while (gameIdList.size() < 32 && index < indexList.size())
|
||||
{
|
||||
GameListData randomGame = this.gameListModel.get(indexList.get(index));
|
||||
if (!randomGame.isInfoSlot())
|
||||
{
|
||||
gameIdList.add(randomGame.getGameId());
|
||||
}
|
||||
index++;
|
||||
}
|
||||
//Only read from db if there are more than 15 games in the current view
|
||||
if (gameIdList.size() > 15)
|
||||
{
|
||||
//Get a screenshot names for the game from db
|
||||
screenShotFileNames = this.dbConnector.getScreenShotNames(gameIdList);
|
||||
Collections.shuffle(screenShotFileNames);
|
||||
}
|
||||
}
|
||||
|
||||
BufferedImage[] images = FileManager.getInfoSlotScreenImage(this.selectedGameView, screenShotFileNames);
|
||||
writeGameViewTextOnScreen(images[0], Color.yellow);
|
||||
infoModel.setScreen1Image(images[0]);
|
||||
writeGameViewTextOnScreen(images[1], Color.red);
|
||||
infoModel.setScreen2Image(images[1]);
|
||||
}
|
||||
|
||||
public void writeGameViewTextOnScreen(BufferedImage image, Color color)
|
||||
|
|
|
@ -274,6 +274,11 @@ public class GameView implements Comparable
|
|||
this.gameCount = count;
|
||||
}
|
||||
|
||||
public int getGameCount()
|
||||
{
|
||||
return this.gameCount;
|
||||
}
|
||||
|
||||
public String getFavNamePreferencesKey()
|
||||
{
|
||||
if (gameViewId < -1)
|
||||
|
|
|
@ -210,7 +210,7 @@ public class FileManager
|
|||
return coverImage;
|
||||
}
|
||||
|
||||
public static BufferedImage getInfoSlotScreenImage(boolean first, String gameviewName)
|
||||
private static BufferedImage getInfoSlotScreenImage(boolean first, String gameviewName)
|
||||
{
|
||||
//Check for USB and check if an existing games folder is available, pick from thumbs...
|
||||
BufferedImage screenImage = null;
|
||||
|
@ -253,6 +253,73 @@ public class FileManager
|
|||
return screenImage;
|
||||
}
|
||||
|
||||
public static BufferedImage[] getInfoSlotScreenImage(GameView gameview, List<String> screenShotFileNames)
|
||||
{
|
||||
BufferedImage[] images = new BufferedImage[2];
|
||||
//Check number of games in current view
|
||||
if (screenShotFileNames.size() > 0)
|
||||
{
|
||||
List<BufferedImage> screenImages = new ArrayList<>();
|
||||
for (String fileName : screenShotFileNames)
|
||||
{
|
||||
try
|
||||
{
|
||||
BufferedImage image = ImageIO.read(new File(SCREENS + fileName));
|
||||
//Scale the image to 80x50
|
||||
screenImages.add(scaleImageTo80x50(image));
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
ExceptionHandler.handleException(e, "Could not read image for info slot screenshot.");
|
||||
}
|
||||
}
|
||||
//Construct the new image
|
||||
BufferedImage combinedImage1 = new BufferedImage(320, 200, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics g1 = combinedImage1.getGraphics();
|
||||
BufferedImage combinedImage2 = new BufferedImage(320, 200, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics g2 = combinedImage2.getGraphics();
|
||||
int yPos = 0;
|
||||
int xPos = 0;
|
||||
|
||||
for (int i = 0; i < screenImages.size(); i++)
|
||||
{
|
||||
if (i == 16)
|
||||
{
|
||||
xPos = 0;
|
||||
yPos = 0;
|
||||
}
|
||||
if (i < 16)
|
||||
{
|
||||
drawInfoSlotCombinedImage(g1, screenImages.get(i), xPos, yPos);
|
||||
}
|
||||
else
|
||||
{
|
||||
drawInfoSlotCombinedImage(g2, screenImages.get(i), xPos, yPos);
|
||||
}
|
||||
//Recalculate position
|
||||
xPos++;
|
||||
if (xPos > 3)
|
||||
{
|
||||
xPos = 0;
|
||||
yPos++;
|
||||
}
|
||||
}
|
||||
images[0] = combinedImage1;
|
||||
images[1] = combinedImage2;
|
||||
}
|
||||
else
|
||||
{
|
||||
images[0] = getInfoSlotScreenImage(true, gameview.getName());
|
||||
images[1] = getInfoSlotScreenImage(true, gameview.getName());
|
||||
}
|
||||
return images;
|
||||
}
|
||||
|
||||
private static void drawInfoSlotCombinedImage(Graphics g, BufferedImage image, int xPos, int yPos)
|
||||
{
|
||||
g.drawImage(image, 0 + xPos * 80, 0 + yPos * 50, null);
|
||||
}
|
||||
|
||||
private static BufferedImage getDefaultInfoSlotImage(boolean first)
|
||||
{
|
||||
BufferedImage screenImage = null;
|
||||
|
@ -1497,6 +1564,26 @@ public class FileManager
|
|||
return newImage;
|
||||
}
|
||||
|
||||
private static BufferedImage scaleImageTo80x50(BufferedImage originalImage)
|
||||
{
|
||||
BufferedImage returnImage = originalImage;
|
||||
if (returnImage.getType() != BufferedImage.TYPE_INT_ARGB)
|
||||
{
|
||||
//Convert to 32 bit
|
||||
BufferedImage copyOfImage =
|
||||
new BufferedImage(returnImage.getWidth(null), returnImage.getHeight(null), BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics g = copyOfImage.createGraphics();
|
||||
g.drawImage(returnImage, 0, 0, null);
|
||||
returnImage = copyOfImage;
|
||||
}
|
||||
Image newImage = returnImage.getScaledInstance(80, 50, Image.SCALE_SMOOTH);
|
||||
BufferedImage copyOfImage =
|
||||
new BufferedImage(newImage.getWidth(null), newImage.getHeight(null), BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics g = copyOfImage.createGraphics();
|
||||
g.drawImage(newImage, 0, 0, null);
|
||||
return copyOfImage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a temporary file from inputStream, just leaving the File as-is.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue