Compare commits

...

2 Commits

2 changed files with 91 additions and 18 deletions

View File

@ -0,0 +1,39 @@
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
#[derive(Default)]
pub struct LabeledFieldPrivate {
value: gtk::Label,
}
#[glib::object_subclass]
impl ObjectSubclass for LabeledFieldPrivate {
const NAME: &'static str = "LabeledField";
type Type = LabeledField;
type ParentType = gtk::Box;
}
impl ObjectImpl for LabeledFieldPrivate {}
impl WidgetImpl for LabeledFieldPrivate {}
impl BoxImpl for LabeledFieldPrivate {}
glib::wrapper! {
pub struct LabeledField(ObjectSubclass<LabeledFieldPrivate>) @extends gtk::Box, gtk::Widget;
}
impl LabeledField {
pub fn new(label: &str, default_value: &str) -> Self {
let field: Self = Object::builder().build();
field.set_hexpand(true);
field.set_spacing(8);
field.set_homogeneous(true);
field.append(&gtk::Label::new(Some(label)));
field.append(&field.imp().value);
field.imp().value.set_label(default_value);
field
}
pub fn set_value(&self, value: &str) {
self.imp().value.set_label(value);
}
}

View File

@ -21,6 +21,8 @@ use gtk::{
use image::{io::Reader as ImageReader, DynamicImage};
use std::{cell::RefCell, io::Cursor, rc::Rc};
mod labeled_field;
const APP_ID: &'static str = "com.luminescent-dreams.hex-grid";
const HEX_RADIUS: f64 = 50.;
const MAP_RADIUS: usize = 3;
@ -28,13 +30,13 @@ const DRAWING_ORIGIN: (f64, f64) = (1024. / 2., 768. / 2.);
#[derive(Clone, Debug)]
enum Terrain {
Empty,
Mountain,
Grasslands,
ShallowWater,
DeepWater,
Badlands,
DeepWater,
Desert,
Empty,
Grasslands,
Mountain,
ShallowWater,
Swamp,
}
@ -110,13 +112,13 @@ fn main() {
app.run();
}
#[derive(Default)]
pub struct HexGridWindowPrivate {
layout: gtk::Box,
palette: gtk::Box,
drawing_area: DrawingArea,
hex_address: Label,
canvas_address: Label,
hex_address: labeled_field::LabeledField,
canvas_address: labeled_field::LabeledField,
current_coordinate: Rc<RefCell<Option<AxialAddr>>>,
}
@ -128,6 +130,7 @@ impl ObjectSubclass for HexGridWindowPrivate {
type ParentType = gtk::ApplicationWindow;
fn new() -> Self {
println!("hexGridWindowPrivate::new()");
let current_coordinate = Rc::new(RefCell::new(None));
let drawing_area = DrawingArea::builder()
@ -159,6 +162,7 @@ impl ObjectSubclass for HexGridWindowPrivate {
.spacing(8)
.build();
/*
let canvas_address_row = gtk::Box::builder()
.hexpand(true)
.spacing(8)
@ -176,7 +180,12 @@ impl ObjectSubclass for HexGridWindowPrivate {
.build();
canvas_address_row.append(&canvas_address);
*/
let canvas_address = labeled_field::LabeledField::new("Canvas Address", "-----");
let hex_address = labeled_field::LabeledField::new("Hex Address", "-----");
/*
let hex_address_row = gtk::Box::builder()
.hexpand(true)
.spacing(8)
@ -194,9 +203,17 @@ impl ObjectSubclass for HexGridWindowPrivate {
.build();
hex_address_row.append(&hex_address);
*/
sidebar.append(&canvas_address_row);
sidebar.append(&hex_address_row);
let palette = gtk::Box::builder()
.spacing(8)
.orientation(gtk::Orientation::Vertical)
.hexpand(true)
.build();
sidebar.append(&canvas_address);
sidebar.append(&hex_address);
sidebar.append(&palette);
layout.append(&drawing_area);
layout.append(&sidebar);
@ -209,12 +226,14 @@ impl ObjectSubclass for HexGridWindowPrivate {
canvas_address,
current_coordinate,
layout,
palette,
}
}
}
impl ObjectImpl for HexGridWindowPrivate {
fn constructed(&self) {
println!("HexGridWindowPrivate::constructed()");
self.parent_constructed();
let map_text_resource = resources_lookup_data(
@ -236,14 +255,29 @@ impl ObjectImpl for HexGridWindowPrivate {
.unwrap();
let image = reader.decode().unwrap();
let deep_water = Tile::new(&image, Terrain::DeepWater);
let shallow_water = Tile::new(&image, Terrain::ShallowWater);
let grasslands = Tile::new(&image, Terrain::Grasslands);
let desert = Tile::new(&image, Terrain::Desert);
let mountain = Tile::new(&image, Terrain::Mountain);
let badlands = Tile::new(&image, Terrain::Badlands);
let deep_water = Tile::new(&image, Terrain::DeepWater);
let desert = Tile::new(&image, Terrain::Desert);
let grasslands = Tile::new(&image, Terrain::Grasslands);
let mountain = Tile::new(&image, Terrain::Mountain);
let shallow_water = Tile::new(&image, Terrain::ShallowWater);
let swamp = Tile::new(&image, Terrain::Swamp);
let badlands_image = gtk::Image::from_pixbuf(Some(&badlands.image));
self.palette.append(&badlands_image);
self.palette
.append(&gtk::Image::from_pixbuf(Some(&deep_water.image)));
self.palette
.append(&gtk::Image::from_pixbuf(Some(&desert.image)));
self.palette
.append(&gtk::Image::from_pixbuf(Some(&grasslands.image)));
self.palette
.append(&gtk::Image::from_pixbuf(Some(&mountain.image)));
self.palette
.append(&gtk::Image::from_pixbuf(Some(&shallow_water.image)));
self.palette
.append(&gtk::Image::from_pixbuf(Some(&swamp.image)));
let motion_controller = gtk::EventControllerMotion::new();
{
let canvas_address = self.canvas_address.clone();
@ -257,13 +291,13 @@ impl ObjectImpl for HexGridWindowPrivate {
let (q, r) = axial_round(q, r);
let coordinate = AxialAddr::new(q, r);
canvas_address.set_label(&format!("{:.0} {:.0}", x, y));
canvas_address.set_value(&format!("{:.0} {:.0}", x, y));
if coordinate.distance(&AxialAddr::origin()) > MAP_RADIUS {
hex_address.set_label(&format!("-----"));
hex_address.set_value(&format!("-----"));
*c.borrow_mut() = None;
} else {
hex_address.set_label(&format!("{:.0} {:.0}", coordinate.q(), coordinate.r()));
hex_address.set_value(&format!("{:.0} {:.0}", coordinate.q(), coordinate.r()));
*c.borrow_mut() = Some(coordinate);
}
});