feat: carousel preview wip

This commit is contained in:
lantzelot-swe 2023-11-27 22:44:51 +01:00
parent c6cb2a1f12
commit 14a22f0921
6 changed files with 159 additions and 116 deletions

View File

@ -1,12 +1,12 @@
package se.lantz.gui;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Map.Entry;
import java.util.Objects;
import javax.swing.JComboBox;
import se.lantz.util.GenreMap;
public class GenreComboBox extends JComboBox<String>
{
@ -15,7 +15,7 @@ public class GenreComboBox extends JComboBox<String>
*/
private static final long serialVersionUID = 8793039092191107043L;
Map<String, String> valueMap = new HashMap<>();
GenreMap genreMap = new GenreMap();
public GenreComboBox()
{
@ -33,22 +33,11 @@ public class GenreComboBox extends JComboBox<String>
this.addItem("Shoot'em up");
this.addItem("Simulation");
this.addItem("Sport");
valueMap.put("", "----");
valueMap.put("adventure", "Adventure");
valueMap.put("driving", "Driving");
valueMap.put("maze", "Maze");
valueMap.put("platform", "Platform");
valueMap.put("programming", "Programming");
valueMap.put("puzzle", "Puzzle");
valueMap.put("shoot", "Shoot'em up");
valueMap.put("simulation", "Simulation");
valueMap.put("sport", "Sport");
}
public void setSelectedGenre(String genre)
{
String item = valueMap.get(genre);
String item = genreMap.get(genre);
if (item != null)
{
this.setSelectedItem(item);
@ -62,7 +51,7 @@ public class GenreComboBox extends JComboBox<String>
public String getSelectedGenre()
{
for (Entry<String, String> entry : valueMap.entrySet())
for (Entry<String, String> entry : genreMap.entrySet())
{
if (Objects.equals(getSelectedItem(), entry.getValue()))
{

View File

@ -1,39 +1,43 @@
package se.lantz.gui.carousel;
import java.awt.Graphics;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import se.lantz.gui.MainWindow;
import se.lantz.model.MainViewModel;
import se.lantz.model.carousel.CarouselPreviewModel;
import se.lantz.model.data.GameDetails;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.beans.Beans;
import java.io.File;
import java.io.IOException;
import java.awt.Color;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import se.lantz.gui.MainWindow;
import se.lantz.model.carousel.CarouselPreviewModel;
public class BackgroundPanel extends JPanel
{
private boolean screen1Showing = true;
private ActionListener screenshotSwitchAction = e -> loadScreenshot(!screen1Showing);
private Timer screenshotRotationTimer = new Timer(4000, screenshotSwitchAction);
private CarouselPreviewModel model;
private MainWindow mainWindow;
private Image background;
private JLabel screenShotLabel;
private TextPanel textPanel;
private CoverPanel coverPanel;
public BackgroundPanel(final CarouselPreviewModel model, final MainWindow mainWindow)
{
this.mainWindow = mainWindow;
@ -85,6 +89,45 @@ public class BackgroundPanel extends JPanel
mainWindow.getMainPanel().runCurrentGame();
}
});
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "scrollRight");
getActionMap().put("scrollRight", new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
getCoverPanel().scrollOneGame(true);
}
});
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "scrollLeft");
getActionMap().put("scrollLeft", new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
getCoverPanel().scrollOneGame(false);
}
});
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0), "pageUp");
getActionMap().put("pageUp", new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
getCoverPanel().pageUpTriggered();
}
});
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0), "pageDown");
getActionMap().put("pageDown", new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
getCoverPanel().pageDownTriggered();
}
});
}
private void reloadScreens()
@ -93,7 +136,13 @@ public class BackgroundPanel extends JPanel
{
return;
}
String filename = model.getSelectedGame().getScreen1();
loadScreenshot(true);
screenshotRotationTimer.restart();
}
private void loadScreenshot(boolean screen1)
{
String filename = screen1 ? model.getSelectedGame().getScreen1() : model.getSelectedGame().getScreen2();
BufferedImage image = null;
if (!filename.isEmpty())
{
@ -113,13 +162,9 @@ public class BackgroundPanel extends JPanel
{
getScreenshotLabel().setIcon(null);
}
screen1Showing = screen1;
}
private Image background;
private JLabel screenShotLabel;
private TextPanel textPanel;
private CoverPanel coverPanel;
public void paintComponent(Graphics g)
{
@ -147,10 +192,6 @@ public class BackgroundPanel extends JPanel
if (screenShotLabel == null)
{
screenShotLabel = new JLabel();
// Image image = new ImageIcon(getClass().getResource(selectedGame.getScreen1())).getImage();
// Image scaledImage = image.getScaledInstance(694, 401, Image.SCALE_SMOOTH);
// screenShotLabel.setIcon(new ImageIcon(scaledImage));
}
return screenShotLabel;
}

View File

@ -6,9 +6,9 @@ import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
import java.beans.Beans;
@ -17,14 +17,12 @@ import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import org.slf4j.Logger;
@ -88,27 +86,6 @@ public class CoverPanel extends JPanel
GridBagLayout gbl_panel = new GridBagLayout();
panel.setLayout(gbl_panel);
panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0),
"scrollRight");
panel.getActionMap().put("scrollRight", new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
scrollOneGame(true);
}
});
panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0),
"scrollLeft");
panel.getActionMap().put("scrollLeft", new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
scrollOneGame(false);
}
});
panel.setBackground(new Color(138, 137, 138));
}
return panel;
@ -134,11 +111,11 @@ public class CoverPanel extends JPanel
if (scrollDirectionRight)
{
gameId = model.getNextGameToSelectWhenScrollingRight().getGameId();
gameId = model.getNextGameToSelectWhenScrollingRight();
}
else
{
gameId = model.getNextGameToSelectWhenScrollingLeft().getGameId();
gameId = model.getNextGameToSelectWhenScrollingLeft();
}
this.mainWindow.setSelectedGameInGameList(gameId);
}
@ -160,7 +137,6 @@ public class CoverPanel extends JPanel
{
scrollPane.removeMouseWheelListener(listener);
}
scrollPane.addMouseWheelListener(a -> scrollOneGame(a.getWheelRotation() > 0));
}
return scrollPane;
}
@ -197,6 +173,19 @@ public class CoverPanel extends JPanel
if (index == 4)
{
label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 5));
label.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2)
{
//trigger run game
mainWindow.getMainPanel().runCurrentGame();
}
}
});
}
return label;
}
@ -210,6 +199,18 @@ public class CoverPanel extends JPanel
scrolingTimer.start();
}
}
protected void pageUpTriggered()
{
String gameId = model.getGameIdForPageUp();
this.mainWindow.setSelectedGameInGameList(gameId);
}
protected void pageDownTriggered()
{
String gameId = model.getGameIdForPageDown();
this.mainWindow.setSelectedGameInGameList(gameId);
}
private void reloadScreens()
{

View File

@ -21,6 +21,7 @@ import javax.swing.text.StyleConstants;
import se.lantz.model.carousel.CarouselPreviewModel;
import se.lantz.model.data.GameDetails;
import se.lantz.util.GenreMap;
public class TextPanel extends JPanel
{
@ -31,8 +32,7 @@ public class TextPanel extends JPanel
private JLabel genreLabel;
private JLabel yearLabel;
private CarouselPreviewModel model;
Map<String, String> genreMap = new HashMap<>();
private GenreMap genreMap = new GenreMap();
public TextPanel(final CarouselPreviewModel model)
{
@ -92,18 +92,6 @@ public class TextPanel extends JPanel
//trigger once at startup
selectedGameChanged();
}
//TODO Centralize this. look at GenreComboBox
genreMap.put("", "----");
genreMap.put("adventure", "Adventure");
genreMap.put("driving", "Driving");
genreMap.put("maze", "Maze");
genreMap.put("platform", "Platform");
genreMap.put("programming", "Programming");
genreMap.put("puzzle", "Puzzle");
genreMap.put("shoot", "Shoot'em up");
genreMap.put("simulation", "Simulation");
genreMap.put("sport", "Sport");
}
private void selectedGameChanged()
@ -113,32 +101,13 @@ public class TextPanel extends JPanel
{
getTitleLabel().setText(selectedGame.getTitle());
getTextPane().setText(selectedGame.getDescription());
getAuthorLabel().setText(selectedGame.getAuthor().isEmpty() ? " " : selectedGame.getAuthor());
getComposerLabel().setText(selectedGame.getComposer().isEmpty() ? " " : selectedGame.getComposer());
getAuthorLabel().setText(selectedGame.getAuthor().isEmpty() ? "-" : selectedGame.getAuthor());
getComposerLabel().setText(selectedGame.getComposer().isEmpty() ? "-" : selectedGame.getComposer());
getGenreLabel().setText(genreMap.get(selectedGame.getGenre()));
getYearLabel().setText(selectedGame.getYear() + "");
}
}
public TextPanel(LayoutManager layout)
{
super(layout);
// TODO Auto-generated constructor stub
}
public TextPanel(boolean isDoubleBuffered)
{
super(isDoubleBuffered);
// TODO Auto-generated constructor stub
}
public TextPanel(LayoutManager layout, boolean isDoubleBuffered)
{
super(layout, isDoubleBuffered);
// TODO Auto-generated constructor stub
}
private JLabel getTitleLabel()
{
if (titleLabel == null)

View File

@ -65,16 +65,28 @@ public class CarouselPreviewModel extends AbstractModel
return selectedGame;
}
public GameDetails getNextGameToSelectWhenScrollingRight()
public String getNextGameToSelectWhenScrollingRight()
{
int index = dataList.indexOf(selectedGame) + 1;
return dataList.get(index);
return dataList.get(index).getGameId();
}
public GameDetails getNextGameToSelectWhenScrollingLeft()
public String getNextGameToSelectWhenScrollingLeft()
{
int index = dataList.indexOf(selectedGame) - 1;
return dataList.get(index);
return dataList.get(index).getGameId();
}
public String getGameIdForPageUp()
{
int index = dataList.indexOf(selectedGame) - 4;
return dataList.get(index).getGameId();
}
public String getGameIdForPageDown()
{
int index = dataList.indexOf(selectedGame) + 4;
return dataList.get(index).getGameId();
}
public void setSelectedGame(GameDetails selectedGame)
@ -82,7 +94,14 @@ public class CarouselPreviewModel extends AbstractModel
logger.debug("setSelectedGame: " + selectedGame);
//Update the entire data list
dataList = mainModel.readGameDetailsForCarouselPreview();
this.selectedGame = dataList.get(4);
this.notifyChange(SELECTED_GAME);
if (dataList.size() < 10)
{
this.notifyChange(CLOSE_PREVIEW);
}
else
{
this.selectedGame = dataList.get(4);
this.notifyChange(SELECTED_GAME);
}
}
}

View File

@ -0,0 +1,24 @@
package se.lantz.util;
import java.util.HashMap;
/**
* Map holding genres supported by the Carousel as keys and Visual strings in the UI as values
*
*/
public class GenreMap extends HashMap<String, String>
{
public GenreMap()
{
this.put("", "----");
this.put("adventure", "Adventure");
this.put("driving", "Driving");
this.put("maze", "Maze");
this.put("platform", "Platform");
this.put("programming", "Programming");
this.put("puzzle", "Puzzle");
this.put("shoot", "Shoot'em up");
this.put("simulation", "Simulation");
this.put("sport", "Sport");
}
}