This commit is contained in:
@@ -15,6 +15,11 @@ public class Main extends Application {
|
||||
config.setTitle("Let's play sudoku!");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void disposeWindow() {
|
||||
stateMachine.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initImGui(Configuration config) {
|
||||
super.initImGui(config);
|
||||
|
||||
@@ -12,36 +12,41 @@ import imgui.flag.ImGuiCol;
|
||||
import imgui.flag.ImGuiStyleVar;
|
||||
import sudoku.structure.Block;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
public class SudokuRenderer {
|
||||
|
||||
private final Sudoku sudoku;
|
||||
private final MultiDoku doku;
|
||||
private Sudoku currentSudoku;
|
||||
private int currentIndex = -1;
|
||||
private final Map<Block, Color> colorPalette;
|
||||
|
||||
public SudokuRenderer(Sudoku sudoku) {
|
||||
this.sudoku = sudoku;
|
||||
this.colorPalette = new HashMap<>();
|
||||
initColors();
|
||||
public SudokuRenderer(MultiDoku doku) {
|
||||
this.doku = doku;
|
||||
this.currentSudoku = doku.getSubGrid(0);
|
||||
this.colorPalette = initColors();
|
||||
}
|
||||
|
||||
private void initColors() {
|
||||
List<Color> colors = ColorGenerator.greatPalette(sudoku.getSize());
|
||||
private Map<Block, Color> initColors() {
|
||||
List<Color> colors = ColorGenerator.greatPalette(currentSudoku.getSize());
|
||||
Map<Block, Color> colorPalette = new HashMap<>();
|
||||
int index = 0;
|
||||
for (Block block : sudoku.getBlocks()) {
|
||||
for (Block block : currentSudoku.getBlocks()) {
|
||||
colorPalette.put(block, colors.get(index));
|
||||
index++;
|
||||
}
|
||||
return colorPalette;
|
||||
}
|
||||
|
||||
private void renderPopup() {
|
||||
if (ImGui.beginPopup("editPopup")) {
|
||||
for (int i = 1; i < sudoku.getSize() + 1; i++) {
|
||||
if (i % (int) (Math.sqrt(sudoku.getSize())) != 1)
|
||||
for (int i = 1; i < currentSudoku.getSize() + 1; i++) {
|
||||
if (i % (int) (Math.sqrt(currentSudoku.getSize())) != 1)
|
||||
ImGui.sameLine();
|
||||
if (ImGui.button(Integer.toString(i), new ImVec2(50, 50))) {
|
||||
this.sudoku.setCellSymbol(currentIndex % sudoku.getSize(), currentIndex / sudoku.getSize(), i - 1);
|
||||
this.currentSudoku.setCellSymbol(currentIndex % currentSudoku.getSize(),
|
||||
currentIndex / currentSudoku.getSize(), i - 1);
|
||||
ImGui.closeCurrentPopup();
|
||||
}
|
||||
}
|
||||
@@ -50,30 +55,31 @@ public class SudokuRenderer {
|
||||
}
|
||||
|
||||
public void render() {
|
||||
ImGui.begin("Sudoku Window");
|
||||
for (int y = 0; y < sudoku.getSize(); y++) {
|
||||
for (int x = 0; x < sudoku.getSize(); x++) {
|
||||
ImGui.pushStyleVar(ImGuiStyleVar.FrameBorderSize, 2.0f);
|
||||
ImGui.pushStyleVar(ImGuiStyleVar.ItemSpacing, new ImVec2(0.0f, 0.0f));
|
||||
ImGui.pushStyleColor(ImGuiCol.Border, new ImVec4(0.0f, 0.0f, 0.0f, 1.0f));
|
||||
for (int y = 0; y < currentSudoku.getSize(); y++) {
|
||||
for (int x = 0; x < currentSudoku.getSize(); x++) {
|
||||
if (x > 0)
|
||||
ImGui.sameLine();
|
||||
int index = y * sudoku.getSize() + x;
|
||||
Cell cell = sudoku.getCell(x, y);
|
||||
int index = y * currentSudoku.getSize() + x;
|
||||
Cell cell = currentSudoku.getCell(x, y);
|
||||
int symbol = cell.getSymbolIndex();
|
||||
Color blockColor = colorPalette.get(cell.getBlock());
|
||||
ImGui.pushStyleVar(ImGuiStyleVar.SelectableTextAlign, new ImVec2(0.5f, 0.5f));
|
||||
ImGui.pushStyleColor(ImGuiCol.Header, new ImVec4(blockColor.r, blockColor.g, blockColor.b, 1.0f));
|
||||
ImGui.pushStyleColor(ImGuiCol.Button, new ImVec4(blockColor.r, blockColor.g, blockColor.b, 1.0f));
|
||||
String cellText = "";
|
||||
if (symbol != -1)
|
||||
cellText += Integer.toString(symbol + 1);
|
||||
if (ImGui.selectable(cellText + "##" + index, true, 0, new ImVec2(50, 50))) {
|
||||
if (ImGui.button(cellText + "##" + index, new ImVec2(50, 50))) {
|
||||
ImGui.openPopup("editPopup");
|
||||
currentIndex = index;
|
||||
}
|
||||
ImGui.popStyleVar();
|
||||
ImGui.popStyleColor();
|
||||
}
|
||||
}
|
||||
ImGui.popStyleColor();
|
||||
ImGui.popStyleVar(2);
|
||||
renderPopup();
|
||||
ImGui.end();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,8 +12,16 @@ public abstract class BaseView {
|
||||
|
||||
public abstract void render();
|
||||
|
||||
public void cleanResources() {}
|
||||
|
||||
public void closeMenu(int count) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
this.stateMachine.popState();
|
||||
}
|
||||
}
|
||||
|
||||
public void closeMenu() {
|
||||
this.stateMachine.popState();
|
||||
closeMenu(1);
|
||||
}
|
||||
|
||||
protected void renderReturnButton() {
|
||||
|
||||
105
app/src/main/java/gui/menu/ConnexionStatusView.java
Normal file
105
app/src/main/java/gui/menu/ConnexionStatusView.java
Normal file
@@ -0,0 +1,105 @@
|
||||
package gui.menu;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import imgui.ImGui;
|
||||
import network.client.Client;
|
||||
import network.server.Server;
|
||||
|
||||
public class ConnexionStatusView extends BaseView {
|
||||
|
||||
private Client client;
|
||||
private Server server;
|
||||
|
||||
private String displayText = "Connecting ...";
|
||||
|
||||
public ConnexionStatusView(StateMachine stateMachine, String address, short port)
|
||||
throws UnknownHostException, IOException {
|
||||
super(stateMachine);
|
||||
Thread t = new Thread(() -> {
|
||||
try {
|
||||
this.client = new Client(address, port);
|
||||
bindListeners();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
onDisconnect();
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
|
||||
public ConnexionStatusView(StateMachine stateMachine, short port) throws UnknownHostException, IOException {
|
||||
super(stateMachine);
|
||||
Thread t = new Thread(() -> {
|
||||
try {
|
||||
this.server = new Server(port);
|
||||
this.client = new Client("localhost", port);
|
||||
bindListeners();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
onDisconnect();
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
|
||||
private void bindListeners() {
|
||||
this.client.onConnect.connect(this::onConnect);
|
||||
this.client.onClosed.connect(this::onLeave);
|
||||
this.client.onDisconnect.connect(this::onDisconnect);
|
||||
}
|
||||
|
||||
private ConnexionStatusView(StateMachine stateMachine, Server server, Client client) {
|
||||
super(stateMachine);
|
||||
this.client = client;
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public void onConnect() {
|
||||
this.stateMachine.pushState(new MultiPlayerView(stateMachine, client, server));
|
||||
}
|
||||
|
||||
public void onDisconnect() {
|
||||
if (client != null) {
|
||||
String reason = client.getDisconnectReason();
|
||||
if (reason == null)
|
||||
displayText = "Le serveur a fermé la connexion !";
|
||||
else
|
||||
displayText = "Vous avez été déconnecté ! Raison : " + client.getDisconnectReason();
|
||||
} else {
|
||||
displayText = "La connexion a échoué !";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void onLeave() {
|
||||
this.client = null;
|
||||
// on passe le menu de la connexion
|
||||
this.closeMenu();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
ImGui.text(displayText);
|
||||
renderReturnButton();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeMenu() {
|
||||
super.closeMenu();
|
||||
cleanResources();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanResources() {
|
||||
// System.out.println("Bye bye !");
|
||||
if (this.server != null) {
|
||||
this.server.stop();
|
||||
}
|
||||
if (this.client != null) {
|
||||
this.client.stop();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package gui.menu;
|
||||
|
||||
import imgui.ImGui;
|
||||
|
||||
public class CreateGameMenu extends BaseView {
|
||||
|
||||
public CreateGameMenu(StateMachine stateMachine) {
|
||||
super(stateMachine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
ImGui.text("Créer");
|
||||
renderReturnButton();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package gui.menu;
|
||||
|
||||
import imgui.ImGui;
|
||||
|
||||
public class JoinGameMenu extends BaseView {
|
||||
|
||||
public JoinGameMenu(StateMachine stateMachine) {
|
||||
super(stateMachine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
ImGui.text("Rejoindre");
|
||||
renderReturnButton();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package gui.menu;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec2;
|
||||
import imgui.type.ImInt;
|
||||
@@ -12,6 +14,7 @@ public class MultiMenu extends BaseView {
|
||||
|
||||
public MultiMenu(StateMachine stateMachine) {
|
||||
super(stateMachine);
|
||||
address.resize(20);
|
||||
}
|
||||
|
||||
private void renderCreate() {
|
||||
@@ -19,7 +22,11 @@ public class MultiMenu extends BaseView {
|
||||
ImGui.beginChild("##CreateGame", new ImVec2(displaySize.x / 2.0f, displaySize.y * 8.0f / 9.0f));
|
||||
ImGui.inputInt("Port", port);
|
||||
if (ImGui.button("Créer")) {
|
||||
// TODO: create game
|
||||
try {
|
||||
this.stateMachine.pushState(new ConnexionStatusView(stateMachine, (short) port.get()));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
ImGui.endChild();
|
||||
}
|
||||
@@ -30,7 +37,12 @@ public class MultiMenu extends BaseView {
|
||||
ImGui.inputText("Adresse", address);
|
||||
ImGui.inputInt("Port", port);
|
||||
if (ImGui.button("Rejoindre")) {
|
||||
// TODO: join game
|
||||
try {
|
||||
this.stateMachine.pushState(new ConnexionStatusView(stateMachine, address.get(), (short) port.get()));
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
ImGui.endChild();
|
||||
}
|
||||
|
||||
37
app/src/main/java/gui/menu/MultiPlayerDokuView.java
Normal file
37
app/src/main/java/gui/menu/MultiPlayerDokuView.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package gui.menu;
|
||||
|
||||
import gui.SudokuRenderer;
|
||||
import imgui.ImGui;
|
||||
import network.client.Client;
|
||||
import network.server.Server;
|
||||
|
||||
public class MultiPlayerDokuView extends BaseView{
|
||||
|
||||
private final Client client;
|
||||
private final Server server;
|
||||
private final SudokuRenderer sudokuRenderer;
|
||||
|
||||
public MultiPlayerDokuView(StateMachine stateMachine, Client client, Server server) {
|
||||
super(stateMachine);
|
||||
this.client = client;
|
||||
this.server = server;
|
||||
this.sudokuRenderer = new SudokuRenderer(this.client.getGame().getDoku());
|
||||
this.client.onDisconnect.connect(this::onDisconnect);
|
||||
}
|
||||
|
||||
public void onDisconnect() {
|
||||
if (server == null) {
|
||||
closeMenu();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
this.sudokuRenderer.render();
|
||||
if (ImGui.button("Quitter")) {
|
||||
this.client.stop();
|
||||
this.closeMenu(3);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
56
app/src/main/java/gui/menu/MultiPlayerView.java
Normal file
56
app/src/main/java/gui/menu/MultiPlayerView.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package gui.menu;
|
||||
|
||||
import game.Player;
|
||||
import imgui.ImGui;
|
||||
import network.client.Client;
|
||||
import network.server.Server;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
public class MultiPlayerView extends BaseView {
|
||||
|
||||
private final Client client;
|
||||
private final Server server;
|
||||
|
||||
public MultiPlayerView(StateMachine stateMachine, Client client, Server server) {
|
||||
super(stateMachine);
|
||||
this.client = client;
|
||||
this.server = server;
|
||||
this.client.onDisconnect.connect(this::onDisconnect);
|
||||
this.client.onGameStarted.connect(() -> this.stateMachine.pushState(new MultiPlayerDokuView(stateMachine, client, server)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeMenu() {
|
||||
this.client.forceDisconnect();
|
||||
super.closeMenu();
|
||||
}
|
||||
|
||||
public void onDisconnect() {
|
||||
this.stateMachine.popState();
|
||||
}
|
||||
|
||||
public void renderGameStatus() {
|
||||
if (this.server == null) {
|
||||
ImGui.text("En attente de l'administrateur du serveur ...");
|
||||
} else {
|
||||
if (ImGui.button("Démarrer")) {
|
||||
// temp
|
||||
MultiDoku doku = SudokuFactory.createBasicEmptySquareSudoku(5);
|
||||
this.server.startGame(doku);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
ImGui.text("Joueurs :");
|
||||
{
|
||||
for (Player player : this.client.getGame().getPlayers().values()) {
|
||||
ImGui.bulletText(player.getPseudo());
|
||||
}
|
||||
}
|
||||
renderGameStatus();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,11 +15,19 @@ public class StateMachine {
|
||||
this.menus = new Stack<>();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
for (BaseView view : menus) {
|
||||
view.cleanResources();
|
||||
}
|
||||
menus.clear();
|
||||
}
|
||||
|
||||
public void pushState(BaseView menu) {
|
||||
menus.add(menu);
|
||||
}
|
||||
|
||||
public void popState() {
|
||||
menus.getLast().cleanResources();
|
||||
menus.pop();
|
||||
}
|
||||
|
||||
@@ -35,8 +43,9 @@ public class StateMachine {
|
||||
ImGui.setNextWindowSize(displaySize);
|
||||
ImGui.begin("##Main Window", null, ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoMove
|
||||
| ImGuiWindowFlags.NoSavedSettings | ImGuiWindowFlags.NoBackground);
|
||||
menus.getLast().render();
|
||||
menus.get(menus.size() - 1).render();
|
||||
ImGui.end();
|
||||
// ImGui.showDemoWindow();
|
||||
checkEscape();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user