Add an icon to represent the currently selected paintbrush

This commit is contained in:
Savanni D'Gerinel 2023-03-11 22:12:42 -05:00
parent 75c3c5c42d
commit fa8cdf4492
2 changed files with 33 additions and 5 deletions

View File

@ -44,6 +44,7 @@ fn main() {
pub struct HexGridWindowPrivate {
layout: gtk::Box,
paintbrush_icon: gtk::Box,
palette: gtk::ListBox,
drawing_area: DrawingArea,
@ -51,6 +52,7 @@ pub struct HexGridWindowPrivate {
canvas_address: labeled_field::LabeledField,
current_coordinate: Rc<RefCell<Option<AxialAddr>>>,
paintbrush: Rc<RefCell<Option<tile::Tile>>>,
}
#[glib::object_subclass]
@ -95,10 +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 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);
@ -113,6 +117,8 @@ impl ObjectSubclass for HexGridWindowPrivate {
current_coordinate,
layout,
palette,
paintbrush_icon,
paintbrush: Rc::new(RefCell::new(None)),
}
}
}
@ -150,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
@ -165,11 +177,26 @@ impl ObjectImpl for HexGridWindowPrivate {
self.palette
.append(&palette_entry::PaletteEntry::new(&swamp));
self.palette.connect_selected_rows_changed(|palette| {
let row: Option<palette_entry::PaletteEntry> =
palette.selected_row().and_then(|r| r.downcast().ok());
println!("selected rows changed to: {:?}", row.map(|r| r.terrain()));
});
{
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();
{

View File

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