From b0cfa3f9aa855dfb3fb95cf2e252748af3712dba Mon Sep 17 00:00:00 2001 From: Artus Date: Wed, 5 Jun 2019 22:01:24 +0200 Subject: [PATCH] pits and exits --- res/main.glade | 34 +++++++++++++++++++++++-- src/grid.rs | 67 +++++++++++++++++++++++++++++++++++--------------- src/pawn.rs | 5 ++++ 3 files changed, 84 insertions(+), 22 deletions(-) diff --git a/res/main.glade b/res/main.glade index 9e33240..594b2a3 100644 --- a/res/main.glade +++ b/res/main.glade @@ -72,10 +72,40 @@ True True - + + True + False + + + + True + False + label + + + + + + 1 + 0 + - + + True + False + + + True + False + label + + + + + 1 + 1 + diff --git a/src/grid.rs b/src/grid.rs index 6e86a90..c9d20e1 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -18,7 +18,7 @@ impl Grid { CellPosition(x,y) ); cell.inner.connect_clicked(app_state.clone()); - cell.inner.connect_drag_drop(app_state.clone()); + cell.inner.connect_dragging(app_state.clone()); inner.attach(cell.as_ref(), x, y, 1, 1); } Grid { inner } @@ -43,7 +43,6 @@ impl CellWidget { builder .get_object("cell") .unwrap(); - eventbox.set_events(gdk::EventMask::BUTTON_PRESS_MASK); let header: gtk::Label = builder @@ -55,6 +54,19 @@ impl CellWidget { .get_object("position") .unwrap(); position.set_text(&format!("{}x{}", pos.0, pos.1)); + // Drag-and-drop capacity + let targets = vec![]; + eventbox.drag_source_set( + gdk::ModifierType::MODIFIER_MASK, + &targets, + gdk::DragAction::COPY, + ); + eventbox.drag_dest_set( + gtk::DestDefaults::ALL, + &targets, + gdk::DragAction::COPY, + ); + let cell = CellWidget { eventbox, @@ -80,32 +92,47 @@ impl CellWidget { }); } - pub fn connect_drag_drop(&self, _state: AppState) { - // Drag-and-drop capacity - let targets = vec![]; - self.eventbox.drag_dest_set( - gtk::DestDefaults::ALL, - &targets, - gdk::DragAction::COPY, - ); - self.eventbox.drag_source_set( - gdk::ModifierType::MODIFIER_MASK, - &targets, - gdk::DragAction::COPY, - ); + pub fn connect_dragging(&self, state: AppState) { - //let app_state = state.clone(); - self.eventbox.connect_drag_begin(|_,_| { + // TODO: start over. + // Use this for reference : + // https://python-gtk-3-tutorial.readthedocs.io/en/latest/drag_and_drop.html + + // For starters, avoid using app_state and just copy the content of + // source cell inside dest cell. + + let state1 = state.clone(); + self.eventbox.connect_drag_begin(move |_,_| { println!("Lift..."); + let mut state = state1.borrow_mut(); + state.pending = Some(pawn::PawnData{name: "Dropped".to_string(), position: None}); // Copy the current instance data // inside app_state.pending }); - self.eventbox.connect_drag_end(|_,_| { + // TODO: Add failed signal to remove data + let state2 = state.clone(); + let content = self.content.clone(); + self.eventbox.connect_drag_end(move |_,_| { println!("Drop !"); + let mut state = state2.borrow_mut(); + if state.pending.is_none() { + content.set_text("Emptied"); + } + state.pending = None; // Same as clicked, grab data }); - // TODO: Add failed signal to remove data - } + let state3 = state.clone(); + let content = self.content.clone(); + self.eventbox.connect_drag_drop(move |_,drag_ctx,_,_,time| { + println!("Got ! {:?}", ""); + let mut state = state3.borrow_mut(); + if let Some(data) = state.pending.take() { + content.set_text(&data.name); + }; + drag_ctx.drag_finish(true, true, time); + gtk::Inhibit(true) + }); + } } impl AsRef for CellWidget { diff --git a/src/pawn.rs b/src/pawn.rs index f500795..84d3347 100644 --- a/src/pawn.rs +++ b/src/pawn.rs @@ -1,16 +1,19 @@ use super::*; +/// A wrapper around gtk::ListBox for Pawns #[derive(Debug,Clone)] pub struct PawnList { inner: gtk::ListBox, } impl PawnList { + /// Initialize from the given existing ListBox pub fn init(inner: gtk::ListBox) -> Self { PawnList{ inner } } + /// Adds a Pawn to the list pub fn add(&self, pawn: &Pawn) { let row = gtk::ListBoxRow::new(); row.add(pawn.as_ref()); @@ -18,12 +21,14 @@ impl PawnList { } } +/// Application data related to a Pawn #[derive(Debug,Clone)] pub struct PawnData { pub name: String, pub position: Option, // Content of label from CellWidget } +/// A wrapper widget for pawns. #[derive(Debug, Clone)] pub struct Pawn { data: PawnData,