Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use iced::advanced::layout::{self, Layout};
- use iced::advanced::widget::{self, Widget};
- use iced::advanced::{mouse, renderer};
- use iced::{Border, Color, Element, Length, Rectangle, Size};
- #[derive(Clone, Copy)]
- pub struct BoardColors {
- background_color: Color,
- white_cell_color: Color,
- black_cell_color: Color,
- }
- impl Default for BoardColors {
- fn default() -> Self {
- Self {
- background_color: Color::from_rgb(31f32 / 255f32, 70f32 / 255f32, 117f32 / 255f32),
- white_cell_color: Color::from_rgb(255f32 / 255f32, 222f32 / 255f32, 173f32 / 255f32),
- black_cell_color: Color::from_rgb(205f32 / 255f32, 133f32 / 255f32, 63f32 / 255f32),
- }
- }
- }
- #[derive(Default, Clone, Copy)]
- pub struct ChessBoard {
- size: f32,
- colors: BoardColors,
- //white_turn: bool,
- }
- impl ChessBoard {
- pub fn new(size: f32) -> Self {
- let mut instance = ChessBoard::default();
- instance.size = size;
- instance
- }
- pub fn set_colors(&mut self, colors: BoardColors) {
- self.colors = colors;
- }
- }
- impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> for ChessBoard
- where
- Renderer: renderer::Renderer,
- {
- fn size(&self) -> Size<Length> {
- Size {
- width: Length::Fixed(self.size),
- height: Length::Fixed(self.size),
- }
- }
- fn layout(
- &self,
- _tree: &mut widget::Tree,
- _renderer: &Renderer,
- _limits: &layout::Limits,
- ) -> layout::Node {
- layout::Node::new(Size::new(self.size, self.size))
- }
- fn draw(
- &self,
- _state: &widget::Tree,
- renderer: &mut Renderer,
- _theme: &Theme,
- _style: &renderer::Style,
- layout: Layout<'_>,
- _cursor: mouse::Cursor,
- _viewport: &Rectangle,
- ) {
- renderer.fill_quad(
- renderer::Quad {
- bounds: layout.bounds(),
- border: Border::default(),
- ..renderer::Quad::default()
- },
- self.colors.background_color,
- );
- }
- }
- impl<'a, Message, Theme, Renderer> From<ChessBoard> for Element<'a, Message, Theme, Renderer>
- where
- Renderer: renderer::Renderer,
- {
- fn from(chess_board: ChessBoard) -> Self {
- Self::new(chess_board)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement