From ec36e19c4e93b1afe9018997862f97ba74351ee2 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 29 Jan 2025 11:05:15 +0100 Subject: [PATCH] Fixes #2 --- app/src/main/java/gui/SudokuRenderer.java | 29 ++++++++++------ app/src/main/java/gui/menu/SudokuView.java | 39 ++++++++++------------ 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/gui/SudokuRenderer.java b/app/src/main/java/gui/SudokuRenderer.java index 6e2a73d..604d38d 100644 --- a/app/src/main/java/gui/SudokuRenderer.java +++ b/app/src/main/java/gui/SudokuRenderer.java @@ -20,6 +20,10 @@ public class SudokuRenderer { private Cell currentCell = null; private final Map colorPalette; + private static final ImVec4 BLACK = new ImVec4(0, 0, 0, 1); + private static final ImVec4 TRANSPARENT = new ImVec4(); + private static final ImVec2 cellSize = new ImVec2(50, 50); + public SudokuRenderer(MultiDoku doku) { this.doku = RenderableMultidoku.fromMultidoku(doku); this.colorPalette = initColors(); @@ -37,7 +41,6 @@ public class SudokuRenderer { } private void renderPopup() { - final ImVec2 buttonSize = new ImVec2(50, 50); if (ImGui.beginPopup("editPopup")) { Block block = currentCell.getBlock(); int symbolCount = block.getCells().size(); @@ -45,12 +48,12 @@ public class SudokuRenderer { if ((i + 1) % (int) (Math.sqrt(symbolCount)) != 1) ImGui.sameLine(); if (currentCell.getSymbolIndex() == i) { - if (ImGui.button("X", buttonSize)) { + if (ImGui.button("X", cellSize)) { currentCell.setSymbolIndex(Cell.NOSYMBOL); ImGui.closeCurrentPopup(); } - }else { - if (ImGui.button(Integer.toString(i + 1), buttonSize)) { + } else { + if (ImGui.button(Integer.toString(i + 1), cellSize)) { this.doku.setCellValue(currentCell, i); ImGui.closeCurrentPopup(); } @@ -61,9 +64,13 @@ public class SudokuRenderer { } public void render() { + final float sudokuViewWidth = cellSize.x * doku.getWidth(); + final float displayWidth = ImGui.getIO().getDisplaySizeX(); + ImGui.setCursorPosX(displayWidth / 2.0f - sudokuViewWidth / 2.0f); + ImGui.beginChild(1, new ImVec2(cellSize.x * doku.getWidth(), cellSize.y * doku.getHeight())); + 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 < doku.getHeight(); y++) { for (int x = 0; x < doku.getWidth(); x++) { if (x > 0) @@ -71,9 +78,11 @@ public class SudokuRenderer { int index = y * doku.getWidth() + x; Cell cell = doku.getCell(x, y); if (cell == null) { - ImGui.pushStyleColor(ImGuiCol.Button, new ImVec4(0.0f, 0.0f, 0.0f, 1.0f)); - ImGui.button("##" + index, new ImVec2(50, 50)); + ImGui.pushStyleColor(ImGuiCol.Border, TRANSPARENT); + ImGui.pushStyleColor(ImGuiCol.Button, TRANSPARENT); + ImGui.button("##" + index, cellSize); } else { + ImGui.pushStyleColor(ImGuiCol.Border, BLACK); int symbol = cell.getSymbolIndex(); Color blockColor = colorPalette.get(cell.getBlock()); if (!cell.isMutable()) { @@ -87,7 +96,7 @@ public class SudokuRenderer { String cellText = ""; if (symbol != -1) cellText += Integer.toString(symbol + 1); - if (ImGui.button(cellText + "##" + index, new ImVec2(50, 50))) { + if (ImGui.button(cellText + "##" + index, cellSize)) { ImGui.openPopup("editPopup"); currentCell = cell; } @@ -96,12 +105,12 @@ public class SudokuRenderer { } // ImGui.popFont(); } - ImGui.popStyleColor(); + ImGui.popStyleColor(2); } } - ImGui.popStyleColor(); ImGui.popStyleVar(2); renderPopup(); + ImGui.endChild(); } } diff --git a/app/src/main/java/gui/menu/SudokuView.java b/app/src/main/java/gui/menu/SudokuView.java index 00f2bf1..f0b18d1 100644 --- a/app/src/main/java/gui/menu/SudokuView.java +++ b/app/src/main/java/gui/menu/SudokuView.java @@ -5,6 +5,7 @@ import java.util.concurrent.CancellationException; import gui.SudokuRenderer; import imgui.ImGui; +import imgui.ImGuiStyle; import sudoku.solver.Solver; import sudoku.structure.MultiDoku; @@ -18,25 +19,6 @@ public class SudokuView extends BaseView { super(stateMachine); this.doku = doku; this.sudokuRenderer = new SudokuRenderer(doku); - // int level = 0; - // for (Sudoku sudoku : this.doku.getSubGrids()) { - // level += sudoku.getSize() * sudoku.getSize() / 10; - // } - // try { - // Solver.solve(doku); - // SudokuFactory.newDokuFromFilledOne(doku, level); - // for (Sudoku sudoku : this.doku.getSubGrids()) { - // for (Cell cell : sudoku.getCells()) { - // if (cell.getSymbolIndex() != Cell.NOSYMBOL) { - // cell.setImmutable(); - // } - // } - // } - // } catch (Exception e) { - // e.printStackTrace(); - // System.out.println("non ça n'arrivera pas"); - // //TODO: ça va arriver - // } } private void stopResolve() { @@ -46,11 +28,24 @@ public class SudokuView extends BaseView { } } + boolean centeredButton(String label) { + ImGuiStyle style = ImGui.getStyle(); + + float size = ImGui.calcTextSizeX(label) + style.getFramePaddingX() * 2.0f; + float avail = ImGui.getContentRegionAvailX(); + + float off = (avail - size) * 0.5f; + if (off > 0.0f) + ImGui.setCursorPosX(ImGui.getCursorPosX() + off); + + return ImGui.button(label); + } + private void renderCancelButton() { boolean wantsToStop = false; if (resolveThread != null && resolveThread.isAlive()) { // ImGui.endDisabled(); - if (ImGui.button("Annuler")) { + if (centeredButton("Annuler")) { // we can't stop the Thread right now wantsToStop = true; } @@ -67,8 +62,8 @@ public class SudokuView extends BaseView { ImGui.beginDisabled(); boolean beginSolve = false; - - if (ImGui.button("Résoudre")) { + + if (centeredButton("Résoudre")) { beginSolve = true; } if (resolveThread != null)