Create a labeled field that can be styled into a stylish setup

This commit is contained in:
Savanni D'Gerinel 2023-02-19 01:37:16 -05:00
parent 85b80d78ff
commit 4c86ef5444
2 changed files with 55 additions and 8 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 image::{io::Reader as ImageReader, DynamicImage};
use std::{cell::RefCell, io::Cursor, rc::Rc}; use std::{cell::RefCell, io::Cursor, rc::Rc};
mod labeled_field;
const APP_ID: &'static str = "com.luminescent-dreams.hex-grid"; const APP_ID: &'static str = "com.luminescent-dreams.hex-grid";
const HEX_RADIUS: f64 = 50.; const HEX_RADIUS: f64 = 50.;
const MAP_RADIUS: usize = 3; const MAP_RADIUS: usize = 3;
@ -110,14 +112,13 @@ fn main() {
app.run(); app.run();
} }
#[derive(Default)]
pub struct HexGridWindowPrivate { pub struct HexGridWindowPrivate {
layout: gtk::Box, layout: gtk::Box,
palette: gtk::Box, palette: gtk::Box,
drawing_area: DrawingArea, drawing_area: DrawingArea,
hex_address: Label, hex_address: labeled_field::LabeledField,
canvas_address: Label, canvas_address: labeled_field::LabeledField,
current_coordinate: Rc<RefCell<Option<AxialAddr>>>, current_coordinate: Rc<RefCell<Option<AxialAddr>>>,
} }
@ -161,6 +162,7 @@ impl ObjectSubclass for HexGridWindowPrivate {
.spacing(8) .spacing(8)
.build(); .build();
/*
let canvas_address_row = gtk::Box::builder() let canvas_address_row = gtk::Box::builder()
.hexpand(true) .hexpand(true)
.spacing(8) .spacing(8)
@ -178,7 +180,12 @@ impl ObjectSubclass for HexGridWindowPrivate {
.build(); .build();
canvas_address_row.append(&canvas_address); 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() let hex_address_row = gtk::Box::builder()
.hexpand(true) .hexpand(true)
.spacing(8) .spacing(8)
@ -196,6 +203,7 @@ impl ObjectSubclass for HexGridWindowPrivate {
.build(); .build();
hex_address_row.append(&hex_address); hex_address_row.append(&hex_address);
*/
let palette = gtk::Box::builder() let palette = gtk::Box::builder()
.spacing(8) .spacing(8)
@ -203,8 +211,8 @@ impl ObjectSubclass for HexGridWindowPrivate {
.hexpand(true) .hexpand(true)
.build(); .build();
sidebar.append(&canvas_address_row); sidebar.append(&canvas_address);
sidebar.append(&hex_address_row); sidebar.append(&hex_address);
sidebar.append(&palette); sidebar.append(&palette);
layout.append(&drawing_area); layout.append(&drawing_area);
@ -283,13 +291,13 @@ impl ObjectImpl for HexGridWindowPrivate {
let (q, r) = axial_round(q, r); let (q, r) = axial_round(q, r);
let coordinate = AxialAddr::new(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 { if coordinate.distance(&AxialAddr::origin()) > MAP_RADIUS {
hex_address.set_label(&format!("-----")); hex_address.set_value(&format!("-----"));
*c.borrow_mut() = None; *c.borrow_mut() = None;
} else { } 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); *c.borrow_mut() = Some(coordinate);
} }
}); });