use crate::prelude::*; use anyhow::Result; use assetpath::AssetPath; use super::validator::buttoninfo::{NeighbourDirection, NeighbourInfo}; use super::validator::gridinfo::GridInfo; use super::validator::uiinfoelement::UiInfoElement; use super::validator::validator::{handle_function_suffix, Validator}; use std::any::Any; use std::collections::HashMap; use std::sync::{Arc, RwLock}; pub struct GuiBuilder { grids: Vec>, default_select: Option>, ids: HashMap, decline_callback: RwLock Result<()> + Send + Sync>>>, } impl GuiBuilder { pub fn new(gui_handler: &Arc, path: &AssetPath) -> Result> { let validator = Validator::new(gui_handler, path)?; Self::_new(gui_handler, validator) } pub fn from_str(gui_handler: &Arc, s: &str) -> Result> { let validator = Validator::from_str(gui_handler, s)?; Self::_new(gui_handler, validator) } #[inline] fn _new(gui_handler: &Arc, validator: Validator) -> Result> { let root = validator.root(); let mut ids = HashMap::new(); let mut opt_default_select = None; let mut custom_neighbours = Vec::new(); let mut grids = Vec::new(); for child in &root.children { let tree = Self::create_tree( gui_handler, &mut ids, child, &mut opt_default_select, &mut custom_neighbours, validator.dimensions(), )?; if let Some(layer) = validator.layer() { tree.set_layer(layer)?; } grids.push(tree); } Self::connect_custom_neighbours(&ids, custom_neighbours)?; Ok(Arc::new(GuiBuilder { grids, default_select: opt_default_select, ids, decline_callback: RwLock::new(None), })) } pub(crate) fn connect_custom_neighbours( ids: &HashMap, neighbour_infos: Vec<(String, Vec)>, ) -> Result<()> { for (id, neighbour_infos) in neighbour_infos.iter() { let button: Arc