From 226329ab6d4231f5cb9f59020f46ffb01ac44126 Mon Sep 17 00:00:00 2001 From: savanni Date: Wed, 1 Mar 2023 14:28:38 +0000 Subject: [PATCH] Convert glade components to Rust and add the palette (#26) Co-authored-by: Savanni D'Gerinel Reviewed-on: https://git.luminescent-dreams.com/savanni/tools/pulls/26 --- hex-grid/resources/main.glade | 99 ------- hex-grid/resources/resources.gresources.xml | 1 - hex-grid/src/labeled_field.rs | 39 +++ hex-grid/src/main.rs | 288 ++++++++------------ hex-grid/src/palette_entry.rs | 52 ++++ hex-grid/src/tile.rs | 108 ++++++++ hex-grid/src/utilities.rs | 46 ++++ 7 files changed, 356 insertions(+), 277 deletions(-) delete mode 100644 hex-grid/resources/main.glade create mode 100644 hex-grid/src/labeled_field.rs create mode 100644 hex-grid/src/palette_entry.rs create mode 100644 hex-grid/src/tile.rs create mode 100644 hex-grid/src/utilities.rs diff --git a/hex-grid/resources/main.glade b/hex-grid/resources/main.glade deleted file mode 100644 index 1b7442b..0000000 --- a/hex-grid/resources/main.glade +++ /dev/null @@ -1,99 +0,0 @@ - - - - diff --git a/hex-grid/resources/resources.gresources.xml b/hex-grid/resources/resources.gresources.xml index 30f14bd..cb89341 100644 --- a/hex-grid/resources/resources.gresources.xml +++ b/hex-grid/resources/resources.gresources.xml @@ -1,7 +1,6 @@ - main.glade terrain.ppm map.txt 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 7d62cc2..61f0b54 100644 --- a/hex-grid/src/main.rs +++ b/hex-grid/src/main.rs @@ -10,92 +10,24 @@ Luminescent Dreams Tools is distributed in the hope that it will be useful, but You should have received a copy of the GNU General Public License along with Lumeto. If not, see . */ -use cairo::{Context, Path}; +use cairo::Context; use coordinates::{hex_map::parse_data, AxialAddr}; use gio::resources_lookup_data; use glib::{subclass::InitializingObject, Object}; -use gtk::{ - gdk_pixbuf::Pixbuf, gio, prelude::*, subclass::prelude::*, Application, CompositeTemplate, - DrawingArea, Label, -}; -use image::{io::Reader as ImageReader, DynamicImage}; +use gtk::{gio, prelude::*, subclass::prelude::*, Application, DrawingArea}; +use image::io::Reader as ImageReader; use std::{cell::RefCell, io::Cursor, rc::Rc}; +mod labeled_field; +mod palette_entry; +mod tile; +mod utilities; + const APP_ID: &'static str = "com.luminescent-dreams.hex-grid"; const HEX_RADIUS: f64 = 50.; const MAP_RADIUS: usize = 3; const DRAWING_ORIGIN: (f64, f64) = (1024. / 2., 768. / 2.); -#[derive(Clone, Debug)] -enum Terrain { - Empty, - Mountain, - Grasslands, - ShallowWater, - DeepWater, - Badlands, - Desert, - Swamp, -} - -impl Default for Terrain { - fn default() -> Self { - Self::Empty - } -} - -impl From<&str> for Terrain { - fn from(s: &str) -> Self { - match s { - "m" => Self::Mountain, - "g" => Self::Grasslands, - "sw" => Self::ShallowWater, - "dw" => Self::DeepWater, - "b" => Self::Badlands, - "d" => Self::Desert, - "s" => Self::Swamp, - _ => Self::Empty, - } - } -} - -impl From for Terrain { - fn from(s: String) -> Self { - Self::from(s.as_ref()) - } -} - -struct Tile { - terrain: Terrain, - image: Pixbuf, -} - -impl Tile { - fn new(source: &DynamicImage, terrain: Terrain) -> Tile { - let image = match terrain { - Terrain::DeepWater => pixbuf_from_image_tile(source.clone().crop(0, 0, 100, 88)), - Terrain::ShallowWater => pixbuf_from_image_tile(source.clone().crop(100, 0, 100, 88)), - Terrain::Grasslands => pixbuf_from_image_tile(source.clone().crop(200, 0, 100, 88)), - Terrain::Desert => pixbuf_from_image_tile(source.clone().crop(300, 0, 100, 88)), - Terrain::Mountain => pixbuf_from_image_tile(source.clone().crop(0, 88, 100, 88)), - Terrain::Badlands => pixbuf_from_image_tile(source.clone().crop(100, 88, 100, 88)), - Terrain::Swamp => pixbuf_from_image_tile(source.clone().crop(0, 176, 100, 88)), - Terrain::Empty => pixbuf_from_image_tile(source.clone().crop(300, 176, 100, 88)), - }; - - Tile { terrain, image } - } - - fn render_on_context(&self, context: &Context, translate_x: f64, translate_y: f64) { - context.save().unwrap(); - context.append_path(&hexagon_path(context, translate_x, translate_y, 100., 88.)); - context.clip(); - context.set_source_pixbuf(&self.image, translate_x, translate_y); - context.paint().expect("paint should succeed"); - context.restore().unwrap(); - } -} - fn main() { gio::resources_register_include!("com.luminescent-dreams.hex-grid.gresource") .expect("Failed to register resources"); @@ -110,15 +42,13 @@ fn main() { app.run(); } -#[derive(CompositeTemplate, Default)] -#[template(resource = "/com/luminescent-dreams/hex-grid/main.glade")] pub struct HexGridWindowPrivate { - #[template_child] - pub drawing_area: TemplateChild, - #[template_child] - pub hex_address: TemplateChild