use super::uiinfoelement::UiInfoElement; use crate::prelude::*; use anyhow::Result; use assetpath::AssetPath; use std::str::from_utf8; use std::sync::{RwLock, Weak}; use super::mandatory::Mandatory; use super::validator::{ cow_to_fill_type_info, cow_to_path, cow_to_str, str_into, str_to_hori_align, str_to_vert_align, }; pub struct GridInfo { pub id: String, pub x_slot: Mandatory, pub y_slot: Mandatory, pub x_dimension: Mandatory, pub y_dimension: Mandatory, pub x_dim: u32, pub y_dim: u32, pub x_offset: Mandatory, pub y_offset: Mandatory, pub width: Mandatory, pub height: Mandatory, pub padding: u32, pub margin: u32, pub vertical_alignment: Mandatory, pub horizontal_alignment: Mandatory, pub background_type: Option, pub button_normal: Option, pub button_selected: Option, pub click_sound: Option, pub hover_sound: Option, pub children: RwLock>, pub parent: Option>, pub layer: Option, } impl GridInfo { pub fn new<'a>(attributes: quick_xml::events::attributes::Attributes<'a>) -> Result { let mut grid_info = GridInfo { id: String::new(), x_slot: Mandatory::default(), y_slot: Mandatory::default(), x_dimension: Mandatory::default(), y_dimension: Mandatory::default(), x_dim: 1, y_dim: 1, x_offset: Mandatory::default(), y_offset: Mandatory::default(), width: Mandatory::default(), height: Mandatory::default(), padding: 0, margin: 10, vertical_alignment: Mandatory::new(VerticalAlign::Top), horizontal_alignment: Mandatory::new(HorizontalAlign::Left), background_type: None, button_normal: None, button_selected: None, click_sound: None, hover_sound: None, children: RwLock::new(Vec::new()), parent: None, layer: None, }; for attribute_res in attributes { let attribute = attribute_res?; match attribute.key.into_inner() { b"id" => grid_info.id = cow_to_str(attribute.value), b"x_slot" => grid_info.x_slot.set(str_into(attribute.value)?), b"y_slot" => grid_info.y_slot.set(str_into(attribute.value)?), b"x_dim" => grid_info.x_dimension.set(str_into(attribute.value)?), b"y_dim" => grid_info.y_dimension.set(str_into(attribute.value)?), b"x_size" => grid_info.x_dim = str_into(attribute.value)?, b"y_size" => grid_info.y_dim = str_into(attribute.value)?, b"x_offset" => grid_info.x_offset.set(str_into(attribute.value)?), b"y_offset" => grid_info.y_offset.set(str_into(attribute.value)?), b"width" => grid_info.width.set(str_into(attribute.value)?), b"height" => grid_info.height.set(str_into(attribute.value)?), b"padding" => grid_info.padding = str_into(attribute.value)?, b"margin" => grid_info.margin = str_into(attribute.value)?, b"vert_align" => grid_info .vertical_alignment .set(str_to_vert_align(attribute.value)?), b"hori_align" => grid_info .horizontal_alignment .set(str_to_hori_align(attribute.value)?), b"background" => { grid_info.background_type = Some(cow_to_fill_type_info(attribute.value)) } b"button_normal" => { grid_info.button_normal = Some(cow_to_fill_type_info(attribute.value)) } b"button_selected" => { grid_info.button_selected = Some(cow_to_fill_type_info(attribute.value)) } b"click_sound" => { grid_info.click_sound = Some(cow_to_path(attribute.value)); } b"hover_sound" => { grid_info.hover_sound = Some(cow_to_path(attribute.value)); } b"layer" => { grid_info.layer = Some(str_into(attribute.value)?); } _ => { return Err(anyhow::Error::msg(format!( "Unsupported attribute in Grid: {}", from_utf8(attribute.key.into_inner())? ))) } } } Ok(grid_info) } }