summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/SettingsControl.java
blob: 17a1dbf59b1f8a53518c42eeff78bd57fb7ec4d1 (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
package jrummikub.control;

import java.awt.Color;

import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
import jrummikub.util.Event1;
import jrummikub.util.IEvent1;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.util.IListener2;
import jrummikub.view.ISettingsPanel;
import jrummikub.view.IView;

/**
 * The settings control controls the settings panel
 */
public class SettingsControl {
	private IView view;
	private Event1<GameSettings> startGameEvent = new Event1<GameSettings>();

	private GameSettings settings;

	/**
	 * Create a new settings control
	 * 
	 * @param view
	 *          the view to use
	 * @param settings
	 * 			initial game settings
	 */
	public SettingsControl(IView view, GameSettings settings) {
		this.view = view;
		this.settings = settings;
		addPlayer();
		addPlayer();
	}

	/**
	 * the start game event is emitted when the user wants to start a game and the
	 * settings made are valid
	 * 
	 * @return the event
	 */
	public IEvent1<GameSettings> getStartGameEvent() {
		return startGameEvent;
	}

	/**
	 * Start the operation of the settings control
	 */
	public void startSettings() {
		view.getSettingsPanel().setInitialMeldThreshold(settings.getInitialMeldThreshold());
		
		view.getSettingsPanel().getChangeInitialMeldThresholdEvent().add(new IListener1<Integer>() {
			@Override
			public void handle(Integer value) {
				settings.setInitialMeldThreshold(value);
				update();
			}
		});
		
		addPlayerSettingsListeners();

		view.showSettingsPanel(true);
	}

	private void addPlayerSettingsListeners() {
		view.getSettingsPanel().getStartGameEvent().add(new IListener() {
			@Override
			public void handle() {
				startGame();
			}
		});
		view.getSettingsPanel().getAddPlayerEvent().add(new IListener() {
			@Override
			public void handle() {
				addPlayer();
			}
		});
		view.getSettingsPanel().getRemovePlayerEvent()
				.add(new IListener1<Integer>() {
					@Override
					public void handle(Integer i) {
						removePlayer(i);
					}
				});
		view.getSettingsPanel().getChangePlayerColorEvent()
				.add(new IListener2<Integer, Color>() {
					@Override
					public void handle(Integer i, Color color) {
						setPlayerColor(i, color);
					}
				});
		view.getSettingsPanel().getChangePlayerNameEvent()
				.add(new IListener2<Integer, String>() {
					@Override
					public void handle(Integer i, String name) {
						setPlayerName(i, name);
					}
				});
	}

	private void addPlayer() {
		if (settings.getPlayerList().size() >= ISettingsPanel.PLAYER_COLORS.length) {
			return;
		}

		// Find unused player name
		int num = settings.getPlayerList().size();

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

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

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

		settings.getPlayerList().add(new PlayerSettings("Spieler " + num, color));

		update();
	}

	private void removePlayer(int i) {
		settings.getPlayerList().remove(i);
		update();
	}

	private void setPlayerColor(int i, Color color) {
		PlayerSettings player = settings.getPlayerList().get(i);

		if (player.getColor() == color) {
			return;
		}

		for (PlayerSettings other : settings.getPlayerList()) {
			if (other.getColor() == color) {
				other.setColor(player.getColor());
				break;
			}
		}

		player.setColor(color);
		update();
	}

	private void setPlayerName(int i, String name) {
		settings.getPlayerList().get(i).setName(name);
		update();
	}

	private void update() {
		view.getSettingsPanel().enableRemovePlayerButtons(
				settings.getPlayerList().size() > 2);
		view.getSettingsPanel().enableAddPlayerButton(
				settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length);

		checkSettings();

		view.getSettingsPanel().setGameSettings(settings);
	}

	private boolean checkSettings() {
		for (PlayerSettings player : settings.getPlayerList()) {
			if (player.getName().isEmpty()) {
				view.getSettingsPanel().setError(
						ISettingsPanel.SettingsError.NO_PLAYER_NAME);
				view.getSettingsPanel().enableStartGameButton(false);
				return false;
			}
		}
		for (int i = 0; i < settings.getPlayerList().size(); ++i) {
			String name = settings.getPlayerList().get(i).getName();

			for (int j = i + 1; j < settings.getPlayerList().size(); ++j) {
				if (settings.getPlayerList().get(j).getName().equals(name)) {
					view.getSettingsPanel().setError(
							ISettingsPanel.SettingsError.DUPLICATE_PLAYER_NAME);
					view.getSettingsPanel().enableStartGameButton(false);
					return false;
				}
			}
		}

		view.getSettingsPanel().setError(ISettingsPanel.SettingsError.NO_ERROR);
		view.getSettingsPanel().enableStartGameButton(true);
		return true;
	}

	private void startGame() {
		if (!checkSettings()) {
			return;
		}

		view.showSettingsPanel(false);

		startGameEvent.emit(settings);
	}
}