refactors cell content updating

This commit is contained in:
2019-06-06 21:28:02 +02:00
parent c67c4145a7
commit 1223167405
2 changed files with 39 additions and 36 deletions

View File

@@ -18,7 +18,6 @@ impl Grid {
CellPosition(x,y)
);
cell.inner.connect_clicked(app_state.clone());
cell.inner.desc_btn.set_visible(false);
inner.attach(cell.as_ref(), x, y, 1, 1);
}
Grid { inner }
@@ -86,8 +85,7 @@ impl CellWidget {
eventbox.connect_drag_data_delete(
move |w,drag| {
println!("Cleaning... {:#?}", (&drag.drag_drop_succeeded()));
h.set_text("");
b.set_visible(false);
Self::set_content((&h,&b), None);
}
);
@@ -100,23 +98,19 @@ impl CellWidget {
let h = header.clone();
let b = desc_btn.clone();
eventbox.connect_drag_data_received(
move |_,drag,_,_,data,info,time| {
let del =
move |w,_,_,_,data,_,_| {
// Check if cell is not already occupied
if !(h.get_text().unwrap().as_str() == "") {
// TODO: Find what to do to abort, since
// DragContext methods don't seem to work...
println!("Overriding !!");
false
} else {
println!("Dropped !");
if let Some(text) = data.get_text() {
h.set_text(&text);
b.set_visible(true);
Self::set_content((&h,&b), Some(text.as_str()));
} else {
eprintln!("No data !");
}
true
};
}
);
@@ -129,14 +123,28 @@ impl CellWidget {
cell
}
/// Updates content of the Cell
fn set_content(widgets: (&gtk::Label, &gtk::Button), data: Option<&str>) {
println!("Set content to {:?}", &data);
// TODO: there is surely something cleaner...
if let Some(name) = data {
widgets.0.set_text(name);
widgets.1.set_visible(true);
} else {
widgets.0.set_text("");
widgets.1.set_visible(false);
};
}
pub fn connect_clicked(&self, state: AppState) {
let header = self.header.clone();
let btn = self.desc_btn.clone();
self.eventbox
.connect_button_press_event(move |_,_| {
let mut state = state.borrow_mut();
if let Some(data) = state.pending.take() {
if let Some(ref data) = state.pending.take() {
println!("{:?}", data);
header.set_text(&data.name);
Self::set_content((&header,&btn), Some(&data.name));
};
gtk::Inhibit(true)
});

View File

@@ -68,7 +68,6 @@ impl App {
.get_object("app")
.unwrap();
win.set_application(app);
// Set up a simple switch for the Pawn list panel
let panel: gtk::Paned = builder.get_object("panel").unwrap();
Self::new_action(app, "panel_switch", move |_,_| {
@@ -78,18 +77,7 @@ impl App {
panel.set_position(0);
}
});
// Initialize grid
let grid: gtk::Grid = builder.get_object("map").unwrap();
// TODO: implement drag-drop events
// * From pawn_list to cell : place pawn on dest cell
// * From cell to cell : move pawn from source to dest cell
let _grid = grid::Grid::init(grid, 10, app_state.clone());
// TODO:
// * Display creature description when hovering over its position (cell)
// TODO: Pawn list
// Pawn list
let pawn_list = builder.get_object("pawn_list").unwrap();
let pawn_list = pawn::PawnList::init(pawn_list);
@@ -99,7 +87,14 @@ impl App {
pawn.connect_stats();
app_state.borrow_mut().pawns.push(pawn);
}
win.show_all();
win.show_all(); // Before grid because we want to hide some widgets there
// Initialize grid
let grid: gtk::Grid = builder.get_object("map").unwrap();
// TODO: implement drag-drop events
// * From pawn_list to cell : place pawn on dest cell
// * From cell to cell : move pawn from source to dest cell
let _grid = grid::Grid::init(grid, 10, app_state.clone());
}
);
}