From 6c8bfad18d029bdefaf65c9a7d28ca7b9767022f Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Thu, 23 Jan 2025 10:33:59 +0100 Subject: [PATCH] add SudokuRenderer --- app/src/main/java/gui/Main.java | 52 ++-------------- app/src/main/java/gui/SudokuRenderer.java | 73 +++++++++++++++++++++++ 2 files changed, 77 insertions(+), 48 deletions(-) create mode 100644 app/src/main/java/gui/SudokuRenderer.java diff --git a/app/src/main/java/gui/Main.java b/app/src/main/java/gui/Main.java index 76f59d7..9573aa1 100644 --- a/app/src/main/java/gui/Main.java +++ b/app/src/main/java/gui/Main.java @@ -22,69 +22,25 @@ import sudoku.SudokuFactory; public class Main extends Application { // temp thing - private Sudoku sudoku; - private int currentIndex = -1; - private Map colorPalette = new HashMap<>(); - private static final int SUDOKU_SIZE = 5; + private static final int SUDOKU_SIZE = 4; + private SudokuRenderer renderer; @Override protected void configure(Configuration config) { config.setTitle("Let's play sudoku!"); } - private void initColors() { - List colors = ColorGenerator.greatPalette(sudoku.getSize()); - int index = 0; - for (Block block : sudoku.getBlocks()) { - colorPalette.put(block, colors.get(index)); - index++; - } - } - @Override protected void initImGui(Configuration config) { super.initImGui(config); ImGui.getIO().getFonts().addFontFromFileTTF("comic.ttf", 50.0f); MultiDoku doku = SudokuFactory.createBasicEmptySquareSudoku(SUDOKU_SIZE); - sudoku = doku.getSubGrid(0); - initColors(); - } - - private void renderPopup() { - if (ImGui.beginPopup("editPopup")) { - for (int i = 1; i < sudoku.getSize() + 1; i++) { - if(i % (int) (Math.sqrt(sudoku.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); - ImGui.closeCurrentPopup(); - } - } - ImGui.endPopup(); - } + renderer = new SudokuRenderer(doku.getSubGrid(0)); } @Override public void process() { - ImGui.begin("Sudoku Window"); - for (int y = 0; y < sudoku.getSize(); y++) { - for (int x = 0; x < sudoku.getSize(); x++) { - if (x > 0) - ImGui.sameLine(); - int index = y * sudoku.getSize() + x; - Cell cell = sudoku.getCell(x, y); - int symbol = cell.getSymbolIndex(); - Color blockColor = colorPalette.get(cell.getBlock()); - ImGui.pushStyleColor(ImGuiCol.Header, new ImVec4(blockColor.r, blockColor.g, blockColor.b, 1.0f)); - if (ImGui.selectable(Integer.toString(symbol + 1) + "##" + index, true, 0, new ImVec2(50, 50))) { - ImGui.openPopup("editPopup"); - currentIndex = index; - } - ImGui.popStyleColor(); - } - } - renderPopup(); - ImGui.end(); + renderer.render(); ImGui.showDemoWindow(); } diff --git a/app/src/main/java/gui/SudokuRenderer.java b/app/src/main/java/gui/SudokuRenderer.java new file mode 100644 index 0000000..dbf5a54 --- /dev/null +++ b/app/src/main/java/gui/SudokuRenderer.java @@ -0,0 +1,73 @@ +package gui; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import gui.ColorGenerator.Color; +import imgui.ImGui; +import imgui.ImVec2; +import imgui.ImVec4; +import imgui.flag.ImGuiCol; +import sudoku.Block; +import sudoku.Cell; +import sudoku.Sudoku; + +public class SudokuRenderer { + + private final Sudoku sudoku; + private int currentIndex = -1; + private final Map colorPalette; + + public SudokuRenderer(Sudoku sudoku) { + this.sudoku = sudoku; + this.colorPalette = new HashMap<>(); + initColors(); + } + + private void initColors() { + List colors = ColorGenerator.greatPalette(sudoku.getSize()); + int index = 0; + for (Block block : sudoku.getBlocks()) { + colorPalette.put(block, colors.get(index)); + index++; + } + } + + private void renderPopup() { + if (ImGui.beginPopup("editPopup")) { + for (int i = 1; i < sudoku.getSize() + 1; i++) { + if (i % (int) (Math.sqrt(sudoku.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); + ImGui.closeCurrentPopup(); + } + } + ImGui.endPopup(); + } + } + + public void render() { + ImGui.begin("Sudoku Window"); + for (int y = 0; y < sudoku.getSize(); y++) { + for (int x = 0; x < sudoku.getSize(); x++) { + if (x > 0) + ImGui.sameLine(); + int index = y * sudoku.getSize() + x; + Cell cell = sudoku.getCell(x, y); + int symbol = cell.getSymbolIndex(); + Color blockColor = colorPalette.get(cell.getBlock()); + ImGui.pushStyleColor(ImGuiCol.Header, new ImVec4(blockColor.r, blockColor.g, blockColor.b, 1.0f)); + if (ImGui.selectable(Integer.toString(symbol + 1) + "##" + index, true, 0, new ImVec2(50, 50))) { + ImGui.openPopup("editPopup"); + currentIndex = index; + } + ImGui.popStyleColor(); + } + } + renderPopup(); + ImGui.end(); + } + +}