Compare commits

...

2 Commits

Author SHA1 Message Date
96b6c523d7 adds comment on pawn placement 2019-06-07 15:36:11 +02:00
de02b27949 refactors. pawn placement is weird but usable 2019-06-07 15:32:32 +02:00

View File

@@ -4,6 +4,7 @@ pub struct Grid {
inner: gtk::Grid, inner: gtk::Grid,
} }
/// Position of a cell as (x,y) tuple.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct CellPosition(i32, i32); pub struct CellPosition(i32, i32);
@@ -22,6 +23,36 @@ impl Grid {
} }
} }
/// A single cell of the grid
struct Cell {
/// The inner widget
inner: CellWidget,
position: CellPosition,
}
impl Cell {
fn new(position: CellPosition) -> Self {
Cell {
inner: CellWidget::new(position, ""),
position,
}
}
}
impl AsRef<gtk::EventBox> for Cell {
#[inline]
fn as_ref(&self) -> &gtk::EventBox {
self.inner.as_ref()
}
}
/// The cell widget is either empty or has a pawn
/// Pawn can be moved around cells using drag-n-drop. However, due to my inability
/// to dynamically change the behavior with set/unset methods, all cells are always
/// drag and drop at once.
/// It is usable though, just weird. When you grab an empty cell you'll make a button appear.
/// Know that if you drop a cell on itself, it will empty itself !
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct CellWidget { struct CellWidget {
eventbox: gtk::EventBox, eventbox: gtk::EventBox,
@@ -48,45 +79,63 @@ impl CellWidget {
header, header,
desc_btn, desc_btn,
}; };
// Drag-and-drop capacity Self::set_content(&cell, None);
// The data to be sent cell.set_drag();
let targets = vec![gtk::TargetEntry::new( cell.set_drop();
"text/plain", cell
gtk::TargetFlags::SAME_APP, }
0,
)]; // The data to be sent
fn targets() -> Vec<gtk::TargetEntry> {
vec![
gtk::TargetEntry::new("text/plain", gtk::TargetFlags::SAME_APP, 0)
]
}
fn unset_drag(&self) { dbg!("Unset drag"); self.eventbox.drag_source_unset(); }
fn set_drag(&self) {
dbg!("Set drag");
// Acting as source // Acting as source
cell.eventbox.drag_source_set( self.eventbox.drag_source_set(
gdk::ModifierType::MODIFIER_MASK, gdk::ModifierType::MODIFIER_MASK,
&targets, &Self::targets(),
gdk::DragAction::MOVE, gdk::DragAction::MOVE,
); );
let h = cell.header.clone(); let c = self.clone();
// Send data to the drop site // Send data to the drop site
cell.eventbox self.eventbox
.connect_drag_data_get(move |_, _, data, info, time| { .connect_drag_data_get(move |_, _, data, info, time| {
println!("Send..."); println!("Send...");
// TODO: Refactoring, this is the inverse of 'placing', // TODO: Refactoring, this is the inverse of 'placing',
// building a PawnData instead of destructuring it. // building a PawnData instead of destructuring it.
if let Some(to_send) = h.get_text() { if let Some(to_send) = c.header.get_text() {
data.set_text(&to_send); data.set_text(&to_send);
} else {
dbg!("Should not happen !!");
} }
}); });
let c = cell.clone();
// Empty the cell on successfull move // Empty the cell on successfull move
cell.eventbox.connect_drag_data_delete(move |w, drag| { let c = self.clone();
self.eventbox.connect_drag_data_delete(move |w, drag| {
println!("Cleaning... {:#?}", (&drag.drag_drop_succeeded())); println!("Cleaning... {:#?}", (&drag.drag_drop_succeeded()));
Self::set_content(&c, None); Self::set_content(&c, None);
}); });
}
fn unset_drop(&self) { dbg!("Unset drop"); self.eventbox.drag_dest_unset(); }
fn set_drop(&self) {
dbg!("Set drop");
// Acting as destination // Acting as destination
cell.eventbox.drag_dest_set( self.eventbox.drag_dest_set(
gtk::DestDefaults::ALL, gtk::DestDefaults::ALL,
&targets, &Self::targets(),
gdk::DragAction::MOVE, gdk::DragAction::MOVE,
); );
// Retrieve data from the source // Retrieve data from the source
let c = cell.clone(); let c = self.clone();
cell.eventbox self.eventbox
.connect_drag_data_received(move |w, _, _, _, data, _, _| { .connect_drag_data_received(move |w, _, _, _, data, _, _| {
// Check if cell is not already occupied // Check if cell is not already occupied
if !(c.header.get_text().unwrap().as_str() == "") { if !(c.header.get_text().unwrap().as_str() == "") {
@@ -102,7 +151,6 @@ impl CellWidget {
} }
}; };
}); });
cell
} }
/// Updates content of the Cell /// Updates content of the Cell
@@ -138,23 +186,3 @@ impl AsRef<gtk::EventBox> for CellWidget {
} }
} }
struct Cell {
inner: CellWidget,
position: CellPosition,
}
impl Cell {
fn new(position: CellPosition) -> Self {
Cell {
inner: CellWidget::new(position, ""),
position,
}
}
}
impl AsRef<gtk::EventBox> for Cell {
#[inline]
fn as_ref(&self) -> &gtk::EventBox {
self.inner.as_ref()
}
}