Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate gtk;
- extern crate chessground;
- #[macro_use]
- extern crate relm;
- extern crate relm_attributes;
- #[macro_use]
- extern crate relm_derive;
- extern crate shakmaty;
- extern crate rsvg;
- use gtk::prelude::*;
- use gtk::GtkWindowExt;
- use relm::Widget;
- use relm_attributes::widget;
- use shakmaty::{Square, Chess, Role, Position, Move};
- use chessground::{Ground, UserMove, SetPos, Pos};
- use rsvg::{Handle};
- use rsvg::HandleExt;
- use self::Msg::*;
- #[derive(Msg)]
- pub enum Msg {
- Quit,
- PieceMoved{orig: Square, dest: Square, promotion: Option<Role>},
- }
- #[derive(Default)]
- pub struct Model {
- position: Chess,
- }
- impl Model
- {
- fn play(&mut self, move_to_play: &Move) -> &mut Model {
- let play_result = self.position.play(move_to_play);
- match play_result {
- Ok(position) => self.position = position,
- _ => {}
- }
- self
- }
- }
- #[widget]
- impl Widget for Win {
- fn model() -> Model {
- Model ::default()
- }
- fn update(&mut self, event: Msg) {
- match event {
- Quit => gtk::main_quit(),
- PieceMoved{orig, dest, promotion} => self.handle_move(orig, dest, promotion),
- }
- }
- fn handle_move(&mut self, orig: Square, dest: Square, promotion: Option<Role>)
- {
- let legals = self.model.position.legals();
- let move_to_play = legals.iter().find(|m| {
- m.from() == Some(orig) && m.to() == dest &&
- m.promotion() == promotion
- });
- match move_to_play {
- Some(move_to_play) => {
- &mut self.model.play(move_to_play);
- self.ground.emit(SetPos(Pos::new(&self.model.position)));
- },
- _ => {}
- };
- }
- view! {
- gtk::Window {
- title: "Chessground",
- property_default_width: 600,
- property_default_height: 600,
- icon: &Handle::new_from_str(include_str!("wQ.svg"))
- .expect("Could not find icon !")
- .get_pixbuf()
- .expect("No pixbuf for the icon !"),
- #[name="ground"]
- Ground {
- UserMove(orig, dest, promotion) => PieceMoved{orig, dest, promotion},
- },
- delete_event(_, _) => (Quit, Inhibit(false)),
- }
- }
- }
- fn main() {
- Win::run(()).expect("Failed to initialize gtk !");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement