From 67503963112b3d3a225ae00ebc899a34e54b6519 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 10 Mar 2014 15:55:25 +0100 Subject: Refactoring (structure + doc) --- .../wordpress/tipsforjava/swing/StretchIcon.java | 47 ++--- src/esieequest/Main.java | 36 ++-- src/esieequest/controller/GameEngine.java | 45 ++--- src/esieequest/controller/Interpreter.java | 41 +++- src/esieequest/controller/Parser.java | 30 ++- src/esieequest/controller/Performer.java | 107 ++++++---- src/esieequest/controller/package-info.java | 5 + src/esieequest/model/Game.java | 136 +++++++++---- src/esieequest/model/Item.java | 44 ++-- src/esieequest/model/Room.java | 141 +++++++------ src/esieequest/model/command/Command.java | 37 +++- src/esieequest/model/command/CommandWord.java | 60 ++++-- src/esieequest/model/command/package-info.java | 5 + src/esieequest/model/package-info.java | 5 + src/esieequest/package-info.java | 5 + src/esieequest/view/View.java | 32 ++- src/esieequest/view/app/Applet.java | 20 +- src/esieequest/view/app/UserInterface.java | 164 +++++++++------ src/esieequest/view/app/Window.java | 15 +- src/esieequest/view/app/package-info.java | 5 + src/esieequest/view/console/Console.java | 42 ++-- src/esieequest/view/console/package-info.java | 5 + src/esieequest/view/package-info.java | 5 + src/esieequest/view/web/Main.java | 16 +- src/esieequest/view/web/WebInterface.java | 223 ++++++++++++++++++--- src/esieequest/view/web/WebInterface.ui.xml | 25 +-- src/esieequest/view/web/package-info.java | 5 + 27 files changed, 911 insertions(+), 390 deletions(-) create mode 100644 src/esieequest/controller/package-info.java create mode 100644 src/esieequest/model/command/package-info.java create mode 100644 src/esieequest/model/package-info.java create mode 100644 src/esieequest/package-info.java create mode 100644 src/esieequest/view/app/package-info.java create mode 100644 src/esieequest/view/console/package-info.java create mode 100644 src/esieequest/view/package-info.java create mode 100644 src/esieequest/view/web/package-info.java (limited to 'src') diff --git a/src/com/wordpress/tipsforjava/swing/StretchIcon.java b/src/com/wordpress/tipsforjava/swing/StretchIcon.java index 667f1cd..d41bbfe 100644 --- a/src/com/wordpress/tipsforjava/swing/StretchIcon.java +++ b/src/com/wordpress/tipsforjava/swing/StretchIcon.java @@ -10,6 +10,7 @@ import java.awt.Image; import java.awt.Insets; import java.awt.image.ImageObserver; import java.net.URL; + import javax.swing.ImageIcon; /** @@ -44,7 +45,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(byte[]) */ - public StretchIcon(byte[] imageData) { + public StretchIcon(final byte[] imageData) { super(imageData); } @@ -62,7 +63,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(byte[]) */ - public StretchIcon(byte[] imageData, boolean proportionate) { + public StretchIcon(final byte[] imageData, final boolean proportionate) { super(imageData); this.proportionate = proportionate; } @@ -78,7 +79,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(byte[], java.lang.String) */ - public StretchIcon(byte[] imageData, String description) { + public StretchIcon(final byte[] imageData, final String description) { super(imageData, description); } @@ -99,7 +100,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(byte[], java.lang.String) */ - public StretchIcon(byte[] imageData, String description, boolean proportionate) { + public StretchIcon(final byte[] imageData, final String description, final boolean proportionate) { super(imageData, description); this.proportionate = proportionate; } @@ -112,7 +113,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(java.awt.Image) */ - public StretchIcon(Image image) { + public StretchIcon(final Image image) { super(image); } @@ -128,7 +129,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(java.awt.Image) */ - public StretchIcon(Image image, boolean proportionate) { + public StretchIcon(final Image image, final boolean proportionate) { super(image); this.proportionate = proportionate; } @@ -143,7 +144,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(java.awt.Image, java.lang.String) */ - public StretchIcon(Image image, String description) { + public StretchIcon(final Image image, final String description) { super(image, description); } @@ -161,7 +162,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(java.awt.Image, java.lang.String) */ - public StretchIcon(Image image, String description, boolean proportionate) { + public StretchIcon(final Image image, final String description, final boolean proportionate) { super(image, description); this.proportionate = proportionate; } @@ -174,7 +175,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(java.lang.String) */ - public StretchIcon(String filename) { + public StretchIcon(final String filename) { super(filename); } @@ -191,7 +192,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(java.lang.String) */ - public StretchIcon(String filename, boolean proportionate) { + public StretchIcon(final String filename, final boolean proportionate) { super(filename); this.proportionate = proportionate; } @@ -206,7 +207,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(java.lang.String, java.lang.String) */ - public StretchIcon(String filename, String description) { + public StretchIcon(final String filename, final String description) { super(filename, description); } @@ -225,7 +226,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(java.awt.Image, java.lang.String) */ - public StretchIcon(String filename, String description, boolean proportionate) { + public StretchIcon(final String filename, final String description, final boolean proportionate) { super(filename, description); this.proportionate = proportionate; } @@ -238,7 +239,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(java.net.URL) */ - public StretchIcon(URL location) { + public StretchIcon(final URL location) { super(location); } @@ -254,7 +255,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(java.net.URL) */ - public StretchIcon(URL location, boolean proportionate) { + public StretchIcon(final URL location, final boolean proportionate) { super(location); this.proportionate = proportionate; } @@ -269,7 +270,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(java.net.URL, java.lang.String) */ - public StretchIcon(URL location, String description) { + public StretchIcon(final URL location, final String description) { super(location, description); } @@ -287,7 +288,7 @@ public class StretchIcon extends ImageIcon { * * @see ImageIcon#ImageIcon(java.net.URL, java.lang.String) */ - public StretchIcon(URL location, String description, boolean proportionate) { + public StretchIcon(final URL location, final String description, final boolean proportionate) { super(location, description); this.proportionate = proportionate; } @@ -317,23 +318,23 @@ public class StretchIcon extends ImageIcon { * @see ImageIcon#paintIcon(java.awt.Component, java.awt.Graphics, int, int) */ @Override - public synchronized void paintIcon(Component c, Graphics g, int x, int y) { - if (getImage() == null) { + public synchronized void paintIcon(final Component c, final Graphics g, int x, int y) { + if (this.getImage() == null) { return; } - Insets insets = ((Container) c).getInsets(); + final Insets insets = ((Container) c).getInsets(); x = insets.left; y = insets.top; int w = c.getWidth() - x - insets.right; int h = c.getHeight() - y - insets.bottom; - Image image = getImage(); + final Image image = this.getImage(); - if (proportionate) { + if (this.proportionate) { int iw = image.getWidth(c); int ih = image.getHeight(c); - if (iw * h < ih * w) { + if ((iw * h) < (ih * w)) { iw = (h * iw) / ih; x += (w - iw) / 2; w = iw; @@ -344,7 +345,7 @@ public class StretchIcon extends ImageIcon { } } - ImageObserver io = getImageObserver(); + final ImageObserver io = this.getImageObserver(); g.drawImage(image, x, y, w, h, io == null ? c : io); } diff --git a/src/esieequest/Main.java b/src/esieequest/Main.java index b4bfb59..86e6c5e 100755 --- a/src/esieequest/Main.java +++ b/src/esieequest/Main.java @@ -12,31 +12,39 @@ import esieequest.view.app.Window; import esieequest.view.console.Console; /** - * This class instantiates the game and makes it possible to run it via the - * command line, as a graphical application in a window or as an applet. + * The main class of the program. * - * @author Pacien TRAN-GIRARD - * @author Benoit LUBRANO DI SBARAGLIONE + * Instantiates the game and makes it possible to run it via the command line, + * as a graphical application in a window or as an applet. * - * @version February 2014 + * @author Pacien TRAN-GIRARD + * @author Benoît LUBRANO DI SBARAGLIONE */ public class Main extends JApplet { + private static final long serialVersionUID = 1L; + /** - * + * The applet initialiser. Automatically called when the program is started + * as an applet. */ - private static long serialVersionUID = 1726402114771940798L; - + @Override public void init() { - // applet - Game game = new Game(); - Applet applet = new Applet(this); + final Game game = new Game(); + final Applet applet = new Applet(this); new GameEngine(game, applet); } - public static void main(String[] args) { - // cli or standalone - Game game = new Game(); + /** + * The main initialiser for the windowed and the command line views. + * Automatically called when the program is started as a standalone + * application. + * + * @param args + * the command line arguments + */ + public static void main(final String[] args) { + final Game game = new Game(); View view; if (Arrays.asList(args).contains("--nogui")) { diff --git a/src/esieequest/controller/GameEngine.java b/src/esieequest/controller/GameEngine.java index 4a1f47f..c072e77 100644 --- a/src/esieequest/controller/GameEngine.java +++ b/src/esieequest/controller/GameEngine.java @@ -4,22 +4,27 @@ import esieequest.model.Game; import esieequest.view.View; /** - * This class creates all rooms, creates the parser and starts the game. It also - * evaluates and executes the commands that the parser returns. + * The game main controller class. * * @author Pacien TRAN-GIRARD - * @author Benoit LUBRANO DI SBARAGLIONE - * - * @version February 2014 + * @author Benoît LUBRANO DI SBARAGLIONE */ public class GameEngine { - private Game game; - private View view; + private final Game game; + private final View view; - private Interpreter interpreter; + private final Interpreter interpreter; - public GameEngine(Game game, View view) { + /** + * Instantiates a game engine with the given model and view. + * + * @param game + * the game model + * @param view + * the view + */ + public GameEngine(final Game game, final View view) { this.game = game; this.view = view; @@ -27,22 +32,16 @@ public class GameEngine { this.view.setController(this); this.interpreter = new Interpreter(this.game, this.view); - - this.startGame(); - this.startView(); - } - - private void startGame() { - this.game.setRunning(true); - this.interpreter.interpret("new"); - } - - private void startView() { - this.view.enable(); } - public void interpret(String commandString) { - this.interpreter.interpret(commandString); + /** + * Interprets a command (forward it to the interpreter). + * + * @param commandString + * the command string + */ + public void interpret(final String command) { + this.interpreter.interpret(command); } } diff --git a/src/esieequest/controller/Interpreter.java b/src/esieequest/controller/Interpreter.java index 55d468b..5c60c0b 100644 --- a/src/esieequest/controller/Interpreter.java +++ b/src/esieequest/controller/Interpreter.java @@ -1,6 +1,3 @@ -/** - * - */ package esieequest.controller; import esieequest.model.Game; @@ -8,25 +5,47 @@ import esieequest.model.command.Command; import esieequest.view.View; /** - * @author pacien + * The command interpreter. * + * @author Pacien TRAN-GIRARD + * @author Benoît LUBRANO DI SBARAGLIONE */ -public class Interpreter { +class Interpreter { - private Performer performer; - private Parser parser; + private final Performer performer; + private final Parser parser; - public Interpreter(Game game, View view) { + /** + * Instantiates the Interpreter. + * + * @param game + * the Game model + * @param view + * the View + */ + public Interpreter(final Game game, final View view) { this.performer = new Performer(game, view); this.parser = new Parser(); } - public void interpret(String commandString) { - Command command = this.parser.getCommand(commandString); + /** + * Interprets a command. + * + * @param commandString + * the command String + */ + public void interpret(final String commandString) { + final Command command = this.parser.getCommand(commandString); this.dispatch(command); } - public void dispatch(Command command) { + /** + * Dispatches to the method associated with the command's action. + * + * @param command + * the command + */ + public void dispatch(final Command command) { if (command.getAction() != null) { switch (command.getAction()) { case "new": diff --git a/src/esieequest/controller/Parser.java b/src/esieequest/controller/Parser.java index 117d69f..0f97fd1 100644 --- a/src/esieequest/controller/Parser.java +++ b/src/esieequest/controller/Parser.java @@ -2,13 +2,29 @@ package esieequest.controller; import esieequest.model.command.Command; -public class Parser { - +/** + * The command parser. + * + * @author Pacien TRAN-GIRARD + */ +class Parser { + + /** + * The default constructor. + */ public Parser() { } - public Command getCommand(String commandString) { - String[] elements = commandString.split(" "); + /** + * Creates a command from a String. + * + * @param commandString + * the command as a String + * + * @return the command + */ + public Command getCommand(final String commandString) { + final String[] elements = commandString.split(" "); String action = null; String option = null; @@ -23,8 +39,12 @@ public class Parser { return new Command(action, option); } - private void validateCommand() { + /** + * Checks whether the command contains a valid action with a valid option. + */ + private boolean validateCommand() { // TODO + return true; } } diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java index e8aefec..6da3901 100644 --- a/src/esieequest/controller/Performer.java +++ b/src/esieequest/controller/Performer.java @@ -1,71 +1,105 @@ -/** - * - */ package esieequest.controller; -import java.util.Iterator; -import java.util.Set; - import esieequest.model.Game; import esieequest.model.Room; import esieequest.view.View; /** - * @author pacien + * Performs actions and modifies the Game model. * + * @author Pacien TRAN-GIRARD + * @author Benoît LUBRANO DI SBARAGLIONE */ -public class Performer { - - private Game game; - private View view; - - public Performer(Game game, View view) { +class Performer { + + private final Game game; + private final View view; + + /** + * Instantiates a Performer. + * + * @param game + * the Game model + * @param view + * the view + */ + public Performer(final Game game, final View view) { this.game = game; this.view = view; } - private String buildListString(Set elements) { - StringBuilder list = new StringBuilder(); - for (Iterator iterator = elements.iterator(); iterator.hasNext();) { - list.append(" "); - list.append(iterator.next()); - } - return list.toString(); + /** + * Displays the message "Not implemented." in the view. + */ + private void notImplemented() { + this.echo("Not implemented."); } - public void echo(String message) { + /** + * Displays a message in the view. + * + * @param message + * the message + */ + public void echo(final String message) { this.view.echo(message); } + /** + * Loads a new game. + */ public void newGame() { // this.loadGame(default game); - this.echo(this.game.getWelcomeMessage() + "\n" + this.game.getLocationInfo()); + this.view.enable(); + this.echo(this.game.getWelcomeMessage()); } + /** + * Loads a saved game. + */ public void loadGame() { - this.echo("Not implemented."); + this.notImplemented(); } + /** + * Saves the current game. + */ public void saveGame() { - this.echo("Not implemented."); + this.notImplemented(); } + /** + * Toggles the sound. + */ public void toggleSound() { - this.echo("Not implemented."); + this.notImplemented(); } + /** + * Ends the current game. + */ public void quitGame() { this.echo(this.game.getQuitMessage()); - this.game.setRunning(false); + this.view.disable(); } + /** + * Displays the help message and the available commands. + */ public void showHelp() { - this.echo(this.game.getHelpMessage()); + this.notImplemented(); + // this.echo(this.game.getHelpMessage()); // TODO: list commands } - public void goTo(String direction) { - Room nextRoom = this.game.getRoomExit(direction); + /** + * Changes the current room to the one placed in the given direction. + * + * @param direction + * the direction of the new room + */ + public void goTo(final String direction) { + final Room nextRoom = this.game.getRoomExit(direction); if (nextRoom != null) { this.game.goToRoom(nextRoom); this.echo(this.game.getLocationInfo()); @@ -74,17 +108,16 @@ public class Performer { } } + /** + * Displays informations about the current place. + */ public void look() { - this.echo(this.game.getLocationInfo()+"\n"+this.listItems()); - } - - private String listItems() { - if (!this.game.getItemList().isEmpty()) { - return this.game.getItemListPrefix() + this.buildListString(this.game.getItemList()); - } - return ""; + this.echo(this.game.getLocationInfo()); } + /** + * Displays a special message. + */ public void eat() { this.echo(this.game.getEatMessage()); } diff --git a/src/esieequest/controller/package-info.java b/src/esieequest/controller/package-info.java new file mode 100644 index 0000000..dd67b33 --- /dev/null +++ b/src/esieequest/controller/package-info.java @@ -0,0 +1,5 @@ +/** + * The controller package. Contains the logical part of the game. + */ +package esieequest.controller; + diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index c9d7933..99afa7c 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -1,23 +1,22 @@ package esieequest.model; import java.util.HashMap; -import java.util.Set; /** + * Represents the game. * * @author Pacien TRAN-GIRARD - * @author Benoit LUBRANO DI SBARAGLIONE - * - * @version February 2014 + * @author Benoît LUBRANO DI SBARAGLIONE */ public class Game { - private boolean running; - private HashMap rooms; + private final HashMap rooms; private Room currentRoom; + /** + * The default constructor. + */ public Game() { - this.running = false; this.rooms = new HashMap(); this.currentRoom = null; @@ -27,14 +26,36 @@ public class Game { this.goToRoom("AmphitheaterSeat"); } - private void createRoom(String name, String description) { + /** + * Creates a new room. + * + * @param name + * the name of the room + * @param description + * the description of the room + */ + private void createRoom(final String name, final String description) { this.rooms.put(name, new Room(description)); } - private void setRoomExit(String roomName, String direction, String exitRoomName) { - this.rooms.get(roomName).setExit(direction, this.rooms.get(exitRoomName)); + /** + * Sets the exit room of a given room designated by its name. + * + * @param roomName + * the name of the room + * @param direction + * the direction of the exit + * @param exitRoomName + * the name of the exit room + */ + private void setRoomExit(final String roomName, final String direction, + final String exitRoomName) { + this.rooms.get(roomName).addExit(direction, this.rooms.get(exitRoomName)); } + /** + * Creates all the rooms. + */ private void createRooms() { this.createRoom("AmphitheaterSeat", "in the amphitheater"); this.createRoom("AmphitheaterStage", "on the amphitheater stage"); @@ -84,6 +105,9 @@ public class Game { } + /** + * Connects all the rooms. + */ private void linkRooms() { this.setRoomExit("AmphitheaterSeat", "north", "AmphitheaterStage"); this.setRoomExit("AmphitheaterStage", "west", "Cafeteria"); @@ -166,73 +190,117 @@ public class Game { this.setRoomExit("OffscriptMovingcharacter", "west", "OffscriptAlea"); } + /** + * Creates and adds items into rooms. + */ private void createItems() { this.rooms.get("Cafeteria").addItem("Banana", new Item("A yellow banana", 12)); this.rooms.get("Cafeteria").addItem("Orange", new Item("An orange orange", 15)); } - public void setRunning(boolean state) { - this.running = state; - } - - public boolean isRunning() { - return this.running; - } - - public void goToRoom(Room room) { + /** + * Sets the current room. + * + * @param room + * the destination room + */ + public void goToRoom(final Room room) { this.currentRoom = room; } - public void goToRoom(String roomName) { + /** + * Sets the current room designated by its name. + * + * @param roomName + * the destination room name + */ + public void goToRoom(final String roomName) { this.currentRoom = this.rooms.get(roomName); } - public Room getRoomExit(String direction) { + /** + * Returns the current room's exit located in the given direction. + * + * @param direction + * the direction of the exit + * + * @return the exit room + */ + public Room getRoomExit(final String direction) { return this.currentRoom.getExit(direction); } + /** + * Returns informations about the current room (description, exits and + * items). + * + * @return the informations about the current room + */ public String getLocationInfo() { - return this.currentRoom.getLongDescription(); + return this.currentRoom.getInformations(); } + /** + * Returns the current room's image name. + * + * @return the current room's image name. + */ public String getLocationImageName() { return this.currentRoom.getImageName(); } - public String getItemDescription(String itemName) { - return this.currentRoom.getItem(itemName).getDescription(); - } - - public Set getItemList() { - return this.currentRoom.getItemList(); - } - + /** + * Returns the "eat" message. + * + * @return the "eat message + */ public String getEatMessage() { return "oNommNommNomm..."; } + /** + * Returns the welcome message. + * + * @return the welcome message + */ public String getWelcomeMessage() { return "Welcome to ESIEEquest! ESIEEquest is a new, amazingly boring adventure game."; } + /** + * Returns the help message. + * + * @return the help message + */ public String getHelpMessage() { return "Everybody else disappeared. You are alone. You wander around at the ESIEE."; } + /** + * Returns the quit message. + * + * @return the quit message + */ public String getQuitMessage() { return "Thank you for wasting your time. Good bye."; } + /** + * Returns the "no exit" message. + * + * @return the "no exit" message + */ public String getNoExitMessage() { return "There is no exit."; } + /** + * Returns the command list prefix. + * + * @return the command list prefix + */ public String getCommandListPrefix() { return "Available commands:"; } - public String getItemListPrefix() { - return "This room contains:"; - } - } diff --git a/src/esieequest/model/Item.java b/src/esieequest/model/Item.java index dded1a1..d1659ab 100644 --- a/src/esieequest/model/Item.java +++ b/src/esieequest/model/Item.java @@ -1,27 +1,39 @@ -/** - * - */ package esieequest.model; /** - * @author pacien + * Represents an item with a description and a weight, stored in a room. Can be + * picked, used and dropped. * + * @author Pacien TRAN-GIRARD */ public class Item { - private String description; - private int weight; + private final String description; + private final int weight; + /** + * The default constructor. + */ public Item() { this(null, 0); } - public Item(String description, int weight) { + /** + * Creates an item with the given characteristics. + * + * @param description + * the description of the item + * @param weight + * the weight of the item + */ + public Item(final String description, final int weight) { this.description = description; this.weight = weight; } /** + * Returns the description of the item. + * * @return the description */ public String getDescription() { @@ -29,26 +41,12 @@ public class Item { } /** - * @param description - * the description to set - */ - public void setDescription(String description) { - this.description = description; - } - - /** + * Returns the weight of the item. + * * @return the weight */ public int getWeight() { return this.weight; } - /** - * @param weight - * the weight to set - */ - public void setWeight(int weight) { - this.weight = weight; - } - } diff --git a/src/esieequest/model/Room.java b/src/esieequest/model/Room.java index 80b5ccd..2260a3f 100644 --- a/src/esieequest/model/Room.java +++ b/src/esieequest/model/Room.java @@ -1,130 +1,153 @@ package esieequest.model; import java.util.HashMap; -import java.util.Iterator; import java.util.Set; /** - * A room. - * - * A "Room" represents one location in the scenery of the game. It is connected - * to other rooms via exits. The exits are labeled north, east, south, west. For - * each direction, the room stores a reference to the neighboring room, or null - * if there is no exit in that direction. + * Represents one location in the scenery of the game, connected to others via + * exits (labelled north, east, south, west, up, down). For each direction, the + * room stores a reference to the neighbouring room, or null if there is no exit + * in that direction. * * @author Pacien TRAN-GIRARD - * @author Benoit LUBRANO DI SBARAGLIONE - * - * @version February 2014 + * @author Benoît LUBRANO DI SBARAGLIONE */ public class Room { - private String description; - private String imageName; - private HashMap exits; - private HashMap items; + private final String description; + private final String imageName; + private final HashMap exits; + private final HashMap items; /** - * Create a room described "description". Initially, it has no exits. - * "description" is something like "a kitchen" or "an open court yard". + * Creates a room with a description and an associated image. * * @param description - * The room's description. + * the description of the room * @param image - * name The room's image + * the image URL of the room */ - public Room(String description, String imageName) { + public Room(final String description, final String imageName) { this.description = description; this.imageName = imageName; this.exits = new HashMap(); this.items = new HashMap(); } - public Room(String description) { + public Room(final String description) { this(description, null); } /** - * Return the description of the room (the one that was defined in the - * constructor). + * Returns the description of the room. + * + * @return the description of the room */ - public String getShortDescription() { + public String getDescription() { return this.description; } /** - * Return a long description of this room, of the form: - * - * You are in the kitchen. - * - * Exits: north west + * Returns the informations about the room (its description, exits and + * items). * - * @return A description of the room, including exits. + * @return the informations about the room, including exits and items */ - public String getLongDescription() { - return "You are " + this.description + ".\n" + getExitString(); + public String getInformations() { + return "You are " + this.description + ".\n" + this.listExits() + "\n" + this.listItems(); } /** - * Return a string describing the room's image name + * Returns the room's image name. + * + * @return the room's image name */ public String getImageName() { return this.imageName; } /** - * Return the room that is reached if we go from this room in direction - * "direction". If there is no room in that direction, return null. + * Returns the room in the relative given direction or null if there is no + * associated exit. + * + * @return the exit room */ - public Room getExit(String direction) { + public Room getExit(final String direction) { return this.exits.get(direction); } /** - * Return a description of the room's exits, for example - * "Exits: north west". + * Returns an item of the room * - * @return A description of the available exits. + * @param itemName + * the item name + * + * @return the item */ - public String getExitString() { - StringBuilder strBuilder = new StringBuilder("Available exits:"); - Set keys = this.exits.keySet(); - for (Iterator iter = keys.iterator(); iter.hasNext();) { - strBuilder.append(" ").append(iter.next()); - } - strBuilder.append("."); - return strBuilder.toString(); + public Item getItem(final String itemName) { + return this.items.get(itemName); } /** - * Define an exit from this room. + * Defines an exit for the room. * * @param direction - * The direction of the exit. + * the direction of the exit * @param neighbor - * The room in the given direction. + * the room in the given direction */ - public void setExit(String direction, Room neighbor) { + public void addExit(final String direction, final Room neighbor) { this.exits.put(direction, neighbor); } - public Set getItemList() { - return this.items.keySet(); + /** + * Adds an item to the room. + * + * @param item + * the item to set + */ + public void addItem(final String itemName, final Item item) { + this.items.put(itemName, item); } /** - * @return the item + * Lists a description of the room's possible exits. + * + * @return the list of the available exits */ - public Item getItem(String itemName) { - return this.items.get(itemName); + private String listExits() { + if (!this.exits.isEmpty()) { + return "Available exits:" + this.buildListString(this.exits.keySet()) + "."; + } + return "This room has no exit."; } /** - * @param item - * the item to set + * Lists the items contained in the room. + * + * @return the list of the items */ - public void addItem(String itemName, Item item) { - this.items.put(itemName, item); + private String listItems() { + if (!this.items.isEmpty()) { + return "This room contains:" + this.buildListString(this.items.keySet()) + "."; + } + return "This room does not contain any item."; + } + + /** + * Builds a list in the form of a String from given a Set. + * + * @param elements + * the Set of Strings + * + * @return the list + */ + private String buildListString(final Set elements) { + final StringBuilder list = new StringBuilder(); + for (final String string : elements) { + list.append(" ").append(string); + } + return list.toString(); } } diff --git a/src/esieequest/model/command/Command.java b/src/esieequest/model/command/Command.java index 17c17f6..3d76b06 100644 --- a/src/esieequest/model/command/Command.java +++ b/src/esieequest/model/command/Command.java @@ -1,33 +1,60 @@ package esieequest.model.command; +/** + * Represents a command constituted by an action and a parameter. + * + * @author Pacien TRAN-GIRARD + */ public class Command { - private String action; - private String option; + private final String action; + private final String option; - public Command(String action, String option) { + /** + * The constructor. + * + * @param action + * the action + * @param option + * the option + */ + public Command(final String action, final String option) { this.action = action; this.option = option; } /** - * @return the Action + * Returns the action. + * + * @return the action */ public String getAction() { return this.action; } + /** + * Tells if the action is unknown. + * + * @return + */ public boolean isUnknown() { return this.action == null; } /** - * @return the Option + * Returns the option. + * + * @return the option */ public String getOption() { return this.option; } + /** + * Tells if the command contains an option + * + * @return + */ public boolean hasOption() { return this.option != null; } diff --git a/src/esieequest/model/command/CommandWord.java b/src/esieequest/model/command/CommandWord.java index 7f11c0b..b96bcef 100644 --- a/src/esieequest/model/command/CommandWord.java +++ b/src/esieequest/model/command/CommandWord.java @@ -1,41 +1,69 @@ -/** - * - */ package esieequest.model.command; /** - * @author pacien + * Contains the commands that the user can enter * + * @author Pacien TRAN-GIRARD */ public enum CommandWord { - // "" = no option - // new String[] { "" } = any option - NEW("new", new String[] { "" }), LOAD("load", new String[] { "" }), SAVE("save", - new String[] { "" }), SOUND("sound", new String[] { "" }), HELP("help", - new String[] { "" }), QUIT("quit", new String[] { "" }), LOOK("look", - new String[] { "" }), EAT("eat", new String[] { "" }), FORWARD("forward", - new String[] { "" }), BACK("back", new String[] { "" }), GO("go", new String[] { - "north", "south", "east", "west", "up", "down" }), TURN("turn", new String[] { "left", - "right" }), DO("do", new String[] { "" }), TAKE("take", new String[] { "" }), TALK( - "talk", new String[] { "" }), INVENTORY("inventory", new String[] { "" }), USE("use", - null), UNKNOWN("?", new String[] { "" }); + + // @formatter:off + // COMMAND ARGUMENTS ("" = no one ; null = any) + NEW( "new", new String[] { "" }), + LOAD( "load", new String[] { "" }), + SAVE( "save", new String[] { "" }), + SOUND( "sound", new String[] { "" }), + HELP( "help", new String[] { "" }), + QUIT( "quit", new String[] { "" }), + LOOK( "look", new String[] { "" }), + EAT( "eat", new String[] { "" }), + FORWARD( "forward", new String[] { "" }), + BACK( "back", new String[] { "" }), + GO( "go", new String[] { "north", "south", "east", "west", "up", "down" }), + TURN( "turn", new String[] { "left", "right" }), + DO( "do", new String[] { "" }), + TAKE( "take", new String[] { "" }), + TALK( "talk", new String[] { "" }), + INVENTORY( "inventory", new String[] { "" }), + USE( "use", null), + UNKNOWN( "?", new String[] { "" }); + // @formatter:on private String action; private String[] validOptions; - CommandWord(String action, String[] validOption) { + /** + * The constructor. + * + * @param action + * the action command + * @param validOption + * the list of the valid parameters + */ + CommandWord(final String action, final String[] validOption) { this.action = action; this.validOptions = validOption; } + @Override public String toString() { return this.action; } + /** + * Returns the action. + * + * @return the action + */ public String getAction() { return this.action; } + /** + * Returns the valid options. + * + * @return the valid options list + */ public String[] getValidOptions() { return this.validOptions; } diff --git a/src/esieequest/model/command/package-info.java b/src/esieequest/model/command/package-info.java new file mode 100644 index 0000000..9cbeaba --- /dev/null +++ b/src/esieequest/model/command/package-info.java @@ -0,0 +1,5 @@ +/** + * The command model package. Contains the models associated with commands. + */ +package esieequest.model.command; + diff --git a/src/esieequest/model/package-info.java b/src/esieequest/model/package-info.java new file mode 100644 index 0000000..b9f9aa6 --- /dev/null +++ b/src/esieequest/model/package-info.java @@ -0,0 +1,5 @@ +/** + * The model package. Contains the datas of the game's objects. + */ +package esieequest.model; + diff --git a/src/esieequest/package-info.java b/src/esieequest/package-info.java new file mode 100644 index 0000000..f2ca0cb --- /dev/null +++ b/src/esieequest/package-info.java @@ -0,0 +1,5 @@ +/** + * The main package of the ESIEEquest game. + */ +package esieequest; + diff --git a/src/esieequest/view/View.java b/src/esieequest/view/View.java index 8dbd42b..b41a214 100644 --- a/src/esieequest/view/View.java +++ b/src/esieequest/view/View.java @@ -7,19 +7,49 @@ import esieequest.controller.GameEngine; import esieequest.model.Game; /** - * @author pacien + * The view interface that describes an user interface. * + * @author Pacien TRAN-GIRARD */ public interface View { + /** + * Sets the model to use. + * + * @param game + * the Game model + */ public void setModel(Game game); + /** + * Sets the controller to use. + * + * @param gameEngine + * the GameEngine controller + */ public void setController(GameEngine gameEngine); + /** + * Enables the user interface. + */ public void enable(); + /** + * Disables the user interface. + */ + public void disable(); + + /** + * Refreshes the user interface. + */ public void refresh(); + /** + * Displays a message. + * + * @param message + * the message + */ public void echo(String message); } diff --git a/src/esieequest/view/app/Applet.java b/src/esieequest/view/app/Applet.java index 485dbda..3d5bc75 100644 --- a/src/esieequest/view/app/Applet.java +++ b/src/esieequest/view/app/Applet.java @@ -1,25 +1,27 @@ -/** - * - */ package esieequest.view.app; import javax.swing.JApplet; -import esieequest.model.Game; - /** - * @author pacien + * The applet view. * + * @author Pacien TRAN-GIRARD */ public class Applet extends UserInterface { - private JApplet applet; + private final JApplet applet; - public Applet(JApplet applet) { - //this.aGame = pGame; + /** + * The constructor. + * + * @param applet + * the JApplet + */ + public Applet(final JApplet applet) { this.applet = applet; } + @Override public void enable() { this.applet.add(this.getLayout()); } diff --git a/src/esieequest/view/app/UserInterface.java b/src/esieequest/view/app/UserInterface.java index 4f87ef6..b6035c5 100644 --- a/src/esieequest/view/app/UserInterface.java +++ b/src/esieequest/view/app/UserInterface.java @@ -1,29 +1,32 @@ 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.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.URL; import java.util.HashMap; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; import javax.swing.JTextField; +import javax.swing.JTextPane; +import javax.swing.border.EmptyBorder; import com.wordpress.tipsforjava.swing.StretchIcon; -import java.awt.Font; +import esieequest.controller.GameEngine; +import esieequest.model.Game; +import esieequest.view.View; -public abstract class UserInterface implements View, ActionListener { +/** + * The Swing based graphical user interface. + * + * @author Pacien TRAN-GIRARD + */ +abstract class UserInterface implements View, ActionListener { protected Game game; private GameEngine gameEngine; @@ -40,24 +43,27 @@ public abstract class UserInterface implements View, ActionListener { private JTextField inputField; /** - * Create the panel. + * The default constructor. */ public UserInterface() { this.buildUI(); this.inputField.addActionListener(this); this.setControlsState(false); - inputField.setEnabled(true); + this.inputField.setEnabled(true); } + /** + * Creates the interface widgets. + */ private void buildUI() { this.layout = new JPanel(); this.layout.setLayout(new BorderLayout(0, 0)); - JPanel menuPanel = new JPanel(); + final JPanel menuPanel = new JPanel(); this.layout.add(menuPanel, BorderLayout.NORTH); menuPanel.setLayout(new BorderLayout(0, 0)); - JPanel questPanel = new JPanel(); + final JPanel questPanel = new JPanel(); menuPanel.add(questPanel, BorderLayout.CENTER); this.questTextPane = new JTextPane(); @@ -65,40 +71,40 @@ public abstract class UserInterface implements View, ActionListener { this.questTextPane.setText("ESIEEquest"); questPanel.add(this.questTextPane); - JPanel gamePanel = new JPanel(); + final JPanel gamePanel = new JPanel(); menuPanel.add(gamePanel, BorderLayout.WEST); - JButton newButton = new JButton("New"); + final JButton newButton = new JButton("New"); newButton.setToolTipText("New game"); gamePanel.add(newButton); - JButton soundButton = new JButton("Sound"); + final JButton soundButton = new JButton("Sound"); soundButton.setToolTipText("Toggle sound"); gamePanel.add(soundButton); - JPanel filePanel = new JPanel(); + final JPanel filePanel = new JPanel(); menuPanel.add(filePanel, BorderLayout.EAST); - JButton loadButton = new JButton("Load"); + final JButton loadButton = new JButton("Load"); loadButton.setToolTipText("Load a game"); filePanel.add(loadButton); - JButton saveButton = new JButton("Save"); + final JButton saveButton = new JButton("Save"); saveButton.setToolTipText("Save the current game"); filePanel.add(saveButton); - JPanel imagePanel = new JPanel(); - layout.add(imagePanel, BorderLayout.CENTER); + final JPanel imagePanel = new JPanel(); + this.layout.add(imagePanel, BorderLayout.CENTER); imagePanel.setLayout(new BorderLayout(0, 0)); this.imageLabel = new JLabel(); imagePanel.add(this.imageLabel); - JPanel userPanel = new JPanel(); + final JPanel userPanel = new JPanel(); this.layout.add(userPanel, BorderLayout.SOUTH); userPanel.setLayout(new BorderLayout(0, 0)); - JPanel dispPanel = new JPanel(); + final JPanel dispPanel = new JPanel(); dispPanel.setBorder(new EmptyBorder(5, 5, 5, 0)); userPanel.add(dispPanel, BorderLayout.CENTER); dispPanel.setLayout(new BorderLayout(0, 0)); @@ -108,54 +114,54 @@ public abstract class UserInterface implements View, ActionListener { this.infoTextPane.setText("Welcome to ESIEEquest!"); dispPanel.add(this.infoTextPane); - inputField = new JTextField(); - dispPanel.add(inputField, BorderLayout.SOUTH); - inputField.setColumns(10); + this.inputField = new JTextField(); + dispPanel.add(this.inputField, BorderLayout.SOUTH); + this.inputField.setColumns(10); - JPanel controlPanel = new JPanel(); + final 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(); + final JPanel topControlPanel = new JPanel(); controlPanel.add(topControlPanel, BorderLayout.NORTH); topControlPanel.setLayout(new BorderLayout(0, 0)); - JButton forwardButton = new JButton("↥"); + final 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("⇱"); + final 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("⇲"); + final 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(); + final JPanel bottomControlPanel = new JPanel(); controlPanel.add(bottomControlPanel, BorderLayout.SOUTH); bottomControlPanel.setLayout(new BorderLayout(0, 0)); - JButton backButton = new JButton("↧"); + final 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("↰"); + final 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("↱"); + final 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); + final Dimension squareButton = new Dimension(50, 50); forwardButton.setPreferredSize(squareButton); inventoryButton.setPreferredSize(squareButton); actionButton.setPreferredSize(squareButton); @@ -192,71 +198,117 @@ public abstract class UserInterface implements View, ActionListener { this.controlButtons.put("rightButton", rightButton); } - public void actionPerformed(ActionEvent actionEvent) { - this.gameEngine.interpret(actionEvent.getActionCommand()); - } - + /** + * Returns the layout. + * + * @return the layout + */ public JPanel getLayout() { return this.layout; } - private void setActionListener(HashMap hashMap, ActionListener actionListener) { - for (JButton vButton : hashMap.values()) { + /** + * Sets the action listener for the given JButtons. + * + * @param hashMap + * the map of the JButtons + * @param actionListener + * the action listener + */ + private void setActionListener(final HashMap hashMap, + final ActionListener actionListener) { + for (final JButton vButton : hashMap.values()) { vButton.addActionListener(actionListener); } } - public void setActionListener(ActionListener actionListener) { + /** + * Sets the action listener for all UI elements. + * + * @param actionListener + * the action listener + */ + public void setActionListener(final ActionListener actionListener) { this.inputField.addActionListener(actionListener); this.setActionListener(this.gameButtons, actionListener); this.setActionListener(this.controlButtons, actionListener); } + /** + * Clears the textual input field. + */ private void clearInputField() { this.inputField.setText(null); } - private void setControlsState(boolean state) { + /** + * Sets the controls state. + * + * @param state + * the controls state + */ + private void setControlsState(final boolean state) { this.inputField.setEnabled(state); - for (JButton vButton : this.controlButtons.values()) { + for (final JButton vButton : this.controlButtons.values()) { vButton.setEnabled(state); } } - private void setQuest(String quest) { + /** + * Sets the quest title. + * + * @param quest + * the quest title + */ + private void setQuest(final String quest) { this.questTextPane.setText(quest); } + /** + * Updates the room illustration. + */ 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); + final URL imageURL = this.getClass().getClassLoader().getResource(imageName); + final StretchIcon imageIcon = new StretchIcon(imageURL, true); this.imageLabel.setIcon(imageIcon); } + /** + * Translates the received ActionEvent into the corresponding game command + * and forwards it to the game engine. + */ + @Override + public void actionPerformed(final ActionEvent actionEvent) { + this.gameEngine.interpret(actionEvent.getActionCommand()); + } + @Override - public void setModel(Game game) { + public void setModel(final Game game) { this.game = game; } @Override - public void setController(GameEngine gameEngine) { + public void setController(final GameEngine gameEngine) { this.gameEngine = gameEngine; } @Override - public void echo(String message) { + public void echo(final String message) { this.infoTextPane.setText(message); this.clearInputField(); } @Override - public void refresh() { - // TODO Auto-generated method stub + public void disable() { + } + @Override + public void refresh() { + // TODO } } diff --git a/src/esieequest/view/app/Window.java b/src/esieequest/view/app/Window.java index 8ced556..e400fde 100644 --- a/src/esieequest/view/app/Window.java +++ b/src/esieequest/view/app/Window.java @@ -1,33 +1,30 @@ -/** - * - */ package esieequest.view.app; import javax.swing.JFrame; -import esieequest.model.Game; - /** - * @author pacien + * The window view. * + * @author Pacien TRAN-GIRARD */ public class Window extends UserInterface { - private JFrame window; + private final JFrame window; /** - * Create the frame. + * The default constructor that creates the frame and attaches the main + * panel to it. * * @wbp.parser.entryPoint */ public Window() { - //this.aGame = pGame; this.window = new JFrame("ESIEEquest"); this.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.window.setBounds(100, 100, 700, 500); this.window.setContentPane(this.getLayout()); } + @Override public void enable() { this.window.setVisible(true); } diff --git a/src/esieequest/view/app/package-info.java b/src/esieequest/view/app/package-info.java new file mode 100644 index 0000000..af15ea3 --- /dev/null +++ b/src/esieequest/view/app/package-info.java @@ -0,0 +1,5 @@ +/** + * The graphical view package. Contains the graphical user interface builder and binder for the Applet and Windowed views. + */ +package esieequest.view.app; + diff --git a/src/esieequest/view/console/Console.java b/src/esieequest/view/console/Console.java index 4fdad5e..a8acbe0 100644 --- a/src/esieequest/view/console/Console.java +++ b/src/esieequest/view/console/Console.java @@ -1,6 +1,3 @@ -/** - * - */ package esieequest.view.console; import java.util.Scanner; @@ -10,40 +7,57 @@ import esieequest.model.Game; import esieequest.view.View; /** - * @author pacien + * The textual console view. * + * @author Pacien TRAN-GIRARD */ public class Console implements View { private Game game; private GameEngine gameEngine; - private Scanner scanner; + private boolean running; + + /** + * The default constructor. + */ public Console() { - this.scanner = new Scanner(System.in); + this.running = false; } - private void play() { - while (this.game.isRunning()) { + /** + * Runs the input scanner. + */ + private void run() { + Scanner scanner = new Scanner(System.in); + while (this.running) { System.out.print("> "); - String vInputString = this.scanner.nextLine(); - this.gameEngine.interpret(vInputString); + this.gameEngine.interpret(scanner.nextLine()); } + scanner.close(); } @Override - public void setModel(Game game) { + public void setModel(final Game game) { this.game = game; } @Override - public void setController(GameEngine gameEngine) { + public void setController(final GameEngine gameEngine) { this.gameEngine = gameEngine; } @Override public void enable() { - this.play(); + if (!this.running) { + this.running = true; + this.run(); + } + } + + @Override + public void disable() { + this.running = false; } @Override @@ -52,7 +66,7 @@ public class Console implements View { } @Override - public void echo(String message) { + public void echo(final String message) { System.out.println(message); } diff --git a/src/esieequest/view/console/package-info.java b/src/esieequest/view/console/package-info.java new file mode 100644 index 0000000..24089ab --- /dev/null +++ b/src/esieequest/view/console/package-info.java @@ -0,0 +1,5 @@ +/** + * The console view package. Contains the textual user interface and its associated input stream processor. + */ +package esieequest.view.console; + diff --git a/src/esieequest/view/package-info.java b/src/esieequest/view/package-info.java new file mode 100644 index 0000000..c42ddc8 --- /dev/null +++ b/src/esieequest/view/package-info.java @@ -0,0 +1,5 @@ +/** + * The view package. Contains the different user interfaces of the game. + */ +package esieequest.view; + diff --git a/src/esieequest/view/web/Main.java b/src/esieequest/view/web/Main.java index 4e58fd7..5417ca2 100644 --- a/src/esieequest/view/web/Main.java +++ b/src/esieequest/view/web/Main.java @@ -1,17 +1,27 @@ package esieequest.view.web; import com.google.gwt.core.client.EntryPoint; -import com.google.gwt.user.client.ui.RootLayoutPanel; import esieequest.controller.GameEngine; import esieequest.model.Game; +/** + * The web view entry point and initialisation class. The recursively imported + * code will be translated to Javascript by the GWT compiler to build a web + * application. + * + * @author Pacien TRAN-GIRARD + */ public class Main implements EntryPoint { + /** + * The web view initialiser. Automatically called when the program is + * started as a web application. + */ @Override public void onModuleLoad() { - Game game = new Game(); - WebInterface webInterface = new WebInterface(); + final Game game = new Game(); + final WebInterface webInterface = new WebInterface(); new GameEngine(game, webInterface); } diff --git a/src/esieequest/view/web/WebInterface.java b/src/esieequest/view/web/WebInterface.java index 03be1ac..ca1598c 100644 --- a/src/esieequest/view/web/WebInterface.java +++ b/src/esieequest/view/web/WebInterface.java @@ -1,9 +1,8 @@ -/** - * - */ package esieequest.view.web; import com.google.gwt.core.client.GWT; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyDownEvent; import com.google.gwt.event.dom.client.KeyDownHandler; @@ -11,32 +10,38 @@ import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.Composite; +import com.google.gwt.user.client.ui.Frame; +import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.RootLayoutPanel; +import com.google.gwt.user.client.ui.TabLayoutPanel; +import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.Widget; -import com.google.gwt.user.client.ui.Label; import esieequest.controller.GameEngine; import esieequest.model.Game; import esieequest.view.View; -import com.google.gwt.user.client.ui.TextBox; - /** - * @author pacien + * The web view. * + * @author Pacien TRAN-GIRARD */ -public class WebInterface extends Composite implements View { +class WebInterface extends Composite implements View { private GameEngine gameEngine; private Game game; private static WebInterfaceUiBinder uiBinder = GWT.create(WebInterfaceUiBinder.class); + @UiField + TabLayoutPanel bottomPanel; @UiField Label displayLabel; @UiField Label questLabel; @UiField + Frame illustrationFrame; + @UiField TextBox inputField; @UiField Button newButton; @@ -59,61 +64,217 @@ public class WebInterface extends Composite implements View { @UiField Button leftButton; + /** + * The web user interface binder interface. + */ interface WebInterfaceUiBinder extends UiBinder { } /** - * Because this class has a default constructor, it can be used as a binder - * template. In other words, it can be used in other *.ui.xml files as - * follows: - * Hello! Note that - * depending on the widget that is used, it may be necessary to implement - * HasHTML instead of HasText. + * The default constructor that initialises the user interface and binds + * event handlers. */ public WebInterface() { - initWidget(uiBinder.createAndBindUi(this)); + this.initWidget(WebInterface.uiBinder.createAndBindUi(this)); + this.bindInputField(); + this.bindButtons(); + RootLayoutPanel.get().add(this); + } + + /** + * Forwards a command string to the game engine for interpretation. + * + * @param commandString + * the command string + */ + private void interpret(final String commandString) { + this.gameEngine.interpret(commandString); + } - inputField.addKeyDownHandler(new KeyDownHandler() { - public void onKeyDown(KeyDownEvent event) { + /** + * Creates a custom click handler that sends a command to the controller on + * click. + * + * @param command + * the command String + * @return the click handler + */ + private ClickHandler makeClickHandler(final String command) { + return new ClickHandler() { + @Override + public void onClick(final ClickEvent event) { + WebInterface.this.closeInventory(); + WebInterface.this.interpret(command); + } + }; + } + + /** + * Binds the textual input field to send the entered command to the + * controller. + */ + private void bindInputField() { + this.inputField.addKeyDownHandler(new KeyDownHandler() { + @Override + public void onKeyDown(final KeyDownEvent event) { if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) { - gameEngine.interpret(inputField.getText()); - inputField.setText(null); + WebInterface.this.interpret(WebInterface.this.inputField.getText()); + WebInterface.this.inputField.setText(null); + WebInterface.this.closeInventory(); } } }); + } + /** + * Binds the buttons to send a defined command to the controller. + */ + private void bindButtons() { + this.newButton.addClickHandler(this.makeClickHandler("new")); + this.loadButton.addClickHandler(this.makeClickHandler("load")); + this.saveButton.addClickHandler(this.makeClickHandler("save")); + this.actionButton.addClickHandler(this.makeClickHandler("action")); + this.forwardButton.addClickHandler(this.makeClickHandler("go forward")); + this.backButton.addClickHandler(this.makeClickHandler("go back")); + this.leftButton.addClickHandler(this.makeClickHandler("turn left")); + this.rightButton.addClickHandler(this.makeClickHandler("turn right")); + + this.soundButton.addClickHandler(new ClickHandler() { + @Override + public void onClick(final ClickEvent event) { + WebInterface.this.toggleSound(); + } + }); + + this.inventoryButton.addClickHandler(new ClickHandler() { + @Override + public void onClick(final ClickEvent event) { + WebInterface.this.toggleInventory(); + } + }); + } + + /** + * Sets the controls (the text input field and the control buttons) state. + * + * @param state + * the controls state + */ + private void setControlsEnabled(final boolean state) { + this.inputField.setEnabled(state); + this.inventoryButton.setEnabled(state); + this.actionButton.setEnabled(state); + this.forwardButton.setEnabled(state); + this.backButton.setEnabled(state); + this.leftButton.setEnabled(state); + this.rightButton.setEnabled(state); + } + + /** + * Sets the quest label. + * + * @param quest + * the quest title + */ + private void setQuestLabel(final String quest) { + this.questLabel.setText(quest); + } + + /** + * Sets the current music playing in loop. + * + * @param music + * the music URL + */ + private void setMusic(final String music) { + // TODO + this.echo("Not implemented."); + } + + /** + * Toggles the sound (music). + */ + private void toggleSound() { + // TODO + this.echo("Not implemented."); + } + + /** + * Sets the illustration of the side of the room. + * + * @param illustration + * the illustration page URL + */ + private void setIllustration(final String illustration) { + this.illustrationFrame.setUrl(illustration); + } + + /** + * Updates the item list in the inventory. + */ + private void updateInventory() { + // TODO + } + + /** + * Opens the inventory (switches to the inventory tab). + */ + private void openInventory() { + this.bottomPanel.selectTab(1); + this.inventoryButton.addStyleName("button-activated"); + } + + /** + * Closes the inventory (switches to the console tab). + */ + private void closeInventory() { + this.bottomPanel.selectTab(0); + this.inventoryButton.removeStyleName("button-activated"); + } + + /** + * Toggles the inventory tab. + */ + private void toggleInventory() { + if (this.bottomPanel.getSelectedIndex() == 0) { + this.openInventory(); + } else { + this.closeInventory(); + } } @Override - public void setModel(Game game) { + public void setModel(final Game game) { this.game = game; } @Override - public void setController(GameEngine gameEngine) { + public void setController(final GameEngine gameEngine) { this.gameEngine = gameEngine; } @Override public void enable() { - RootLayoutPanel.get().add(this); + this.setControlsEnabled(true); } @Override - public void refresh() { - // TODO Auto-generated method stub + public void disable() { + this.setControlsEnabled(false); + this.setQuestLabel("ESIEEquest"); + } + @Override + public void refresh() { + // TODO: current quest title and music + // TODO: current room/side illustration + // TODO: control buttons state + // TODO: inventory } @Override - public void echo(String message) { + public void echo(final String message) { this.displayLabel.setText(message); } - /* - * @UiHandler("button") void onClick(ClickEvent e) { Window.alert("Hello!"); - * } - */ - } diff --git a/src/esieequest/view/web/WebInterface.ui.xml b/src/esieequest/view/web/WebInterface.ui.xml index 755990e..4ef88a0 100644 --- a/src/esieequest/view/web/WebInterface.ui.xml +++ b/src/esieequest/view/web/WebInterface.ui.xml @@ -1,41 +1,31 @@ - - /* Add CSS here. See the GWT docs on UI Binder for more details */ - .important { - font-weight: bold; - } - -