first steps

This commit is contained in:
2019-06-03 14:59:52 +02:00
commit ca3696f63f
6 changed files with 443 additions and 0 deletions

104
src/grid.rs Normal file
View File

@@ -0,0 +1,104 @@
use crate::*;
pub struct Grid {
inner: gtk::Grid,
}
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 {
for i in 0..(size * size) {
let x = i % size;
let y = i / size;
let cell = grid::Cell::new(CellPosition(x,y));
inner.attach(cell.as_ref(), x, y, 1, 1);
// Signals
}
Grid { inner }
}
}
struct CellWidget {
eventbox: gtk::EventBox,
position: gtk::Label,
header : gtk::Label,
}
impl CellWidget {
fn new(pos: CellPosition, header_text: &str) -> Self {
let cell_src = include_str!("../res/cell.glade");
let builder = gtk::Builder::new_from_string(&cell_src);
let eventbox: gtk::EventBox =
builder
.get_object("cell")
.unwrap();
eventbox.set_events(gdk::EventMask::BUTTON_PRESS_MASK);
let header: gtk::Label =
builder
.get_object("header")
.unwrap();
header.set_text(header_text);
let position: gtk::Label =
builder
.get_object("position")
.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 {
eventbox,
position,
header,
}
}
// Handler for button press events
fn on_click(
data: gtk::Label
) -> impl Fn(&gtk::EventBox, &gdk::EventButton) -> gtk::Inhibit
{
move |_,_| {
data.set_text("Clicked.");
gtk::Inhibit(true)
}
}
}
impl AsRef<gtk::EventBox> for CellWidget {
#[inline]
fn as_ref(&self) -> &gtk::EventBox {
&self.eventbox
}
}
pub struct Cell {
inner: CellWidget,
is_active: bool,
is_occupied: bool,
}
impl Cell {
pub fn new(pos: CellPosition) -> Self {
let inner = CellWidget::new(pos, "Empty cell");
Cell {
inner,
is_active: false,
is_occupied: false,
}
}
}
impl AsRef<gtk::EventBox> for Cell {
#[inline]
fn as_ref(&self) -> &gtk::EventBox {
self.inner.as_ref()
}
}