pits and exits

This commit is contained in:
2019-06-05 22:01:24 +02:00
parent ad9cad533f
commit b0cfa3f9aa
3 changed files with 84 additions and 22 deletions

View File

@@ -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<gtk::EventBox> for CellWidget {