88 lines
2.4 KiB
Java
88 lines
2.4 KiB
Java
package gui.menu;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import gui.ColorGenerator;
|
|
import gui.ColorGenerator.Color;
|
|
import imgui.ImGui;
|
|
import imgui.ImVec2;
|
|
import imgui.ImVec4;
|
|
import imgui.flag.ImGuiCol;
|
|
import imgui.flag.ImGuiStyleVar;
|
|
import sudoku.Block;
|
|
import sudoku.Cell;
|
|
import sudoku.MultiDoku;
|
|
import sudoku.Sudoku;
|
|
import sudoku.SudokuFactory;
|
|
|
|
public class SudokuView extends BaseView {
|
|
|
|
private final Sudoku sudoku;
|
|
private int currentIndex = -1;
|
|
private final Map<Block, Color> colorPalette;
|
|
|
|
public SudokuView(StateMachine stateMachine, int width, int height) {
|
|
super(stateMachine);
|
|
MultiDoku doku = SudokuFactory.createBasicEmptyRectangleSudoku(width, height);
|
|
this.sudoku = doku.getSubGrid(0);
|
|
this.colorPalette = new HashMap<>();
|
|
initColors();
|
|
}
|
|
|
|
private void initColors() {
|
|
List<Color> 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();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void render() {
|
|
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.pushStyleVar(ImGuiStyleVar.SelectableTextAlign, new ImVec2(0.5f, 0.5f));
|
|
ImGui.pushStyleColor(ImGuiCol.Header, 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))) {
|
|
ImGui.openPopup("editPopup");
|
|
currentIndex = index;
|
|
}
|
|
ImGui.popStyleVar();
|
|
ImGui.popStyleColor();
|
|
}
|
|
}
|
|
renderPopup();
|
|
if (ImGui.button("Résoudre")) {
|
|
// TODO: solve
|
|
}
|
|
renderReturnButton();
|
|
}
|
|
|
|
}
|