summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/SettingsPanel.java
blob: 03cfd4c37321327fdce197cb3f0ca29507fb1408 (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package jrummikub.view.impl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
import jrummikub.util.Event1;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.util.IEvent2;
import jrummikub.view.ISettingsPanel;

@SuppressWarnings("serial")
class SettingsPanel extends JPanel implements ISettingsPanel {
	private final static Color[] PLAYER_COLORS = { new Color(1.0f, 0, 0), // red
			new Color(0, 1.0f, 0), // lime
			new Color(1.0f, 1.0f, 0), // yellow
			new Color(0, 0, 1.0f), // blue
			new Color(1.0f, 0, 1.0f), // fuchsia
			new Color(0, 1.0f, 1.0f), // aqua
			new Color(0.5f, 0, 0), // maroon
			new Color(0, 0.5f, 0), // green
			new Color(0.5f, 0.5f, 0), // olive
			new Color(0, 0, 0.5f), // navy
			new Color(0.5f, 0, 0.5f), // purple
			new Color(0, 0.5f, 0.5f), // teal
			new Color(0, 0, 0), // black
			new Color(0.5f, 0.5f, 0.5f), // gray
			new Color(0.75f, 0.75f, 0.75f), // silver
			new Color(1.0f, 1.0f, 1.0f), // white
	};

	private JPanel playerSetupPanel;
	private JPanel playerSettingsViewport;
	private JPanel ruleSetupPanel;

	private GameSettings gameSettings = new GameSettings();

	private LinkedList<PlayerSettingsPanel> playerSettingsPanels = new LinkedList<PlayerSettingsPanel>();

	private Event1<GameSettings> settingsChangeEvent = new Event1<GameSettings>();
	private Event1<GameSettings> startGameEvent = new Event1<GameSettings>();

	/*
	 * @Override public IEvent1<GameSettings> getSettingsChangeEvent() { return
	 * settingsChangeEvent; }
	 * 
	 * @Override public IEvent1<GameSettings> getStartGameEvent() { return
	 * startGameEvent; }
	 */

	private void addPlayerSettings() {
		// Find unused player name
		int num = playerSettingsPanels.size();

		nameLoop: while (true) {
			num++;
			String playerName = "Spieler " + num;

			for (PlayerSettings player : gameSettings.getPlayerList()) {
				if (playerName.equals(player.getName())) {
					continue nameLoop;
				}
			}
			break;
		}

		// Next, find unused color
		Color color = null;
		colorLoop: for (Color c : PLAYER_COLORS) {
			color = c;
			for (PlayerSettings player : gameSettings.getPlayerList()) {
				if (c == player.getColor()) {
					continue colorLoop;
				}
			}
			break;
		}

		PlayerSettingsPanel panel = new PlayerSettingsPanel("Spieler " + num,
				color);
		playerSettingsPanels.add(panel);
		playerSettingsViewport.add(panel,
				playerSettingsViewport.getComponentCount() - 1);

		settingsChangeEvent.emit(gameSettings);
	}

	private void createPlayerSetupPanel() {
		playerSetupPanel = new JPanel();
		playerSetupPanel.setLayout(new BorderLayout());

		playerSettingsViewport = new JPanel();
		playerSettingsViewport.setLayout(new BoxLayout(playerSettingsViewport,
				BoxLayout.Y_AXIS));

		JScrollPane scrollPane = new JScrollPane(playerSettingsViewport,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		playerSetupPanel.add(scrollPane);

		JPanel addPlayerPanel = new JPanel();
		addPlayerPanel.setLayout(new FlowLayout(FlowLayout.TRAILING, 0, 2));
		playerSettingsViewport.add(addPlayerPanel);

		JButton addPlayerButton = new JButton("+");
		addPlayerButton
				.setFont(addPlayerButton.getFont().deriveFont(Font.BOLD));
		addPlayerButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				addPlayerSettings();
				playerSettingsViewport.revalidate();
			}
		});
		addPlayerPanel.add(addPlayerButton);

		addPlayerSettings();
		addPlayerSettings();
	}

	private void createRuleSetupPanel() {
		ruleSetupPanel = new JPanel();
	}

	SettingsPanel() {
		setLayout(new GridBagLayout());

		final JTabbedPane tabbedPane = new JTabbedPane();

		createPlayerSetupPanel();
		tabbedPane.addTab("Spieler", playerSetupPanel);

		createRuleSetupPanel();
		tabbedPane.addTab("Regeln", ruleSetupPanel);

		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.weightx = 1;
		c.weighty = 1;
		add(tabbedPane, c);

		JLabel errorMessageLabel = new JLabel(" ");
		errorMessageLabel.setForeground(Color.RED);
		c.weighty = 0;
		add(errorMessageLabel, c);

		JButton startButton = new JButton("Spiel starten");
		startButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				startGameEvent.emit(gameSettings);
			}
		});
		add(startButton, c);

		setBorder(new CompoundBorder(new LineBorder(Color.BLACK),
				new EmptyBorder(10, 10, 10, 10)));
	}

	private class PlayerSettingsPanel extends JPanel {
		private JTextField nameField;
		private JButton colorButton;

		private PlayerSettings settings;

		private void remove() {
			playerSettingsPanels.remove(PlayerSettingsPanel.this);
			playerSettingsViewport.remove(PlayerSettingsPanel.this);
			playerSettingsViewport.revalidate();

			gameSettings.getPlayerList().remove(settings);
			settingsChangeEvent.emit(gameSettings);
		}

		private void updateColor() {
			colorButton.setIcon(ImageUtil.createColorIcon(settings.getColor(),
					16, 2));
		}

		private void setName() {
			if (nameField.getText() == settings.getName()) {
				return;
			}

			settings.setName(nameField.getText());
			settingsChangeEvent.emit(gameSettings);
		}

		private void setColor(Color c) {
			if (c == settings.getColor()) {
				return;
			}

			// Exchange colors if the color is already used
			for (PlayerSettingsPanel other : playerSettingsPanels) {
				if (other.settings.getColor() == c) {
					other.settings.setColor(settings.getColor());
					other.updateColor();
					break;
				}
			}

			settings.setColor(c);
			updateColor();

			settingsChangeEvent.emit(gameSettings);
		}

		PlayerSettingsPanel(String name, Color color) {
			settings = new PlayerSettings(name, color);

			setLayout(new BorderLayout());

			colorButton = new JButton();
			updateColor();
			colorButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					new ColorMenu();
				}
			});
			add(colorButton, BorderLayout.WEST);

			nameField = new JTextField(settings.getName());
			nameField.getDocument().addDocumentListener(new DocumentListener() {
				@Override
				public void insertUpdate(DocumentEvent e) {
					setName();
				}

				@Override
				public void removeUpdate(DocumentEvent e) {
					setName();
				}

				@Override
				public void changedUpdate(DocumentEvent e) {
					setName();
				}
			});
			add(nameField);

			JButton removeButton = new JButton("\u00d7");
			removeButton.setFont(removeButton.getFont().deriveFont(Font.BOLD));
			removeButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					remove();
				}
			});
			add(removeButton, BorderLayout.EAST);

			gameSettings.getPlayerList().add(settings);
		}

		@Override
		public Dimension getPreferredSize() {
			return getComponent(1).getPreferredSize();
		}

		@Override
		public Dimension getMaximumSize() {
			return new Dimension(Integer.MAX_VALUE, getPreferredSize().height);
		}

		private class ColorMenu extends JPanel {
			ColorMenu() {
				setLayout(new GridLayout(4, 4, 1, 1));

				JPopupMenu menu = new JPopupMenu();

				for (Color c : PLAYER_COLORS) {
					add(new ColorButton(c, menu));
				}

				menu.setLayout(new BorderLayout());
				menu.add(this);
				menu.show(colorButton, 0, colorButton.getHeight());
			}

			private class ColorButton extends JLabel {
				ColorButton(final Color c, final JPopupMenu menu) {
					final Icon normalIcon = ImageUtil.createColorIcon(c, 16, 1);
					final Icon hoverIcon = ImageUtil.createColorIcon(c, 16, 2);

					setIcon(normalIcon);

					addMouseListener(new MouseAdapter() {
						@Override
						public void mouseEntered(MouseEvent e) {
							setIcon(hoverIcon);
						}

						@Override
						public void mouseExited(MouseEvent e) {
							setIcon(normalIcon);
						}

						@Override
						public void mouseClicked(MouseEvent e) {
							menu.setVisible(false);
							setColor(c);
						}
					});
				}
			}
		}
	}

	@Override
	public IEvent getAddPlayerEvent() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IEvent1<Integer> getRemovePlayerEvent() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IEvent2<Integer, Color> getChangePlayerColorEvent() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IEvent2<Integer, String> getChangePlayerNameEvent() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IEvent1<Integer> getChangeInitialMeldThresholdEvent() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setError(SettingsError error) {
		// TODO Auto-generated method stub

	}

	@Override
	public void enableStartGameButton(boolean enable) {
		// TODO Auto-generated method stub

	}

	@Override
	public void enableAddPlayerButton(boolean enable) {
		// TODO Auto-generated method stub

	}

	@Override
	public void enableRemovePlayerButtons(boolean enable) {
		// TODO Auto-generated method stub

	}

	@Override
	public IEvent getStartGameEvent() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setGameSettings(GameSettings gameSettings) {
		// TODO Auto-generated method stub

	}
}