Skip to content

Commit

Permalink
refactor: begin adding empty cli modules
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoDog896 committed Jun 6, 2024
1 parent 42fed08 commit 14b7ca1
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 26 deletions.
Empty file added crates/games/src/chomp/gui.rs
Empty file.
2 changes: 2 additions & 0 deletions crates/games/src/chomp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![doc = include_str!("./README.md")]

pub mod cli;
#[cfg(feature = "egui")]
pub mod gui;

use array2d::Array2D;
use game_solver::game::{Game, ZeroSumPlayer};
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions crates/games/src/domineering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

pub mod cli;

#[cfg(feature = "egui")]
pub mod gui;

use array2d::Array2D;
use game_solver::game::{Game, ZeroSumPlayer};

Expand Down
56 changes: 30 additions & 26 deletions crates/games/src/nim/gui.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
use egui::{emath, Color32, Pos2, Rect, Rounding, Sense, Ui, Vec2};

use crate::util::gui::egui_display::EguiDisplay;

use super::Nim;

const CELL_HEIGHT: usize = 10;
const CELL_WIDTH: usize = 5;
const CELL_MARGIN_Y: usize = 5;
const CELL_MARGIN_X: usize = 3;

pub fn display(game: &Nim, ui: &mut Ui) {
if let Some(max_cell_count) = game.heaps.iter().max() {
let max_size = Vec2 {
x: (CELL_WIDTH * max_cell_count + CELL_MARGIN_X * (max_cell_count - 1)) as f32,
y: (CELL_HEIGHT * game.heaps.len() + CELL_MARGIN_Y * (game.heaps.len() - 1)) as f32
};
impl EguiDisplay for Nim {
fn display(&self, ui: &mut Ui) {
if let Some(max_cell_count) = self.heaps.iter().max() {
let max_size = Vec2 {
x: (CELL_WIDTH * max_cell_count + CELL_MARGIN_X * (max_cell_count - 1)) as f32,
y: (CELL_HEIGHT * self.heaps.len() + CELL_MARGIN_Y * (self.heaps.len() - 1)) as f32
};

let (response, painter) =
ui.allocate_painter(max_size, Sense::drag());
let (response, painter) =
ui.allocate_painter(max_size, Sense::drag());

let to_screen = emath::RectTransform::from_to(
Rect::from_min_size(Pos2::ZERO, max_size),
response.rect,
);
let to_screen = emath::RectTransform::from_to(
Rect::from_min_size(Pos2::ZERO, max_size),
response.rect,
);

painter.extend(game.heaps.iter().enumerate().map(|(i, heap)| {
(0..(*heap)).map(|j| {
egui::Shape::rect_filled(Rect::from_two_pos(
to_screen * Pos2 {
x: (j * CELL_WIDTH + (j * CELL_MARGIN_X)) as f32,
y: (i * CELL_HEIGHT + (i * CELL_MARGIN_Y)) as f32
},
to_screen * Pos2 {
x: (j * CELL_WIDTH + (j * CELL_MARGIN_X) + CELL_WIDTH) as f32,
y: (i * CELL_HEIGHT + (i * CELL_MARGIN_Y) + CELL_HEIGHT) as f32
}
), Rounding::ZERO, Color32::LIGHT_GRAY)
}).collect::<Vec<_>>()
}).flatten().collect::<Vec<_>>());
painter.extend(self.heaps.iter().enumerate().map(|(i, heap)| {
(0..(*heap)).map(|j| {
egui::Shape::rect_filled(Rect::from_two_pos(
to_screen * Pos2 {
x: (j * CELL_WIDTH + (j * CELL_MARGIN_X)) as f32,
y: (i * CELL_HEIGHT + (i * CELL_MARGIN_Y)) as f32
},
to_screen * Pos2 {
x: (j * CELL_WIDTH + (j * CELL_MARGIN_X) + CELL_WIDTH) as f32,
y: (i * CELL_HEIGHT + (i * CELL_MARGIN_Y) + CELL_HEIGHT) as f32
}
), Rounding::ZERO, Color32::LIGHT_GRAY)
}).collect::<Vec<_>>()
}).flatten().collect::<Vec<_>>());
}
}
}
1 change: 1 addition & 0 deletions crates/games/src/nim/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![doc = include_str!("./README.md")]

pub mod cli;
#[cfg(feature = "egui")]
pub mod gui;

use game_solver::game::{Game, ZeroSumPlayer};
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions crates/games/src/order_and_chaos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

pub mod cli;

#[cfg(feature = "egui")]
pub mod gui;

use array2d::Array2D;
use game_solver::game::{Game, ZeroSumPlayer};
use std::{
Expand Down
Empty file added crates/games/src/reversi/gui.rs
Empty file.
3 changes: 3 additions & 0 deletions crates/games/src/reversi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

pub mod cli;

#[cfg(feature = "egui")]
pub mod gui;

use array2d::Array2D;
use game_solver::game::{Game, ZeroSumPlayer};
use std::hash::Hash;
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions crates/games/src/tic_tac_toe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

pub mod cli;

#[cfg(feature = "egui")]
pub mod gui;

use game_solver::game::{Game, ZeroSumPlayer};
use itertools::Itertools;
use ndarray::{iter::IndexedIter, ArrayD, Dim, Dimension, IntoDimension, IxDyn, IxDynImpl};
Expand Down
6 changes: 6 additions & 0 deletions crates/games/src/util/gui/egui_display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use egui::Ui;

pub trait EguiDisplay {
/// Renders
fn display(&self, ui: &mut Ui);
}
1 change: 1 addition & 0 deletions crates/games/src/util/gui/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod egui_display;
2 changes: 2 additions & 0 deletions crates/games/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod cli;
#[cfg(feature = "egui")]
pub mod gui;
pub mod move_natural;
pub mod state;

0 comments on commit 14b7ca1

Please sign in to comment.