More character menu fixes

This commit is contained in:
hodasemi 2025-04-05 13:45:08 +02:00
parent ac1af484b8
commit abf0be7881
7 changed files with 63 additions and 61 deletions

View file

@ -123,11 +123,10 @@ impl<A: Ability + 'static> AbilityPageRightSide<A> {
ControllerButton::Y => { ControllerButton::Y => {
let entity = world.entity_mut(hero)?; let entity = world.entity_mut(hero)?;
let abilities = multi_mut.get::<AbilitySlots<A>>()?; let (abilities, materials): (&mut AbilitySlots<A>, &mut CraftingMaterials) =
entity.get_components_mut()?;
if let Some(ability) = abilities.book_mut(index) { if let Some(ability) = abilities.book_mut(index) {
let materials = multi_mut.get::<CraftingMaterials>()?;
ability.upgrade(materials); ability.upgrade(materials);
if let Some(menu) = reference.upgrade() { if let Some(menu) = reference.upgrade() {

View file

@ -48,10 +48,10 @@ impl<A: Ability + 'static> Content<A, AbilityAddon> {
) -> Result<()> { ) -> Result<()> {
let entity = world.entity_mut(hero)?; let entity = world.entity_mut(hero)?;
let inventory = multi_mut.get::<Inventory<A>>()?; let (inventory, abilitiy_slots): (&mut Inventory<A>, &mut AbilitySlots<A>) =
let abilities = multi_mut.get::<AbilitySlots<A>>()?; entity.get_components_mut()?;
if let Some(book) = abilities.book_mut(ability_page.right_side.selected_ability()) { if let Some(book) = abilitiy_slots.book_mut(ability_page.right_side.selected_ability()) {
if book.has_free_addon_slots() { if book.has_free_addon_slots() {
book.addons_mut() book.addons_mut()
.insert_addon(inventory.remove_addon(addon_index)); .insert_addon(inventory.remove_addon(addon_index));
@ -148,8 +148,8 @@ impl<A: Ability + 'static> Content<A, AbilityBook<A>> {
) -> Result<()> { ) -> Result<()> {
let entity = world.entity_mut(hero)?; let entity = world.entity_mut(hero)?;
let inventory = multi_mut.get::<Inventory<A>>()?; let (inventory, abilitiy_slots): (&mut Inventory<A>, &mut AbilitySlots<A>) =
let abilitiy_slots = multi_mut.get::<AbilitySlots<A>>()?; entity.get_components_mut()?;
if let Some(old_book) = abilitiy_slots.insert_book( if let Some(old_book) = abilitiy_slots.insert_book(
inventory.remove_book(book_index), inventory.remove_book(book_index),

View file

@ -132,7 +132,7 @@ impl<A: Ability + 'static, T: Send + Sync> Content<A, T> {
let label: Arc<Label> = self.base.element("tab_info")?; let label: Arc<Label> = self.base.element("tab_info")?;
Self::clear_grid(world, &grid)?; Self::clear_grid(world, &grid)?;
self.set_tab(world.resources.get_mut::<GuiHandler>(), &label)?; self.set_tab(world.resources.get_mut()?, &label)?;
let (rows, columns) = grid.dimensions(); let (rows, columns) = grid.dimensions();

View file

@ -51,12 +51,12 @@ impl<A: Ability + 'static> Content<A, Item> {
false false
} }
}) { }) {
let socket_object = world.resources.get_mut::<Option<ReferenceObject>>(); let socket_object: &mut ReferenceObject = world.resources.get_mut()?;
*socket_object = Some(ReferenceObject::Item { *socket_object = ReferenceObject::Item {
item, item,
source: ReferenceItemSource::Inventory(item_index), source: ReferenceItemSource::Inventory(item_index),
}); };
} else { } else {
has_empty_sockets = false; has_empty_sockets = false;
} }
@ -67,8 +67,6 @@ impl<A: Ability + 'static> Content<A, Item> {
fn equip_item(world: &mut World, hero: Entity, item_index: usize) -> Result<()> { fn equip_item(world: &mut World, hero: Entity, item_index: usize) -> Result<()> {
let (entity, resources) = world.entity_resources(hero)?; let (entity, resources) = world.entity_resources(hero)?;
let mut multi_mut = entity.multi_mut();
let hero_items = multi_mut.get::<ItemSlotContainer>()?; let hero_items = multi_mut.get::<ItemSlotContainer>()?;
let inventory = multi_mut.get::<Inventory<A>>()?; let inventory = multi_mut.get::<Inventory<A>>()?;
let attributes = multi_mut.get::<Attributes>()?; let attributes = multi_mut.get::<Attributes>()?;
@ -127,7 +125,7 @@ impl<A: Ability + 'static> Content<A, Item> {
compare_gui.enable(world)?; compare_gui.enable(world)?;
gui.perform_double_check( gui.perform_double_check(
&compare_gui, &compare_gui,
world.resources.get_mut::<GuiHandler>(), world.resources.get_mut()?,
x, x,
spacing as u32, spacing as u32,
)?; )?;
@ -135,7 +133,7 @@ impl<A: Ability + 'static> Content<A, Item> {
window.add_tooltip("equip", compare_gui); window.add_tooltip("equip", compare_gui);
} }
None => { None => {
gui.perform_single_check(world.resources.get_mut::<GuiHandler>(), x, y)?; gui.perform_single_check(world.resources.get_mut()?, x, y)?;
} }
} }
@ -150,7 +148,7 @@ impl<A: Ability + 'static> ContentUpdate for Content<A, Item> {
let reference = self.reference.clone(); let reference = self.reference.clone();
self.update_base(world, |world, button, t, index| { self.update_base(world, |world, button, t, index| {
button.set_icon(world.resources.get_mut::<GuiHandler>(), &t.icon)?; button.set_icon(world.resources.get_mut()?, &t.icon)?;
button.set_custom_callback({ button.set_custom_callback({
let reference = reference.clone(); let reference = reference.clone();
@ -230,7 +228,7 @@ impl<A: Ability + 'static> ContentUpdate for Content<A, Item> {
} }
fn select(&self, world: &mut World) -> Result<()> { fn select(&self, world: &mut World) -> Result<()> {
self.select(world.resources.get_mut::<GuiHandler>()) self.select(world.resources.get_mut()?)
} }
} }
@ -253,7 +251,7 @@ impl<A: Ability + 'static> Content<A, Jewel> {
let gui = jewel.create_tooltip(world, item_settings, (target_x, target_y))?; let gui = jewel.create_tooltip(world, item_settings, (target_x, target_y))?;
gui.enable(world)?; gui.enable(world)?;
gui.perform_single_check(world.resources.get_mut::<GuiHandler>(), x, y)?; gui.perform_single_check(world.resources.get_mut()?, x, y)?;
reference reference
.upgrade() .upgrade()
.unwrap() .unwrap()
@ -268,14 +266,14 @@ impl<A: Ability + 'static> Content<A, Jewel> {
let jewel = inventory.jewel_at(jewel_index).clone(); let jewel = inventory.jewel_at(jewel_index).clone();
// add to reference // add to reference
let socket_object = world.resources.get_mut::<Option<ReferenceObject>>(); let socket_object: &mut ReferenceObject = world.resources.get_mut()?;
*socket_object = Some(ReferenceObject::Jewel { *socket_object = ReferenceObject::Jewel {
jewel, jewel,
index: jewel_index, index: jewel_index,
}); };
// remove from lower if placed there // remove from lower if placed there
let lower_jewels = world.resources.get_mut::<LowerJewels>(); let lower_jewels: &mut LowerJewels = world.resources.get_mut()?;
if let Some(position) = lower_jewels.jewels.iter().position(|jewel| match jewel { if let Some(position) = lower_jewels.jewels.iter().position(|jewel| match jewel {
Some((_, index)) => *index == jewel_index, Some((_, index)) => *index == jewel_index,
@ -323,7 +321,7 @@ impl<A: Ability + 'static> ContentUpdate for Content<A, Jewel> {
let reference = self.reference.clone(); let reference = self.reference.clone();
self.update_base(world, |world, button, t, index| { self.update_base(world, |world, button, t, index| {
button.set_icon(world.resources.get_mut::<GuiHandler>(), &t.icon())?; button.set_icon(world.resources.get_mut()?, &t.icon())?;
button.set_select_callback({ button.set_select_callback({
let weak_button = Arc::downgrade(&button); let weak_button = Arc::downgrade(&button);

View file

@ -113,7 +113,7 @@ impl ItemRightSide {
let gui = item.create_tooltip(world, attributes, (target_x, target_y))?; let gui = item.create_tooltip(world, attributes, (target_x, target_y))?;
gui.enable(world)?; gui.enable(world)?;
gui.perform_single_check(world.resources.get_mut::<GuiHandler>(), x, y)?; gui.perform_single_check(world.resources.get_mut()?, x, y)?;
Ok(gui) Ok(gui)
} }
@ -124,7 +124,7 @@ impl RightSide for ItemRightSide {
let (hero_object, resources) = world.entity_resources(hero)?; let (hero_object, resources) = world.entity_resources(hero)?;
let items = hero_object.get_component::<ItemSlotContainer>()?; let items = hero_object.get_component::<ItemSlotContainer>()?;
self.update_icons(resources.get_mut::<GuiHandler>(), items) self.update_icons(resources.get_mut()?, items)
} }
fn base(&self) -> &Arc<GuiSnippet> { fn base(&self) -> &Arc<GuiSnippet> {
@ -229,7 +229,6 @@ mod macros {
let mut found_item = false; let mut found_item = false;
let (entity, resources) = world.entity_resources($hero)?; let (entity, resources) = world.entity_resources($hero)?;
let mut multi_mut = entity.multi_mut();
let items = multi_mut.get::<ItemSlotContainer>()?; let items = multi_mut.get::<ItemSlotContainer>()?;
let inventory = multi_mut.get::<Inventory<A>>()?; let inventory = multi_mut.get::<Inventory<A>>()?;
@ -364,7 +363,6 @@ mod macros {
let mut found_item = false; let mut found_item = false;
let (entity, resources) = world.entity_resources($hero)?; let (entity, resources) = world.entity_resources($hero)?;
let mut multi_mut = entity.multi_mut();
let items = multi_mut.get::<ItemSlotContainer>()?; let items = multi_mut.get::<ItemSlotContainer>()?;
let inventory = multi_mut.get::<Inventory<A>>()?; let inventory = multi_mut.get::<Inventory<A>>()?;

View file

@ -25,6 +25,7 @@ pub enum ReferenceItemSource {
Slots(Option<usize>), Slots(Option<usize>),
} }
#[derive(Default, Resource)]
pub enum ReferenceObject { pub enum ReferenceObject {
Item { Item {
item: Item, item: Item,
@ -34,9 +35,27 @@ pub enum ReferenceObject {
jewel: Jewel, jewel: Jewel,
index: usize, index: usize,
}, },
#[default]
Empty,
} }
#[derive(Default)] impl ReferenceObject {
pub fn some(&self) -> Option<&Self> {
match self {
ReferenceObject::Item { .. } | ReferenceObject::Jewel { .. } => Some(self),
ReferenceObject::Empty => None,
}
}
pub fn some_mut(&mut self) -> Option<&mut Self> {
match self {
ReferenceObject::Item { .. } | ReferenceObject::Jewel { .. } => Some(self),
ReferenceObject::Empty => None,
}
}
}
#[derive(Default, Resource)]
pub struct LowerJewels { pub struct LowerJewels {
pub jewels: [Option<(Jewel, usize)>; 3], pub jewels: [Option<(Jewel, usize)>; 3],
} }
@ -59,11 +78,9 @@ impl JewelRightSide {
world.resources.get::<Context>(), world.resources.get::<Context>(),
ControllerButton::RightStick, ControllerButton::RightStick,
)?; )?;
combine.set_info_icon(world.resources.get_mut::<GuiHandler>(), &icon)?; combine.set_info_icon(world.resources.get_mut()?, &icon)?;
world world.resources.insert_if_not_exists::<ReferenceObject>();
.resources
.insert_if_not_exists::<Option<ReferenceObject>>();
world.resources.insert_if_not_exists::<LowerJewels>(); world.resources.insert_if_not_exists::<LowerJewels>();
let me = Self { snippet }; let me = Self { snippet };
@ -93,9 +110,9 @@ impl JewelRightSide {
let (x, y, w, _h) = button.position_extent(); let (x, y, w, _h) = button.position_extent();
let reference_info = world.resources.get_unchecked::<Option<ReferenceObject>>(); let reference_info = world.resources.get_unchecked::<ReferenceObject>();
if let Some(reference_info) = reference_info { if let Some(reference_info) = reference_info.some() {
let tooltip = match reference_info { let tooltip = match reference_info {
ReferenceObject::Item { item, .. } => { ReferenceObject::Item { item, .. } => {
let attributes = unsafe { remove_life_time(world.entity(hero)?) } let attributes = unsafe { remove_life_time(world.entity(hero)?) }
@ -108,14 +125,12 @@ impl JewelRightSide {
jewel.create_tooltip(world, item_settings, (x + w as i32, y))? jewel.create_tooltip(world, item_settings, (x + w as i32, y))?
} }
ReferenceObject::Empty => unreachable!(),
}; };
tooltip.enable(world)?; tooltip.enable(world)?;
tooltip.perform_single_check( tooltip.perform_single_check(world.resources.get_mut()?, x, y)?;
world.resources.get_mut::<GuiHandler>(),
x,
y,
)?;
menu.add_tooltip("upper", tooltip); menu.add_tooltip("upper", tooltip);
} }
@ -154,11 +169,7 @@ impl JewelRightSide {
)?; )?;
tooltip.enable(world)?; tooltip.enable(world)?;
tooltip.perform_single_check( tooltip.perform_single_check(world.resources.get_mut()?, x, y)?;
world.resources.get_mut::<GuiHandler>(),
x,
y,
)?;
menu.add_tooltip(format!("lower_{index}",), tooltip); menu.add_tooltip(format!("lower_{index}",), tooltip);
} }
@ -174,7 +185,7 @@ impl JewelRightSide {
let reference = reference.clone(); let reference = reference.clone();
move |world| { move |world| {
let lower_info = world.resources.get_mut::<LowerJewels>(); let lower_info: &mut LowerJewels = world.resources.get_mut()?;
if lower_info.jewels[index].is_some() { if lower_info.jewels[index].is_some() {
lower_info.jewels[index] = None; lower_info.jewels[index] = None;
@ -208,19 +219,18 @@ impl JewelRightSide {
)) ))
} }
pub fn clear(world: &mut World) { pub fn clear(world: &mut World) -> Result<()> {
*world.resources.get_mut::<Option<ReferenceObject>>() = None; let reference_object: &mut ReferenceObject = world.resources.get_mut()?;
world *reference_object = ReferenceObject::Empty;
.resources
.get_mut::<LowerJewels>() let lower_juwels: &mut LowerJewels = world.resources.get_mut()?;
.jewels lower_juwels.jewels.iter_mut().for_each(|j| *j = None);
.iter_mut()
.for_each(|j| *j = None); Ok(())
} }
pub fn combine<A: Ability + 'static>(world: &mut World, hero: Entity) -> Result<bool> { pub fn combine<A: Ability + 'static>(world: &mut World, hero: Entity) -> Result<bool> {
let (entity, resources) = world.entity_resources(hero)?; let (entity, resources) = world.entity_resources(hero)?;
let mut resources = resources.multi_mut();
let reference_info = resources.get::<Option<ReferenceObject>>(); let reference_info = resources.get::<Option<ReferenceObject>>();
@ -271,8 +281,6 @@ impl JewelRightSide {
let attribute_settings = resources.get::<AttributeSettings>(); let attribute_settings = resources.get::<AttributeSettings>();
let item_settings = resources.get::<ItemSettings>(); let item_settings = resources.get::<ItemSettings>();
let mut multi_mut = entity.multi_mut();
let inventory = multi_mut.get::<Inventory<A>>()?; let inventory = multi_mut.get::<Inventory<A>>()?;
let item_slots = multi_mut.get::<ItemSlotContainer>()?; let item_slots = multi_mut.get::<ItemSlotContainer>()?;
@ -365,7 +373,6 @@ impl RightSide for JewelRightSide {
fn refresh(&mut self, world: &mut World, _hero: Entity) -> Result<()> { fn refresh(&mut self, world: &mut World, _hero: Entity) -> Result<()> {
let (reference, lower) = self.elements()?; let (reference, lower) = self.elements()?;
let mut resources = world.resources.multi_mut();
let gui_handler = resources.get::<GuiHandler>(); let gui_handler = resources.get::<GuiHandler>();
let reference_info = resources.get::<Option<ReferenceObject>>(); let reference_info = resources.get::<Option<ReferenceObject>>();
let lower_info = resources.get::<LowerJewels>(); let lower_info = resources.get::<LowerJewels>();

View file

@ -42,7 +42,7 @@ impl<A: Ability + 'static> InventoryPage<A> {
reference: Weak<CharacterWindow>, reference: Weak<CharacterWindow>,
close: &Arc<Button>, close: &Arc<Button>,
) -> Result<Self> { ) -> Result<Self> {
let gui_handler = world.resources.get_mut::<GuiHandler>(); let gui_handler = world.resources.get_mut()?;
let grid = Grid::new(gui_handler, 2, 1, false)?; let grid = Grid::new(gui_handler, 2, 1, false)?;
let left_base = GuiSnippet::from_str( let left_base = GuiSnippet::from_str(
@ -274,7 +274,7 @@ impl<A: Ability + 'static> InventoryPage<A> {
if mode.content_mut().is_empty() { if mode.content_mut().is_empty() {
if let Some(close) = self.close.upgrade() { if let Some(close) = self.close.upgrade() {
close.select(world.resources.get_mut::<GuiHandler>())?; close.select(world.resources.get_mut()?)?;
} }
} }
} }
@ -410,7 +410,7 @@ impl<A: Ability + 'static> Page for InventoryPage<A> {
if mode.content().is_empty() { if mode.content().is_empty() {
if let Some(close) = self.close.upgrade() { if let Some(close) = self.close.upgrade() {
close.select(world.resources.get_mut::<GuiHandler>())?; close.select(world.resources.get_mut()?)?;
} }
} }