Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use pleco::{board::Board, Piece, File, Player, Rank, SQ};
- use vizia::prelude::*;
- use super::svg_zone::SvgZone;
- #[derive(Default, Lens)]
- pub struct ChessBoard {
- board_state: Board,
- }
- impl ChessBoard {
- pub fn new<'a>(cx: &'a mut Context /* todo pass init parameters */) -> Handle<'a, Self> {
- Self {
- board_state: Board::from_fen(
- "rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2",
- )
- .expect("Bad fen position."),
- }
- .build(cx, |cx| {
- let turn_color = ChessBoard::board_state.map(|board_state| {
- let turn = board_state.turn();
- if turn == Player::White {
- 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)
- };
- HStack::new(cx, |cx| {
- if i > 0 && i < 9 && j > 0 && j < 9 {
- let file = i - 1;
- let rank = 9 - j;
- let pleco_file = build_file(file).unwrap();
- let pleco_rank = build_rank(rank).unwrap();
- let square = SQ::make(pleco_file, pleco_rank);
- let piece = ChessBoard::board_state.get(cx).piece_at_sq(square);
- if piece != Piece::None {
- get_image_from_piece(cx, piece, 30.0).unwrap();
- }
- }
- })
- .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),
- _ => Err(format!("Not available rank {}.", rank)),
- }
- }
- fn get_image_from_piece(cx: &mut Context, piece: Piece, sizePx: f32) -> Option<Handle<SvgZone>> {
- match piece {
- Piece::WhitePawn => {
- Some(SvgZone::new(cx, include_bytes!("./vectors/Chess_plt45.svg")).size(Pixels(sizePx)))
- },
- Piece::WhiteKnight => {
- Some(SvgZone::new(cx, include_bytes!("./vectors/Chess_nlt45.svg")).size(Pixels(sizePx)))
- },
- Piece::WhiteBishop => {
- Some(SvgZone::new(cx, include_bytes!("./vectors/Chess_blt45.svg")).size(Pixels(sizePx)))
- },
- Piece::WhiteRook => {
- Some(SvgZone::new(cx, include_bytes!("./vectors/Chess_rlt45.svg")).size(Pixels(sizePx)))
- },
- Piece::WhiteQueen => {
- Some(SvgZone::new(cx, include_bytes!("./vectors/Chess_qlt45.svg")).size(Pixels(sizePx)))
- },
- Piece::WhiteKing => {
- Some(SvgZone::new(cx, include_bytes!("./vectors/Chess_klt45.svg")).size(Pixels(sizePx)))
- },
- Piece::BlackPawn => {
- Some(SvgZone::new(cx, include_bytes!("./vectors/Chess_pdt45.svg")).size(Pixels(sizePx)))
- },
- Piece::BlackKnight => {
- Some(SvgZone::new(cx, include_bytes!("./vectors/Chess_ndt45.svg")).size(Pixels(sizePx)))
- },
- Piece::BlackBishop => {
- Some(SvgZone::new(cx, include_bytes!("./vectors/Chess_bdt45.svg")).size(Pixels(sizePx)))
- },
- Piece::BlackRook => {
- Some(SvgZone::new(cx, include_bytes!("./vectors/Chess_rdt45.svg")).size(Pixels(sizePx)))
- },
- Piece::BlackQueen => {
- Some(SvgZone::new(cx, include_bytes!("./vectors/Chess_qdt45.svg")).size(Pixels(sizePx)))
- },
- Piece::BlackKing => {
- Some(SvgZone::new(cx, include_bytes!("./vectors/Chess_kdt45.svg")).size(Pixels(sizePx)))
- },
- _ => None
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement