1 package nl.tudelft.jpacman.ui;
2
3 import java.util.HashMap;
4 import java.util.LinkedHashMap;
5 import java.util.Map;
6
7 import nl.tudelft.jpacman.game.Game;
8 import nl.tudelft.jpacman.ui.ScorePanel.ScoreFormatter;
9
10 /**
11 * Builder for the JPac-Man UI.
12 *
13 * @author Jeroen Roosen
14 */
15 public class PacManUiBuilder {
16
17 /**
18 * Caption for the default stop button.
19 */
20 private static final String STOP_CAPTION = "Stop";
21
22 /**
23 * Caption for the default start button.
24 */
25 private static final String START_CAPTION = "Start";
26
27 /**
28 * Map of buttons and their actions.
29 */
30 private final Map<String, Action> buttons;
31
32 /**
33 * Map of key events and their actions.
34 */
35 private final Map<Integer, Action> keyMappings;
36
37 /**
38 * <code>true</code> iff this UI has the default buttons.
39 */
40 private boolean defaultButtons;
41
42 /**
43 * Way to format the score.
44 */
45 private ScoreFormatter scoreFormatter = null;
46
47 /**
48 * Creates a new Pac-Man UI builder without any mapped keys or buttons.
49 */
50 public PacManUiBuilder() {
51 this.defaultButtons = false;
52 this.buttons = new LinkedHashMap<>();
53 this.keyMappings = new HashMap<>();
54 }
55
56 /**
57 * Creates a new Pac-Man UI with the set keys and buttons.
58 *
59 * @param game
60 * The game to build the UI for.
61 * @return A new Pac-Man UI with the set keys and buttons.
62 */
63 public PacManUI build(final Game game) {
64 assert game != null;
65
66 if (defaultButtons) {
67 addStartButton(game);
68 addStopButton(game);
69 }
70 return new PacManUI(game, buttons, keyMappings, scoreFormatter);
71 }
72
73 /**
74 * Adds a button with the caption {@value #STOP_CAPTION} that stops the
75 * game.
76 *
77 * @param game
78 * The game to stop.
79 */
80 private void addStopButton(final Game game) {
81 assert game != null;
82
83 buttons.put(STOP_CAPTION, game::stop);
84 }
85
86 /**
87 * Adds a button with the caption {@value #START_CAPTION} that starts the
88 * game.
89 *
90 * @param game
91 * The game to start.
92 */
93 private void addStartButton(final Game game) {
94 assert game != null;
95
96 buttons.put(START_CAPTION, game::start);
97 }
98
99 /**
100 * Adds a key listener to the UI.
101 *
102 * @param keyCode
103 * The key code of the key as used by {@link java.awt.event.KeyEvent}.
104 * @param action
105 * The action to perform when the key is pressed.
106 * @return The builder.
107 */
108 public PacManUiBuilder addKey(Integer keyCode, Action action) {
109 assert keyCode != null;
110 assert action != null;
111
112 keyMappings.put(keyCode, action);
113 return this;
114 }
115
116 /**
117 * Adds a button to the UI.
118 *
119 * @param caption
120 * The caption of the button.
121 * @param action
122 * The action to execute when the button is clicked.
123 * @return The builder.
124 */
125 public PacManUiBuilder addButton(String caption, Action action) {
126 assert caption != null;
127 assert !caption.isEmpty();
128 assert action != null;
129
130 buttons.put(caption, action);
131 return this;
132 }
133
134 /**
135 * Adds a start and stop button to the UI. The actual actions for these
136 * buttons will be added upon building the UI.
137 *
138 * @return The builder.
139 */
140 public PacManUiBuilder withDefaultButtons() {
141 defaultButtons = true;
142 buttons.put(START_CAPTION, null);
143 buttons.put(STOP_CAPTION, null);
144 return this;
145 }
146
147 /**
148 * Provide formatter for the score.
149 *
150 * @param scoreFormatter
151 * The score formatter to be used.
152 *
153 * @return The builder.
154 */
155 public PacManUiBuilder withScoreFormatter(ScoreFormatter scoreFormatter) {
156 this.scoreFormatter = scoreFormatter;
157 return this;
158 }
159 }