Skip to content

Commit 8e96f9a

Browse files
committed
chore: wip
lichess-org#18
1 parent 3bf3949 commit 8e96f9a

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

src/api.rs

+3
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ pub struct RequestBody {
191191
pub piece: PieceSet,
192192
#[serde(default)]
193193
pub coordinates: Coordinates,
194+
#[serde(default)]
195+
pub game_summary: Option<String>,
194196
}
195197

196198
#[serde_as]
@@ -258,6 +260,7 @@ impl RequestBody {
258260
theme: BoardTheme::default(),
259261
piece: PieceSet::default(),
260262
coordinates: Coordinates::default(),
263+
game_summary: Some(String::from("White resigned - Black is victorious")),
261264
}
262265
}
263266
}

src/render.rs

+82-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub struct Render {
6868
coordinates: Coordinates,
6969
frames: vec::IntoIter<RenderFrame>,
7070
kork: bool,
71+
game_summary: Option<String>,
7172
}
7273

7374
impl Render {
@@ -91,6 +92,7 @@ impl Render {
9192
}]
9293
.into_iter(),
9394
kork: false,
95+
game_summary: None,
9496
}
9597
}
9698

@@ -114,11 +116,12 @@ impl Render {
114116
highlighted: highlight_uci(frame.last_move),
115117
checked: frame.check.to_square(&frame.fen.0).into_iter().collect(),
116118
board: frame.fen.0.board,
117-
delay: Some(frame.delay.unwrap_or(default_delay)),
119+
delay: Some(5),
118120
})
119121
.collect::<Vec<_>>()
120122
.into_iter(),
121123
kork: true,
124+
game_summary: params.game_summary,
122125
}
123126
}
124127
}
@@ -270,6 +273,59 @@ impl Iterator for Render {
270273

271274
self.state = RenderState::Frame(frame);
272275
} else {
276+
if let Some(game_summary) = &self.game_summary {
277+
println!("render game summary: {}", game_summary);
278+
let mut ctrl = block::GraphicControl::default();
279+
ctrl.set_disposal_method(block::DisposalMethod::Keep);
280+
ctrl.set_transparent_color(Some(self.theme.transparent_color()));
281+
ctrl.set_delay_time_cs(500);
282+
blocks.encode(ctrl).expect("enc graphic control");
283+
284+
let summary_box_height = 100;
285+
let summary_box_width = 400;
286+
let summary_box_left = (self.theme.width() - summary_box_width) / 2;
287+
let summary_box_top =
288+
(self.theme.height(self.bars.is_some()) - summary_box_height) / 2;
289+
let summary_font_scale = Scale { x: 32.0, y: 32.0 };
290+
let v_metrics = self.font.v_metrics(summary_font_scale);
291+
292+
blocks
293+
.encode(
294+
block::ImageDesc::default()
295+
.with_left(summary_box_left as u16)
296+
.with_top(summary_box_top as u16)
297+
.with_height(summary_box_height as u16)
298+
.with_width(summary_box_width as u16),
299+
)
300+
.expect("enc image desc");
301+
302+
let mut view = ArrayViewMut2::from_shape(
303+
(summary_box_height, summary_box_width),
304+
&mut self.buffer,
305+
)
306+
.expect("summary view box view");
307+
view.fill(self.theme.bar_color());
308+
309+
let glyphs = self.font.layout(
310+
game_summary,
311+
summary_font_scale,
312+
rusttype::point(5.0, 40.0),
313+
);
314+
render_summary(
315+
&mut view,
316+
glyphs,
317+
self.theme,
318+
self.theme.text_color(),
319+
self.theme.bar_color(),
320+
);
321+
322+
let mut image_data =
323+
block::ImageData::new(summary_box_width * summary_box_height);
324+
image_data.data_mut().extend_from_slice(
325+
&self.buffer[..(summary_box_width * summary_box_height)],
326+
);
327+
blocks.encode(image_data).expect("enc summary box");
328+
}
273329
// Add a black frame at the end, to work around twitter
274330
// cutting off the last frame.
275331
if self.kork {
@@ -526,6 +582,31 @@ fn render_coord(
526582
}
527583
}
528584

585+
fn render_summary(
586+
square_buffer: &mut ArrayViewMut2<u8>,
587+
glyphs: LayoutIter,
588+
theme: &Theme,
589+
text_color: u8,
590+
background_color: u8,
591+
) {
592+
for g in glyphs {
593+
if let Some(bb) = g.pixel_bounding_box() {
594+
// Poor man's anti-aliasing.
595+
g.draw(|left, top, intensity| {
596+
let left = left as i32 + bb.min.x;
597+
let top = top as i32 + bb.min.y;
598+
if 0 <= left && left < theme.width() as i32 && 0 <= top && intensity >= 0.01 {
599+
if intensity < 0.5 {
600+
square_buffer[(top as usize, left as usize)] = background_color;
601+
} else {
602+
square_buffer[(top as usize, left as usize)] = text_color;
603+
}
604+
}
605+
});
606+
};
607+
}
608+
}
609+
529610
fn get_square_background_color(is_highlighted: bool, is_dark: bool, theme: &Theme) -> u8 {
530611
match is_highlighted {
531612
true => match is_dark {

0 commit comments

Comments
 (0)