From d06c60a6ccc4d97df62b19e91f18d4aa292d9c57 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 24 May 2014 21:31:11 +0200 Subject: Better load/save --- src/esieequest/controller/commands/Command.java | 1 + .../controller/commands/LoadCommand.java | 17 ++++-- .../controller/commands/SaveCommand.java | 11 +++- src/esieequest/view/Viewable.java | 25 +++++++++ src/esieequest/view/app/UserInterface.java | 63 +++++++++++++++++++++- src/esieequest/view/text/TextInterface.java | 34 ++++++++++++ src/esieequest/view/web/WebInterface.java | 41 +++++++++++--- 7 files changed, 180 insertions(+), 12 deletions(-) diff --git a/src/esieequest/controller/commands/Command.java b/src/esieequest/controller/commands/Command.java index a29b79b..906a312 100644 --- a/src/esieequest/controller/commands/Command.java +++ b/src/esieequest/controller/commands/Command.java @@ -24,6 +24,7 @@ public enum Command { QUIT(new QuitCommand()), HELP(new HelpCommand()), ALEA(new AleaCommand()), + SOUND(new NewCommand()), // map related GO(new GoCommand()), diff --git a/src/esieequest/controller/commands/LoadCommand.java b/src/esieequest/controller/commands/LoadCommand.java index 896d85b..26e18e2 100644 --- a/src/esieequest/controller/commands/LoadCommand.java +++ b/src/esieequest/controller/commands/LoadCommand.java @@ -18,7 +18,16 @@ public class LoadCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { - final String jsonString = argument; + if (argument == null) { + return; + } + + final String jsonString; + if (argument.startsWith("{")) { + jsonString = argument; + } else { + jsonString = view.load(argument); + } JSONObject datas = new JSONObject(); try { @@ -39,12 +48,12 @@ public class LoadCommand implements Executable { return; } - // view.updateQuest(); - // view.updateInventory(game.getPlayer().getItems()); + view.enableInput(); + view.updateQuest(game.getPlayer().getCurrentQuest()); + view.updateInventory(game.getPlayer().getInventory()); view.updateLocation(game.getPlayer().getCurrentRoom(), game.getPlayer() .getCurrentDirection(), game.getPlayer().getCurrentSide(), game.getPlayer() .canGoBack()); } - } diff --git a/src/esieequest/controller/commands/SaveCommand.java b/src/esieequest/controller/commands/SaveCommand.java index 9e32762..3332059 100644 --- a/src/esieequest/controller/commands/SaveCommand.java +++ b/src/esieequest/controller/commands/SaveCommand.java @@ -13,7 +13,16 @@ public class SaveCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { - view.echo(game.serialise().toJSONString()); + if (argument == null) { + return; + } + + if (argument.isEmpty()) { + return; + } + + final String serialisedGame = game.serialise().toJSONString(); + view.save(argument, serialisedGame); } diff --git a/src/esieequest/view/Viewable.java b/src/esieequest/view/Viewable.java index fa53e1a..c650759 100644 --- a/src/esieequest/view/Viewable.java +++ b/src/esieequest/view/Viewable.java @@ -88,4 +88,29 @@ public interface Viewable { */ public void stopMusic(); + /** + * Toggles the sound. + */ + public void toggleSound(); + + /** + * Loads a saved game file. + * + * @param path + * the path of the file + * + * @return the serialised Game + */ + public String load(final String path); + + /** + * Saves a game as a file. + * + * @param path + * the path of the file + * @param serialisedGame + * the serialised Game + */ + public void save(final String path, final String serialisedGame); + } diff --git a/src/esieequest/view/app/UserInterface.java b/src/esieequest/view/app/UserInterface.java index 0f426bd..b00673c 100644 --- a/src/esieequest/view/app/UserInterface.java +++ b/src/esieequest/view/app/UserInterface.java @@ -12,8 +12,12 @@ import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; +import java.io.FileWriter; import java.io.IOException; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.HashMap; import java.util.Map.Entry; import java.util.Timer; @@ -22,12 +26,14 @@ import java.util.TimerTask; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JComponent; +import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JTextPane; import javax.swing.KeyStroke; import javax.swing.border.EmptyBorder; +import javax.swing.filechooser.FileNameExtensionFilter; import lombok.Getter; @@ -62,6 +68,9 @@ abstract class UserInterface implements Viewable, ActionListener { private static final String SOUND_DIR = "res/snd/"; private static final String SOUND_EXT = ".ogg"; + private static final String SAVE_LABEL = "ESIEEquest Game"; + private static final String SAVE_EXT = ".eqg"; + private GameEngine gameEngine; @Getter @@ -154,6 +163,7 @@ abstract class UserInterface implements Viewable, ActionListener { this.soundButton = new JButton(Text.TOGGLE_SOUND_BUTTON.toString()); this.soundButton.setToolTipText(Text.TOGGLE_SOUND_TOOLTIP.toString()); + this.newButton.setActionCommand(Command.SOUND.name()); this.gamePanel.add(this.soundButton); this.filePanel = new JPanel(); @@ -232,6 +242,7 @@ abstract class UserInterface implements Viewable, ActionListener { this.inventoryButton = new JButton(Text.INVENTORY_BUTTON.toString()); this.inventoryButton.setToolTipText(Text.INVENTORY_TOOLTIP.toString()); + this.inventoryButton.setActionCommand(Command.INVENTORY.name()); this.inventoryButton.setFont(font); this.inventoryButton.setPreferredSize(squareButton); this.topControlPanel.add(this.inventoryButton, BorderLayout.WEST); @@ -283,6 +294,7 @@ abstract class UserInterface implements Viewable, ActionListener { this.newButton.addActionListener(actionListener); this.soundButton.addActionListener(actionListener); this.loadButton.addActionListener(actionListener); + this.saveButton.addActionListener(actionListener); this.forwardButton.addActionListener(actionListener); this.inventoryButton.addActionListener(actionListener); this.actionButton.addActionListener(actionListener); @@ -535,10 +547,31 @@ abstract class UserInterface implements Viewable, ActionListener { */ @Override public void actionPerformed(final ActionEvent actionEvent) { - if (actionEvent.getActionCommand() == Text.INVENTORY_BUTTON.toString()) { + if (actionEvent.getActionCommand() == Command.INVENTORY.name()) { this.toggleInventory(); } else if (actionEvent.getActionCommand() == Text.TOGGLE_SOUND_BUTTON.toString()) { this.toggleAudio(); + } else if (actionEvent.getActionCommand() == Command.LOAD.name()) { + final JFileChooser fileChooser = new JFileChooser(); + fileChooser.setFileFilter(new FileNameExtensionFilter(UserInterface.SAVE_LABEL, + UserInterface.SAVE_EXT)); + final int option = fileChooser.showOpenDialog(this.layout); + if (option == JFileChooser.APPROVE_OPTION) { + this.gameEngine.interpret(actionEvent.getActionCommand() + Text.COMMAND_SEPARATOR + + fileChooser.getSelectedFile().getPath()); + } + } else if (actionEvent.getActionCommand() == Command.SAVE.name()) { + final JFileChooser fileChooser = new JFileChooser(); + fileChooser.setFileFilter(new FileNameExtensionFilter(UserInterface.SAVE_LABEL, + UserInterface.SAVE_EXT)); + final int option = fileChooser.showSaveDialog(this.layout); + if (option == JFileChooser.APPROVE_OPTION) { + final String path = fileChooser.getSelectedFile().getPath(); + final String filePath = path.endsWith(UserInterface.SAVE_EXT) ? path : path + + UserInterface.SAVE_EXT; + this.gameEngine.interpret(actionEvent.getActionCommand() + Text.COMMAND_SEPARATOR + + filePath); + } } else { this.gameEngine.interpret(actionEvent.getActionCommand()); this.clearInputField(); @@ -640,4 +673,32 @@ abstract class UserInterface implements Viewable, ActionListener { } } + @Override + public void toggleSound() { + return; + } + + @Override + public String load(final String path) { + final Path filePath = Paths.get(path); + String serialisedGame = null; + try { + serialisedGame = new String(Files.readAllBytes(filePath)); + } catch (final IOException e) { + e.printStackTrace(); + } + return serialisedGame; + } + + @Override + public void save(final String path, final String serialisedGame) { + try { + final FileWriter fileWriter = new FileWriter(path); + fileWriter.write(serialisedGame); + fileWriter.close(); + } catch (final IOException e) { + e.printStackTrace(); + } + } + } diff --git a/src/esieequest/view/text/TextInterface.java b/src/esieequest/view/text/TextInterface.java index 308a4e0..a4a3dad 100644 --- a/src/esieequest/view/text/TextInterface.java +++ b/src/esieequest/view/text/TextInterface.java @@ -1,5 +1,11 @@ package esieequest.view.text; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + import esieequest.controller.GameEngine; import esieequest.controller.commands.Command; import esieequest.model.Text; @@ -107,4 +113,32 @@ abstract class TextInterface implements Viewable { return; } + @Override + public void toggleSound() { + return; + } + + @Override + public String load(final String path) { + final Path filePath = Paths.get(path); + String serialisedGame = null; + try { + serialisedGame = new String(Files.readAllBytes(filePath)); + } catch (final IOException e) { + e.printStackTrace(); + } + return serialisedGame; + } + + @Override + public void save(final String path, final String serialisedGame) { + try { + final FileWriter fileWriter = new FileWriter(path); + fileWriter.write(serialisedGame); + fileWriter.close(); + } catch (final IOException e) { + e.printStackTrace(); + } + } + } diff --git a/src/esieequest/view/web/WebInterface.java b/src/esieequest/view/web/WebInterface.java index 6a4ac35..e749c1f 100644 --- a/src/esieequest/view/web/WebInterface.java +++ b/src/esieequest/view/web/WebInterface.java @@ -7,6 +7,7 @@ import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyDownEvent; import com.google.gwt.event.dom.client.KeyDownHandler; import com.google.gwt.media.client.Audio; +import com.google.gwt.storage.client.Storage; import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; import com.google.gwt.user.client.Event; @@ -49,6 +50,8 @@ class WebInterface extends Composite implements Viewable { private static final String SOUND_DIR = "res/audio/"; private static final String SOUND_EXT = ".ogg"; + private static final String SAVE_KEY = "eqg"; + private GameEngine gameEngine; private static WebInterfaceUiBinder uiBinder = GWT.create(WebInterfaceUiBinder.class); @@ -86,9 +89,11 @@ class WebInterface extends Composite implements Viewable { @UiField Button leftButton; - private Audio audio; + private final Audio audio; private Timer timer; + private final Storage storage; + /** * The web user interface binder interface. */ @@ -112,11 +117,12 @@ class WebInterface extends Composite implements Viewable { this.bindKeys(); this.bindButtons(); + this.storage = Storage.getLocalStorageIfSupported(); + this.audio = Audio.createIfSupported(); - if (this.audio == null) { - return; + if (this.audio != null) { + this.bottomPanel.add(this.audio); } - this.bottomPanel.add(this.audio); } /** @@ -228,11 +234,13 @@ class WebInterface extends Composite implements Viewable { this.loadButton.setText(Text.LOAD_GAME_BUTTON.toString()); this.loadButton.setTitle(Text.LOAD_GAME_TOOLTIP.toString()); - this.loadButton.addClickHandler(this.makeClickHandler(Command.LOAD.name())); + this.loadButton.addClickHandler(this.makeClickHandler(Command.LOAD.name() + + Text.COMMAND_SEPARATOR + WebInterface.SAVE_KEY)); this.saveButton.setText(Text.SAVE_GAME_BUTTON.toString()); this.saveButton.setTitle(Text.SAVE_GAME_TOOLTIP.toString()); - this.saveButton.addClickHandler(this.makeClickHandler(Command.SAVE.name())); + this.saveButton.addClickHandler(this.makeClickHandler(Command.SAVE.name() + + Text.COMMAND_SEPARATOR + WebInterface.SAVE_KEY)); // controls this.actionButton.setTitle(Text.ACTION_TOOLTIP.toString()); @@ -452,4 +460,25 @@ class WebInterface extends Composite implements Viewable { this.audio.setMuted(true); } + @Override + public void toggleSound() { + return; + } + + @Override + public String load(final String path) { + if (this.storage == null) { + return null; + } + return this.storage.getItem(path); + } + + @Override + public void save(final String path, final String serialisedGame) { + if (this.storage == null) { + return; + } + this.storage.setItem(path, serialisedGame); + } + } -- cgit v1.2.3