From d8f5f3bbf475f21746c319f7a04052a59b14b26f Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Thu, 18 Jul 2019 15:01:15 +0200 Subject: [PATCH] Fixed input listening bug in NaturalPlayer + disconnect methods in Game and Player + NaturalPlayer removes its MouseListener from OverlayComponent after the disconnect method is called --- src/dev/kske/chess/game/Game.java | 7 ++ src/dev/kske/chess/game/NaturalPlayer.java | 85 +++++++++++++--------- src/dev/kske/chess/game/Player.java | 2 + src/dev/kske/chess/game/ai/AIPlayer.java | 3 + src/dev/kske/chess/ui/MenuBar.java | 8 +- 5 files changed, 68 insertions(+), 37 deletions(-) diff --git a/src/dev/kske/chess/game/Game.java b/src/dev/kske/chess/game/Game.java index a7cd531..e3a83f6 100644 --- a/src/dev/kske/chess/game/Game.java +++ b/src/dev/kske/chess/game/Game.java @@ -93,5 +93,12 @@ public class Game { overlayComponent.clearArrow(); } + /** + * Removed all connections between the game and the ui. + */ + public void disconnect() { + players.values().forEach(Player::disconnect); + } + public Board getBoard() { return board; } } diff --git a/src/dev/kske/chess/game/NaturalPlayer.java b/src/dev/kske/chess/game/NaturalPlayer.java index f105862..e181320 100644 --- a/src/dev/kske/chess/game/NaturalPlayer.java +++ b/src/dev/kske/chess/game/NaturalPlayer.java @@ -1,7 +1,7 @@ package dev.kske.chess.game; -import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; import java.util.List; import java.util.stream.Collectors; @@ -18,43 +18,19 @@ import dev.kske.chess.ui.OverlayComponent; * Created: 06.07.2019
* Author: Kai S. K. Engelbart */ -public class NaturalPlayer extends Player { +public class NaturalPlayer extends Player implements MouseListener { - private boolean moveRequested; + private final OverlayComponent overlayComponent; + + private boolean moveRequested; + private Position pos; public NaturalPlayer(Color color, OverlayComponent overlayComponent) { super(color); - moveRequested = false; - overlayComponent.addMouseListener(new MouseAdapter() { + this.overlayComponent = overlayComponent; + moveRequested = false; - private Position pos; - - @Override - public void mousePressed(MouseEvent evt) { - if (!moveRequested) return; - if (pos == null) { - pos = new Position(evt.getPoint().x / overlayComponent.getTileSize(), - evt.getPoint().y / overlayComponent.getTileSize()); - - Board board = (Board) NaturalPlayer.this.board.clone(); - if (board.get(pos) != null && board.get(pos).getColor() == color) { - List positions = board.getMoves(pos) - .stream() - .map(move -> move.dest) - .collect(Collectors.toList()); - overlayComponent.displayDots(positions); - } else pos = null; - } else { - Position dest = new Position(evt.getPoint().x / overlayComponent.getTileSize(), - evt.getPoint().y / overlayComponent.getTileSize()); - - overlayComponent.clearDots(); - moveRequested = false; - game.onMove(NaturalPlayer.this, new Move(pos, dest, Type.UNKNOWN)); - pos = null; - } - } - }); + overlayComponent.addMouseListener(this); } @Override @@ -66,4 +42,47 @@ public class NaturalPlayer extends Player { public void cancelMove() { moveRequested = false; } + + @Override + public void disconnect() { + overlayComponent.removeMouseListener(this); + } + + @Override + public void mousePressed(MouseEvent evt) { + if (!moveRequested) return; + if (pos == null) { + pos = new Position(evt.getPoint().x / overlayComponent.getTileSize(), + evt.getPoint().y / overlayComponent.getTileSize()); + + Board board = (Board) NaturalPlayer.this.board.clone(); + if (board.get(pos) != null && board.get(pos).getColor() == color) { + List positions = board.getMoves(pos) + .stream() + .map(move -> move.dest) + .collect(Collectors.toList()); + overlayComponent.displayDots(positions); + } else pos = null; + } else { + Position dest = new Position(evt.getPoint().x / overlayComponent.getTileSize(), + evt.getPoint().y / overlayComponent.getTileSize()); + + overlayComponent.clearDots(); + moveRequested = false; + game.onMove(NaturalPlayer.this, new Move(pos, dest, Type.UNKNOWN)); + pos = null; + } + } + + @Override + public void mouseClicked(MouseEvent e) {} + + @Override + public void mouseReleased(MouseEvent e) {} + + @Override + public void mouseEntered(MouseEvent e) {} + + @Override + public void mouseExited(MouseEvent e) {} } diff --git a/src/dev/kske/chess/game/Player.java b/src/dev/kske/chess/game/Player.java index 07d578a..9e55756 100644 --- a/src/dev/kske/chess/game/Player.java +++ b/src/dev/kske/chess/game/Player.java @@ -23,6 +23,8 @@ public abstract class Player { public abstract void cancelMove(); + public abstract void disconnect(); + public Game getGame() { return game; } public void setGame(Game game) { diff --git a/src/dev/kske/chess/game/ai/AIPlayer.java b/src/dev/kske/chess/game/ai/AIPlayer.java index 6adf76e..9e1f2fe 100644 --- a/src/dev/kske/chess/game/ai/AIPlayer.java +++ b/src/dev/kske/chess/game/ai/AIPlayer.java @@ -98,4 +98,7 @@ public class AIPlayer extends Player { } } } + + @Override + public void disconnect() {} } diff --git a/src/dev/kske/chess/ui/MenuBar.java b/src/dev/kske/chess/ui/MenuBar.java index 471166f..362e684 100644 --- a/src/dev/kske/chess/ui/MenuBar.java +++ b/src/dev/kske/chess/ui/MenuBar.java @@ -16,12 +16,12 @@ public class MenuBar extends JMenuBar { private static final long serialVersionUID = -7221583703531248228L; - private final MainWindow mainWindow; - private final BoardPane boardPane; + private final MainWindow mainWindow; + private final BoardPane boardPane; public MenuBar(MainWindow mainWindow) { - this.mainWindow = mainWindow; - boardPane = mainWindow.getBoardPane(); + this.mainWindow = mainWindow; + boardPane = mainWindow.getBoardPane(); initGameMenu(); }