Advertisement
loloof64

Gtk Chessground

Nov 10th, 2018
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.28 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) {
  38.         let result = self.position.play(move_to_play);
  39.         result
  40.     }
  41. }
  42.  
  43.  
  44. #[widget]
  45. impl Widget for Win {
  46.     fn model() -> Model {
  47.         Model ::default()
  48.     }
  49.  
  50.     fn update(&mut self, event: Msg) {
  51.         match event {
  52.             Quit => gtk::main_quit(),
  53.             PieceMoved{orig, dest, promotion} => self.handle_move(orig, dest, promotion),
  54.         }
  55.     }
  56.  
  57.     fn handle_move(&mut self, orig: Square, dest: Square, promotion: Option<Role>)
  58.     {
  59.         let legals = self.model.position.legals();
  60.         let move_to_play = legals.iter().find(|m| {
  61.             m.from() == Some(orig) && m.to() == dest &&
  62.             m.promotion() == promotion
  63.         });
  64.  
  65.         match move_to_play {
  66.             Some(move_to_play) => {
  67.                 &mut self.model.play(move_to_play);
  68.                 self.ground.emit(SetPos(Pos::new(&self.model.position)));
  69.             },
  70.             _ => {}
  71.         };
  72.     }
  73.  
  74.     view! {
  75.         gtk::Window {
  76.             title: "Chessground",
  77.             property_default_width: 600,
  78.             property_default_height: 600,
  79.             icon: &Handle::new_from_str(include_str!("wQ.svg"))
  80.                 .expect("Could not find icon !")
  81.                 .get_pixbuf()
  82.                 .expect("No pixbuf for the icon !"),
  83.             #[name="ground"]
  84.             Ground {
  85.                 UserMove(orig, dest, promotion) => PieceMoved{orig, dest, promotion},
  86.             },
  87.             delete_event(_, _) => (Quit, Inhibit(false)),
  88.         }
  89.     }
  90. }
  91.  
  92. fn main() {
  93.     Win::run(()).expect("Failed to initialize gtk !");
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement