Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use pleco::{board::Board, File, Piece, Player, Rank, SQ};
- use vizia::prelude::*;
- #[derive(Lens)]
- pub struct ChessBoard {
- board_state: Board,
- white_turn: bool,
- pieces: [[Piece; 8]; 8],
- }
- impl ChessBoard {
- pub fn new<'a>(cx: &'a mut Context /* todo pass init parameters */) -> Handle<'a, Self> {
- let board = Board::from_fen("rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2").expect("Bad board position.");
- Self {
- board_state: board,
- white_turn: board.turn() == Player::White,
- pieces: get_pieces_from_board(board),
- }
- .build(cx, |cx| {
- let turn_color = ChessBoard::white_turn.map(|turn| {
- if *turn {
- Color::white()
- } else {
- Color::black()
- }
- });
- HStack::new(cx, |cx| {
- for i in 0..=9 {
- for j in 0..=9 {
- let bg_color = if i > 0 && i < 9 && j > 0 && j < 9 {
- if (i % 2 == 0) ^ (j % 2 == 0) {
- Color::rgb(125, 47, 31)
- } else {
- Color::rgb(200, 200, 120)
- }
- } else {
- Color::rgb(20, 60, 200)
- };
- if i > 0 && i < 9 && j > 0 && j < 9 {
- let file = i - 1 as u8;
- let rank = 9 - j as u8;
- let piece = ChessBoard::pieces[rank as usize][file as usize];
- }
- Element::new(cx)
- .col_index(i)
- .row_index(j)
- .background_color(bg_color);
- }
- }
- Element::new(cx)
- .col_index(9)
- .row_index(9)
- .background_color(turn_color)
- .border_radius(Percentage(50.0));
- })
- .layout_type(LayoutType::Grid)
- .grid_rows(vec![
- Stretch(1.0),
- Stretch(2.0),
- Stretch(2.0),
- Stretch(2.0),
- Stretch(2.0),
- Stretch(2.0),
- Stretch(2.0),
- Stretch(2.0),
- Stretch(2.0),
- Stretch(1.0),
- ])
- .grid_cols(vec![
- Stretch(1.0),
- Stretch(2.0),
- Stretch(2.0),
- Stretch(2.0),
- Stretch(2.0),
- Stretch(2.0),
- Stretch(2.0),
- Stretch(2.0),
- Stretch(2.0),
- Stretch(1.0),
- ])
- .background_color(Color::rgb(20, 60, 200));
- })
- .focusable(false)
- }
- }
- impl View for ChessBoard {
- fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
- event.map(|window_event, meta| match window_event {
- WindowEvent::TriggerDown { .. } => {}
- WindowEvent::TriggerUp { .. } => {}
- _ => {}
- });
- }
- }
- fn build_file(file: u8) -> Result<File, String> {
- match file {
- 0 => Ok(File::A),
- 1 => Ok(File::B),
- 2 => Ok(File::C),
- 3 => Ok(File::D),
- 4 => Ok(File::E),
- 5 => Ok(File::F),
- 6 => Ok(File::G),
- 7 => Ok(File::H),
- _ => Err(format!("Not available file {}.", file)),
- }
- }
- fn build_rank(rank: u8) -> Result<Rank, String> {
- match rank {
- 0 => Ok(Rank::R1),
- 1 => Ok(Rank::R2),
- 2 => Ok(Rank::R3),
- 3 => Ok(Rank::R4),
- 4 => Ok(Rank::R5),
- 5 => Ok(Rank::R6),
- 6 => Ok(Rank::R7),
- 7 => Ok(Rank::R8),
- }
- }
- fn get_pieces_from_board(board: Board) -> [[Piece; 8]; 8] {
- let mut result = [[Piece::None; 8]; 8];
- for row in 0..=7 {
- for col in 0..=7 {
- let file = col;
- let rank = row;
- let pleco_file = build_file(file).unwrap();
- let pleco_rank = build_rank(rank).unwrap();
- result[rank as usize][file as usize] = board.piece_at_sq(SQ::make(pleco_file, pleco_rank));
- }
- }
- result
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement