From b416d4d7d8a2b42f7a0e8b105a3b6ce14d2a50ae Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 17 May 2014 19:39:05 +0200 Subject: Implement graphical inventory --- .../controller/commands/DropCommand.java | 2 + .../controller/commands/TakeCommand.java | 2 + src/esieequest/controller/commands/UseCommand.java | 2 +- src/esieequest/model/Text.java | 2 + src/esieequest/model/items/Inventory.java | 2 + src/esieequest/view/Viewable.java | 6 +- src/esieequest/view/app/UserInterface.java | 91 +++++++++++++++++++--- src/esieequest/view/text/TextInterface.java | 8 +- src/esieequest/view/web/WebInterface.java | 40 ++++++---- src/esieequest/view/web/WebInterface.ui.xml | 17 ++-- 10 files changed, 129 insertions(+), 43 deletions(-) diff --git a/src/esieequest/controller/commands/DropCommand.java b/src/esieequest/controller/commands/DropCommand.java index 09b2394..b253d98 100644 --- a/src/esieequest/controller/commands/DropCommand.java +++ b/src/esieequest/controller/commands/DropCommand.java @@ -31,6 +31,8 @@ public class DropCommand implements Executable { final Item item = game.getPlayer().getInventory().takeItem(itemName); game.getPlayer().getCurrentSide().getInventory().putItem(item); + view.updateInventory(game.getPlayer().getInventory()); + } } diff --git a/src/esieequest/controller/commands/TakeCommand.java b/src/esieequest/controller/commands/TakeCommand.java index 8b8a461..f83c218 100644 --- a/src/esieequest/controller/commands/TakeCommand.java +++ b/src/esieequest/controller/commands/TakeCommand.java @@ -47,6 +47,8 @@ public class TakeCommand implements Executable { game.getPlayer().getCurrentRoom().removeItem(item); game.getPlayer().getInventory().putItem(item); + view.updateInventory(game.getPlayer().getInventory()); + } } diff --git a/src/esieequest/controller/commands/UseCommand.java b/src/esieequest/controller/commands/UseCommand.java index 00fc234..3328c7c 100644 --- a/src/esieequest/controller/commands/UseCommand.java +++ b/src/esieequest/controller/commands/UseCommand.java @@ -15,7 +15,7 @@ public class UseCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { - final String itemName = argument; + final String itemName = argument.toLowerCase(); if (itemName == null) { view.echo(Text.NO_ITEM_SPECIFIED.getText()); diff --git a/src/esieequest/model/Text.java b/src/esieequest/model/Text.java index 1b47c6f..d46b4b1 100644 --- a/src/esieequest/model/Text.java +++ b/src/esieequest/model/Text.java @@ -53,6 +53,8 @@ public enum Text { INVENTORY_WEIGHT_PREFIX("Total inventory weight: "), INVENTORY_WEIGHT_SUFFIX("."), + INVENTORY_EMPTY("Your inventory is empty."), + // formatting LIST_SEPARATOR(", "), NEW_LINE("\n"), diff --git a/src/esieequest/model/items/Inventory.java b/src/esieequest/model/items/Inventory.java index 68d1838..a4a0e84 100644 --- a/src/esieequest/model/items/Inventory.java +++ b/src/esieequest/model/items/Inventory.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import lombok.Getter; import net.pacien.util.IntrinsicMap; import org.json.simple.JSONArray; @@ -21,6 +22,7 @@ import esieequest.model.Text; */ public class Inventory implements SerialisableList { + @Getter private final IntrinsicMap items; /** diff --git a/src/esieequest/view/Viewable.java b/src/esieequest/view/Viewable.java index d2aeaff..0b23b8c 100644 --- a/src/esieequest/view/Viewable.java +++ b/src/esieequest/view/Viewable.java @@ -1,10 +1,8 @@ package esieequest.view; -import java.util.HashMap; - import esieequest.controller.GameEngine; import esieequest.model.events.Quest; -import esieequest.model.items.SimpleItem; +import esieequest.model.items.Inventory; import esieequest.model.map.Direction; import esieequest.model.map.Room; import esieequest.model.map.Side; @@ -73,6 +71,6 @@ public interface Viewable { * @param items * the items */ - public void updateInventory(final HashMap items); + public void updateInventory(final Inventory inventory); } diff --git a/src/esieequest/view/app/UserInterface.java b/src/esieequest/view/app/UserInterface.java index f492090..c4e072d 100644 --- a/src/esieequest/view/app/UserInterface.java +++ b/src/esieequest/view/app/UserInterface.java @@ -4,6 +4,7 @@ import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; +import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; @@ -12,6 +13,7 @@ import java.util.HashMap; import javax.swing.JButton; import javax.swing.JLabel; +import javax.swing.JLayeredPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JTextPane; @@ -21,8 +23,10 @@ import com.wordpress.tipsforjava.swing.StretchIcon; import esieequest.controller.GameEngine; import esieequest.controller.commands.Command; +import esieequest.model.Text; import esieequest.model.events.Quest; -import esieequest.model.items.SimpleItem; +import esieequest.model.items.Inventory; +import esieequest.model.items.Item; import esieequest.model.map.Direction; import esieequest.model.map.Orientation; import esieequest.model.map.Room; @@ -39,6 +43,8 @@ abstract class UserInterface implements Viewable, ActionListener { private GameEngine gameEngine; private JPanel layout; + private JPanel consolePanel; + private JPanel inventoryPanel; private JTextPane questTextPane; private JTextPane infoTextPane; @@ -110,20 +116,28 @@ abstract class UserInterface implements Viewable, ActionListener { this.layout.add(userPanel, BorderLayout.SOUTH); userPanel.setLayout(new BorderLayout(0, 0)); - final JPanel dispPanel = new JPanel(); + final JLayeredPane dispPanel = new JLayeredPane(); dispPanel.setBorder(new EmptyBorder(5, 5, 5, 0)); userPanel.add(dispPanel, BorderLayout.CENTER); dispPanel.setLayout(new BorderLayout(0, 0)); + this.consolePanel = new JPanel(); + dispPanel.add(this.consolePanel, BorderLayout.CENTER); + this.consolePanel.setLayout(new BorderLayout(0, 0)); + this.infoTextPane = new JTextPane(); + this.consolePanel.add(this.infoTextPane, BorderLayout.CENTER); this.infoTextPane.setEditable(false); this.infoTextPane.setText("Welcome to ESIEEquest!"); - dispPanel.add(this.infoTextPane); this.inputField = new JTextField(); - dispPanel.add(this.inputField, BorderLayout.SOUTH); + this.consolePanel.add(this.inputField, BorderLayout.SOUTH); this.inputField.setColumns(10); + this.inventoryPanel = new JPanel(); + dispPanel.add(this.inventoryPanel, BorderLayout.NORTH); + this.inventoryPanel.setLayout(new GridLayout(4, 3, 0, 0)); + final JPanel controlPanel = new JPanel(); controlPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); userPanel.add(controlPanel, BorderLayout.EAST); @@ -142,7 +156,7 @@ abstract class UserInterface implements Viewable, ActionListener { final JButton inventoryButton = new JButton("⇱"); inventoryButton.setFont(font); - inventoryButton.setToolTipText("Use item"); + inventoryButton.setToolTipText("Open inventory"); topControlPanel.add(inventoryButton, BorderLayout.WEST); final JButton actionButton = new JButton("⇲"); @@ -183,7 +197,7 @@ abstract class UserInterface implements Viewable, ActionListener { saveButton.setActionCommand(Command.SAVE.name()); forwardButton.setActionCommand(Command.FORWARD.name()); - inventoryButton.setActionCommand(Command.INVENTORY.name()); + // inventoryButton.setActionCommand(Command.INVENTORY.name()); actionButton.setActionCommand(Command.DO.name()); backButton.setActionCommand(Command.BACK.name()); leftButton.setActionCommand(Command.TURN.name() + " " + Orientation.LEFT); @@ -204,6 +218,12 @@ abstract class UserInterface implements Viewable, ActionListener { this.controlButtons.put("backButton", backButton); this.controlButtons.put("leftButton", leftButton); this.controlButtons.put("rightButton", rightButton); + + final JTextPane emptyTextPane = new JTextPane(); + emptyTextPane.setText(Text.INVENTORY_EMPTY.getText()); + this.inventoryPanel.add(emptyTextPane); + this.inventoryPanel.setVisible(false); + } /** @@ -297,8 +317,12 @@ abstract class UserInterface implements Viewable, ActionListener { */ @Override public void actionPerformed(final ActionEvent actionEvent) { - this.gameEngine.interpret(actionEvent.getActionCommand()); - this.clearInputField(); + if (actionEvent.getActionCommand() == "⇱") { + this.toggleInventory(); + } else { + this.gameEngine.interpret(actionEvent.getActionCommand()); + this.clearInputField(); + } } @Override @@ -319,6 +343,7 @@ abstract class UserInterface implements Viewable, ActionListener { @Override public void echo(final String message) { + this.closeInventory(); this.infoTextPane.setText(message); this.clearInputField(); } @@ -334,9 +359,53 @@ abstract class UserInterface implements Viewable, ActionListener { this.setQuestLabel(quest.getTitle()); } - @Override - public void updateInventory(final HashMap items) { - // TODO Auto-generated method stub + /** + * Opens the inventory (switches to the inventory tab). + */ + private void openInventory() { + this.consolePanel.setVisible(false); + this.inventoryPanel.setVisible(true); + // this.inventoryButton.addStyleName("button-activated"); + } + + /** + * Closes the inventory (switches to the console tab). + */ + private void closeInventory() { + this.inventoryPanel.setVisible(false); + this.consolePanel.setVisible(true); + // this.inventoryButton.removeStyleName("button-activated"); + } + + /** + * Toggles the inventory tab. + */ + private void toggleInventory() { + if (this.inventoryPanel.isVisible()) { + this.closeInventory(); + } else { + this.openInventory(); + } } + @Override + public void updateInventory(final Inventory inventory) { + this.inventoryPanel.removeAll(); + + if (inventory.getSize() < 1) { + final JTextPane emptyTextPane = new JTextPane(); + emptyTextPane.setText(Text.INVENTORY_EMPTY.getText()); + this.inventoryPanel.add(emptyTextPane); + return; + } + + for (final Item item : inventory.getItems().values()) { + final JButton button = new JButton(item.getName()); + button.setActionCommand(Command.USE.name() + " " + item.getName()); + button.addActionListener(this); + this.inventoryPanel.add(button); + } + + this.openInventory(); + } } diff --git a/src/esieequest/view/text/TextInterface.java b/src/esieequest/view/text/TextInterface.java index 3fc2800..a59bca4 100644 --- a/src/esieequest/view/text/TextInterface.java +++ b/src/esieequest/view/text/TextInterface.java @@ -1,11 +1,9 @@ package esieequest.view.text; -import java.util.HashMap; - import esieequest.controller.GameEngine; import esieequest.controller.commands.Command; import esieequest.model.events.Quest; -import esieequest.model.items.SimpleItem; +import esieequest.model.items.Inventory; import esieequest.model.map.Direction; import esieequest.model.map.Room; import esieequest.model.map.Side; @@ -93,8 +91,8 @@ abstract class TextInterface implements Viewable { } @Override - public void updateInventory(final HashMap items) { - // TODO Auto-generated method stub + public void updateInventory(final Inventory inventory) { + this.gameEngine.interpret(Command.INVENTORY.name()); } } diff --git a/src/esieequest/view/web/WebInterface.java b/src/esieequest/view/web/WebInterface.java index 9ac7846..4099063 100644 --- a/src/esieequest/view/web/WebInterface.java +++ b/src/esieequest/view/web/WebInterface.java @@ -1,7 +1,5 @@ package esieequest.view.web; -import java.util.HashMap; - import com.google.gwt.core.client.GWT; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; @@ -15,6 +13,7 @@ import com.google.gwt.user.client.Event.NativePreviewEvent; import com.google.gwt.user.client.Event.NativePreviewHandler; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.Composite; +import com.google.gwt.user.client.ui.FlowPanel; import com.google.gwt.user.client.ui.Frame; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.RootLayoutPanel; @@ -24,8 +23,10 @@ import com.google.gwt.user.client.ui.Widget; import esieequest.controller.GameEngine; import esieequest.controller.commands.Command; +import esieequest.model.Text; import esieequest.model.events.Quest; -import esieequest.model.items.SimpleItem; +import esieequest.model.items.Inventory; +import esieequest.model.items.Item; import esieequest.model.map.Direction; import esieequest.model.map.Orientation; import esieequest.model.map.Room; @@ -46,6 +47,8 @@ class WebInterface extends Composite implements Viewable { @UiField TabLayoutPanel bottomPanel; @UiField + FlowPanel inventoryPanel; + @UiField Label displayLabel; @UiField Label questLabel; @@ -86,6 +89,10 @@ class WebInterface extends Composite implements Viewable { */ public WebInterface() { this.initWidget(WebInterface.uiBinder.createAndBindUi(this)); + + final Label emptyLabel = new Label(Text.INVENTORY_EMPTY.getText()); + this.inventoryPanel.add(emptyLabel); + this.bindInputField(); this.bindKeys(); this.bindButtons(); @@ -130,7 +137,6 @@ class WebInterface extends Composite implements Viewable { if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) { WebInterface.this.interpret(WebInterface.this.inputField.getText()); WebInterface.this.inputField.setText(null); - WebInterface.this.closeInventory(); } } }); @@ -232,13 +238,6 @@ class WebInterface extends Composite implements Viewable { this.illustrationFrame.setUrl(illustration); } - /** - * Updates the item list in the inventory. - */ - private void updateInventory() { - // TODO - } - /** * Opens the inventory (switches to the inventory tab). */ @@ -289,6 +288,7 @@ class WebInterface extends Composite implements Viewable { @Override public void echo(final String message) { + this.closeInventory(); this.displayLabel.setText(message); } @@ -304,8 +304,22 @@ class WebInterface extends Composite implements Viewable { } @Override - public void updateInventory(final HashMap items) { - // TODO Auto-generated method stub + public void updateInventory(final Inventory inventory) { + this.inventoryPanel.clear(); + + if (inventory.getSize() < 1) { + final Label emptyLabel = new Label(Text.INVENTORY_EMPTY.getText()); + this.inventoryPanel.add(emptyLabel); + return; + } + + for (final Item item : inventory.getItems().values()) { + final Button button = new Button(item.getName()); + button.addClickHandler(this.makeClickHandler(Command.USE.name() + " " + item.getName())); + this.inventoryPanel.add(button); + } + + this.openInventory(); } } diff --git a/src/esieequest/view/web/WebInterface.ui.xml b/src/esieequest/view/web/WebInterface.ui.xml index 473358d..6de3d85 100644 --- a/src/esieequest/view/web/WebInterface.ui.xml +++ b/src/esieequest/view/web/WebInterface.ui.xml @@ -45,9 +45,8 @@ Inventory - - - + + @@ -62,29 +61,29 @@ + enabled="false" title="Toggle inventory" /> + enabled="false" title="Go forward" /> + ui:field="actionButton" enabled="false" title="Take/Talk" /> + enabled="false" title="Turn left" /> + enabled="false" title="Go back" /> + enabled="false" title="Turn right" /> -- cgit v1.2.3