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

@@ -72,10 +72,40 @@
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<child>
<placeholder/>
<object class="GtkEventBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<signal name="drag-begin" handler="drag_begin" swapped="no"/>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
<signal name="drag-begin" handler="drag_begin" swapped="no"/>
</object>
</child>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<placeholder/>
<object class="GtkEventBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<placeholder/>

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,31 +92,46 @@ 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)
});
}
}

View File

@@ -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<String>, // Content of label from CellWidget
}
/// A wrapper widget for pawns.
#[derive(Debug, Clone)]
pub struct Pawn {
data: PawnData,