Advertisement
loloof64

Rust/Relm/ drawing refactor

May 24th, 2020
2,585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.94 KB | None | 0 0
  1. use relm::{Widget};
  2. use gtk::prelude::*;
  3. use gtk::{Inhibit};
  4. use relm_derive::{Msg, widget};
  5.  
  6. struct ChessBoardPainter {
  7.    
  8. }
  9.  
  10. impl ChessBoardPainter {
  11.     fn draw_background(context: &cairo::Context, background_color: (f64, f64, f64)) {
  12.         let (bg_red, bg_green, bg_blue) = background_color;
  13.  
  14.         context.set_source_rgb(bg_red, bg_green, bg_blue);
  15.         context.paint();
  16.     }
  17.  
  18.     fn draw_cells(
  19.         context: &cairo::Context,
  20.         white_cells_color: (f64, f64, f64),
  21.         black_cells_color: (f64, f64, f64),
  22.         size: i32,
  23.     ) {
  24.         let (w_cells_red, w_cells_green, w_cells_blue) = white_cells_color;
  25.         let (b_cells_red, b_cells_green, b_cells_blue) = black_cells_color;
  26.  
  27.         let cells_size = (size as f64) / 9.0;
  28.  
  29.         for row in 0..8 {
  30.             for col in 0..8 {
  31.                 let is_white_cell = (row+col) % 2 == 0;
  32.                 if is_white_cell {
  33.                     context.set_source_rgb(w_cells_red, w_cells_green, w_cells_blue);
  34.                 }
  35.                 else {
  36.                     context.set_source_rgb(b_cells_red, b_cells_green, b_cells_blue);
  37.                 }
  38.  
  39.                 let cell_x = cells_size * (0.5 + (col as f64));
  40.                 let cell_y = cells_size * (0.5 + (row as f64));
  41.                 context.rectangle(cell_x, cell_y, cells_size, cells_size);
  42.                 context.fill();
  43.             }
  44.         }
  45.     }
  46. }
  47.  
  48. #[allow(dead_code)]
  49. pub struct ChessBoardModel {
  50.     size: i32,
  51.     background_color: (f64, f64, f64),
  52.     white_cells_color: (f64, f64, f64),
  53.     black_cells_color: (f64, f64, f64),
  54. }
  55.  
  56. pub struct ChessBoardModelBuilder {
  57.     size: i32,
  58.     background_color: (f64, f64, f64),
  59.     white_cells_color: (f64, f64, f64),
  60.     black_cells_color: (f64, f64, f64),
  61. }
  62.  
  63. #[allow(dead_code)]
  64. impl ChessBoardModelBuilder {
  65.     fn new() -> ChessBoardModelBuilder {
  66.         ChessBoardModelBuilder {
  67.             size: 300,
  68.             background_color: (0.5, 0.4, 0.9),
  69.             white_cells_color: (1.0, 0.85, 0.6),
  70.             black_cells_color: (0.85, 0.55, 0.25),
  71.         }
  72.     }
  73.  
  74.     fn build(self) -> ChessBoardModel {
  75.         ChessBoardModel {
  76.             size: self.size,
  77.             background_color: self.background_color,
  78.             white_cells_color: self.white_cells_color,
  79.             black_cells_color: self.black_cells_color,
  80.         }
  81.     }
  82.  
  83.     fn set_size(&mut self, size: i32) {
  84.         self.size = size;
  85.     }
  86.  
  87.     fn set_background_color(&mut self, background_color: (f64, f64, f64)) {
  88.         self.background_color = background_color;
  89.     }
  90.  
  91.     fn set_white_cells_color(&mut self, white_cells_color: (f64, f64, f64)) {
  92.         self.white_cells_color = white_cells_color;
  93.     }
  94.  
  95.     fn set_black_cells_color(&mut self, black_cells_color: (f64, f64, f64)) {
  96.         self.black_cells_color = black_cells_color;
  97.     }
  98. }
  99.  
  100. #[derive(Msg)]
  101. pub enum ChessBoardMsg {
  102.    
  103. }
  104.  
  105. #[widget]
  106. impl Widget for ChessBoard {
  107.     fn model(size: i32) -> ChessBoardModel {
  108.         let mut model_builder = ChessBoardModelBuilder::new();
  109.         model_builder.set_size(size);
  110.         model_builder.build()
  111.     }
  112.  
  113.     fn update(&mut self, event: ChessBoardMsg) {
  114.         match event {
  115.  
  116.         }
  117.     }
  118.  
  119.     fn init_view(&mut self) {
  120.         self.canvas.set_size_request(self.model.size, self.model.size);
  121.  
  122.  
  123.         let background_color = self.model.background_color;
  124.         let white_cells_color = self.model.white_cells_color;
  125.         let black_cells_color = self.model.black_cells_color;
  126.         let size = self.model.size;
  127.  
  128.         self.canvas.connect_draw(move |_source, context| {
  129.             ChessBoardPainter::draw_background(context, background_color);
  130.             ChessBoardPainter::draw_cells(context, white_cells_color, black_cells_color, size);
  131.  
  132.             Inhibit(true)
  133.         });
  134.     }
  135.  
  136.     view! {
  137.         #[name="canvas"]
  138.         gtk::DrawingArea {
  139.            
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement