feat: uggly leaderboard
All checks were successful
Linux arm64 / Build (push) Successful in 27s

This commit is contained in:
2025-01-31 13:48:51 +01:00
parent 25c2270a37
commit a160042ef4
12 changed files with 61 additions and 14 deletions

View File

@@ -0,0 +1,25 @@
package gui.widget;
import game.Game;
import game.Player;
import imgui.ImGui;
public class LeaderboardRenderer {
private final Game game;
private final Player currentPlayer;
public LeaderboardRenderer(Game game, Player player) {
this.game = game;
this.currentPlayer = player;
}
public void render() {
ImGui.text("Leaderboard");
for (var entry : game.getPlayers().entrySet()) {
Player player = entry.getValue();
ImGui.text(player.getPseudo() + " : " + player.getScore());
}
}
}

View File

@@ -0,0 +1,155 @@
package gui.widget;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import common.ConsumerSignal;
import common.Signal;
import gui.ColorGenerator;
import gui.Fonts;
import gui.Options;
import gui.RenderableMultidoku;
import gui.Symbols;
import gui.ColorGenerator.Color;
import imgui.ImGui;
import imgui.ImVec2;
import imgui.ImVec4;
import imgui.flag.ImGuiCol;
import imgui.flag.ImGuiStyleVar;
import sudoku.constraint.Constraint;
import sudoku.structure.Block;
import sudoku.structure.Cell;
import sudoku.structure.MultiDoku;
import sudoku.structure.Sudoku;
public class SudokuRenderer {
private final RenderableMultidoku doku;
private Cell currentCell = null;
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 ImVec4 WHITE = new ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
private static final ImVec2 cellSize = new ImVec2(50, 50);
private final Set<Cell> diagonals = new HashSet<>();
public final Signal onResolve = new Signal();
public final ConsumerSignal<Cell> onCellChange = new ConsumerSignal<>();
public SudokuRenderer(MultiDoku doku) {
this.doku = RenderableMultidoku.fromMultidoku(doku);
this.colorPalette = initColors();
initDiagonals();
}
private void initDiagonals() {
for (Sudoku sudoku : this.doku.getDoku().getSubGrids()) {
if (sudoku.hasConstraint(Constraint.Diagonal)) {
for (int i = 0; i < sudoku.getSize(); i++) {
this.diagonals.add(sudoku.getCell(i, i));
this.diagonals.add(sudoku.getCell(sudoku.getSize() - i - 1, i));
}
}
}
}
private Map<Block, Color> initColors() {
List<Color> colors = ColorGenerator.greatPalette(doku.getBlocks().size());
Map<Block, Color> colorPalette = new HashMap<>();
int index = 0;
for (Block block : doku.getBlocks()) {
colorPalette.put(block, colors.get(index));
index++;
}
return colorPalette;
}
private void renderPopup() {
if (ImGui.beginPopup("editPopup")) {
Block block = currentCell.getBlock();
int symbolCount = block.getCells().size();
for (int i = 0; i < symbolCount; i++) {
if ((i + 1) % (int) (Math.sqrt(symbolCount)) != 1)
ImGui.sameLine();
if (currentCell.getSymbolIndex() == i) {
if (ImGui.button("X", cellSize)) {
currentCell.setSymbolIndex(Cell.NOSYMBOL);
this.onCellChange.emit(currentCell);
ImGui.closeCurrentPopup();
}
} else {
if (ImGui.button(Options.Symboles.getSymbols().get(i), cellSize)) {
if (currentCell.trySetValue(i))
this.onCellChange.emit(currentCell);
if (this.doku.getDoku().isSolved())
this.onResolve.emit();
ImGui.closeCurrentPopup();
}
}
}
ImGui.endPopup();
}
}
public void render() {
if (Options.Symboles == Symbols.Russian) {
ImGui.pushFont(Fonts.ARIAL);
}
final float sudokuViewWidth = cellSize.x * doku.getWidth();
final float displayWidth = ImGui.getIO().getDisplaySizeX();
float offsetX = displayWidth / 2.0f - sudokuViewWidth / 2.0f;
// if the grid is too big, don't offset it
if (offsetX > 0) {
ImGui.setCursorPosX(offsetX);
}
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));
for (int y = 0; y < doku.getHeight(); y++) {
for (int x = 0; x < doku.getWidth(); x++) {
if (x > 0)
ImGui.sameLine();
int index = y * doku.getWidth() + x;
Cell cell = doku.getCell(x, y);
if (cell == null) {
ImGui.pushStyleColor(ImGuiCol.Border, TRANSPARENT);
ImGui.pushStyleColor(ImGuiCol.Button, TRANSPARENT);
ImGui.button("##" + index, cellSize);
} else {
if (diagonals.contains(cell)) {
ImGui.pushStyleColor(ImGuiCol.Border, WHITE);
} else {
ImGui.pushStyleColor(ImGuiCol.Border, BLACK);
}
int symbol = cell.getSymbolIndex();
Color blockColor = colorPalette.get(cell.getBlock());
if (!cell.isMutable()) {
blockColor = new Color(blockColor.r - 0.20f, blockColor.g - 0.20f, blockColor.b - 0.20f);
}
ImGui.pushStyleColor(ImGuiCol.Button, new ImVec4(blockColor.r, blockColor.g, blockColor.b, 1.0f));
String cellText = "";
if (symbol != -1)
cellText += Options.Symboles.getSymbols().get(symbol);
if (ImGui.button(cellText + "##" + index, cellSize) && cell.isMutable()) {
ImGui.openPopup("editPopup");
currentCell = cell;
}
}
ImGui.popStyleColor(2);
}
}
ImGui.popStyleVar(2);
renderPopup();
ImGui.endChild();
if (Options.Symboles == Symbols.Russian) {
ImGui.popFont();
}
}
}

View File

@@ -0,0 +1,133 @@
package gui.widget;
import java.util.ArrayList;
import java.util.List;
import common.ConsumerSignal;
import gui.SudokuType;
import imgui.ImGui;
import imgui.extension.imguifiledialog.ImGuiFileDialog;
import imgui.extension.imguifiledialog.flag.ImGuiFileDialogFlags;
import imgui.type.ImBoolean;
import imgui.type.ImInt;
import sudoku.constraint.Constraint;
import sudoku.structure.Difficulty;
import sudoku.structure.MultiDoku;
import sudoku.structure.SudokuFactory;
public class SudokuSelector {
public final ConsumerSignal<MultiDoku> onSelect = new ConsumerSignal<>();
private MultiDoku doku;
private final boolean canGenEmptyGrid;
private final ImInt sudokuType = new ImInt(0);
private final ImInt difficulty = new ImInt(Difficulty.Medium.ordinal());
private final List<ImBoolean> contraints = new ArrayList<>();
private final ImInt sudokuSize = new ImInt(3);
private final ImInt sudokuWidth = new ImInt(3);
private final ImInt sudokuHeight = new ImInt(3);
private final String confirmMessage;
public SudokuSelector(boolean canGenEmptyGrid, String confirmMessage) {
this.canGenEmptyGrid = canGenEmptyGrid;
this.confirmMessage = confirmMessage;
initConstraints();
}
private List<Constraint> getConstraints() {
List<Constraint> constraints = new ArrayList<>();
for (int i = 0; i < this.contraints.size(); i++) {
if (this.contraints.get(i).get())
constraints.add(Constraint.values()[i]);
}
return constraints;
}
private void initConstraints() {
for (Constraint cons : Constraint.values()) {
contraints.add(new ImBoolean(SudokuFactory.DEFAULT_CONSTRAINTS.contains(cons)));
}
}
private void selectSudoku(MultiDoku doku, boolean empty) {
this.doku = doku;
if (!empty) {
try {
SudokuFactory.fillDoku(doku, Difficulty.values()[difficulty.get()]);
} catch (Exception e) {
e.printStackTrace();
}
}
this.onSelect.emit(this.doku);
}
public void renderFileDialog() {
if (ImGuiFileDialog.display("browse-sudoku", ImGuiFileDialogFlags.None)) {
if (ImGuiFileDialog.isOk()) {
var selection = ImGuiFileDialog.getSelection();
for (var entry : selection.entrySet()) {
try {
String filePath = entry.getValue();
this.doku = SudokuFactory.fromfile(filePath);
if (this.doku != null)
this.onSelect.emit(this.doku);
} catch (Exception e) {
e.printStackTrace();
}
}
}
ImGuiFileDialog.close();
}
}
public void render() {
ImGui.combo("Difficulté", difficulty, Difficulty.getDifficultyNames());
if (ImGui.treeNode("Constraintes")) {
for (Constraint cons : Constraint.values()) {
ImGui.checkbox(cons.getDisplayName(), contraints.get(cons.ordinal()));
}
ImGui.treePop();
}
ImGui.combo("Type de Sudoku", sudokuType, SudokuType.getTypeNames());
SudokuType currentType = SudokuType.values()[sudokuType.get()];
switch (currentType.getMakerParamCount()) {
case 1:
ImGui.inputInt("Taille", sudokuSize);
if (ImGui.button(confirmMessage)) {
selectSudoku(currentType.createDoku(getConstraints(), sudokuSize.get()), false);
}
if (canGenEmptyGrid && ImGui.button("Générer une grille vide")) {
selectSudoku(currentType.createDoku(getConstraints(), sudokuSize.get()), true);
}
break;
case 2:
ImGui.inputInt("Largeur", sudokuHeight);
ImGui.inputInt("Longueur", sudokuWidth);
if (ImGui.button(confirmMessage)) {
selectSudoku(currentType.createDoku(getConstraints(), sudokuWidth.get(), sudokuHeight.get()),
false);
}
if (canGenEmptyGrid && ImGui.button("Générer une grille vide")) {
selectSudoku(currentType.createDoku(getConstraints(), sudokuWidth.get(), sudokuHeight.get()), true);
}
break;
default:
assert (false);
break;
}
if (ImGui.button("À partir d'un fichier")) {
ImGuiFileDialog.openDialog("browse-sudoku", "Choisissez un fichier", ".json", ".");
}
renderFileDialog();
}
}