rg-chess 0.2.4

Chess Game with graphical interface.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
use std::cmp::max;

use ggez::event::{KeyCode, KeyMods, MouseButton};
use ggez::{event, graphics, Context, GameError, GameResult};
use log::{debug, info};
use unicode_segmentation::UnicodeSegmentation;

use crate::{
    Align, Button, Chess, GameState, Square, Theme, ALL_SQUARES, BOARD_CELL_PX_SIZE, BOARD_PX_SIZE,
    BOARD_SIZE, INDEX_THEME, NUM_THEMES, SIDE_SCREEN_PX_SIZE, THEMES,
};

/// GUI for the [`Chess`] game.
#[derive(Debug)]
pub struct ChessGui {
    pub(crate) chess: Chess,
    theme: Theme,
    buttons: Vec<Button>,
}

impl ChessGui {
    /// Create a new instance of ChessGui.
    pub fn new(chess: Chess, theme: Theme, buttons: Vec<Button>) -> Self {
        ChessGui {
            chess,
            theme,
            buttons,
        }
    }

    /// Reset The chess game and buttons but not the theme.
    pub fn reset(&mut self) {
        self.chess.reset();
        self.buttons.clear();
        self.init_buttons();
    }

    /// Set the theme for the GUI.
    ///
    /// # Examples
    ///
    /// ```
    /// use chess::{ChessGui, THEME_SANDCASTLE};
    ///
    /// let mut game = ChessGui::default();
    /// game.set_theme(THEME_SANDCASTLE);
    /// ```
    pub fn set_theme(&mut self, theme: Theme) {
        self.theme = theme;
    }

    /// Set the theme to the next one for the GUI.
    ///
    /// # Safety
    ///
    /// This function use/set a static variable.
    pub unsafe fn next_theme(&mut self) {
        INDEX_THEME = (INDEX_THEME + 1) % NUM_THEMES;
        self.theme = THEMES[INDEX_THEME % 6];
    }

    /// Add a button in the GUI.
    pub fn add_button(&mut self, button: Button) {
        self.buttons.push(button);
    }

    /// Set all the buttons in the GUI.
    fn init_buttons(&mut self) {
        self.buttons.push(
            Button::new(
                "theme",
                true,
                graphics::Rect::new(
                    BOARD_PX_SIZE.0 + SIDE_SCREEN_PX_SIZE.0 - 70.0,
                    20.0,
                    50.0,
                    50.0,
                ),
                graphics::Color::new(1.0, 1.0, 1.0, 1.0),
                "Theme",
                Align::Center,
                Some(|chess_gui| unsafe {
                    chess_gui.next_theme();
                }),
            )
            .set_image(self.theme.theme_icon_path),
        );
        self.buttons.push(Button::new(
            "undo",
            true,
            graphics::Rect::new(
                BOARD_PX_SIZE.0 + 20.0,
                SIDE_SCREEN_PX_SIZE.1 - 210.0,
                150.0,
                50.0,
            ),
            graphics::Color::new(0.65, 0.44, 0.78, 1.0),
            "Undo",
            Align::Center,
            Some(|chess_gui| {
                chess_gui.chess.undo();
            }),
        ));
        self.buttons.push(Button::new(
            "declare-draw",
            false,
            graphics::Rect::new(
                BOARD_PX_SIZE.0 + 190.0,
                SIDE_SCREEN_PX_SIZE.1 - 210.0,
                150.0,
                50.0,
            ),
            graphics::Color::new(0.89, 0.8, 0.35, 1.0),
            "Declare Draw",
            Align::Center,
            Some(|chess_gui| {
                chess_gui.chess.declare_draw();
            }),
        ));
        self.buttons.push(Button::new(
            "offer-draw",
            true,
            graphics::Rect::new(
                BOARD_PX_SIZE.0 + 20.0,
                SIDE_SCREEN_PX_SIZE.1 - 140.0,
                150.0,
                50.0,
            ),
            graphics::Color::new(1.0, 0.64, 0.38, 1.0),
            "Offer Draw",
            Align::Center,
            Some(|chess_gui| {
                chess_gui.chess.offer_draw();
            }),
        ));
        self.buttons.push(Button::new(
            "accept-draw",
            false,
            graphics::Rect::new(
                BOARD_PX_SIZE.0 + 190.0,
                SIDE_SCREEN_PX_SIZE.1 - 140.0,
                150.0,
                50.0,
            ),
            graphics::Color::new(0.56, 0.78, 0.4, 1.0),
            "Accept Draw",
            Align::Center,
            Some(|chess_gui| {
                chess_gui.chess.accept_draw();
            }),
        ));
        self.buttons.push(Button::new(
            "reset",
            true,
            graphics::Rect::new(
                BOARD_PX_SIZE.0 + 20.0,
                SIDE_SCREEN_PX_SIZE.1 - 70.0,
                150.0,
                50.0,
            ),
            graphics::Color::new(0.65, 0.44, 0.78, 1.0),
            "Reset",
            Align::Center,
            Some(|chess_gui| {
                chess_gui.reset();
            }),
        ));
        self.buttons.push(Button::new(
            "resign",
            true,
            graphics::Rect::new(
                BOARD_PX_SIZE.0 + 190.0,
                SIDE_SCREEN_PX_SIZE.1 - 70.0,
                150.0,
                50.0,
            ),
            graphics::Color::new(0.98, 0.3, 0.3, 1.0),
            "Resign",
            Align::Center,
            Some(|chess_gui| {
                chess_gui.chess.resign(chess_gui.chess.board.side_to_move());
            }),
        ));
    }

    /// Base function to call when a user click on the screen.
    pub fn click(&mut self, x: f32, y: f32) {
        if x < BOARD_PX_SIZE.0 && self.chess.state.is_ongoing() {
            self.click_on_board(x, y);
        } else {
            self.click_on_side(x, y);
        }
    }

    /// React when the user click on the board screen.
    ///
    /// It is the callers responsibility to ensure the coordinate is in the board.
    fn click_on_board(&mut self, x: f32, y: f32) {
        let current_square = Square::from_screen(x, y);
        debug!("Click at: ({x},{y}) -> on the square: {current_square}");
        match self.chess.square_focused {
            Some(square_selected) => self.chess.play(square_selected, current_square),
            None => {
                if self
                    .chess
                    .board
                    .color_on_is(current_square, self.chess.board.side_to_move())
                {
                    self.chess.square_focused = Some(current_square);
                }
            }
        }
    }

    /// React when the user click on the side screen.
    ///
    /// It is the callers responsibility to ensure the coordinate is in the side.
    fn click_on_side(&mut self, x: f32, y: f32) {
        info!("Click at: ({x},{y}) -> on the side screen");
        let buttons = self.buttons.clone();
        for button in buttons.iter() {
            if button.contains(x, y) {
                button.clicked(self);
            }
        }
    }

    /// Draw all of the board side.
    fn draw_board(&self, ctx: &mut Context) -> GameResult {
        self.draw_empty_board(ctx)?;
        self.draw_legal_moves(ctx)?;
        self.draw_pinned_piece(ctx)?;
        self.draw_content_board(ctx)?;
        Ok(())
    }

    /// Draw the empty chess board (without pieces).
    fn draw_empty_board(&self, ctx: &mut Context) -> GameResult {
        for y in 0..BOARD_SIZE.1 {
            for x in 0..BOARD_SIZE.0 {
                let color_index = if (x % 2 == 1 && y % 2 == 1) || (x % 2 == 0 && y % 2 == 0) {
                    0
                } else {
                    1
                };
                let mesh = graphics::MeshBuilder::new()
                    .rectangle(
                        graphics::DrawMode::fill(),
                        graphics::Rect::new(
                            x as f32 * BOARD_CELL_PX_SIZE.0,
                            y as f32 * BOARD_CELL_PX_SIZE.1,
                            BOARD_CELL_PX_SIZE.0,
                            BOARD_CELL_PX_SIZE.1,
                        ),
                        self.theme.board_color[color_index],
                    )?
                    .build(ctx)?;
                graphics::draw(ctx, &mesh, graphics::DrawParam::default())?;
            }
        }
        Ok(())
    }

    /// Draw pieces on the board.
    fn draw_content_board(&self, ctx: &mut Context) -> GameResult {
        let mut path;
        let mut image;
        for square in ALL_SQUARES {
            if let Some((piece, color)) = self.chess.board.on(square) {
                path = self.theme.piece_path[color.to_index()][piece.to_index()];
                image = graphics::Image::new(ctx, path).expect("Image load error");
                let (x, y) = square.to_screen();
                let dest_point = [x, y];
                let image_scale = [0.5, 0.5];
                let dp = graphics::DrawParam::new()
                    .dest(dest_point)
                    .scale(image_scale);
                graphics::draw(ctx, &image, dp)?;
            }
        }
        Ok(())
    }

    /// Draw all the possible destination of the selected piece.
    fn draw_legal_moves(&self, ctx: &mut Context) -> GameResult {
        if self.theme.valid_moves_color.is_some() {
            if let Some(square) = self.chess.square_focused {
                for dest in self.chess.board.get_legal_moves(square) {
                    let (x, y) = dest.to_screen();
                    let mesh = graphics::MeshBuilder::new()
                        .rectangle(
                            graphics::DrawMode::fill(),
                            graphics::Rect::new(x, y, BOARD_CELL_PX_SIZE.0, BOARD_CELL_PX_SIZE.1),
                            self.theme.valid_moves_color.unwrap(),
                        )?
                        .build(ctx)?;
                    graphics::draw(ctx, &mesh, graphics::DrawParam::default())?;
                }
            }
        }
        Ok(())
    }

    /// Draw a cross on [`Square`] that are pinned (i.e. can't move).
    fn draw_pinned_piece(&self, ctx: &mut Context) -> GameResult {
        if self.theme.piece_pinned_path.is_some() {
            let mut path;
            let mut image;
            for square in self.chess.board.pinned() {
                path = self.theme.piece_pinned_path.unwrap();
                image = graphics::Image::new(ctx, path).expect("Image load error");
                let (x, y) = square.to_screen();
                let dest_point = [x, y];
                // We set the scale at 1.0 because we want the same size
                // for the image and a Board_cell
                const SCALE: f32 = 1.0;
                let image_scale = [
                    SCALE * (BOARD_CELL_PX_SIZE.0 / image.width() as f32),
                    SCALE * (BOARD_CELL_PX_SIZE.1 / image.height() as f32),
                ];
                let dp = graphics::DrawParam::new()
                    .dest(dest_point)
                    .scale(image_scale);
                graphics::draw(ctx, &image, dp)?;
            }
        } else if self.theme.piece_pinned_color.is_some() {
            for piece in self.chess.board.pinned() {
                let (x, y) = piece.to_screen();
                let mesh = graphics::MeshBuilder::new()
                    .rectangle(
                        graphics::DrawMode::fill(),
                        graphics::Rect::new(x, y, BOARD_CELL_PX_SIZE.0, BOARD_CELL_PX_SIZE.1),
                        self.theme.piece_pinned_color.unwrap(),
                    )?
                    .build(ctx)?;
                graphics::draw(ctx, &mesh, graphics::DrawParam::default())?;
            }
        }
        Ok(())
    }

    /// Draw all the side screen.
    fn draw_side(&self, ctx: &mut Context) -> GameResult {
        for button in self.buttons.iter() {
            button.draw(ctx, self.theme.font_path, self.theme.font_scale)?;
        }
        self.draw_timers(ctx)?;
        self.draw_winner(ctx)?;
        Ok(())
    }

    /// Draw timers on the side screen.
    fn draw_timers(&self, ctx: &mut Context) -> GameResult {
        // Draw the rect background
        let bounds_white = graphics::Rect::new(BOARD_PX_SIZE.0 + 20.0, 20.0, 115.0, 50.0);
        let bounds_black = graphics::Rect::new(BOARD_PX_SIZE.0 + 155.0, 20.0, 115.0, 50.0);
        let background_mesh_white = graphics::MeshBuilder::new()
            .rectangle(
                graphics::DrawMode::fill(),
                bounds_white,
                graphics::Color::new(0.5, 0.5, 0.5, 1.0),
            )?
            .build(ctx)?;
        graphics::draw(ctx, &background_mesh_white, graphics::DrawParam::default())?;
        let background_mesh_black = graphics::MeshBuilder::new()
            .rectangle(
                graphics::DrawMode::fill(),
                bounds_black,
                graphics::Color::new(0.5, 0.5, 0.5, 1.0),
            )?
            .build(ctx)?;
        graphics::draw(ctx, &background_mesh_black, graphics::DrawParam::default())?;

        // Draw the text
        let text_white = format!("{}:{}", "--", "--");
        let font = graphics::Font::new(ctx, self.theme.font_path)?;
        let text_white = graphics::Text::new((text_white, font, self.theme.font_scale * 2.0));
        let dest_point = [
            bounds_white.x + (bounds_white.w - text_white.width(ctx)) / 2.0,
            bounds_white.y + (bounds_white.h - text_white.height(ctx)) / 2.0,
        ];
        graphics::draw(ctx, &text_white, (dest_point,))?;
        let text_black = format!("{}:{}", "--", "--");
        let font = graphics::Font::new(ctx, self.theme.font_path)?;
        let text_black = graphics::Text::new((text_black, font, self.theme.font_scale * 2.0));
        let dest_point = [
            bounds_black.x + (bounds_black.w - text_black.width(ctx)) / 2.0,
            bounds_black.y + (bounds_black.h - text_black.height(ctx)) / 2.0,
        ];
        graphics::draw(ctx, &text_black, (dest_point, graphics::Color::BLACK))?;

        Ok(())
    }

    /// Draw the winner on the side screen.
    fn draw_winner(&self, ctx: &mut Context) -> GameResult {
        // Draw the rect background
        let bounds = graphics::Rect::new(
            BOARD_PX_SIZE.0 + 20.0,
            90.0,
            320.0,
            SIDE_SCREEN_PX_SIZE.1 - 250.0 - 70.0,
        );
        let background_mesh = graphics::MeshBuilder::new()
            .rectangle(
                graphics::DrawMode::stroke(3.0),
                bounds,
                graphics::Color::new(0.7, 0.7, 0.7, 1.0),
            )?
            .build(ctx)?;
        graphics::draw(ctx, &background_mesh, graphics::DrawParam::default())?;

        // Draw the text (9 caractères max avec une font_scale de 20.0)
        let text = match self.chess.state {
            GameState::Ongoing => {
                let line1 = "Ongoing:".to_string();
                let line2 = format!("{:?} turn", self.chess.board.side_to_move());
                let line1_size = line1.graphemes(true).count();
                let line2_size = line2.graphemes(true).count();
                let max_size = max(line1_size, line2_size);
                format!("{: ^max_size$}\n{}", line1, line2)
            }
            GameState::Checkmates(color) => {
                format!("{:?} is checkmate\n\n    {:?} win !", color, !color)
            }
            GameState::Stalemate => "Draw: Stalemate".to_string(),
            GameState::DrawAccepted => "Draw: Accepted".to_string(),
            GameState::DrawDeclared => "Draw: Declared".to_string(),
            GameState::Resigns(color) => format!("{:?} resigns\n\n {:?} win !", color, !color),
        };
        let font = graphics::Font::new(ctx, self.theme.font_path)?;
        let text = graphics::Text::new((text, font, self.theme.font_scale * 2.0));
        let dest_point = [
            bounds.x + (bounds.w - text.width(ctx)) / 2.0,
            bounds.y + (bounds.h - text.height(ctx)) / 2.0,
        ];
        graphics::draw(ctx, &text, (dest_point,))?;
        Ok(())
    }
}

impl event::EventHandler<GameError> for ChessGui {
    /// Update will happen on every frame before it is drawn.
    fn update(&mut self, _ctx: &mut Context) -> GameResult {
        for button in self.buttons.iter_mut() {
            match button.id {
                "declare-draw" => {
                    if self.chess.can_declare_draw() {
                        button.enable();
                    } else {
                        button.disable();
                    }
                }
                "accept-draw" => {
                    if self.chess.offer_draw {
                        button.enable();
                    } else {
                        button.disable();
                    }
                }
                _ => {}
            }
        }
        if self.chess.state.is_finish() {
            for button in self.buttons.iter_mut() {
                match button.id {
                    "reset" | "theme" => {}
                    _ => button.disable(),
                }
            }
        }
        Ok(())
    }

    /// Render the game's current state.
    fn draw(&mut self, ctx: &mut Context) -> GameResult {
        // First we clear the screen and set the background color
        graphics::clear(ctx, self.theme.background_color);

        // Draw the board and the side screen (that contains all button/info)
        self.draw_board(ctx)?;
        self.draw_side(ctx)?;

        // Finally we call graphics::present to cycle the gpu's framebuffer and display
        // the new frame we just drew.
        graphics::present(ctx)?;

        // And return success.
        Ok(())
    }

    /// Called every time a mouse button gets pressed
    fn mouse_button_down_event(&mut self, _ctx: &mut Context, button: MouseButton, x: f32, y: f32) {
        if button == MouseButton::Left {
            self.click(x, y);
        }
    }

    /// Change the [`ggez::input::mouse::CursorIcon`] when the mouse is on a button.
    fn mouse_motion_event(&mut self, ctx: &mut Context, x: f32, y: f32, _dx: f32, _dy: f32) {
        if x > BOARD_PX_SIZE.0 {
            let mut on_button = false;
            for button in self.buttons.iter() {
                if button.contains(x, y) {
                    on_button = true;
                    break;
                }
            }
            if on_button {
                ggez::input::mouse::set_cursor_type(ctx, ggez::input::mouse::CursorIcon::Hand);
            } else {
                ggez::input::mouse::set_cursor_type(ctx, ggez::input::mouse::CursorIcon::Default);
            }
        }
    }

    /// Called every time a key gets pressed.
    ///
    /// # Keys
    ///
    /// |  Keys  |          Actions           |
    /// |--------|----------------------------|
    /// | Escape | Quit the game              |
    /// | R      | Reset the game and buttons |
    /// | CTRL+Z | Undo                       |
    fn key_down_event(
        &mut self,
        ctx: &mut Context,
        keycode: KeyCode,
        keymod: KeyMods,
        _repeat: bool,
    ) {
        match keycode {
            KeyCode::Escape => event::quit(ctx),
            KeyCode::R => self.reset(),
            KeyCode::Z if keymod == KeyMods::CTRL => self.chess.undo(),
            _ => {}
        };
    }
}

impl Default for ChessGui {
    fn default() -> Self {
        let mut chess_gui = ChessGui::new(
            Default::default(),
            Default::default(),
            Vec::with_capacity(7),
        );
        chess_gui.init_buttons();
        chess_gui
    }
}