From 5aa79ea898e16cee1265ba9457c1af11616584cc Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 28 May 2011 20:13:35 +0200 Subject: Implement settings control git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@291 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/control/SettingsControl.java | 178 +++++++++++++++++++++++++---- 1 file changed, 155 insertions(+), 23 deletions(-) (limited to 'src/jrummikub/control') diff --git a/src/jrummikub/control/SettingsControl.java b/src/jrummikub/control/SettingsControl.java index adcc369..7def918 100644 --- a/src/jrummikub/control/SettingsControl.java +++ b/src/jrummikub/control/SettingsControl.java @@ -8,56 +8,188 @@ 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 startGameEvent = new Event1(); + private GameSettings settings = new GameSettings(); + + /** + * Create a new settings control + * + * @param view + * the view to use + */ public SettingsControl(IView view) { this.view = view; + + 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 getStartGameEvent() { return startGameEvent; } + /** + * Start the operation of the settings control + */ public void startSettings() { - /* - * view.getSettingsPanel().getSettingsChangeEvent() .add(new - * IListener1() { - * - * @Override public void handle(GameSettings settings) { - * checkSettings(settings); } }); - * view.getSettingsPanel().getStartGameEvent() .add(new - * IListener1() { - * - * @Override public void handle(GameSettings settings) { - * startGame(settings); } }); - */ - // TODO vvv this is just a temp. fix view.getSettingsPanel().getStartGameEvent().add(new IListener() { - @Override public void handle() { - GameSettings defaultSettings = new GameSettings(); - defaultSettings.getPlayerList().add(new PlayerSettings("Foo", new Color(1.0f, 0, 0))); - defaultSettings.getPlayerList().add(new PlayerSettings("Bar", new Color(0, 1.0f, 0))); - startGame(defaultSettings); + startGame(); + } + }); + view.getSettingsPanel().getAddPlayerEvent().add(new IListener() { + @Override + public void handle() { + addPlayer(); } }); + view.getSettingsPanel().getRemovePlayerEvent() + .add(new IListener1() { + @Override + public void handle(Integer i) { + removePlayer(i); + } + }); + view.getSettingsPanel().getChangePlayerColorEvent() + .add(new IListener2() { + @Override + public void handle(Integer i, Color color) { + setPlayerColor(i, color); + } + }); + view.getSettingsPanel().getChangePlayerNameEvent() + .add(new IListener2() { + @Override + public void handle(Integer i, String name) { + setPlayerName(i, name); + } + }); + view.showSettingsPanel(true); } - private boolean checkSettings(GameSettings settings) { - // TODO Check - // TODO Show error + 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(GameSettings settings) { - if (!checkSettings(settings)) { + private void startGame() { + if (!checkSettings()) { return; } -- cgit v1.2.3