/* Copyright 2023, Savanni D'Gerinel This file is part of the Luminescent Dreams Tools. Luminescent Dreams Tools is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Luminescent Dreams Tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Lumeto. If not, see . */ use cairo::{Context, Path}; 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 std::{cell::RefCell, io::Cursor, rc::Rc}; 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"); let app = Application::builder().application_id(APP_ID).build(); app.connect_activate(|app| { let window = HexGridWindow::new(app); window.present(); }); 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