aboutsummaryrefslogtreecommitdiff
path: root/src/esieequest/view/app/UserInterface.java
blob: 4f87ef62dfa5102dafe6994f71c9d7cba0986297 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package esieequest.view.app;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.border.EmptyBorder;

import esieequest.controller.GameEngine;
import esieequest.model.Game;
import esieequest.view.View;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.HashMap;

import javax.swing.JTextField;

import com.wordpress.tipsforjava.swing.StretchIcon;

import java.awt.Font;

public abstract class UserInterface implements View, ActionListener {

	protected Game game;
	private GameEngine gameEngine;

	private JPanel layout;

	private JTextPane questTextPane;
	private JTextPane infoTextPane;

	private JLabel imageLabel;

	private HashMap<String, JButton> gameButtons;
	private HashMap<String, JButton> controlButtons;
	private JTextField inputField;

	/**
	 * Create the panel.
	 */
	public UserInterface() {
		this.buildUI();
		this.inputField.addActionListener(this);
		this.setControlsState(false);
		inputField.setEnabled(true);
	}

	private void buildUI() {
		this.layout = new JPanel();
		this.layout.setLayout(new BorderLayout(0, 0));

		JPanel menuPanel = new JPanel();
		this.layout.add(menuPanel, BorderLayout.NORTH);
		menuPanel.setLayout(new BorderLayout(0, 0));

		JPanel questPanel = new JPanel();
		menuPanel.add(questPanel, BorderLayout.CENTER);

		this.questTextPane = new JTextPane();
		this.questTextPane.setEditable(false);
		this.questTextPane.setText("ESIEEquest");
		questPanel.add(this.questTextPane);

		JPanel gamePanel = new JPanel();
		menuPanel.add(gamePanel, BorderLayout.WEST);

		JButton newButton = new JButton("New");
		newButton.setToolTipText("New game");
		gamePanel.add(newButton);

		JButton soundButton = new JButton("Sound");
		soundButton.setToolTipText("Toggle sound");
		gamePanel.add(soundButton);

		JPanel filePanel = new JPanel();
		menuPanel.add(filePanel, BorderLayout.EAST);

		JButton loadButton = new JButton("Load");
		loadButton.setToolTipText("Load a game");
		filePanel.add(loadButton);

		JButton saveButton = new JButton("Save");
		saveButton.setToolTipText("Save the current game");
		filePanel.add(saveButton);

		JPanel imagePanel = new JPanel();
		layout.add(imagePanel, BorderLayout.CENTER);
		imagePanel.setLayout(new BorderLayout(0, 0));

		this.imageLabel = new JLabel();
		imagePanel.add(this.imageLabel);

		JPanel userPanel = new JPanel();
		this.layout.add(userPanel, BorderLayout.SOUTH);
		userPanel.setLayout(new BorderLayout(0, 0));

		JPanel dispPanel = new JPanel();
		dispPanel.setBorder(new EmptyBorder(5, 5, 5, 0));
		userPanel.add(dispPanel, BorderLayout.CENTER);
		dispPanel.setLayout(new BorderLayout(0, 0));

		this.infoTextPane = new JTextPane();
		this.infoTextPane.setEditable(false);
		this.infoTextPane.setText("Welcome to ESIEEquest!");
		dispPanel.add(this.infoTextPane);

		inputField = new JTextField();
		dispPanel.add(inputField, BorderLayout.SOUTH);
		inputField.setColumns(10);

		JPanel controlPanel = new JPanel();
		controlPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		userPanel.add(controlPanel, BorderLayout.EAST);
		controlPanel.setLayout(new BorderLayout(0, 0));

		JPanel topControlPanel = new JPanel();
		controlPanel.add(topControlPanel, BorderLayout.NORTH);
		topControlPanel.setLayout(new BorderLayout(0, 0));

		JButton forwardButton = new JButton("↥");
		forwardButton.setFont(new Font("Dialog", Font.BOLD, 19));
		forwardButton.setToolTipText("Go forward");
		topControlPanel.add(forwardButton, BorderLayout.CENTER);

		JButton inventoryButton = new JButton("⇱");
		inventoryButton.setFont(new Font("Dialog", Font.BOLD, 19));
		inventoryButton.setToolTipText("Use item");
		topControlPanel.add(inventoryButton, BorderLayout.WEST);

		JButton actionButton = new JButton("⇲");
		actionButton.setFont(new Font("Dialog", Font.BOLD, 19));
		actionButton.setToolTipText("Talk/Take item");
		topControlPanel.add(actionButton, BorderLayout.EAST);

		JPanel bottomControlPanel = new JPanel();
		controlPanel.add(bottomControlPanel, BorderLayout.SOUTH);
		bottomControlPanel.setLayout(new BorderLayout(0, 0));

		JButton backButton = new JButton("↧");
		backButton.setFont(new Font("Dialog", Font.BOLD, 19));
		backButton.setToolTipText("Go back");
		bottomControlPanel.add(backButton, BorderLayout.CENTER);

		JButton leftButton = new JButton("↰");
		leftButton.setFont(new Font("Dialog", Font.BOLD, 19));
		leftButton.setToolTipText("Turn left");
		bottomControlPanel.add(leftButton, BorderLayout.WEST);

		JButton rightButton = new JButton("↱");
		rightButton.setFont(new Font("Dialog", Font.BOLD, 19));
		rightButton.setToolTipText("Turn right");
		bottomControlPanel.add(rightButton, BorderLayout.EAST);

		Dimension squareButton = new Dimension(50, 50);
		forwardButton.setPreferredSize(squareButton);
		inventoryButton.setPreferredSize(squareButton);
		actionButton.setPreferredSize(squareButton);
		backButton.setPreferredSize(squareButton);
		leftButton.setPreferredSize(squareButton);
		rightButton.setPreferredSize(squareButton);

		newButton.setActionCommand("new");
		soundButton.setActionCommand("sound");
		loadButton.setActionCommand("load");
		saveButton.setActionCommand("save");

		forwardButton.setActionCommand("forward");
		inventoryButton.setActionCommand("inventory");
		actionButton.setActionCommand("do");
		backButton.setActionCommand("back");
		leftButton.setActionCommand("turn left");
		rightButton.setActionCommand("turn right");

		this.gameButtons = new HashMap<String, JButton>();

		this.gameButtons.put("newButton", newButton);
		this.gameButtons.put("soundButton", soundButton);
		this.gameButtons.put("loadButton", loadButton);
		this.gameButtons.put("saveButton", saveButton);

		this.controlButtons = new HashMap<String, JButton>();

		this.controlButtons.put("forwardButton", forwardButton);
		this.controlButtons.put("inventoryButton", inventoryButton);
		this.controlButtons.put("actionButton", actionButton);
		this.controlButtons.put("backButton", backButton);
		this.controlButtons.put("leftButton", leftButton);
		this.controlButtons.put("rightButton", rightButton);
	}

	public void actionPerformed(ActionEvent actionEvent) {
		this.gameEngine.interpret(actionEvent.getActionCommand());
	}

	public JPanel getLayout() {
		return this.layout;
	}

	private void setActionListener(HashMap<String, JButton> hashMap, ActionListener actionListener) {
		for (JButton vButton : hashMap.values()) {
			vButton.addActionListener(actionListener);
		}
	}

	public void setActionListener(ActionListener actionListener) {
		this.inputField.addActionListener(actionListener);
		this.setActionListener(this.gameButtons, actionListener);
		this.setActionListener(this.controlButtons, actionListener);
	}

	private void clearInputField() {
		this.inputField.setText(null);
	}

	private void setControlsState(boolean state) {
		this.inputField.setEnabled(state);
		for (JButton vButton : this.controlButtons.values()) {
			vButton.setEnabled(state);
		}
	}

	private void setQuest(String quest) {
		this.questTextPane.setText(quest);
	}

	private void updateIllustration() {
		String imageName = this.game.getLocationImageName();
		if (imageName == null) {
			imageName = "res/img/placeholder.jpg";
		}
		URL imageURL = this.getClass().getClassLoader().getResource(imageName);
		StretchIcon imageIcon = new StretchIcon(imageURL, true);
		this.imageLabel.setIcon(imageIcon);
	}

	@Override
	public void setModel(Game game) {
		this.game = game;
	}

	@Override
	public void setController(GameEngine gameEngine) {
		this.gameEngine = gameEngine;
	}

	@Override
	public void echo(String message) {
		this.infoTextPane.setText(message);
		this.clearInputField();
	}

	@Override
	public void refresh() {
		// TODO Auto-generated method stub

	}

}