Continuing gradual process of collecting state from various GUI components and handlers and aggregating it in PlayerController.
Removed references to Refree from some handlers, replacing them with direct use of PlayerController (Law of Demeter).
This commit is contained in:
54
src/controller/BoardPanelMouseListener.java
Normal file
54
src/controller/BoardPanelMouseListener.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package controller;
|
||||
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.awt.event.MouseWheelEvent;
|
||||
import java.awt.event.MouseWheelListener;
|
||||
|
||||
public class BoardPanelMouseListener implements MouseListener, MouseWheelListener {
|
||||
|
||||
private final int col;
|
||||
private final int row;
|
||||
private final PlayerController pc;
|
||||
|
||||
public BoardPanelMouseListener(int row, int col, PlayerController pc) {
|
||||
this.row = row;
|
||||
this.col = col;
|
||||
this.pc = pc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
pc.setCell(row, col);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent arg0) {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent arg0) {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent arg0) {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent arg0) {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseWheelMoved(MouseWheelEvent e) {
|
||||
if (e.getWheelRotation() > 0) {
|
||||
pc.incrementColor();
|
||||
} else if (e.getWheelRotation() < 0) {
|
||||
pc.decrementColor();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +1,16 @@
|
||||
package controller;
|
||||
|
||||
import model.Board.TileColor;
|
||||
import model.Board;
|
||||
import model.CellPointer;
|
||||
import model.Move;
|
||||
|
||||
public class PlayerController {
|
||||
private final CellPointer cell = new CellPointer(0, 0);
|
||||
private TileColor color = TileColor.BLUE;
|
||||
|
||||
private boolean current = true;
|
||||
private boolean ready = false;
|
||||
private TileColor color = TileColor.BLUE;
|
||||
|
||||
public void denyMove() {
|
||||
ready = false;
|
||||
@@ -23,6 +26,50 @@ public class PlayerController {
|
||||
|
||||
}
|
||||
|
||||
public void decrementColor() {
|
||||
TileColor[] colors = Board.TileColor.values();
|
||||
int currentColor = -1;
|
||||
for (int i = 0; i < colors.length; i++) {
|
||||
if (colors[i] == color) {
|
||||
currentColor = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (currentColor == -1) {
|
||||
throw new RuntimeException("Color not found: " + color + "!");
|
||||
}
|
||||
currentColor = (currentColor + colors.length -1) % colors.length;
|
||||
if (colors[currentColor] == TileColor.NONE) {
|
||||
currentColor = (currentColor + colors.length -1) % colors.length;
|
||||
}
|
||||
color = colors[currentColor];
|
||||
setCurrent(false);
|
||||
}
|
||||
|
||||
public void incrementColor() {
|
||||
TileColor[] colors = Board.TileColor.values();
|
||||
int currentColor = -1;
|
||||
for (int i = 0; i < colors.length; i++) {
|
||||
if (colors[i] == color) {
|
||||
currentColor = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (currentColor == -1) {
|
||||
throw new RuntimeException("Color not found: " + color + "!");
|
||||
}
|
||||
currentColor = (currentColor+1) % colors.length;
|
||||
if (colors[currentColor] == TileColor.NONE) {
|
||||
currentColor = (currentColor+1) % colors.length;
|
||||
}
|
||||
color = colors[currentColor];
|
||||
setCurrent(false);
|
||||
}
|
||||
|
||||
public boolean isCurrent() {
|
||||
return current;
|
||||
}
|
||||
|
||||
public boolean isReady() {
|
||||
return ready;
|
||||
}
|
||||
@@ -37,4 +84,8 @@ public class PlayerController {
|
||||
public void setColor(TileColor clr) {
|
||||
color = clr;
|
||||
}
|
||||
|
||||
public void setCurrent(boolean current) {
|
||||
this.current = current;
|
||||
}
|
||||
}
|
||||
|
||||
42
src/controller/TSPMouseListener.java
Normal file
42
src/controller/TSPMouseListener.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package controller;
|
||||
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
|
||||
import view.TileSelectionPanel;
|
||||
|
||||
import model.Board.TileColor;
|
||||
|
||||
public class TSPMouseListener implements MouseListener{
|
||||
private PlayerController pc;
|
||||
private TileColor tileColor;
|
||||
private TileSelectionPanel tsp;
|
||||
|
||||
public TSPMouseListener(PlayerController pc, TileSelectionPanel tsp, TileColor tileColor) {
|
||||
this.pc = pc;
|
||||
this.tileColor = tileColor;
|
||||
this.tsp = tsp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent arg0) {
|
||||
pc.setColor(tileColor);
|
||||
tsp.setCurrent(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent arg0) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent arg0) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent arg0) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent arg0) {
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package view;
|
||||
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.security.auth.Refreshable;
|
||||
@@ -10,46 +8,13 @@ import javax.swing.ImageIcon;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import controller.BoardPanelMouseListener;
|
||||
|
||||
import model.Board;
|
||||
import model.Referee;
|
||||
|
||||
public class BoardPanel extends JPanel implements Refreshable {
|
||||
private class BoardPanelMouseListener implements MouseListener {
|
||||
|
||||
private final int col;
|
||||
private final int row;
|
||||
|
||||
public BoardPanelMouseListener(int r, int c) {
|
||||
row = r;
|
||||
col = c;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
referee.getPlayer().setCell(row, col);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent arg0) {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent arg0) {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent arg0) {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent arg0) {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static final URL BLUE_ICON = BoardPanel.class
|
||||
.getResource("/img/blue.png");
|
||||
@@ -74,7 +39,12 @@ public class BoardPanel extends JPanel implements Refreshable {
|
||||
for (int r = 0; r < Board.NUM_ROWS; r++) {
|
||||
for (int c = 0; c < Board.NUM_COLS; c++) {
|
||||
board[r][c] = new JLabel(new ImageIcon(NONE_ICON));
|
||||
board[r][c].addMouseListener(new BoardPanelMouseListener(r, c));
|
||||
BoardPanelMouseListener bpml = new BoardPanelMouseListener(r, c, referee.getPlayer());
|
||||
|
||||
//separate listener on each JLabel? This should probably be changed.
|
||||
board[r][c].addMouseListener(bpml);
|
||||
board[r][c].addMouseWheelListener(bpml);
|
||||
|
||||
add(board[r][c]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ public class MainFrame extends JFrame {
|
||||
ScorePanel sp = new ScorePanel(game);
|
||||
BoardPanel bp = new BoardPanel(game);
|
||||
MessagePanel mp = new MessagePanel(game);
|
||||
TileSelectionPanel tp = new TileSelectionPanel(game);
|
||||
TileSelectionPanel tp = new TileSelectionPanel(game.getPlayer());
|
||||
|
||||
// Add refreshables.
|
||||
elements.add(sp);
|
||||
|
||||
@@ -4,8 +4,6 @@ import java.awt.Color;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.Insets;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
|
||||
import javax.security.auth.RefreshFailedException;
|
||||
import javax.security.auth.Refreshable;
|
||||
@@ -14,27 +12,25 @@ import javax.swing.ImageIcon;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import controller.PlayerController;
|
||||
import controller.TSPMouseListener;
|
||||
|
||||
import model.Board.TileColor;
|
||||
import model.Referee;
|
||||
|
||||
public class TileSelectionPanel extends JPanel implements Refreshable {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final JLabel blue = new JLabel(new ImageIcon(BoardPanel.BLUE_ICON));
|
||||
private final JLabel green = new JLabel(
|
||||
new ImageIcon(BoardPanel.GREEN_ICON));
|
||||
private boolean isCurrent = true;
|
||||
private final JLabel red = new JLabel(new ImageIcon(BoardPanel.RED_ICON));
|
||||
private final Referee referee;
|
||||
private final JLabel yellow = new JLabel(new ImageIcon(
|
||||
BoardPanel.YELLOW_ICON));
|
||||
private final PlayerController pc;
|
||||
|
||||
public TileSelectionPanel(Referee ref) {
|
||||
referee = ref;
|
||||
public TileSelectionPanel(PlayerController pc) {
|
||||
this.pc = pc;
|
||||
|
||||
initLayout();
|
||||
initActions();
|
||||
@@ -42,12 +38,16 @@ public class TileSelectionPanel extends JPanel implements Refreshable {
|
||||
|
||||
@Override
|
||||
public boolean isCurrent() {
|
||||
return isCurrent;
|
||||
return pc.isCurrent();
|
||||
}
|
||||
|
||||
public void setCurrent(boolean current) {
|
||||
pc.setCurrent(current);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refresh() throws RefreshFailedException {
|
||||
TileColor tile = referee.getPlayer().getColor();
|
||||
TileColor tile = pc.getColor();
|
||||
|
||||
blue.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
|
||||
green.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
|
||||
@@ -69,102 +69,14 @@ public class TileSelectionPanel extends JPanel implements Refreshable {
|
||||
break;
|
||||
}
|
||||
|
||||
isCurrent = true;
|
||||
pc.setCurrent(true);
|
||||
}
|
||||
|
||||
private void initActions() {
|
||||
blue.addMouseListener(new MouseListener() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent arg0) {
|
||||
referee.getPlayer().setColor(TileColor.BLUE);
|
||||
isCurrent = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent arg0) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent arg0) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent arg0) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent arg0) {
|
||||
}
|
||||
});
|
||||
green.addMouseListener(new MouseListener() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent arg0) {
|
||||
referee.getPlayer().setColor(TileColor.GREEN);
|
||||
isCurrent = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent arg0) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent arg0) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent arg0) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent arg0) {
|
||||
}
|
||||
});
|
||||
red.addMouseListener(new MouseListener() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent arg0) {
|
||||
referee.getPlayer().setColor(TileColor.RED);
|
||||
isCurrent = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent arg0) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent arg0) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent arg0) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent arg0) {
|
||||
}
|
||||
});
|
||||
yellow.addMouseListener(new MouseListener() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent arg0) {
|
||||
referee.getPlayer().setColor(TileColor.YELLOW);
|
||||
isCurrent = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent arg0) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent arg0) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent arg0) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent arg0) {
|
||||
}
|
||||
});
|
||||
blue.addMouseListener(new TSPMouseListener(pc, this, TileColor.BLUE));
|
||||
green.addMouseListener(new TSPMouseListener(pc, this, TileColor.GREEN));
|
||||
red.addMouseListener(new TSPMouseListener(pc, this, TileColor.RED));
|
||||
yellow.addMouseListener(new TSPMouseListener(pc, this, TileColor.YELLOW));
|
||||
}
|
||||
|
||||
private void initLayout() {
|
||||
|
||||
Reference in New Issue
Block a user