Skip to content

Commit

Permalink
Make recipe, food, and potion select widgets collapsible
Browse files Browse the repository at this point in the history
  • Loading branch information
KonaeAkira committed Dec 22, 2024
1 parent 776643a commit 45b2a4a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 17 deletions.
2 changes: 1 addition & 1 deletion game_data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Item {
pub always_collectable: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Ingredient {
pub item_id: u32,
Expand Down
14 changes: 12 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,22 @@ impl eframe::App for MacroSolverApp {
self.draw_list_select_widgets(ui);
})
.response;

let config_min_height = match ui.available_size_before_wrap().x {
x if x < config_width => 0.0,
_ => response.rect.height(),
};
let response = ui
.allocate_ui(egui::vec2(config_width, response.rect.height()), |ui| {
.allocate_ui(egui::vec2(config_width, config_min_height), |ui| {
self.draw_config_and_results_widget(ui);
})
.response;
ui.allocate_ui(egui::vec2(macro_width, response.rect.height()), |ui| {

let macro_min_height = match ui.available_size_before_wrap().x {
x if x < macro_width => 0.0,
_ => response.rect.height(),
};
ui.allocate_ui(egui::vec2(macro_width, macro_min_height), |ui| {
self.draw_macro_output_widget(ui);
});
},
Expand Down
22 changes: 22 additions & 0 deletions src/widgets/food_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,24 @@ impl<'a> Widget for FoodSelect<'a> {
ui.group(|ui| {
ui.style_mut().spacing.item_spacing = egui::vec2(8.0, 3.0);
ui.vertical(|ui| {
let mut collapsed = false;

ui.horizontal(|ui| {
let mut collapse_button_text = "⏷";
let collapsed_id = Id::new("FOOD_SEARCH_COLLAPSED");
ui.data_mut(|data| {
collapsed = *data.get_persisted_mut_or_default(collapsed_id);
collapse_button_text = match collapsed {
false => "⏷",
true => "⏵",
}
});
if ui.button(collapse_button_text).clicked() {
ui.data_mut(|data| {
*data.get_persisted_mut_or_default(collapsed_id) = !collapsed;
})
}

ui.label(egui::RichText::new(t!("label.food")).strong());
match self.selected_consumable {
None => {
Expand All @@ -65,6 +82,11 @@ impl<'a> Widget for FoodSelect<'a> {
}
});
});

if collapsed {
return;
}

ui.separator();

let id = Id::new("FOOD_SEARCH_TEXT");
Expand Down
22 changes: 22 additions & 0 deletions src/widgets/potion_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,24 @@ impl<'a> Widget for PotionSelect<'a> {
ui.group(|ui| {
ui.style_mut().spacing.item_spacing = egui::vec2(8.0, 3.0);
ui.vertical(|ui| {
let mut collapsed = false;

ui.horizontal(|ui| {
let mut collapse_button_text = "⏷";
let collapsed_id = Id::new("POTION_SEARCH_COLLAPSED");
ui.data_mut(|data| {
collapsed = *data.get_persisted_mut_or_default(collapsed_id);
collapse_button_text = match collapsed {
false => "⏷",
true => "⏵",
}
});
if ui.button(collapse_button_text).clicked() {
ui.data_mut(|data| {
*data.get_persisted_mut_or_default(collapsed_id) = !collapsed;
})
}

ui.label(egui::RichText::new(t!("label.potion")).strong());
match self.selected_consumable {
None => {
Expand All @@ -65,6 +82,11 @@ impl<'a> Widget for PotionSelect<'a> {
}
});
});

if collapsed {
return;
}

ui.separator();

let id = Id::new("POTION_SEARCH_TEXT");
Expand Down
48 changes: 34 additions & 14 deletions src/widgets/recipe_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,6 @@ impl<'a> RecipeSelect<'a> {
}

fn draw_custom_recipe_select(self, ui: &mut egui::Ui) {
self.recipe_config.recipe.item_id = 0;
self.recipe_config.recipe.material_quality_factor = 0;
self.recipe_config.recipe.ingredients = [Ingredient {
item_id: 0,
amount: 0,
}; 6];

let game_settings = get_game_settings(
self.recipe_config.recipe,
*self.crafter_config.active_stats(),
Expand Down Expand Up @@ -214,7 +207,24 @@ impl<'a> Widget for RecipeSelect<'a> {
}
});

let mut collapsed = false;

ui.horizontal(|ui| {
let mut collapse_button_text = "⏷";
let collapsed_id = Id::new("RECIPE_SEARCH_COLLAPSED");
ui.data_mut(|data| {
collapsed = *data.get_persisted_mut_or_default(collapsed_id);
collapse_button_text = match collapsed {
false => "⏷",
true => "⏵",
}
});
if ui.button(collapse_button_text).clicked() {
ui.data_mut(|data| {
*data.get_persisted_mut_or_default(collapsed_id) = !collapsed;
})
}

ui.label(egui::RichText::new(t!("label.recipe")).strong());
ui.add(ItemNameLabel::new(
self.recipe_config.recipe.item_id,
Expand All @@ -226,23 +236,33 @@ impl<'a> Widget for RecipeSelect<'a> {
.checkbox(&mut custom_recipe, t!("label.custom_recipe"))
.changed()
{
self.recipe_config.quality_source = match custom_recipe {
true => QualitySource::Value(0),
false => QualitySource::HqMaterialList([0; 6]),
if custom_recipe {
self.recipe_config.recipe.item_id = 0;
self.recipe_config.recipe.material_quality_factor = 0;
self.recipe_config.recipe.ingredients = [Ingredient::default(); 6];
self.recipe_config.quality_source = QualitySource::Value(0);
} else {
self.recipe_config.quality_source =
QualitySource::HqMaterialList([0; 6]);
}
ui.ctx().data_mut(|data| {
data.insert_persisted(Id::new("CUSTOM_RECIPE"), custom_recipe);
});
};
});
});

if collapsed {
return;
}

ui.separator();

if custom_recipe {
self.draw_custom_recipe_select(ui);
} else {
self.draw_normal_recipe_select(ui);
}

ui.ctx().data_mut(|data| {
data.insert_persisted(Id::new("CUSTOM_RECIPE"), custom_recipe);
});
});
})
.response
Expand Down

0 comments on commit 45b2a4a

Please sign in to comment.