feat: popups
This commit is contained in:
@@ -3,18 +3,18 @@ package chess.view.DDDrender;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
import chess.controller.commands.MoveCommand;
|
import chess.controller.commands.*;
|
||||||
import chess.controller.event.GameListener;
|
import chess.controller.event.GameListener;
|
||||||
import imgui.ImGui;
|
import imgui.ImGui;
|
||||||
|
import imgui.type.ImBoolean;
|
||||||
import org.joml.Vector2f;
|
import org.joml.Vector2f;
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
|
|
||||||
import chess.controller.Command;
|
import chess.controller.Command;
|
||||||
import chess.controller.Command.CommandResult;
|
import chess.controller.Command.CommandResult;
|
||||||
import chess.controller.CommandExecutor;
|
import chess.controller.CommandExecutor;
|
||||||
import chess.controller.commands.GetAllowedMovesPieceCommand;
|
|
||||||
import chess.controller.commands.GetPieceAtCommand;
|
|
||||||
import chess.controller.event.GameAdaptator;
|
import chess.controller.event.GameAdaptator;
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
@@ -39,6 +39,11 @@ public class DDDView extends GameAdaptator implements GameListener {
|
|||||||
private static final float animationTime = 1.5f; // in seconds
|
private static final float animationTime = 1.5f; // in seconds
|
||||||
private static final int animationTurns = 1;
|
private static final int animationTurns = 1;
|
||||||
|
|
||||||
|
private float moveProgress = 0.0f;
|
||||||
|
|
||||||
|
private String waitingPopup = null;
|
||||||
|
private final ImBoolean popupOpened = new ImBoolean(false);
|
||||||
|
|
||||||
public DDDView(CommandExecutor commandExecutor) {
|
public DDDView(CommandExecutor commandExecutor) {
|
||||||
this.commandExecutor = commandExecutor;
|
this.commandExecutor = commandExecutor;
|
||||||
this.world = new World();
|
this.world = new World();
|
||||||
@@ -49,7 +54,11 @@ public class DDDView extends GameAdaptator implements GameListener {
|
|||||||
private void cancelClick() {
|
private void cancelClick() {
|
||||||
this.click = null;
|
this.click = null;
|
||||||
}
|
}
|
||||||
private void setClick(Coordinate coordinate) {this.click=coordinate;}
|
|
||||||
|
private void setClick(Coordinate coordinate) {
|
||||||
|
this.click = coordinate;
|
||||||
|
}
|
||||||
|
|
||||||
private CommandResult sendCommand(Command command) {
|
private CommandResult sendCommand(Command command) {
|
||||||
return this.commandExecutor.executeCommand(command);
|
return this.commandExecutor.executeCommand(command);
|
||||||
}
|
}
|
||||||
@@ -152,7 +161,11 @@ public class DDDView extends GameAdaptator implements GameListener {
|
|||||||
Piece p = pieceAt(coordinate);
|
Piece p = pieceAt(coordinate);
|
||||||
if (p == null)
|
if (p == null)
|
||||||
return;
|
return;
|
||||||
this.world.getPiece(coordinate).setColor(p.getColor() == Color.White ? WHITE : BLACK);
|
PieceEntity pEntity = this.world.getPiece(coordinate);
|
||||||
|
if (pEntity == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
pEntity.setColor(p.getColor() == Color.White ? WHITE : BLACK);
|
||||||
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate);
|
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate);
|
||||||
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
|
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
|
||||||
return;
|
return;
|
||||||
@@ -210,7 +223,26 @@ public class DDDView extends GameAdaptator implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void onFooterRender() {
|
private void onFooterRender() {
|
||||||
ImGui.button("Coucou");
|
if (ImGui.button("Roque")) {
|
||||||
|
sendCommand(new CastlingCommand(false));
|
||||||
|
}
|
||||||
|
ImGui.sameLine();
|
||||||
|
if (ImGui.button("Grand Roque")) {
|
||||||
|
sendCommand(new CastlingCommand(true));
|
||||||
|
}
|
||||||
|
ImGui.sameLine();
|
||||||
|
if (ImGui.button("Annuler le coup précédent")) {
|
||||||
|
sendCommand(new UndoCommand());
|
||||||
|
}
|
||||||
|
openPopup();
|
||||||
|
renderPopups();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void openPopup() {
|
||||||
|
if (waitingPopup != null) {
|
||||||
|
ImGui.openPopup(waitingPopup);
|
||||||
|
waitingPopup = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initBoard() throws IOException {
|
private void initBoard() throws IOException {
|
||||||
@@ -235,15 +267,67 @@ public class DDDView extends GameAdaptator implements GameListener {
|
|||||||
this.world.addEntity(this.boardEntity);
|
this.world.addEntity(this.boardEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public void onMove(Move move) {
|
* @param begin begin
|
||||||
|
* @param middle control point
|
||||||
|
* @param end end
|
||||||
|
* @param t between 0 and 1
|
||||||
|
* @return the point
|
||||||
|
*/
|
||||||
|
private Vector3f bezierCurve(Vector3f begin, Vector3f middle, Vector3f end, float t) {
|
||||||
|
return begin.mul((1.0f - t) * (1.0f - t)).add(middle.mul(2.0f * t * (1.0f - t))).add(end.mul(t * t));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Function<Float, Float> lagrangeInterpolation(Vector3f begin, Vector3f middle, Vector3f end) {
|
||||||
|
return (t) -> {
|
||||||
|
return t+1.0f;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private void pieceTick(float progress, PieceEntity piece, Move move) {
|
||||||
|
float height = 1; // how much the piece is raised
|
||||||
|
Vector2f pieceStartBoard = DDDPlacement.coordinatesToVector(move.getStart());
|
||||||
|
Vector2f pieceDestinationBoard = DDDPlacement.coordinatesToVector(move.getFinish());
|
||||||
|
Vector3f start = new Vector3f(pieceStartBoard.x(), 0, pieceStartBoard.y());
|
||||||
|
Vector3f top = new Vector3f(pieceDestinationBoard.x() - pieceStartBoard.x(), height, pieceDestinationBoard.y() - pieceStartBoard.y());
|
||||||
|
Vector3f end = new Vector3f(pieceDestinationBoard.x(), 0, pieceDestinationBoard.y());
|
||||||
|
|
||||||
|
piece.setPosition(bezierCurve(start, top, end, progress));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void move3DPiece(Move move) {
|
||||||
|
Vector2f pieceDestBoardPos = DDDPlacement.coordinatesToVector(move.getFinish());
|
||||||
|
Vector3f pieceDestWorldPos = new Vector3f(pieceDestBoardPos.x(), 0, pieceDestBoardPos.y());
|
||||||
|
|
||||||
|
final PieceEntity pEntity = this.world.getPiece(move.getStart());
|
||||||
|
final Move pMove = move;
|
||||||
|
|
||||||
|
this.moveProgress = 0.0f;
|
||||||
|
|
||||||
|
Consumer<Float> moveConsumer = (delta) -> {
|
||||||
|
this.moveProgress += delta;
|
||||||
|
pieceTick(this.moveProgress, pEntity, pMove);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.window.addRegularTask(moveConsumer);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
this.window.removeRegularTask(moveConsumer);
|
||||||
|
|
||||||
if (move.getDeadPieceCoords() != null) {
|
if (move.getDeadPieceCoords() != null) {
|
||||||
this.world.ejectPiece(move.getDeadPieceCoords());
|
this.world.ejectPiece(move.getDeadPieceCoords());
|
||||||
}
|
}
|
||||||
Vector2f pieceBoardPos = DDDPlacement.coordinatesToVector(move.getFinish());
|
pEntity.setPosition(pieceDestWorldPos);
|
||||||
Vector3f pieceWorldPos = new Vector3f(pieceBoardPos.x(), 0, pieceBoardPos.y());
|
|
||||||
this.world.getPiece(move.getStart()).setPosition(pieceWorldPos);
|
this.world.movePiece(pEntity, move);
|
||||||
this.world.movePiece(this.world.getPiece(move.getStart()), move);
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMove(Move move) {
|
||||||
|
move3DPiece(move);
|
||||||
cameraRotate();
|
cameraRotate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,7 +337,7 @@ public class DDDView extends GameAdaptator implements GameListener {
|
|||||||
this.camera.setRotateAngle(this.camera.getRotateAngle() + angle * delta * oddAnimationTurn / animationTime);
|
this.camera.setRotateAngle(this.camera.getRotateAngle() + angle * delta * oddAnimationTurn / animationTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cameraRotate() {
|
private void cameraRotate() {
|
||||||
float end = this.camera.getRotateAngle() + (float) Math.PI;
|
float end = this.camera.getRotateAngle() + (float) Math.PI;
|
||||||
Consumer<Float> rotationConsumer = this::cameraTick;
|
Consumer<Float> rotationConsumer = this::cameraTick;
|
||||||
this.window.addRegularTask(rotationConsumer);
|
this.window.addRegularTask(rotationConsumer);
|
||||||
@@ -278,4 +362,59 @@ public class DDDView extends GameAdaptator implements GameListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void renderPopup(String title, String text) {
|
||||||
|
if(ImGui.beginPopupModal(title, popupOpened)){
|
||||||
|
ImGui.text(text);
|
||||||
|
ImGui.endPopup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void renderPopups() {
|
||||||
|
renderPopup("Check","Your king is in check");
|
||||||
|
renderPopup("Checkmate", "Checkmate, it's a win!");
|
||||||
|
renderPopup("Promotion", "Please promote your pawn.");
|
||||||
|
renderPopup("Pat", "It's a pat!");
|
||||||
|
renderPopup("Tie", "It's a tie!");
|
||||||
|
renderPopup("White surrender", "The white player has surrendered!");
|
||||||
|
renderPopup("Black surrender", "The black player has surrendered!");
|
||||||
|
renderPopup("White victory", "The white player has won !");
|
||||||
|
renderPopup("Black victory", "The black player has won !");
|
||||||
|
renderPopup("End", "End of the game, thank you for playing!");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void openPopup(String title) {
|
||||||
|
this.popupOpened.set(true);
|
||||||
|
this.waitingPopup = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInCheck(){
|
||||||
|
openPopup("Check");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDraw() {
|
||||||
|
openPopup("Tie");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKingInMat() {
|
||||||
|
openPopup("Checkmate");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGameEnd() {
|
||||||
|
openPopup("End");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPatSituation() {
|
||||||
|
openPopup("Pat");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSurrender(Color color) {
|
||||||
|
openPopup(color == Color.White ? "White surrender": "Black surrender");
|
||||||
|
openPopup(color == Color.White ? "Black victory" : "White victory");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user