From a13ae3ba77a0e012fcfc0351b2b989e50fff3202 Mon Sep 17 00:00:00 2001 From: pjht Date: Wed, 7 Feb 2024 15:58:03 -0600 Subject: [PATCH] Allow user to add and remove cards --- src/option_window.rs | 61 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/src/option_window.rs b/src/option_window.rs index cd30a5d..48046eb 100644 --- a/src/option_window.rs +++ b/src/option_window.rs @@ -1,6 +1,7 @@ use eframe::{ egui::{ - self, CentralPanel, DragValue, Layout, Margin, SidePanel, Slider, Style, TopBottomPanel, + self, CentralPanel, ComboBox, Label, Layout, Margin, Sense, SidePanel, Slider, + Style, TopBottomPanel, }, emath::Align, }; @@ -29,17 +30,17 @@ enum OptionsCategory { impl OptionWindow { pub fn new(ctx: &egui::Context, state: &EmuState) -> Self { Modal::new(ctx, "options_modal").open(); - let ramcard_type = Type::get("RAM card").unwrap(); - let def_opts = ramcard_type.default_settings(); - let settings_ui = ramcard_type - .settings_ui(ron::from_str(&def_opts).unwrap()) - .unwrap(); - let card_options = vec![(ramcard_type.name(), settings_ui)]; + let card_options = Vec::new(); + let select_idx = if card_options.is_empty() { + None + } else { + Some(0) + }; Self { options: state.options(), category: OptionsCategory::General, card_options, - select_idx: Some(0), + select_idx, } } } @@ -72,13 +73,53 @@ impl Window for OptionWindow { top: 0.0, bottom: 10.0, }); + let mut delete_idx = None; frame.show(ui, |ui| { for (i, (name, _settings_ui)) in 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:: { + 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| { if let Some(select_idx) = self.select_idx {