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 => {
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) {
let materials = multi_mut.get::<CraftingMaterials>()?;
ability.upgrade(materials);
if let Some(menu) = reference.upgrade() {

View file

@ -48,10 +48,10 @@ impl<A: Ability + 'static> Content<A, AbilityAddon> {
) -> Result<()> {
let entity = world.entity_mut(hero)?;
let inventory = multi_mut.get::<Inventory<A>>()?;
let abilities = multi_mut.get::<AbilitySlots<A>>()?;
let (inventory, abilitiy_slots): (&mut Inventory<A>, &mut 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() {
book.addons_mut()
.insert_addon(inventory.remove_addon(addon_index));
@ -148,8 +148,8 @@ impl<A: Ability + 'static> Content<A, AbilityBook<A>> {
) -> Result<()> {
let entity = world.entity_mut(hero)?;
let inventory = multi_mut.get::<Inventory<A>>()?;
let abilitiy_slots = multi_mut.get::<AbilitySlots<A>>()?;
let (inventory, abilitiy_slots): (&mut Inventory<A>, &mut AbilitySlots<A>) =
entity.get_components_mut()?;
if let Some(old_book) = abilitiy_slots.insert_book(
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")?;
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();

View file

@ -51,12 +51,12 @@ impl<A: Ability + 'static> Content<A, Item> {
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,
source: ReferenceItemSource::Inventory(item_index),
});
};
} else {
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<()> {
let (entity, resources) = world.entity_resources(hero)?;
let mut multi_mut = entity.multi_mut();
let hero_items = multi_mut.get::<ItemSlotContainer>()?;
let inventory = multi_mut.get::<Inventory<A>>()?;
let attributes = multi_mut.get::<Attributes>()?;
@ -127,7 +125,7 @@ impl<A: Ability + 'static> Content<A, Item> {
compare_gui.enable(world)?;
gui.perform_double_check(
&compare_gui,
world.resources.get_mut::<GuiHandler>(),
world.resources.get_mut()?,
x,
spacing as u32,
)?;
@ -135,7 +133,7 @@ impl<A: Ability + 'static> Content<A, Item> {
window.add_tooltip("equip", compare_gui);
}
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();
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({
let reference = reference.clone();
@ -230,7 +228,7 @@ impl<A: Ability + 'static> ContentUpdate for Content<A, Item> {
}
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))?;
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
.upgrade()
.unwrap()
@ -268,14 +266,14 @@ impl<A: Ability + 'static> Content<A, Jewel> {
let jewel = inventory.jewel_at(jewel_index).clone();
// add to reference
let socket_object = world.resources.get_mut::<Option<ReferenceObject>>();
*socket_object = Some(ReferenceObject::Jewel {
let socket_object: &mut ReferenceObject = world.resources.get_mut()?;
*socket_object = ReferenceObject::Jewel {
jewel,
index: jewel_index,
});
};
// 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 {
Some((_, index)) => *index == jewel_index,
@ -323,7 +321,7 @@ impl<A: Ability + 'static> ContentUpdate for Content<A, Jewel> {
let reference = self.reference.clone();
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({
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))?;
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)
}
@ -124,7 +124,7 @@ impl RightSide for ItemRightSide {
let (hero_object, resources) = world.entity_resources(hero)?;
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> {
@ -229,7 +229,6 @@ mod macros {
let mut found_item = false;
let (entity, resources) = world.entity_resources($hero)?;
let mut multi_mut = entity.multi_mut();
let items = multi_mut.get::<ItemSlotContainer>()?;
let inventory = multi_mut.get::<Inventory<A>>()?;
@ -364,7 +363,6 @@ mod macros {
let mut found_item = false;
let (entity, resources) = world.entity_resources($hero)?;
let mut multi_mut = entity.multi_mut();
let items = multi_mut.get::<ItemSlotContainer>()?;
let inventory = multi_mut.get::<Inventory<A>>()?;

View file

@ -25,6 +25,7 @@ pub enum ReferenceItemSource {
Slots(Option<usize>),
}
#[derive(Default, Resource)]
pub enum ReferenceObject {
Item {
item: Item,
@ -34,9 +35,27 @@ pub enum ReferenceObject {
jewel: Jewel,
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 jewels: [Option<(Jewel, usize)>; 3],
}
@ -59,11 +78,9 @@ impl JewelRightSide {
world.resources.get::<Context>(),
ControllerButton::RightStick,
)?;
combine.set_info_icon(world.resources.get_mut::<GuiHandler>(), &icon)?;
combine.set_info_icon(world.resources.get_mut()?, &icon)?;
world
.resources
.insert_if_not_exists::<Option<ReferenceObject>>();
world.resources.insert_if_not_exists::<ReferenceObject>();
world.resources.insert_if_not_exists::<LowerJewels>();
let me = Self { snippet };
@ -93,9 +110,9 @@ impl JewelRightSide {
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 {
ReferenceObject::Item { item, .. } => {
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))?
}
ReferenceObject::Empty => unreachable!(),
};
tooltip.enable(world)?;
tooltip.perform_single_check(
world.resources.get_mut::<GuiHandler>(),
x,
y,
)?;
tooltip.perform_single_check(world.resources.get_mut()?, x, y)?;
menu.add_tooltip("upper", tooltip);
}
@ -154,11 +169,7 @@ impl JewelRightSide {
)?;
tooltip.enable(world)?;
tooltip.perform_single_check(
world.resources.get_mut::<GuiHandler>(),
x,
y,
)?;
tooltip.perform_single_check(world.resources.get_mut()?, x, y)?;
menu.add_tooltip(format!("lower_{index}",), tooltip);
}
@ -174,7 +185,7 @@ impl JewelRightSide {
let reference = reference.clone();
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() {
lower_info.jewels[index] = None;
@ -208,19 +219,18 @@ impl JewelRightSide {
))
}
pub fn clear(world: &mut World) {
*world.resources.get_mut::<Option<ReferenceObject>>() = None;
world
.resources
.get_mut::<LowerJewels>()
.jewels
.iter_mut()
.for_each(|j| *j = None);
pub fn clear(world: &mut World) -> Result<()> {
let reference_object: &mut ReferenceObject = world.resources.get_mut()?;
*reference_object = ReferenceObject::Empty;
let lower_juwels: &mut LowerJewels = world.resources.get_mut()?;
lower_juwels.jewels.iter_mut().for_each(|j| *j = None);
Ok(())
}
pub fn combine<A: Ability + 'static>(world: &mut World, hero: Entity) -> Result<bool> {
let (entity, resources) = world.entity_resources(hero)?;
let mut resources = resources.multi_mut();
let reference_info = resources.get::<Option<ReferenceObject>>();
@ -271,8 +281,6 @@ impl JewelRightSide {
let attribute_settings = resources.get::<AttributeSettings>();
let item_settings = resources.get::<ItemSettings>();
let mut multi_mut = entity.multi_mut();
let inventory = multi_mut.get::<Inventory<A>>()?;
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<()> {
let (reference, lower) = self.elements()?;
let mut resources = world.resources.multi_mut();
let gui_handler = resources.get::<GuiHandler>();
let reference_info = resources.get::<Option<ReferenceObject>>();
let lower_info = resources.get::<LowerJewels>();

View file

@ -42,7 +42,7 @@ impl<A: Ability + 'static> InventoryPage<A> {
reference: Weak<CharacterWindow>,
close: &Arc<Button>,
) -> 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 left_base = GuiSnippet::from_str(
@ -274,7 +274,7 @@ impl<A: Ability + 'static> InventoryPage<A> {
if mode.content_mut().is_empty() {
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 let Some(close) = self.close.upgrade() {
close.select(world.resources.get_mut::<GuiHandler>())?;
close.select(world.resources.get_mut()?)?;
}
}