Advertisement
loloof64

Untitled

Mar 28th, 2024
1,211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.35 KB | None | 0 0
  1. use iced::advanced::layout::{self, Layout};
  2. use iced::advanced::widget::{self, Widget};
  3. use iced::advanced::{mouse, renderer};
  4.  
  5. use iced::{Border, Color, Element, Length, Rectangle, Size};
  6.  
  7. #[derive(Clone, Copy)]
  8. pub struct BoardColors {
  9.     background_color: Color,
  10.     white_cell_color: Color,
  11.     black_cell_color: Color,
  12. }
  13.  
  14. impl Default for BoardColors {
  15.     fn default() -> Self {
  16.         Self {
  17.             background_color: Color::from_rgb(31f32 / 255f32, 70f32 / 255f32, 117f32 / 255f32),
  18.             white_cell_color: Color::from_rgb(255f32 / 255f32, 222f32 / 255f32, 173f32 / 255f32),
  19.             black_cell_color: Color::from_rgb(205f32 / 255f32, 133f32 / 255f32, 63f32 / 255f32),
  20.         }
  21.     }
  22. }
  23.  
  24. #[derive(Default, Clone, Copy)]
  25. pub struct ChessBoard {
  26.     size: f32,
  27.     colors: BoardColors,
  28.     //white_turn: bool,
  29. }
  30.  
  31. impl ChessBoard {
  32.     pub fn new(size: f32) -> Self {
  33.         let mut instance = ChessBoard::default();
  34.         instance.size = size;
  35.         instance
  36.     }
  37.  
  38.     pub fn set_colors(&mut self, colors: BoardColors) {
  39.         self.colors = colors;
  40.     }
  41. }
  42.  
  43. impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> for ChessBoard
  44. where
  45.     Renderer: renderer::Renderer,
  46. {
  47.     fn size(&self) -> Size<Length> {
  48.         Size {
  49.             width: Length::Fixed(self.size),
  50.             height: Length::Fixed(self.size),
  51.         }
  52.     }
  53.  
  54.     fn layout(
  55.         &self,
  56.         _tree: &mut widget::Tree,
  57.         _renderer: &Renderer,
  58.         _limits: &layout::Limits,
  59.     ) -> layout::Node {
  60.         layout::Node::new(Size::new(self.size, self.size))
  61.     }
  62.  
  63.     fn draw(
  64.         &self,
  65.         _state: &widget::Tree,
  66.         renderer: &mut Renderer,
  67.         _theme: &Theme,
  68.         _style: &renderer::Style,
  69.         layout: Layout<'_>,
  70.        _cursor: mouse::Cursor,
  71.        _viewport: &Rectangle,
  72.    ) {
  73.        renderer.fill_quad(
  74.            renderer::Quad {
  75.                bounds: layout.bounds(),
  76.                border: Border::default(),
  77.                ..renderer::Quad::default()
  78.            },
  79.            self.colors.background_color,
  80.        );
  81.    }
  82. }
  83.  
  84. impl<'a, Message, Theme, Renderer> From<ChessBoard> for Element<'a, Message, Theme, Renderer>
  85. where
  86.    Renderer: renderer::Renderer,
  87. {
  88.    fn from(chess_board: ChessBoard) -> Self {
  89.        Self::new(chess_board)
  90.    }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement