Replace old event framework with dev.kske:event-bus:0.0.3
This commit is contained in:
parent
4566b33522
commit
2c5521d2ff
12
pom.xml
12
pom.xml
@ -44,7 +44,19 @@
|
|||||||
<maven.compiler.target>11</maven.compiler.target>
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>kske-repo</id>
|
||||||
|
<url>https://kske.dev/maven-repo</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>dev.kske</groupId>
|
||||||
|
<artifactId>event-bus</artifactId>
|
||||||
|
<version>0.0.3</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter-api</artifactId>
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
@ -3,8 +3,8 @@ package dev.kske.chess.board;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import dev.kske.chess.board.Piece.Color;
|
import dev.kske.chess.board.Piece.Color;
|
||||||
import dev.kske.chess.event.EventBus;
|
|
||||||
import dev.kske.chess.event.MoveEvent;
|
import dev.kske.chess.event.MoveEvent;
|
||||||
|
import dev.kske.eventbus.EventBus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>Chess</strong><br>
|
* Project: <strong>Chess</strong><br>
|
||||||
|
@ -1,18 +0,0 @@
|
|||||||
package dev.kske.chess.event;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Project: <strong>Chess</strong><br>
|
|
||||||
* File: <strong>Event.java</strong><br>
|
|
||||||
* Created: <strong>7 Aug 2019</strong><br>
|
|
||||||
*
|
|
||||||
* @since Chess v0.4-alpha
|
|
||||||
* @author Kai S. K. Engelbart
|
|
||||||
* @param <T> the type of the event's value
|
|
||||||
*/
|
|
||||||
public interface Event<T> {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return The data associated with the event
|
|
||||||
*/
|
|
||||||
T getData();
|
|
||||||
}
|
|
@ -1,59 +0,0 @@
|
|||||||
package dev.kske.chess.event;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Dispatches {@link Event}s to various {@link Subscriber}s.<br>
|
|
||||||
* <br>
|
|
||||||
* Project: <strong>Chess</strong><br>
|
|
||||||
* File: <strong>EventBus.java</strong><br>
|
|
||||||
* Created: <strong>7 Aug 2019</strong><br>
|
|
||||||
*
|
|
||||||
* @since Chess v0.4-alpha
|
|
||||||
* @author Kai S. K. Engelbart
|
|
||||||
*/
|
|
||||||
public class EventBus {
|
|
||||||
|
|
||||||
private List<Subscriber> subscribers;
|
|
||||||
|
|
||||||
private static EventBus instance;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return a singleton instance of {@link EventBus}
|
|
||||||
*/
|
|
||||||
public static EventBus getInstance() {
|
|
||||||
if (instance == null)
|
|
||||||
instance = new EventBus();
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
private EventBus() {
|
|
||||||
subscribers = new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Registers a subscriber to which future events will be dispatched.
|
|
||||||
*
|
|
||||||
* @param subscribable the subscriber to register
|
|
||||||
*/
|
|
||||||
public void register(Subscriber subscribable) {
|
|
||||||
subscribers.add(subscribable);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Dispatches an event to all {@Subscriber}s registered at this event bus.
|
|
||||||
*
|
|
||||||
* @param event the event to dispatch
|
|
||||||
*/
|
|
||||||
public void dispatch(Event<?> event) {
|
|
||||||
subscribers.stream()
|
|
||||||
.filter(e -> e.supports().contains(event.getClass()))
|
|
||||||
.forEach(e -> e.handle(event));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return a list of all registered subscribers
|
|
||||||
*/
|
|
||||||
public List<Subscriber> getSubscribers() { return subscribers; }
|
|
||||||
}
|
|
@ -1,6 +1,7 @@
|
|||||||
package dev.kske.chess.event;
|
package dev.kske.chess.event;
|
||||||
|
|
||||||
import dev.kske.chess.game.Game;
|
import dev.kske.chess.game.Game;
|
||||||
|
import dev.kske.eventbus.IEvent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>Chess</strong><br>
|
* Project: <strong>Chess</strong><br>
|
||||||
@ -10,7 +11,7 @@ import dev.kske.chess.game.Game;
|
|||||||
* @since Chess v0.5-alpha
|
* @since Chess v0.5-alpha
|
||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
*/
|
*/
|
||||||
public class GameStartEvent implements Event<Game> {
|
public class GameStartEvent implements IEvent {
|
||||||
|
|
||||||
private final Game game;
|
private final Game game;
|
||||||
|
|
||||||
@ -23,6 +24,8 @@ public class GameStartEvent implements Event<Game> {
|
|||||||
game = source;
|
game = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public Game getData() { return game; }
|
* @return the started game
|
||||||
|
*/
|
||||||
|
public Game getGame() { return game; }
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package dev.kske.chess.event;
|
package dev.kske.chess.event;
|
||||||
|
|
||||||
import dev.kske.chess.board.BoardState;
|
import dev.kske.chess.board.*;
|
||||||
import dev.kske.chess.board.Move;
|
import dev.kske.eventbus.IEvent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>Chess</strong><br>
|
* Project: <strong>Chess</strong><br>
|
||||||
@ -11,7 +11,7 @@ import dev.kske.chess.board.Move;
|
|||||||
* @since Chess v0.4-alpha
|
* @since Chess v0.4-alpha
|
||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
*/
|
*/
|
||||||
public class MoveEvent implements Event<Move> {
|
public class MoveEvent implements IEvent {
|
||||||
|
|
||||||
private final Move move;
|
private final Move move;
|
||||||
private final BoardState boardState;
|
private final BoardState boardState;
|
||||||
@ -27,8 +27,10 @@ public class MoveEvent implements Event<Move> {
|
|||||||
this.boardState = boardState;
|
this.boardState = boardState;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public Move getData() { return move; }
|
* @return the move
|
||||||
|
*/
|
||||||
|
public Move getMove() { return move; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the state of the board after the move
|
* @return the state of the board after the move
|
||||||
|
@ -1,31 +0,0 @@
|
|||||||
package dev.kske.chess.event;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implementations of this interface can register themselves at the
|
|
||||||
* {@link EventBus} and will be triggered every time an {@link Event} of a
|
|
||||||
* supported type.<br>
|
|
||||||
* <br>
|
|
||||||
* Project: <strong>Chess</strong><br>
|
|
||||||
* File: <strong>Subscribable.java</strong><br>
|
|
||||||
* Created: <strong>7 Aug 2019</strong><br>
|
|
||||||
*
|
|
||||||
* @since Chess v0.4-alpha
|
|
||||||
* @author Kai S. K. Engelbart
|
|
||||||
*/
|
|
||||||
public interface Subscriber {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Consumes an event dispatched by an event bus.
|
|
||||||
*
|
|
||||||
* @param event The event dispatched by the event bus, only of supported
|
|
||||||
* type
|
|
||||||
*/
|
|
||||||
void handle(Event<?> event);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return A set of classes this class is supposed to handle in events
|
|
||||||
*/
|
|
||||||
Set<Class<?>> supports();
|
|
||||||
}
|
|
@ -1,23 +1,17 @@
|
|||||||
package dev.kske.chess.game;
|
package dev.kske.chess.game;
|
||||||
|
|
||||||
import java.util.EnumMap;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
|
|
||||||
import dev.kske.chess.board.Board;
|
import dev.kske.chess.board.*;
|
||||||
import dev.kske.chess.board.BoardState;
|
|
||||||
import dev.kske.chess.board.Move;
|
|
||||||
import dev.kske.chess.board.Piece.Color;
|
import dev.kske.chess.board.Piece.Color;
|
||||||
import dev.kske.chess.event.EventBus;
|
import dev.kske.chess.event.*;
|
||||||
import dev.kske.chess.event.GameStartEvent;
|
|
||||||
import dev.kske.chess.event.MoveEvent;
|
|
||||||
import dev.kske.chess.game.ai.AIPlayer;
|
import dev.kske.chess.game.ai.AIPlayer;
|
||||||
import dev.kske.chess.io.EngineUtil;
|
import dev.kske.chess.io.EngineUtil;
|
||||||
import dev.kske.chess.io.EngineUtil.EngineInfo;
|
import dev.kske.chess.io.EngineUtil.EngineInfo;
|
||||||
import dev.kske.chess.ui.BoardComponent;
|
import dev.kske.chess.ui.*;
|
||||||
import dev.kske.chess.ui.BoardPane;
|
import dev.kske.eventbus.EventBus;
|
||||||
import dev.kske.chess.ui.OverlayComponent;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>Chess</strong><br>
|
* Project: <strong>Chess</strong><br>
|
||||||
|
@ -1,20 +1,15 @@
|
|||||||
package dev.kske.chess.ui;
|
package dev.kske.chess.ui;
|
||||||
|
|
||||||
import java.awt.GridBagConstraints;
|
import java.awt.*;
|
||||||
import java.awt.GridBagLayout;
|
|
||||||
import java.awt.GridLayout;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
import dev.kske.chess.board.BoardState;
|
import dev.kske.chess.board.*;
|
||||||
import dev.kske.chess.board.MoveNode;
|
|
||||||
import dev.kske.chess.board.Piece.Color;
|
import dev.kske.chess.board.Piece.Color;
|
||||||
import dev.kske.chess.event.*;
|
import dev.kske.chess.event.*;
|
||||||
import dev.kske.chess.game.Game;
|
import dev.kske.chess.game.*;
|
||||||
import dev.kske.chess.game.NaturalPlayer;
|
import dev.kske.eventbus.*;
|
||||||
|
import dev.kske.eventbus.Event;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The part of this application's {@link MainWindow} that displays {@link Game}s
|
* The part of this application's {@link MainWindow} that displays {@link Game}s
|
||||||
@ -27,7 +22,7 @@ import dev.kske.chess.game.NaturalPlayer;
|
|||||||
* @since Chess v0.4-alpha
|
* @since Chess v0.4-alpha
|
||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
*/
|
*/
|
||||||
public class GamePane extends JComponent {
|
public class GamePane extends JComponent implements EventListener {
|
||||||
|
|
||||||
private static final long serialVersionUID = 4349772338239617477L;
|
private static final long serialVersionUID = 4349772338239617477L;
|
||||||
|
|
||||||
@ -37,6 +32,7 @@ public class GamePane extends JComponent {
|
|||||||
private Color activeColor;
|
private Color activeColor;
|
||||||
private JPanel moveSelectionPanel;
|
private JPanel moveSelectionPanel;
|
||||||
private JButton btnFirst, btnPrevious, btnNext, btnLast;
|
private JButton btnFirst, btnPrevious, btnNext, btnLast;
|
||||||
|
private JList<MoveNode> pgnList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an instance of {@link GamePane}.
|
* Creates an instance of {@link GamePane}.
|
||||||
@ -170,7 +166,7 @@ public class GamePane extends JComponent {
|
|||||||
gbc_scrollPane.gridy = 1;
|
gbc_scrollPane.gridy = 1;
|
||||||
add(scrollPane, gbc_scrollPane);
|
add(scrollPane, gbc_scrollPane);
|
||||||
|
|
||||||
JList<MoveNode> pgnList = new JList<>();
|
pgnList = new JList<>();
|
||||||
pgnList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
pgnList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||||
pgnList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
|
pgnList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
|
||||||
pgnList.setVisibleRowCount(0);
|
pgnList.setVisibleRowCount(0);
|
||||||
@ -180,38 +176,33 @@ public class GamePane extends JComponent {
|
|||||||
// Listen to moves and game (re-)starts and update the move list or
|
// Listen to moves and game (re-)starts and update the move list or
|
||||||
// disable the
|
// disable the
|
||||||
// color switching buttons if necessary
|
// color switching buttons if necessary
|
||||||
EventBus.getInstance().register(new Subscriber() {
|
EventBus.getInstance().registerListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Event
|
||||||
public void handle(Event<?> event) {
|
public void onMove(MoveEvent evt) {
|
||||||
if (
|
btnSwapColors.setEnabled(
|
||||||
event instanceof MoveEvent && (((MoveEvent) event)
|
evt.getBoardState() != BoardState.CHECKMATE
|
||||||
.getBoardState() == BoardState.CHECKMATE
|
&& evt.getBoardState() != BoardState.STALEMATE
|
||||||
|| ((MoveEvent) event)
|
);
|
||||||
.getBoardState() == BoardState.STALEMATE)
|
updateLog();
|
||||||
)
|
}
|
||||||
btnSwapColors.setEnabled(false);
|
|
||||||
else
|
|
||||||
if (event instanceof GameStartEvent)
|
|
||||||
btnSwapColors.setEnabled(
|
|
||||||
game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer ^ game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer
|
|
||||||
);
|
|
||||||
|
|
||||||
if (game.getBoard().getLog() == null)
|
@Event
|
||||||
return;
|
public void onGameStart(GameStartEvent evt) {
|
||||||
|
btnSwapColors.setEnabled(
|
||||||
|
game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer ^ game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer
|
||||||
|
);
|
||||||
|
updateLog();
|
||||||
|
}
|
||||||
|
|
||||||
DefaultListModel<MoveNode> model = new DefaultListModel<>();
|
private void updateLog() {
|
||||||
game.getBoard().getLog().forEach(model::addElement);
|
if (game.getBoard().getLog() == null)
|
||||||
pgnList.setModel(model);
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
DefaultListModel<MoveNode> model = new DefaultListModel<>();
|
||||||
public Set<Class<?>> supports() {
|
game.getBoard().getLog().forEach(model::addElement);
|
||||||
return new HashSet<>(
|
pgnList.setModel(model);
|
||||||
Arrays.asList(MoveEvent.class, GameStartEvent.class)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user