Advertisement
loloof64

Untitled

Nov 10th, 2018
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.41 KB | None | 0 0
  1. extern crate gtk;
  2. extern crate chessground;
  3. #[macro_use]
  4. extern crate relm;
  5. extern crate relm_attributes;
  6. #[macro_use]
  7. extern crate relm_derive;
  8. extern crate shakmaty;
  9. extern crate rsvg;
  10.  
  11. use gtk::prelude::*;
  12. use gtk::GtkWindowExt;
  13. use relm::Widget;
  14. use relm_attributes::widget;
  15.  
  16. use shakmaty::{Square, Chess, Role, Position, Move};
  17. use chessground::{Ground, UserMove, SetPos, Pos};
  18.  
  19. use rsvg::{Handle};
  20. use rsvg::HandleExt;
  21.  
  22. use self::Msg::*;
  23.  
  24. #[derive(Msg)]
  25. pub enum Msg {
  26.     Quit,
  27.     PieceMoved{orig: Square, dest: Square, promotion: Option<Role>},
  28. }
  29.  
  30. #[derive(Default)]
  31. pub struct Model {
  32.     position: Chess,
  33. }
  34.  
  35. impl Model
  36. {
  37.     fn play(&mut self, move_to_play: &Move) -> &mut Model {
  38.         let play_result = self.position.play(move_to_play);
  39.         match play_result {
  40.             Ok(position) => self.position = position,
  41.             _ => {}
  42.         }
  43.         self
  44.     }
  45. }
  46.  
  47.  
  48. #[widget]
  49. impl Widget for Win {
  50.     fn model() -> Model {
  51.         Model ::default()
  52.     }
  53.  
  54.     fn update(&mut self, event: Msg) {
  55.         match event {
  56.             Quit => gtk::main_quit(),
  57.             PieceMoved{orig, dest, promotion} => self.handle_move(orig, dest, promotion),
  58.         }
  59.     }
  60.  
  61.     fn handle_move(&mut self, orig: Square, dest: Square, promotion: Option<Role>)
  62.     {
  63.         let legals = self.model.position.legals();
  64.         let move_to_play = legals.iter().find(|m| {
  65.             m.from() == Some(orig) && m.to() == dest &&
  66.             m.promotion() == promotion
  67.         });
  68.  
  69.         match move_to_play {
  70.             Some(move_to_play) => {
  71.                 &mut self.model.play(move_to_play);
  72.                 self.ground.emit(SetPos(Pos::new(&self.model.position)));
  73.             },
  74.             _ => {}
  75.         };
  76.     }
  77.  
  78.     view! {
  79.         gtk::Window {
  80.             title: "Chessground",
  81.             property_default_width: 600,
  82.             property_default_height: 600,
  83.             icon: &Handle::new_from_str(include_str!("wQ.svg"))
  84.                 .expect("Could not find icon !")
  85.                 .get_pixbuf()
  86.                 .expect("No pixbuf for the icon !"),
  87.             #[name="ground"]
  88.             Ground {
  89.                 UserMove(orig, dest, promotion) => PieceMoved{orig, dest, promotion},
  90.             },
  91.             delete_event(_, _) => (Quit, Inhibit(false)),
  92.         }
  93.     }
  94. }
  95.  
  96. fn main() {
  97.     Win::run(()).expect("Failed to initialize gtk !");
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement