/*
* This is the main Graphical UI for the game.
* It instatiates a controller, and then calls on suitable view classes
*
*/
package view;
import controller.Sho;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
/**
*
* @author Ten
*/
public class ShoUI {
private static JFrame f;
private static controller.Sho s;
private static BoardV bv;
private static TileV highlightThisplace;
private static int dice1 = 0;
private static int dice2 = 0;
private static ShoMouseEvents mevent;
private static void setPlayerButtonEnablePerTurn() {
//disable button for all player except currentPlayer
for (TileV t : bv.bases) {
if (t.getTile().getOccupyingStack().getOwner() != s.getCurrentPlayer()) {
t.setEnabled(false);
}else{
t.setEnabled(true);
}
}
}
private model.Player currentPlayer;
public ShoUI() {
s = new Sho("A", "B", "C");
mevent = new ShoMouseEvents();
currentPlayer = s.getCurrentPlayer();
}
public static void main(String[] args) {
ShoUI sui = new ShoUI();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
//sho.playTurn();
}
});
}
private static void createAndShowGUI() {
System.out.println("Created GUI on EDT? "
+ SwingUtilities.isEventDispatchThread());
f = new JFrame("Sho");
//TileV t = new TileV();
bv = new BoardV(s.getB());
f.add(bv);
//set the mouselistener for the tiles ( class TileV )
if (bv.normal.length > 0) {
//System.out.println("Got this " + pl.length);
for (int p = 0; p < bv.normal.length; p++) {
bv.normal[p].addMouseListener(mevent);
}
}
//connect each players start button
if (bv.bases.length > 0) {
for (int p = 0; p < bv.bases.length; p++) {
bv.bases[p].addMouseListener(mevent);
}
}
//connect dice throw button and diceresult label
bv.getThrowDice().addMouseListener(
new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
dice1 = bv.getBoard().dice1.throwDice();
dice2 = bv.getBoard().dice2.throwDice();
bv.diceResult.setText("You got "
+ dice1 + " and " + dice2);
}
});
setPlayerButtonEnablePerTurn();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
class ShoMouseEvents extends MouseAdapter {
/**This mouse action listeners are for the 'TileV' class.
* Users are expected to move their mouse cursor into a
* valid source Tile ( which we will detect using the mouseEntered event ),
* then clicking it would move the token to a valid destination
* TileV and release the mouse.This performs a 'move' in game term
// */
@Override
public void mouseClicked(MouseEvent e) {
s.getB().repeatThrow=false;
TileV temp = (TileV) e.getComponent();
if (!temp.isOccupied()) {
//TODO a popup to tell the user that this is empty
return;
}
if(s.getB().checkAndMove(temp.getTile(), dice1+dice2))
{
}else {
return;
}
if (!s.getB().repeatThrow) {
currentPlayer = s.getNextPlayer();
setPlayerButtonEnablePerTurn();
dice1=0;
dice2=0;
bv.diceResult.setText(currentPlayer.getLabel()+" turn:Throw dice!" );
}
bv.repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
//check if player has a token in this place and indicate a possible move
TileV temp = (TileV) e.getComponent();
temp.getTile().getOccupyingStack();
//
if (!temp.isOccupied()) {
return;
} else {
//get the place id==source id
int index = temp.getID();
/* the place might be a starting place, in which case we have an
index not corresponding to the one in array of place*/
if (temp.isStartingPlace()) {
index = 0;
}
index = index + dice1 + dice2 - 1;
if (index < 64 && index > 0) {
highlightThisplace = bv.normal[index];
highlightThisplace.setBorder(highlightThisplace.highLightBorder);
highlightThisplace.repaint(20);
}
}
}
@Override
public void mouseExited(MouseEvent e) {
if (highlightThisplace != null) {
highlightThisplace.setBorder(highlightThisplace.normal);
highlightThisplace.repaint(20);
highlightThisplace = null;
}
}
}
}