Skip to content

Commit

Permalink
Merge pull request #52 from SERG-Delft/score-panel
Browse files Browse the repository at this point in the history
Open Up Score Panel for Extension
  • Loading branch information
avandeursen committed Jun 10, 2015
2 parents b883c2e + d2ccb89 commit 8e477c6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/main/java/nl/tudelft/jpacman/ui/PacManUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.swing.JPanel;

import nl.tudelft.jpacman.game.Game;
import nl.tudelft.jpacman.ui.ScorePanel.ScoreFormatter;

/**
* The default JPacMan UI frame. The PacManUI consists of the following
Expand Down Expand Up @@ -60,9 +61,11 @@ public class PacManUI extends JFrame {
* @param keyMappings
* The map of keyCode-to-action entries that will be added as key
* listeners to the interface.
* @param sf
* The formatter used to display the current score.
*/
public PacManUI(final Game game, final Map<String, Action> buttons,
final Map<Integer, Action> keyMappings) {
final Map<Integer, Action> keyMappings, ScoreFormatter sf) {
super("JPac-Man");
assert game != null;
assert buttons != null;
Expand All @@ -76,8 +79,12 @@ public PacManUI(final Game game, final Map<String, Action> buttons,
JPanel buttonPanel = new ButtonPanel(buttons, this);

scorePanel = new ScorePanel(game.getPlayers());
if (sf != null) {
scorePanel.setScoreFormatter(sf);
}

boardPanel = new BoardPanel(game);

Container contentPanel = getContentPane();
contentPanel.setLayout(new BorderLayout());
contentPanel.add(buttonPanel, BorderLayout.SOUTH);
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/nl/tudelft/jpacman/ui/PacManUiBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;

import nl.tudelft.jpacman.game.Game;
import nl.tudelft.jpacman.ui.ScorePanel.ScoreFormatter;

/**
* Builder for the JPac-Man UI.
Expand Down Expand Up @@ -37,6 +38,11 @@ public class PacManUiBuilder {
* <code>true</code> iff this UI has the default buttons.
*/
private boolean defaultButtons;

/**
* Way to format the score.
*/
private ScoreFormatter scoreFormatter = null;

/**
* Creates a new Pac-Man UI builder without any mapped keys or buttons.
Expand All @@ -61,7 +67,7 @@ public PacManUI build(final Game game) {
addStartButton(game);
addStopButton(game);
}
return new PacManUI(game, buttons, keyMappings);
return new PacManUI(game, buttons, keyMappings, scoreFormatter);
}

/**
Expand Down Expand Up @@ -147,4 +153,17 @@ public PacManUiBuilder withDefaultButtons() {
buttons.put(STOP_CAPTION, null);
return this;
}

/**
* Provide formatter for the score.
*
* @param sf
* The score formatter to be used.
*
* @return The builder.
*/
public PacManUiBuilder withScoreFormatter(ScoreFormatter sf) {
scoreFormatter = sf;
return this;
}
}
47 changes: 43 additions & 4 deletions src/main/java/nl/tudelft/jpacman/ui/ScorePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @author Jeroen Roosen
*
*/
class ScorePanel extends JPanel {
public class ScorePanel extends JPanel {

/**
* Default serialisation ID.
Expand All @@ -28,14 +28,31 @@ class ScorePanel extends JPanel {
* The map of players and the labels their scores are on.
*/
private final Map<Player, JLabel> scoreLabels;

/**
* The default way in which the score is shown.
*/
public static final ScoreFormatter DEFAULT_SCORE_FORMATTER =
// this lambda breaks cobertura 2.7 ...
// player) -> String.format("Score: %3d", player.getScore());
new ScoreFormatter() {
public String format(Player p) {
return String.format("Score: %3d", p.getScore());
}
};

/**
* The way to format the score information.
*/
private ScoreFormatter scoreFormatter = DEFAULT_SCORE_FORMATTER;

/**
* Creates a new score panel with a column for each player.
*
* @param players
* The players to display the scores of.
*/
ScorePanel(List<Player> players) {
public ScorePanel(List<Player> players) {
super();
assert players != null;

Expand All @@ -55,14 +72,36 @@ class ScorePanel extends JPanel {
/**
* Refreshes the scores of the players.
*/
void refresh() {
protected void refresh() {
for (Player p : scoreLabels.keySet()) {
String score = "";
if (!p.isAlive()) {
score = "You died. ";
}
score += String.format("Score: %3d", p.getScore());
score += scoreFormatter.format(p);
scoreLabels.get(p).setText(score);
}
}

/**
* Provide means to format the score for a given player.
*/
public interface ScoreFormatter {

/**
* Format the score of a given player.
* @param p The player and its score
* @return Formatted score.
*/
String format(Player p);
}

/**
* Let the score panel use a dedicated score formatter.
* @param sf Score formatter to be used.
*/
public void setScoreFormatter(ScoreFormatter sf) {
assert sf != null;
scoreFormatter = sf;
}
}

0 comments on commit 8e477c6

Please sign in to comment.