adds basics of pawn's placement
This commit is contained in:
62
src/grid.rs
62
src/grid.rs
@@ -4,18 +4,21 @@ pub struct Grid {
|
||||
inner: gtk::Grid,
|
||||
}
|
||||
|
||||
#[derive(Debug,Copy,Clone)]
|
||||
pub struct CellPosition(i32, i32);
|
||||
|
||||
impl Grid {
|
||||
// Initialize a grid, populating its cells
|
||||
// Only square grid for now
|
||||
pub fn init(inner: gtk::Grid, size: i32) -> Self {
|
||||
/// Initialize a grid, populating its cells
|
||||
/// Only square grid for now
|
||||
pub(crate) fn init(inner: gtk::Grid, size: i32, app_state: AppState) -> Self {
|
||||
for i in 0..(size * size) {
|
||||
let x = i % size;
|
||||
let y = i / size;
|
||||
let cell = grid::Cell::new(CellPosition(x,y));
|
||||
let cell = grid::Cell::new(
|
||||
CellPosition(x,y)
|
||||
);
|
||||
cell.connect_clicked(app_state.clone());
|
||||
inner.attach(cell.as_ref(), x, y, 1, 1);
|
||||
// Signals
|
||||
}
|
||||
Grid { inner }
|
||||
}
|
||||
@@ -26,6 +29,7 @@ struct CellWidget {
|
||||
eventbox: gtk::EventBox,
|
||||
position: gtk::Label,
|
||||
header : gtk::Label,
|
||||
content: gtk::Label,
|
||||
}
|
||||
|
||||
impl CellWidget {
|
||||
@@ -68,25 +72,28 @@ impl CellWidget {
|
||||
.unwrap();
|
||||
position.set_text(&format!("{}x{}", pos.0, pos.1));
|
||||
|
||||
let header_clone = header.clone();
|
||||
eventbox.connect_button_press_event(Self::on_click(header_clone));
|
||||
|
||||
CellWidget {
|
||||
let cell = CellWidget {
|
||||
eventbox,
|
||||
position,
|
||||
header,
|
||||
}
|
||||
content: builder.get_object("content").unwrap(),
|
||||
};
|
||||
cell
|
||||
}
|
||||
|
||||
// Handler for button press events
|
||||
fn on_click(
|
||||
data: gtk::Label
|
||||
) -> impl Fn(>k::EventBox, &gdk::EventButton) -> gtk::Inhibit
|
||||
{
|
||||
move |_,_| {
|
||||
data.set_text("Clicked.");
|
||||
gtk::Inhibit(true)
|
||||
}
|
||||
pub fn connect_clicked(&self, state: AppState) {
|
||||
let header = self.header.clone();
|
||||
let content = self.content.clone();
|
||||
self.eventbox
|
||||
.connect_button_press_event(move |_,_| {
|
||||
let mut state = state.borrow_mut();
|
||||
if let Some(data) = state.pending.take() {
|
||||
println!("{:?}", data);
|
||||
content.set_text(&data.name);
|
||||
};
|
||||
header.set_text("Is Clicked.");
|
||||
gtk::Inhibit(true)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,21 +104,22 @@ impl AsRef<gtk::EventBox> for CellWidget {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Cell {
|
||||
struct Cell {
|
||||
inner: CellWidget,
|
||||
is_active: bool,
|
||||
is_occupied: bool,
|
||||
position: CellPosition,
|
||||
}
|
||||
|
||||
impl Cell {
|
||||
pub fn new(pos: CellPosition) -> Self {
|
||||
let inner = CellWidget::new(pos, "Empty cell");
|
||||
fn new(position: CellPosition) -> Self {
|
||||
Cell {
|
||||
inner,
|
||||
is_active: false,
|
||||
is_occupied: false,
|
||||
inner: CellWidget::new(position, ""),
|
||||
position
|
||||
}
|
||||
}
|
||||
|
||||
fn connect_clicked(&self, state: AppState) {
|
||||
self.inner.connect_clicked(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<gtk::EventBox> for Cell {
|
||||
|
||||
Reference in New Issue
Block a user