package clueless.view;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import clueless.controller.GameController;
import clueless.events.MakeMoveEvent;
import clueless.events.MessageEvent;
import clueless.events.MessageEventListener;
import clueless.main.Main;
import clueless.messaging.AvailableMoveMessage;
import clueless.messaging.DrawPawnsMessage;
import clueless.messaging.MoveMessage;
import clueless.model.Character.CharacterName;
import clueless.model.Hallway;
import clueless.model.Hallway.HallwayName;
import clueless.model.Location;
import clueless.model.Location.LocationType;
import clueless.model.Room;
import clueless.model.Room.RoomName;
/**
* Boardview
* @author JY
**/
public class BoardView extends AbstractView implements MessageEventListener{
private GameController controller = null;
private JPanel boardPanel = null;
private JPanel roomPanel = null;
private JPanel hallwyPanel = null;
private JPanel movePanel = null;
private JLayeredPane boardPane = null;
private GridBagLayout hwLayout = null;
private GridBagLayout rmLayout = null;
private GridBagLayout moveLayout = null;
private JPanel studyPanel = new JPanel(new CardLayout());
private JPanel hallPanel = new JPanel(new CardLayout());
private JPanel loungePanel = new JPanel(new CardLayout());
private JPanel libraryPanel = new JPanel(new CardLayout());
private JPanel billiardPanel = new JPanel(new CardLayout());
private JPanel diningPanel = new JPanel(new CardLayout());
private JPanel conservePanel = new JPanel(new CardLayout());
private JPanel ballRmPanel = new JPanel(new CardLayout());
private JPanel kitchenPanel = new JPanel(new CardLayout());
private JPanel hwStudyHallPanel = new JPanel(new CardLayout());
private JPanel hwStudyLibraryPanel = new JPanel(new CardLayout());
private JPanel hwLoungeDiningPanel = new JPanel(new CardLayout());
private JPanel hwHallBilliardPanel = new JPanel(new CardLayout());
private JPanel hwHallLoungePanel = new JPanel(new CardLayout());
private JPanel hwLibraryBilliardPanel = new JPanel(new CardLayout());
private JPanel hwDiningBilliardPanel = new JPanel(new CardLayout());
private JPanel hwLibraryConservePanel = new JPanel(new CardLayout());
private JPanel hwBallRmConservPanel = new JPanel(new CardLayout());
private JPanel hwKitchenBallRmPanel = new JPanel(new CardLayout());
private JPanel hwKitchenDiningPanel = new JPanel(new CardLayout());
private JPanel hwBilliardBallRmPanel = new JPanel(new CardLayout());
// Player move panels
private JPanel studyMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel hallMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel loungeMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel libraryMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel billiardMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel diningMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel conserveMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel ballRmMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel kitchenMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel hwStudyHallMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel hwStudyLibraryMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel hwLoungeDiningMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel hwHallBilliardMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel hwHallLoungeMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel hwLibraryBilliardMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel hwDiningBilliardMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel hwLibraryConserveMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel hwBallRmConservMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel hwKitchenBallRmMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel hwKitchenDiningMovePanel = new JPanel(new BorderLayout(0,0));
private JPanel hwBilliardBallRmMovePanel = new JPanel(new BorderLayout(0,0));
private JButton studyMoveButton = new JButton();
private JButton hallMoveButton = new JButton();
private JButton loungeMoveButton = new JButton();
private JButton libraryMoveButton = new JButton();
private JButton billiardMoveButton = new JButton();
private JButton diningMoveButton = new JButton();
private JButton conserveMoveButton = new JButton();
private JButton ballRmMoveButton = new JButton();
private JButton kitchenMoveButton = new JButton();
private JButton hwStudyHallMoveButton = new JButton();
private JButton hwStudyLibraryMoveButton = new JButton();
private JButton hwLoungeDiningMoveButton = new JButton();
private JButton hwHallBilliardMoveButton = new JButton();
private JButton hwHallLoungeMoveButton = new JButton();
private JButton hwLibraryBilliardMoveButton = new JButton();
private JButton hwDiningBilliardMoveButton = new JButton();
private JButton hwLibraryConserveMoveButton = new JButton();
private JButton hwBallRmConservMoveButton = new JButton();
private JButton hwKitchenBallRmMoveButton = new JButton();
private JButton hwKitchenDiningMoveButton = new JButton();
private JButton hwBilliardBallRmMoveButton = new JButton();
public static final int BOARD_SIZE = 720;
public static final int ROOM_GAP = 100;
private static final int ROOM_WIDTH = 150;
private static final int ROOM_HEIGHT = 150;
private static final int HALLWY_R1 = 125;
private static final int HALLWY_R2 = 64;
private static final int HALLWY_R3 = 180;
private static final int HALLWY_R4 = 64;
private static final int HALLWY_R5 = 170;
private static final int HALLWY_R6 = 54;
private static final int HALLWY_R7 = 63;
private static final int HALLWY_C1 = 80;
private static final int HALLWY_C2 = 83;
private static final int HALLWY_C3 = 165;
private static final int HALLWY_C4 = 73;
private static final int HALLWY_C5 = 170;
private static final int HALLWY_C6 = 63;
private static final int HALLWY_C7 = 86;
private static final int PAWN_HEIGHT = 75;
private static final int PAWN_WIDTH = 62;
private static final int ROOM_R1 = 75;
private static final int ROOM_R2 = ROOM_HEIGHT;
private static final int ROOM_R3 = 95;
private static final int ROOM_R4 = ROOM_HEIGHT;
private static final int ROOM_R5 = 90;
private static final int ROOM_R6 = ROOM_HEIGHT;
private static final int ROOM_R7 = 10;
private static final int ROOM_C1 = 45;
private static final int ROOM_C2 = ROOM_WIDTH;
private static final int ROOM_C3 = 91;
private static final int ROOM_C4 = ROOM_WIDTH;
private static final int ROOM_C5 = 85;
private static final int ROOM_C6 = ROOM_WIDTH;
private static final int ROOM_C7 = 49;
private static final int ROOM_BUTTON_SIZE = 150;
private static final int HALL_BUTTON_FAT_SIZE = 84;
private static final int HALL_BUTTON_THIN_SIZE = 45;
private Map<CharacterName, JLabel> pawns = new HashMap<CharacterName, JLabel>();
// Default Constructor
public BoardView(GameController controller){
this.controller = controller;
this.controller.addView(this);
this.controller.addMessageEventListener(this);
boardPane = new JLayeredPane();
boardPane.setBounds(0, 0, BOARD_SIZE, BOARD_SIZE);
boardPanel = new BoardPanel();
boardPanel.setSize(BOARD_SIZE,BOARD_SIZE);
rmLayout = new GridBagLayout();
rmLayout.columnWidths = new int[]{ROOM_C1, ROOM_C2, ROOM_C3, ROOM_C4, ROOM_C5, ROOM_C6, ROOM_C7};
rmLayout.rowHeights = new int[]{ROOM_R1, ROOM_R2, ROOM_R3, ROOM_R4, ROOM_R5, ROOM_R6, ROOM_R7};
roomPanel = new JPanel(rmLayout);
roomPanel.setSize(BOARD_SIZE, BOARD_SIZE);
GridBagConstraints rmConstraints = new GridBagConstraints();
rmConstraints.gridy = 1;
rmConstraints.gridx = 1;
roomPanel.add(studyPanel, rmConstraints);
rmConstraints.gridy = 1;
rmConstraints.gridx = 3;
roomPanel.add(hallPanel, rmConstraints);
rmConstraints.gridy = 1;
rmConstraints.gridx = 5;
roomPanel.add(loungePanel, rmConstraints);
rmConstraints.gridy = 3;
rmConstraints.gridx = 1;
roomPanel.add(libraryPanel, rmConstraints);
rmConstraints.gridy = 3;
rmConstraints.gridx = 3;
roomPanel.add(billiardPanel, rmConstraints);
rmConstraints.gridy = 3;
rmConstraints.gridx = 5;
roomPanel.add(diningPanel, rmConstraints);
rmConstraints.gridy = 5;
rmConstraints.gridx = 1;
roomPanel.add(conservePanel, rmConstraints);
rmConstraints.gridy = 5;
rmConstraints.gridx = 3;
roomPanel.add(ballRmPanel, rmConstraints);
rmConstraints.gridy = 5;
rmConstraints.gridx = 5;
roomPanel.add(kitchenPanel, rmConstraints);
studyPanel.setOpaque(false);
hallPanel.setOpaque(false);
loungePanel.setOpaque(false);
libraryPanel.setOpaque(false);
billiardPanel.setOpaque(false);
diningPanel.setOpaque(false);
conservePanel.setOpaque(false);
ballRmPanel.setOpaque(false);
kitchenPanel.setOpaque(false);
roomPanel.setOpaque(false);
hwLayout = new GridBagLayout();
hwLayout.columnWidths = new int[]{HALLWY_C1, HALLWY_C2, HALLWY_C3, HALLWY_C4, HALLWY_C5, HALLWY_C6, HALLWY_C7};
hwLayout.rowHeights = new int[]{HALLWY_R1, HALLWY_R2, HALLWY_R3, HALLWY_R4, HALLWY_R5, HALLWY_R6, HALLWY_R7};
hallwyPanel = new JPanel(hwLayout);
hallwyPanel.setSize(BOARD_SIZE, BOARD_SIZE);
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 1;
constraints.gridy = 2;
hallwyPanel.add(hwStudyLibraryPanel, constraints);
constraints.gridx = 2;
constraints.gridy = 1;
hallwyPanel.add(hwStudyHallPanel, constraints);
constraints.gridx = 5;
constraints.gridy = 2;
hallwyPanel.add(hwLoungeDiningPanel, constraints);
constraints.gridx = 3;
constraints.gridy = 2;
hallwyPanel.add(hwHallBilliardPanel, constraints);
constraints.gridx = 4;
constraints.gridy = 1;
hallwyPanel.add(hwHallLoungePanel, constraints);
constraints.gridx = 2;
constraints.gridy = 3;
hallwyPanel.add(hwLibraryBilliardPanel, constraints);
constraints.gridx = 4;
constraints.gridy = 3;
hallwyPanel.add(hwDiningBilliardPanel, constraints);
constraints.gridx = 1;
constraints.gridy = 4;
hallwyPanel.add(hwLibraryConservePanel, constraints);
constraints.gridx = 2;
constraints.gridy = 5;
hallwyPanel.add(hwBallRmConservPanel, constraints);
constraints.gridx = 4;
constraints.gridy = 5;
hallwyPanel.add(hwKitchenBallRmPanel, constraints);
constraints.gridx = 5;
constraints.gridy = 4;
hallwyPanel.add(hwKitchenDiningPanel, constraints);
constraints.gridx = 3;
constraints.gridy = 4;
hallwyPanel.add(hwBilliardBallRmPanel, constraints);
hwStudyHallPanel.setOpaque(false);
hwStudyLibraryPanel.setOpaque(false);
hwLoungeDiningPanel.setOpaque(false);
hwHallBilliardPanel.setOpaque(false);
hwHallLoungePanel.setOpaque(false);
hwLibraryBilliardPanel.setOpaque(false);
hwDiningBilliardPanel.setOpaque(false);
hwLibraryConservePanel.setOpaque(false);
hwBallRmConservPanel.setOpaque(false);
hwKitchenBallRmPanel.setOpaque(false);
hwKitchenDiningPanel.setOpaque(false);
hwBilliardBallRmPanel.setOpaque(false);
hallwyPanel.setOpaque(false);
moveLayout = new GridBagLayout();
moveLayout.columnWidths = new int[]{34, 152, 90, 152, 80, 152, 34};
moveLayout.rowHeights = new int[]{65, 150, 96, 150, 96, 150, 0};
movePanel = new JPanel(moveLayout);
movePanel.setSize(BOARD_SIZE, BOARD_SIZE);
GridBagConstraints moveConstraints = new GridBagConstraints();
moveConstraints.gridy = 1;
moveConstraints.gridx = 1;
movePanel.add(studyMovePanel, moveConstraints);
moveConstraints.gridy = 1;
moveConstraints.gridx = 2;
movePanel.add(hwStudyHallMovePanel, moveConstraints);
moveConstraints.gridy = 1;
moveConstraints.gridx = 3;
movePanel.add(hallMovePanel, moveConstraints);
moveConstraints.gridy = 1;
moveConstraints.gridx = 4;
movePanel.add(hwHallLoungeMovePanel, moveConstraints);
moveConstraints.gridy = 1;
moveConstraints.gridx = 5;
movePanel.add(loungeMovePanel, moveConstraints);
moveConstraints.gridy = 2;
moveConstraints.gridx = 5;
movePanel.add(hwLoungeDiningMovePanel, moveConstraints);
moveConstraints.gridy = 3;
moveConstraints.gridx = 5;
movePanel.add(diningMovePanel, moveConstraints);
moveConstraints.gridy = 4;
moveConstraints.gridx = 5;
movePanel.add(hwKitchenDiningMovePanel, moveConstraints);
moveConstraints.gridy = 5;
moveConstraints.gridx = 5;
movePanel.add(kitchenMovePanel, moveConstraints);
moveConstraints.gridy = 5;
moveConstraints.gridx = 4;
movePanel.add(hwKitchenBallRmMovePanel, moveConstraints);
moveConstraints.gridy = 5;
moveConstraints.gridx = 3;
movePanel.add(ballRmMovePanel, moveConstraints);
moveConstraints.gridy = 5;
moveConstraints.gridx = 2;
movePanel.add(hwBallRmConservMovePanel, moveConstraints);
moveConstraints.gridy = 5;
moveConstraints.gridx = 1;
movePanel.add(conserveMovePanel, moveConstraints);
moveConstraints.gridy = 4;
moveConstraints.gridx = 1;
movePanel.add(hwLibraryConserveMovePanel, moveConstraints);
moveConstraints.gridy = 3;
moveConstraints.gridx = 1;
movePanel.add(libraryMovePanel, moveConstraints);
moveConstraints.gridy = 2;
moveConstraints.gridx = 1;
movePanel.add(hwStudyLibraryMovePanel, moveConstraints);
moveConstraints.gridy = 3;
moveConstraints.gridx = 2;
movePanel.add(hwLibraryBilliardMovePanel, moveConstraints);
moveConstraints.gridy = 3;
moveConstraints.gridx = 3;
movePanel.add(billiardMovePanel, moveConstraints);
moveConstraints.gridy = 2;
moveConstraints.gridx = 3;
movePanel.add(hwHallBilliardMovePanel, moveConstraints);
moveConstraints.gridy = 3;
moveConstraints.gridx = 4;
movePanel.add(hwDiningBilliardMovePanel, moveConstraints);
moveConstraints.gridy = 4;
moveConstraints.gridx = 3;
movePanel.add(hwBilliardBallRmMovePanel, moveConstraints);
studyMoveButton.setPreferredSize(new Dimension(ROOM_BUTTON_SIZE+6, ROOM_BUTTON_SIZE));
hallMoveButton.setPreferredSize(new Dimension(ROOM_BUTTON_SIZE, ROOM_BUTTON_SIZE));
loungeMoveButton.setPreferredSize(new Dimension(ROOM_BUTTON_SIZE+6, ROOM_BUTTON_SIZE));
libraryMoveButton.setPreferredSize(new Dimension(ROOM_BUTTON_SIZE+6, ROOM_BUTTON_SIZE));
billiardMoveButton.setPreferredSize(new Dimension(ROOM_BUTTON_SIZE, ROOM_BUTTON_SIZE));
diningMoveButton.setPreferredSize(new Dimension(ROOM_BUTTON_SIZE+6, ROOM_BUTTON_SIZE));
conserveMoveButton.setPreferredSize(new Dimension(ROOM_BUTTON_SIZE+6, ROOM_BUTTON_SIZE));
ballRmMoveButton.setPreferredSize(new Dimension(ROOM_BUTTON_SIZE, ROOM_BUTTON_SIZE));
kitchenMoveButton.setPreferredSize(new Dimension(ROOM_BUTTON_SIZE+6, ROOM_BUTTON_SIZE));
hwStudyHallMoveButton.setPreferredSize(new Dimension(HALL_BUTTON_FAT_SIZE, HALL_BUTTON_THIN_SIZE));
hwStudyLibraryMoveButton.setPreferredSize(new Dimension(HALL_BUTTON_THIN_SIZE+8, HALL_BUTTON_FAT_SIZE));
hwLoungeDiningMoveButton.setPreferredSize(new Dimension(HALL_BUTTON_THIN_SIZE+8, HALL_BUTTON_FAT_SIZE));
hwHallBilliardMoveButton.setPreferredSize(new Dimension(HALL_BUTTON_THIN_SIZE+8, HALL_BUTTON_FAT_SIZE));
hwHallLoungeMoveButton.setPreferredSize(new Dimension(HALL_BUTTON_FAT_SIZE-5, HALL_BUTTON_THIN_SIZE));
hwLibraryBilliardMoveButton.setPreferredSize(new Dimension(HALL_BUTTON_FAT_SIZE, HALL_BUTTON_THIN_SIZE));
hwDiningBilliardMoveButton.setPreferredSize(new Dimension(HALL_BUTTON_FAT_SIZE-5, HALL_BUTTON_THIN_SIZE));
hwLibraryConserveMoveButton.setPreferredSize(new Dimension(HALL_BUTTON_THIN_SIZE+8, HALL_BUTTON_FAT_SIZE));
hwBallRmConservMoveButton.setPreferredSize(new Dimension(HALL_BUTTON_FAT_SIZE, HALL_BUTTON_THIN_SIZE));
hwKitchenBallRmMoveButton.setPreferredSize(new Dimension(HALL_BUTTON_FAT_SIZE-5, HALL_BUTTON_THIN_SIZE));
hwKitchenDiningMoveButton.setPreferredSize(new Dimension(HALL_BUTTON_THIN_SIZE+8, HALL_BUTTON_FAT_SIZE));
hwBilliardBallRmMoveButton.setPreferredSize(new Dimension(HALL_BUTTON_THIN_SIZE+8, HALL_BUTTON_FAT_SIZE));
studyMoveButton.setBackground(new Color(0, 0, 0, 200));
studyMoveButton.setForeground(new Color(70, 155, 255));
hallMoveButton.setForeground(new Color(70, 155, 255));
hallMoveButton.setBackground(new Color(0, 0, 0, 200));
loungeMoveButton.setBackground(new Color(0, 0, 0, 200));
loungeMoveButton.setForeground(new Color(70, 155, 255));
libraryMoveButton.setForeground(new Color(70, 155, 255));
libraryMoveButton.setBackground(new Color(0, 0, 0, 200));
billiardMoveButton.setBackground(new Color(0, 0, 0, 200));
billiardMoveButton.setForeground(new Color(70, 155, 255));
diningMoveButton.setForeground(new Color(70, 155, 255));
diningMoveButton.setBackground(new Color(0, 0, 0, 200));
conserveMoveButton.setBackground(new Color(0, 0, 0, 200));
conserveMoveButton.setForeground(new Color(70, 155, 255));
ballRmMoveButton.setForeground(new Color(70, 155, 255));
ballRmMoveButton.setBackground(new Color(0, 0, 0, 200));
kitchenMoveButton.setBackground(new Color(0, 0, 0, 200));
kitchenMoveButton.setForeground(new Color(70, 155, 255));
hwStudyHallMoveButton.setForeground(new Color(70, 155, 255));
hwStudyHallMoveButton.setBackground(new Color(0, 0, 0, 200));
hwStudyLibraryMoveButton.setBackground(new Color(0, 0, 0, 200));
hwStudyLibraryMoveButton.setForeground(new Color(70, 155, 255));
hwLoungeDiningMoveButton.setForeground(new Color(70, 155, 255));
hwLoungeDiningMoveButton.setBackground(new Color(0, 0, 0, 200));
hwHallBilliardMoveButton.setBackground(new Color(0, 0, 0, 200));
hwHallBilliardMoveButton.setForeground(new Color(70, 155, 255));
hwHallLoungeMoveButton.setForeground(new Color(70, 155, 255));
hwHallLoungeMoveButton.setBackground(new Color(0, 0, 0, 200));
hwLibraryBilliardMoveButton.setBackground(new Color(0, 0, 0, 200));
hwLibraryBilliardMoveButton.setForeground(new Color(70, 155, 255));
hwDiningBilliardMoveButton.setForeground(new Color(70, 155, 255));
hwDiningBilliardMoveButton.setBackground(new Color(0, 0, 0, 200));
hwLibraryConserveMoveButton.setBackground(new Color(0, 0, 0, 200));
hwLibraryConserveMoveButton.setForeground(new Color(70, 155, 255));
hwBallRmConservMoveButton.setForeground(new Color(70, 155, 255));
hwBallRmConservMoveButton.setBackground(new Color(0, 0, 0, 200));
hwKitchenBallRmMoveButton.setBackground(new Color(0, 0, 0, 200));
hwKitchenBallRmMoveButton.setForeground(new Color(70, 155, 255));
hwKitchenDiningMoveButton.setForeground(new Color(70, 155, 255));
hwKitchenDiningMoveButton.setBackground(new Color(0, 0, 0, 200));
hwBilliardBallRmMoveButton.setForeground(new Color(70, 155, 255));
hwBilliardBallRmMoveButton.setBackground(new Color(0, 0, 0, 200));
studyMoveButton.setOpaque(false);
hallMoveButton.setOpaque(false);
loungeMoveButton.setOpaque(false);
libraryMoveButton.setOpaque(false);
billiardMoveButton.setOpaque(false);
diningMoveButton.setOpaque(false);
conserveMoveButton.setOpaque(false);
ballRmMoveButton.setOpaque(false);
kitchenMoveButton.setOpaque(false);
hwStudyHallMoveButton.setOpaque(false);
hwStudyLibraryMoveButton.setOpaque(false);
hwLoungeDiningMoveButton.setOpaque(false);
hwHallBilliardMoveButton.setOpaque(false);
hwHallLoungeMoveButton.setOpaque(false);
hwLibraryBilliardMoveButton.setOpaque(false);
hwDiningBilliardMoveButton.setOpaque(false);
hwLibraryConserveMoveButton.setOpaque(false);
hwBallRmConservMoveButton.setOpaque(false);
hwKitchenBallRmMoveButton.setOpaque(false);
hwKitchenDiningMoveButton.setOpaque(false);
hwBilliardBallRmMoveButton.setOpaque(false);
studyMoveButton.setVisible(false);
hallMoveButton.setVisible(false);
loungeMoveButton.setVisible(false);
libraryMoveButton.setVisible(false);
billiardMoveButton.setVisible(false);
diningMoveButton.setVisible(false);
conserveMoveButton.setVisible(false);
ballRmMoveButton.setVisible(false);
kitchenMoveButton.setVisible(false);
hwStudyHallMoveButton.setVisible(false);
hwStudyLibraryMoveButton.setVisible(false);
hwLoungeDiningMoveButton.setVisible(false);
hwHallBilliardMoveButton.setVisible(false);
hwHallLoungeMoveButton.setVisible(false);
hwLibraryBilliardMoveButton.setVisible(false);
hwDiningBilliardMoveButton.setVisible(false);
hwLibraryConserveMoveButton.setVisible(false);
hwBallRmConservMoveButton.setVisible(false);
hwKitchenBallRmMoveButton.setVisible(false);
hwKitchenDiningMoveButton.setVisible(false);
hwBilliardBallRmMoveButton.setVisible(false);
studyMovePanel.setOpaque(false);
hallMovePanel.setOpaque(false);
loungeMovePanel.setOpaque(false);
libraryMovePanel.setOpaque(false);
billiardMovePanel.setOpaque(false);
diningMovePanel.setOpaque(false);
conserveMovePanel.setOpaque(false);
ballRmMovePanel.setOpaque(false);
kitchenMovePanel.setOpaque(false);
hwStudyHallMovePanel.setOpaque(false);
hwStudyLibraryMovePanel.setOpaque(false);
hwLoungeDiningMovePanel.setOpaque(false);
hwHallBilliardMovePanel.setOpaque(false);
hwHallLoungeMovePanel.setOpaque(false);
hwLibraryBilliardMovePanel.setOpaque(false);
hwDiningBilliardMovePanel.setOpaque(false);
hwLibraryConserveMovePanel.setOpaque(false);
hwBallRmConservMovePanel.setOpaque(false);
hwKitchenBallRmMovePanel.setOpaque(false);
hwKitchenDiningMovePanel.setOpaque(false);
hwBilliardBallRmMovePanel.setOpaque(false);
movePanel.setOpaque(false);
//movePanel.setVisible(false);
//movePanel.setEnabled(false);
studyMovePanel.add(studyMoveButton);
hallMovePanel.add(hallMoveButton);
loungeMovePanel.add(loungeMoveButton);
libraryMovePanel.add(libraryMoveButton);
billiardMovePanel.add(billiardMoveButton);
diningMovePanel.add(diningMoveButton);
conserveMovePanel.add(conserveMoveButton);
ballRmMovePanel.add(ballRmMoveButton);
kitchenMovePanel.add(kitchenMoveButton);
hwStudyHallMovePanel.add(hwStudyHallMoveButton);
hwStudyLibraryMovePanel.add(hwStudyLibraryMoveButton);
hwLoungeDiningMovePanel.add(hwLoungeDiningMoveButton);
hwHallBilliardMovePanel.add(hwHallBilliardMoveButton);
hwHallLoungeMovePanel.add(hwHallLoungeMoveButton);
hwLibraryBilliardMovePanel.add(hwLibraryBilliardMoveButton);
hwDiningBilliardMovePanel.add(hwDiningBilliardMoveButton);
hwLibraryConserveMovePanel.add(hwLibraryConserveMoveButton);
hwBallRmConservMovePanel.add(hwBallRmConservMoveButton);
hwKitchenBallRmMovePanel.add(hwKitchenBallRmMoveButton);
hwKitchenDiningMovePanel.add(hwKitchenDiningMoveButton);
hwBilliardBallRmMovePanel.add(hwBilliardBallRmMoveButton);
boardPane.add(boardPanel, new Integer(0), 0);
boardPane.add(roomPanel, new Integer(1), 0);
boardPane.add(hallwyPanel,new Integer(2), 0);
boardPane.add(movePanel, new Integer(3), 0);
//testHalls();
//testRooms();
//testMoves();
}
/** Just for testing. */
private void testHalls(){
for(HallwayName hallway : HallwayName.values()){
addPawnToHall(CharacterName.MISS_SCARLET, hallway);
}
}
/** Just for testing. */
private void testRooms(){
for(RoomName room : RoomName.values()){
addPawnToRoom(CharacterName.MR_GREEN, room);
}
}
/** Just for testing. */
private void testMoves(){
//todo
}
/**
* Initializes the board's pawn locations for the given character.
* @param characterName - the CharacterNames for the characters in play.
*/
public void initializeBoardPawn(CharacterName characterName){
if(characterName != null){
addPawnToHall(characterName, getDefaultHall(characterName));
}
}
/**
* Moves the pawn for the given character as indicated.
* @param name - the name of the character whose pawn is to move
* @param fromSpace - the old location
* @param toSpace - the new location
*/
private void movePawn(CharacterName name, Location fromSpace, Location toSpace ){
JPanel oldPanel = null;
JPanel newPanel = null;
if(fromSpace.getLocationType() == LocationType.ROOM_TYPE){
oldPanel = this.getRoomPanel(((Room)fromSpace).getRoomName());
}else if(fromSpace.getLocationType() == LocationType.HALLWAY_TYPE){
oldPanel = this.getHallPanel(((Hallway)fromSpace).getHallwayName());
}
if(toSpace.getLocationType() == LocationType.ROOM_TYPE){
newPanel = this.getRoomPanel(((Room)toSpace).getRoomName());
}else if(toSpace.getLocationType() == LocationType.HALLWAY_TYPE){
newPanel = this.getHallPanel(((Hallway)toSpace).getHallwayName());
}
// Remove from the old location
if(oldPanel != null){
oldPanel.removeAll();
oldPanel.revalidate();
}
// Add to the new location
if(newPanel != null){
JLabel pawn = pawns.get(name);
if(pawn != null){
newPanel.add(pawn);
newPanel.revalidate();
}
}
}
/**
* Places the pawn associated with the given character in the room indicated.
* @param character - the character whose pawn should be placed
* @param room - the Room in which to place the pawn
*/
public void addPawnToRoom(CharacterName character, RoomName room){
if( character == null || room == null ){
throw new NullPointerException();
}
URL resource = getCharacterResource(character);
JLabel pawn = getPawn(resource);
pawn.setOpaque(true);
JPanel roomPanel = getRoomPanel(room);
roomPanel.add(pawn);
pawns.put(character, pawn);
}
/**
* Places the given character's pawn in the given hallway.
* @param character - the character whose pawn should be placed.
* @param name - the HallwayName of where to place the pawn.
*/
public void addPawnToHall(CharacterName character, HallwayName name){
if( character == null || name == null ){
throw new NullPointerException();
}
URL resource = getCharacterResource(character);
JLabel pawn = getPawn(resource);
pawn.setOpaque(true);
JPanel hallway = getHallPanel(name);
hallway.add(pawn);
pawns.put(character, pawn);
}
/**
* Gets a pawn's image for display.
* @param resource - the URL to the image
* @return a handle to a JLabel holding the pawn's image
*/
private JLabel getPawn(URL resource){
ImageIcon image = new ImageIcon(resource);
BufferedImage bufferedImage = new BufferedImage(PAWN_WIDTH, PAWN_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics();
graphics.drawImage(image.getImage(), 0, 0, PAWN_WIDTH, PAWN_HEIGHT, null);
graphics.dispose();
JLabel pawn = new JLabel(new ImageIcon(bufferedImage));
pawn.setOpaque(true);
return pawn;
}
/**
* Makes visible the button associated with this location.
* @param location - the location to display
* @param name - the name of the character with these this option
*/
private void showButton(final Location location, final CharacterName name){
JButton button = null;
if(location.getLocationType() == LocationType.ROOM_TYPE){
button = getRoomButton(((Room)location).getRoomName());
}else if(location.getLocationType() == LocationType.HALLWAY_TYPE){
button = getHallwayButton(((Hallway)location).getHallwayName());
}
if(button != null){
button.setVisible(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// First set everybody to invisible
for(RoomName room : RoomName.values()){
JButton btn = getRoomButton(room);
btn.setVisible(false);
btn.setEnabled(false);
}
for(HallwayName hallwy : HallwayName.values()){
JButton btn = getHallwayButton(hallwy);
btn.setVisible(false);
btn.setEnabled(false);
}
// Then fire the event
controller.fireMakeMoveEvent(new MakeMoveEvent(this, location, name));
}
});
button.setEnabled(true);
}
}
/**
* @return the game board
*/
public JLayeredPane getBoardPanel()
{
return this.boardPane;
}
/**
* Private inner class for the panel holding the board
* @author tperrin47
*/
private class BoardPanel extends JPanel {
private static final long serialVersionUID = 1L;
/** The background image. */
private Image boardImage;
public BoardPanel() {
this.boardImage = new ImageIcon(getClass().getResource("Gameboard.gif")).getImage();
}
public void paintComponent(Graphics graph) {
graph.drawImage(boardImage, 0, 0, null);
}
}
/**
* Gets the image associated with a character's name.
* @param name the character's name of the pawn to get
* @return a URL to the image
*/
private URL getCharacterResource(CharacterName name){
URL resource = null;
switch(name)
{
case COL_MUSTARD:
resource = Main.class.getResource("/clueless/main/resources/characters/Pawn_Yellow.gif");
break;
case MISS_SCARLET:
resource = Main.class.getResource("/clueless/main/resources/characters/Pawn_Red.gif");
break;
case MR_GREEN:
resource = Main.class.getResource("/clueless/main/resources/characters/Pawn_Green.gif");
break;
case MRS_PEACOCK:
resource = Main.class.getResource("/clueless/main/resources/characters/Pawn_Blue.gif");
break;
case MRS_WHITE:
resource = Main.class.getResource("/clueless/main/resources/characters/Pawn_White.gif");
break;
case PROF_PLUM:
resource = Main.class.getResource("/clueless/main/resources/characters/Pawn_Purple.gif");
break;
}
return resource;
}
/**
* Get the default initial location (hallway) for the given character.
* @param name - the character's name
* @return the HallwayName
*/
public static HallwayName getDefaultHall(CharacterName name){
HallwayName hallway = null;
switch(name)
{
case COL_MUSTARD:
hallway = HallwayName.BTW_LOUNGE_DININGROOM;
break;
case MISS_SCARLET:
hallway = HallwayName.BTW_HALL_LOUNGE;
break;
case MR_GREEN:
hallway = HallwayName.BTW_CONSERVATORY_BALLROOM;
break;
case MRS_PEACOCK:
hallway = HallwayName.BTW_LIBRARY_CONSERVATORY;
break;
case MRS_WHITE:
hallway = HallwayName.BTW_BALLROOM_KITCHEN;
break;
case PROF_PLUM:
hallway = HallwayName.BTW_STUDY_LIBRARY;
break;
}
return hallway;
}
/**
* Maps the room name to the correct panel.
* @param room - the name of the room
* @return the JPanel associated with that room.
*/
private JPanel getRoomPanel(RoomName room){
switch(room)
{
case STUDY:
return studyPanel;
case HALL:
return hallPanel;
case LOUNGE:
return loungePanel;
case LIBRARY:
return libraryPanel;
case BILLIARD_ROOM:
return billiardPanel;
case DINING_ROOM:
return diningPanel;
case CONSERVATORY:
return conservePanel;
case BALLROOM:
return ballRmPanel;
case KITCHEN:
return kitchenPanel;
default:
break;
}
return null;
}
/**
* Maps the hallway name to the correct panel.
* @param name the HallwayName
* @return the JPanel associated with that hall.
*/
private JPanel getHallPanel(HallwayName name){
switch(name)
{
case BTW_STUDY_HALL:
return hwStudyHallPanel;
case BTW_STUDY_LIBRARY:
return hwStudyLibraryPanel;
case BTW_LIBRARY_BILLIARDROOM:
return hwLibraryBilliardPanel;
case BTW_LIBRARY_CONSERVATORY:
return hwLibraryConservePanel;
case BTW_CONSERVATORY_BALLROOM:
return hwBallRmConservPanel;
case BTW_HALL_LOUNGE:
return hwHallLoungePanel;
case BTW_HALL_BILLIARDROOM:
return hwHallBilliardPanel;
case BTW_BILLIARDROOM_DININGROOM:
return hwDiningBilliardPanel;
case BTW_BILLIARDROOM_BALLROOM:
return hwBilliardBallRmPanel;
case BTW_BALLROOM_KITCHEN:
return hwKitchenBallRmPanel;
case BTW_LOUNGE_DININGROOM:
return hwLoungeDiningPanel;
case BTW_DININGROOM_KITCHEN:
return hwKitchenDiningPanel;
default:
break;
}
return null;
}
/**
* Maps the room name to the correct button.
* @param room - the name of the room
* @return the JButton associated with that room.
*/
private JButton getRoomButton(RoomName room){
switch(room)
{
case STUDY:
return studyMoveButton;
case HALL:
return hallMoveButton;
case LOUNGE:
return loungeMoveButton;
case LIBRARY:
return libraryMoveButton;
case BILLIARD_ROOM:
return billiardMoveButton;
case DINING_ROOM:
return diningMoveButton;
case CONSERVATORY:
return conserveMoveButton;
case BALLROOM:
return ballRmMoveButton;
case KITCHEN:
return kitchenMoveButton;
default:
break;
}
return null;
}
/**
* Maps the hallway name to the correct button.
* @param name the HallwayName
* @return the JButton associated with that hallway.
*/
private JButton getHallwayButton(HallwayName name){
switch(name)
{
case BTW_STUDY_HALL:
return hwStudyHallMoveButton;
case BTW_STUDY_LIBRARY:
return hwStudyLibraryMoveButton;
case BTW_LIBRARY_BILLIARDROOM:
return hwLibraryBilliardMoveButton;
case BTW_LIBRARY_CONSERVATORY:
return hwLibraryConserveMoveButton;
case BTW_CONSERVATORY_BALLROOM:
return hwBallRmConservMoveButton;
case BTW_HALL_LOUNGE:
return hwHallLoungeMoveButton;
case BTW_HALL_BILLIARDROOM:
return hwHallBilliardMoveButton;
case BTW_BILLIARDROOM_DININGROOM:
return hwDiningBilliardMoveButton;
case BTW_BILLIARDROOM_BALLROOM:
return hwBilliardBallRmMoveButton;
case BTW_BALLROOM_KITCHEN:
return hwKitchenBallRmMoveButton;
case BTW_LOUNGE_DININGROOM:
return hwLoungeDiningMoveButton;
case BTW_DININGROOM_KITCHEN:
return hwKitchenDiningMoveButton;
default:
break;
}
return null;
}
@Override
public void handleMessageEvent(MessageEvent event) {
if(event.getMessage() != null){
if(event.getMessage() instanceof DrawPawnsMessage)
{
DrawPawnsMessage msg = (DrawPawnsMessage)event.getMessage();
for(CharacterName name : msg.getNames()){
initializeBoardPawn(name);
this.boardPane.repaint();
}
}else if(event.getMessage() instanceof MoveMessage){
MoveMessage msg = (MoveMessage)event.getMessage();
movePawn(msg.getCharacterName(), msg.getOldLocation(), msg.getNewLocation());
}else if (event.getMessage() instanceof AvailableMoveMessage){
AvailableMoveMessage msg = (AvailableMoveMessage)event.getMessage();
for(Location location : msg.getLocations()){
this.showButton(location, msg.getCharacterName());
}
}
}
}
}