From 4c86ef54448e2f4306fb764336834020cd33bbbc Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 19 Feb 2023 01:37:16 -0500 Subject: [PATCH] Create a labeled field that can be styled into a stylish setup --- hex-grid/src/labeled_field.rs | 39 +++++++++++++++++++++++++++++++++++ hex-grid/src/main.rs | 24 ++++++++++++++------- 2 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 hex-grid/src/labeled_field.rs diff --git a/hex-grid/src/labeled_field.rs b/hex-grid/src/labeled_field.rs new file mode 100644 index 0000000..ba3839f --- /dev/null +++ b/hex-grid/src/labeled_field.rs @@ -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) @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(>k::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); + } +} diff --git a/hex-grid/src/main.rs b/hex-grid/src/main.rs index 7830765..b3b88b6 100644 --- a/hex-grid/src/main.rs +++ b/hex-grid/src/main.rs @@ -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; @@ -110,14 +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>>, } @@ -161,6 +162,7 @@ impl ObjectSubclass for HexGridWindowPrivate { .spacing(8) .build(); + /* let canvas_address_row = gtk::Box::builder() .hexpand(true) .spacing(8) @@ -178,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) @@ -196,6 +203,7 @@ impl ObjectSubclass for HexGridWindowPrivate { .build(); hex_address_row.append(&hex_address); + */ let palette = gtk::Box::builder() .spacing(8) @@ -203,8 +211,8 @@ impl ObjectSubclass for HexGridWindowPrivate { .hexpand(true) .build(); - sidebar.append(&canvas_address_row); - sidebar.append(&hex_address_row); + sidebar.append(&canvas_address); + sidebar.append(&hex_address); sidebar.append(&palette); layout.append(&drawing_area); @@ -283,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); } });