Fixes #2
All checks were successful
Linux arm64 / Build (push) Successful in 42s

This commit is contained in:
2025-01-29 11:05:15 +01:00
parent aa86e9b956
commit ec36e19c4e
2 changed files with 36 additions and 32 deletions

View File

@@ -20,6 +20,10 @@ public class SudokuRenderer {
private Cell currentCell = null; private Cell currentCell = null;
private final Map<Block, Color> colorPalette; private final Map<Block, Color> 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) { public SudokuRenderer(MultiDoku doku) {
this.doku = RenderableMultidoku.fromMultidoku(doku); this.doku = RenderableMultidoku.fromMultidoku(doku);
this.colorPalette = initColors(); this.colorPalette = initColors();
@@ -37,7 +41,6 @@ public class SudokuRenderer {
} }
private void renderPopup() { private void renderPopup() {
final ImVec2 buttonSize = new ImVec2(50, 50);
if (ImGui.beginPopup("editPopup")) { if (ImGui.beginPopup("editPopup")) {
Block block = currentCell.getBlock(); Block block = currentCell.getBlock();
int symbolCount = block.getCells().size(); int symbolCount = block.getCells().size();
@@ -45,12 +48,12 @@ public class SudokuRenderer {
if ((i + 1) % (int) (Math.sqrt(symbolCount)) != 1) if ((i + 1) % (int) (Math.sqrt(symbolCount)) != 1)
ImGui.sameLine(); ImGui.sameLine();
if (currentCell.getSymbolIndex() == i) { if (currentCell.getSymbolIndex() == i) {
if (ImGui.button("X", buttonSize)) { if (ImGui.button("X", cellSize)) {
currentCell.setSymbolIndex(Cell.NOSYMBOL); currentCell.setSymbolIndex(Cell.NOSYMBOL);
ImGui.closeCurrentPopup(); ImGui.closeCurrentPopup();
} }
}else { } else {
if (ImGui.button(Integer.toString(i + 1), buttonSize)) { if (ImGui.button(Integer.toString(i + 1), cellSize)) {
this.doku.setCellValue(currentCell, i); this.doku.setCellValue(currentCell, i);
ImGui.closeCurrentPopup(); ImGui.closeCurrentPopup();
} }
@@ -61,9 +64,13 @@ public class SudokuRenderer {
} }
public void render() { 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.FrameBorderSize, 2.0f);
ImGui.pushStyleVar(ImGuiStyleVar.ItemSpacing, new ImVec2(0.0f, 0.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 y = 0; y < doku.getHeight(); y++) {
for (int x = 0; x < doku.getWidth(); x++) { for (int x = 0; x < doku.getWidth(); x++) {
if (x > 0) if (x > 0)
@@ -71,9 +78,11 @@ public class SudokuRenderer {
int index = y * doku.getWidth() + x; int index = y * doku.getWidth() + x;
Cell cell = doku.getCell(x, y); Cell cell = doku.getCell(x, y);
if (cell == null) { if (cell == null) {
ImGui.pushStyleColor(ImGuiCol.Button, new ImVec4(0.0f, 0.0f, 0.0f, 1.0f)); ImGui.pushStyleColor(ImGuiCol.Border, TRANSPARENT);
ImGui.button("##" + index, new ImVec2(50, 50)); ImGui.pushStyleColor(ImGuiCol.Button, TRANSPARENT);
ImGui.button("##" + index, cellSize);
} else { } else {
ImGui.pushStyleColor(ImGuiCol.Border, BLACK);
int symbol = cell.getSymbolIndex(); int symbol = cell.getSymbolIndex();
Color blockColor = colorPalette.get(cell.getBlock()); Color blockColor = colorPalette.get(cell.getBlock());
if (!cell.isMutable()) { if (!cell.isMutable()) {
@@ -87,7 +96,7 @@ public class SudokuRenderer {
String cellText = ""; String cellText = "";
if (symbol != -1) if (symbol != -1)
cellText += Integer.toString(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"); ImGui.openPopup("editPopup");
currentCell = cell; currentCell = cell;
} }
@@ -96,12 +105,12 @@ public class SudokuRenderer {
} }
// ImGui.popFont(); // ImGui.popFont();
} }
ImGui.popStyleColor(); ImGui.popStyleColor(2);
} }
} }
ImGui.popStyleColor();
ImGui.popStyleVar(2); ImGui.popStyleVar(2);
renderPopup(); renderPopup();
ImGui.endChild();
} }
} }

View File

@@ -5,6 +5,7 @@ import java.util.concurrent.CancellationException;
import gui.SudokuRenderer; import gui.SudokuRenderer;
import imgui.ImGui; import imgui.ImGui;
import imgui.ImGuiStyle;
import sudoku.solver.Solver; import sudoku.solver.Solver;
import sudoku.structure.MultiDoku; import sudoku.structure.MultiDoku;
@@ -18,25 +19,6 @@ public class SudokuView extends BaseView {
super(stateMachine); super(stateMachine);
this.doku = doku; this.doku = doku;
this.sudokuRenderer = new SudokuRenderer(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() { 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() { private void renderCancelButton() {
boolean wantsToStop = false; boolean wantsToStop = false;
if (resolveThread != null && resolveThread.isAlive()) { if (resolveThread != null && resolveThread.isAlive()) {
// ImGui.endDisabled(); // ImGui.endDisabled();
if (ImGui.button("Annuler")) { if (centeredButton("Annuler")) {
// we can't stop the Thread right now // we can't stop the Thread right now
wantsToStop = true; wantsToStop = true;
} }
@@ -68,7 +63,7 @@ public class SudokuView extends BaseView {
boolean beginSolve = false; boolean beginSolve = false;
if (ImGui.button("Résoudre")) { if (centeredButton("Résoudre")) {
beginSolve = true; beginSolve = true;
} }
if (resolveThread != null) if (resolveThread != null)