Advertisement
loloof64

Untitled

Nov 1st, 2018
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.01 KB | None | 0 0
  1. use gtk::prelude::*;
  2. use gdk::prelude::*;
  3. use gtk::DrawingArea;
  4. use cairo::Context;
  5. use cairo::enums::FontSlant;
  6. use cairo::enums::FontWeight;
  7. use chess_position_trainer::graphic::PieceImages;
  8.  
  9. pub struct ChessBoard
  10. {
  11.     drawing_area: DrawingArea,
  12.     cells_size: u32,
  13. }
  14.  
  15. impl ChessBoard
  16. {
  17.     pub fn new(cells_size: u32) -> ChessBoard
  18.     {
  19.         let piece_images = PieceImages::new(cells_size as i32);
  20.         let drawing_area = DrawingArea::new();
  21.  
  22.         drawing_area.connect_draw(move |_, cr|{
  23.             ChessBoard::draw_background(cr);
  24.             ChessBoard::draw_cells(cr, cells_size);
  25.             ChessBoard::draw_pieces(cr, cells_size, &piece_images);
  26.             ChessBoard::draw_coordinates(cr, cells_size);
  27.  
  28.             Inhibit(false)
  29.         });
  30.  
  31.         ChessBoard {
  32.             drawing_area: drawing_area,
  33.             cells_size: cells_size,
  34.         }
  35.     }
  36.  
  37.     pub fn get_drawing_area(&self) -> &DrawingArea
  38.     {
  39.         &self.drawing_area
  40.     }
  41.  
  42.     fn draw_background(cr: &Context)
  43.     {
  44.         let pink_color = [255.0/255.0, 204.0/255.0, 204.0/255.0];
  45.         cr.set_source_rgb(
  46.             pink_color[0],
  47.             pink_color[1],
  48.             pink_color[2],
  49.         );
  50.         cr.paint();
  51.     }
  52.  
  53.     fn draw_cells(cr: &Context, cells_size: u32)
  54.     {
  55.         (0..8).for_each(|row| {
  56.             (0..8).for_each(|col| {
  57.                 let white_cell_color = [255.0/255.0, 255.0/255.0, 179.0/255.0];
  58.                 let black_cell_color = [153.0/255.0, 102.0/255.0, 51.0/255.0];
  59.  
  60.                 let is_white_cell = (row+col) % 2 == 0;
  61.                 let cell_color = if is_white_cell {white_cell_color} else {black_cell_color};
  62.  
  63.                 let rect_x = (cells_size as f64) * (0.5 + (col as f64));
  64.                 let rect_y = (cells_size as f64) * (0.5 + (row as f64));
  65.                 let rect_size = cells_size as f64;
  66.  
  67.                 cr.rectangle(
  68.                     rect_x,
  69.                     rect_y,
  70.                     rect_size,
  71.                     rect_size,
  72.                 );
  73.                 cr.set_source_rgb(
  74.                     cell_color[0],
  75.                     cell_color[1],
  76.                     cell_color[2],
  77.                 );
  78.                 cr.fill();
  79.             });
  80.         });
  81.     }
  82.  
  83.     fn draw_pieces(cr: &Context, cells_size: u32, piece_images: &PieceImages)
  84.     {
  85.         let image = piece_images.get_white_queen();
  86.         let location_x = (cells_size as f64) * 0.5;
  87.         let location_y = (cells_size as f64) * 0.5;
  88.         cr.set_source_pixbuf(
  89.             image,
  90.             location_x,
  91.             location_y
  92.         );
  93.         cr.paint();
  94.     }
  95.  
  96.     fn draw_coordinates(cr: &Context, cells_size: u32)
  97.     {
  98.         let files = ["A", "B", "C", "D", "E", "F", "G", "H"];
  99.         let ranks = ["8", "7", "6", "5", "4", "3", "2", "1"];
  100.  
  101.         cr.set_source_rgb(0.2, 0.4, 1.0);
  102.         cr.select_font_face(
  103.             "Sans Serif",
  104.             FontSlant::Normal,
  105.             FontWeight::Bold
  106.         );
  107.         cr.set_font_size((cells_size as f64) * 0.38);
  108.        
  109.         (0..8).for_each(|file_index| {
  110.             let letter = files[file_index];
  111.             let letter_x = (cells_size as f64) * (0.9 + (file_index as f64));
  112.             let letter_y_top = (cells_size as f64) * 0.4;
  113.             let letter_y_bottom = (cells_size as f64) * 8.9;
  114.  
  115.             cr.move_to(letter_x, letter_y_top);
  116.             cr.show_text(letter);
  117.  
  118.             cr.move_to(letter_x, letter_y_bottom);
  119.             cr.show_text(letter);
  120.         });
  121.  
  122.         (0..8).for_each(|rank_index| {
  123.             let letter = ranks[rank_index];
  124.             let letter_y = (cells_size as f64) * (1.2 + (rank_index as f64));
  125.             let letter_x_left = (cells_size as f64) * 0.1;
  126.             let letter_x_right = (cells_size as f64) * 8.6;
  127.  
  128.             cr.move_to(letter_x_left, letter_y);
  129.             cr.show_text(letter);
  130.  
  131.             cr.move_to(letter_x_right, letter_y);
  132.             cr.show_text(letter);
  133.         });
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement