Compare commits

...

4 Commits

4 changed files with 46 additions and 14 deletions

View File

@ -1,8 +0,0 @@
use gtk::prelude::*;
fn main() {
gtk::init();
for name in gtk::IconTheme::new().icon_names() {
println!("{}", name);
}
}

View File

@ -44,13 +44,15 @@ fn main() {
pub struct HexGridWindowPrivate {
layout: gtk::Box,
palette: gtk::Box,
paintbrush_icon: gtk::Box,
palette: gtk::ListBox,
drawing_area: DrawingArea,
hex_address: labeled_field::LabeledField,
canvas_address: labeled_field::LabeledField,
current_coordinate: Rc<RefCell<Option<AxialAddr>>>,
paintbrush: Rc<RefCell<Option<tile::Tile>>>,
}
#[glib::object_subclass]
@ -95,14 +97,12 @@ impl ObjectSubclass for HexGridWindowPrivate {
let canvas_address = labeled_field::LabeledField::new("Canvas Address", "-----");
let hex_address = labeled_field::LabeledField::new("Hex Address", "-----");
let palette = gtk::Box::builder()
.spacing(8)
.orientation(gtk::Orientation::Vertical)
.hexpand(true)
.build();
let paintbrush_icon = gtk::Box::new(gtk::Orientation::Horizontal, 0);
let palette = gtk::ListBox::builder().hexpand(true).build();
sidebar.append(&canvas_address);
sidebar.append(&hex_address);
sidebar.append(&paintbrush_icon);
sidebar.append(&palette);
layout.append(&drawing_area);
@ -117,6 +117,8 @@ impl ObjectSubclass for HexGridWindowPrivate {
current_coordinate,
layout,
palette,
paintbrush_icon,
paintbrush: Rc::new(RefCell::new(None)),
}
}
}
@ -154,6 +156,12 @@ impl ObjectImpl for HexGridWindowPrivate {
let shallow_water = tile::Tile::new(&image, tile::Terrain::ShallowWater);
let swamp = tile::Tile::new(&image, tile::Terrain::Swamp);
let paintbrush_image = gtk::Image::from_pixbuf(Some(&grasslands.image));
paintbrush_image.set_width_request(100);
paintbrush_image.set_height_request(88);
self.paintbrush_icon.append(&paintbrush_image);
*self.paintbrush.borrow_mut() = Some(grasslands.clone());
self.palette
.append(&palette_entry::PaletteEntry::new(&badlands));
self.palette
@ -169,6 +177,27 @@ impl ObjectImpl for HexGridWindowPrivate {
self.palette
.append(&palette_entry::PaletteEntry::new(&swamp));
{
let paintbrush = self.paintbrush.clone();
let image = image.clone();
let paintbrush_icon = self.paintbrush_icon.clone();
self.palette.connect_selected_rows_changed(move |palette| {
let row: Option<palette_entry::PaletteEntry> =
palette.selected_row().and_then(|r| r.downcast().ok());
if let Some(row) = row {
let new_tile = tile::Tile::new(&image, row.terrain());
*paintbrush.borrow_mut() = Some(new_tile.clone());
while let Some(icon) = paintbrush_icon.first_child() {
paintbrush_icon.remove(&icon);
}
let image = gtk::Image::from_pixbuf(Some(&new_tile.image));
image.set_width_request(100);
image.set_height_request(88);
paintbrush_icon.append(&image);
}
});
}
let motion_controller = gtk::EventControllerMotion::new();
{
let canvas_address = self.canvas_address.clone();
@ -228,7 +257,13 @@ impl ObjectImpl for HexGridWindowPrivate {
}
});
}
let click_controller = gtk::GestureClick::new();
click_controller.connect_released(|_, press, x, y| {
println!("key pressed: {} {} {}", press, x, y);
});
self.drawing_area.add_controller(&motion_controller);
self.drawing_area.add_controller(&click_controller);
}
}

View File

@ -49,4 +49,8 @@ impl PaletteEntry {
row
}
pub fn terrain(&self) -> tile::Terrain {
self.imp().terrain.borrow().clone()
}
}

View File

@ -58,6 +58,7 @@ impl From<&Terrain> for String {
}
}
#[derive(Clone)]
pub struct Tile {
pub terrain: Terrain,
pub image: Pixbuf,