Allow user to add and remove cards

This commit is contained in:
pjht 2024-02-07 15:58:03 -06:00
parent e322c4084c
commit a13ae3ba77
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E

View File

@ -1,6 +1,7 @@
use eframe::{ use eframe::{
egui::{ egui::{
self, CentralPanel, DragValue, Layout, Margin, SidePanel, Slider, Style, TopBottomPanel, self, CentralPanel, ComboBox, Label, Layout, Margin, Sense, SidePanel, Slider,
Style, TopBottomPanel,
}, },
emath::Align, emath::Align,
}; };
@ -29,17 +30,17 @@ enum OptionsCategory {
impl OptionWindow { impl OptionWindow {
pub fn new(ctx: &egui::Context, state: &EmuState) -> Self { pub fn new(ctx: &egui::Context, state: &EmuState) -> Self {
Modal::new(ctx, "options_modal").open(); Modal::new(ctx, "options_modal").open();
let ramcard_type = Type::get("RAM card").unwrap(); let card_options = Vec::new();
let def_opts = ramcard_type.default_settings(); let select_idx = if card_options.is_empty() {
let settings_ui = ramcard_type None
.settings_ui(ron::from_str(&def_opts).unwrap()) } else {
.unwrap(); Some(0)
let card_options = vec![(ramcard_type.name(), settings_ui)]; };
Self { Self {
options: state.options(), options: state.options(),
category: OptionsCategory::General, category: OptionsCategory::General,
card_options, card_options,
select_idx: Some(0), select_idx,
} }
} }
} }
@ -72,13 +73,53 @@ impl Window for OptionWindow {
top: 0.0, top: 0.0,
bottom: 10.0, bottom: 10.0,
}); });
let mut delete_idx = None;
frame.show(ui, |ui| { frame.show(ui, |ui| {
for (i, (name, _settings_ui)) in for (i, (name, _settings_ui)) in
self.card_options.iter().enumerate() self.card_options.iter().enumerate()
{ {
ui.selectable_value(&mut self.select_idx, Some(i), *name); ui.horizontal(|ui| {
} ui.selectable_value(&mut self.select_idx, Some(i), *name);
if ui.button("-").clicked() {
delete_idx = Some(i);
}
});
};
}); });
if let Some(delete_idx) = delete_idx {
self.card_options.remove(delete_idx);
if let Some(select_idx) = self.select_idx {
if select_idx >= self.card_options.len() {
if self.card_options.is_empty() {
self.select_idx = None;
} else {
self.select_idx = Some(self.card_options.len() - 1);
}
}
}
}
ComboBox::from_id_source("cards_opts_add")
.selected_text("Add")
.show_ui(ui, |ui| {
for typ in inventory::iter::<Type> {
if ui
.add(Label::new(typ.name()).sense(Sense::click()))
.clicked()
{
self.card_options.push((
typ.name(),
typ.settings_ui(
ron::from_str(&typ.default_settings())
.unwrap(),
)
.unwrap(),
));
if self.select_idx.is_none() {
self.select_idx = Some(0);
}
};
}
});
}); });
CentralPanel::default().show_inside(ui, |ui| { CentralPanel::default().show_inside(ui, |ui| {
if let Some(select_idx) = self.select_idx { if let Some(select_idx) = self.select_idx {