use std::{ str::{from_utf8, FromStr}, sync::Arc, }; use anyhow::Result; use engine::prelude::*; use super::{Rarities, Tooltip}; use crate::{ components::{attributes::Attribute, inventory::Storable, statistic_types::StatisticType}, config::items::ItemSettings, }; #[derive(Clone, Debug)] pub struct Jewel { pub rarity: Rarities, pub level: u32, pub attribute: Attribute, pub stat: StatisticType, pub icon: Option>, } impl PartialEq for Jewel { fn eq(&self, other: &Self) -> bool { self.rarity == other.rarity && self.level == other.level } } impl Jewel { pub fn into_persistent(&self) -> String { format!( "{}|{}|{}|{}", self.rarity, self.level, self.attribute, self.stat ) } pub fn from_persistent<'a>(split: &mut impl Iterator) -> Result { let rarity = Rarities::from_str(split.next().unwrap())?; let level = split.next().unwrap().parse::()?; let attribute = Attribute::from_str(split.next().unwrap())?; let stat = StatisticType::from_str(split.next().unwrap())?; Ok(Self { rarity, level, attribute, stat, icon: None, }) } pub fn update_stat(&mut self, item_settings: &ItemSettings) { self.stat .apply_for_jewel(self.rarity, self.level, item_settings); } pub fn create_tooltip( &self, gui_handler: &Arc, item_settings: &ItemSettings, position: (i32, i32), ) -> Result { let inspector_snippet: Arc = GuiBuilder::from_str( gui_handler, include_str!("../../resources/jewel_tooltip.xml"), )?; let main_grid: Arc = inspector_snippet.element("main_grid")?; main_grid.change_position_unscaled(position.0, position.1)?; let jewel_icon: Arc = inspector_snippet.element("jewel_icon")?; let rarity_label: Arc