Skip to content

Commit 6d5bd01

Browse files
committed
Add Fission SVG rendering
1 parent 405c582 commit 6d5bd01

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

src/drawing/svg.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Simple SVG immediate drawing utilities
2+
#![allow(missing_docs)]
23

34
use std::fmt::{self, Write};
45

@@ -61,6 +62,16 @@ impl Default for Text {
6162
}
6263
}
6364

65+
/// SVG circle element
66+
pub struct Circle {
67+
pub cx: i32,
68+
pub cy: i32,
69+
pub r: u32,
70+
pub stroke: String,
71+
pub stroke_width: u32,
72+
pub fill: String,
73+
}
74+
6475
/// Custom grid element
6576
pub struct Grid {
6677
/// Top left corner X position
@@ -147,6 +158,17 @@ impl ImmSvg {
147158
)
148159
}
149160

161+
pub fn circle<W>(w: &mut W, circle: &Circle) -> fmt::Result
162+
where
163+
W: Write,
164+
{
165+
write!(
166+
w,
167+
"<circle cx=\"{}\" cy=\"{}\" r=\"{}\" stroke=\"{}\" stroke-width=\"{}\" fill=\"{}\" />",
168+
circle.cx, circle.cy, circle.r, circle.stroke, circle.stroke_width, circle.fill,
169+
)
170+
}
171+
150172
/// Draw grid
151173
pub fn grid<W>(w: &mut W, grid: &Grid) -> fmt::Result
152174
where

src/short/partizan/games/fission.rs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
//! Fission game
22
33
use crate::{
4+
drawing::svg::{self, ImmSvg, Svg},
45
grid::{small_bit_grid::SmallBitGrid, FiniteGrid, Grid},
56
short::partizan::partizan_game::PartizanGame,
67
};
78
use cgt_derive::Tile;
8-
use std::{fmt::Display, hash::Hash, str::FromStr};
9+
use std::{
10+
fmt::{self, Display},
11+
hash::Hash,
12+
str::FromStr,
13+
};
914

1015
/// Tile in the game of Fission
1116
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Tile)]
@@ -105,6 +110,64 @@ where
105110
}
106111
}
107112

113+
impl<G> Svg for Fission<G>
114+
where
115+
G: Grid<Item = Tile> + FiniteGrid,
116+
{
117+
fn to_svg<W>(&self, buf: &mut W) -> fmt::Result
118+
where
119+
W: fmt::Write,
120+
{
121+
// Chosen arbitrarily
122+
let tile_size = 48;
123+
let grid_width = 4;
124+
125+
let offset = grid_width / 2;
126+
let svg_width = self.grid.width() as u32 * tile_size + grid_width;
127+
let svg_height = self.grid.height() as u32 * tile_size + grid_width;
128+
129+
ImmSvg::new(buf, svg_width, svg_height, |buf| {
130+
for y in 0..self.grid.height() {
131+
for x in 0..self.grid.width() {
132+
match self.grid.get(x, y) {
133+
Tile::Empty => {
134+
ImmSvg::rect(
135+
buf,
136+
(x as u32 * tile_size + offset) as i32,
137+
(y as u32 * tile_size + offset) as i32,
138+
tile_size,
139+
tile_size,
140+
"white",
141+
)?;
142+
}
143+
Tile::Stone => {
144+
let circle = svg::Circle {
145+
cx: (x as u32 * tile_size + offset + tile_size / 2) as i32,
146+
cy: (y as u32 * tile_size + offset + tile_size / 2) as i32,
147+
r: tile_size / 3,
148+
stroke: "black".to_owned(),
149+
stroke_width: 2,
150+
fill: "gray".to_owned(),
151+
};
152+
ImmSvg::circle(buf, &circle)?;
153+
}
154+
}
155+
}
156+
}
157+
158+
let grid = svg::Grid {
159+
x1: 0,
160+
y1: 0,
161+
x2: svg_width as i32,
162+
y2: svg_height as i32,
163+
grid_width,
164+
tile_size,
165+
};
166+
ImmSvg::grid(buf, &grid)
167+
})
168+
}
169+
}
170+
108171
#[cfg(test)]
109172
mod tests {
110173
use super::*;

0 commit comments

Comments
 (0)