Refactored the wad selector bits

This commit is contained in:
2023-06-02 17:05:51 +01:00
parent 5a8d57ee4f
commit 29eed3aad6

View File

@@ -1,4 +1,5 @@
use eframe::egui; use eframe::egui;
use std::path::PathBuf;
fn main() -> Result<(), eframe::Error> { fn main() -> Result<(), eframe::Error> {
println!("Hello, world!"); println!("Hello, world!");
@@ -12,24 +13,35 @@ fn main() -> Result<(), eframe::Error> {
) )
} }
struct WadInfo {
path: PathBuf,
name: String,
}
struct RustDoomLauncher { struct RustDoomLauncher {
launchers: Vec<String>, all_launchers: Vec<String>,
all_iwads: Vec<String>, all_iwads: Vec<WadInfo>,
all_pwads: Vec<String>, all_pwads: Vec<WadInfo>,
selected_launcher: Option<String>, selected_launcher: Option<String>,
selected_iwad: Option<String>, selected_iwad: Option<usize>,
selected_pwads: Vec<String>, selected_pwads: Vec<usize>,
launcher_name: String,
iwad_name: String,
pwad_name: String,
} }
impl Default for RustDoomLauncher { impl Default for RustDoomLauncher {
fn default() -> Self { fn default() -> Self {
Self { Self {
launchers: vec!["GZDoom 1.9".to_string(), "PRBoom+".to_string()], all_launchers: vec!["GZDoom 1.9".to_string(), "PRBoom+".to_string()],
all_iwads: Vec::new(), all_iwads: Vec::new(),
all_pwads: Vec::new(), all_pwads: Vec::new(),
selected_launcher: None, selected_launcher: None,
selected_iwad: None, selected_iwad: None,
selected_pwads: Vec::new(), selected_pwads: Vec::new(),
launcher_name: "".to_string(),
iwad_name: "".to_string(),
pwad_name: "".to_string(),
} }
} }
} }
@@ -39,12 +51,14 @@ impl eframe::App for RustDoomLauncher {
egui::containers::panel::CentralPanel::default().show(ctx, |ui| { egui::containers::panel::CentralPanel::default().show(ctx, |ui| {
ui.heading("RustDoomLauncher"); ui.heading("RustDoomLauncher");
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("Launcers"); ui.label("Launchers");
if ui.button("Add Launcher").clicked() { if ui.button("Add Launcher").clicked() {
todo!(); if let Some(path) = rfd::FileDialog::new().pick_file() {
self.all_launchers.push(path.display().to_string());
}
} }
}); });
for launcher in &self.launchers { for launcher in &self.all_launchers {
if ui if ui
.add(egui::SelectableLabel::new( .add(egui::SelectableLabel::new(
self.selected_launcher.is_some() self.selected_launcher.is_some()
@@ -59,44 +73,46 @@ impl eframe::App for RustDoomLauncher {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("IWADs"); ui.label("IWADs");
if ui.button("Add IWAD").clicked() { if ui.button("Add IWAD").clicked() {
if let Some(paths) = rfd::FileDialog::new().pick_files() { if let Some(path) = rfd::FileDialog::new().pick_file() {
for path in paths { self.all_iwads.push(WadInfo {
self.all_iwads.push(path.display().to_string()); name: path.display().to_string(),
} path: path,
});
} }
} }
}); });
for iwad in &self.all_iwads { for (pos, iwad) in self.all_iwads.iter().enumerate() {
if ui if ui
.add(egui::SelectableLabel::new( .add(egui::SelectableLabel::new(
self.selected_iwad.is_some() self.selected_iwad.is_some()
&& self.selected_iwad.as_ref().unwrap() == iwad, && *self.selected_iwad.as_ref().unwrap() == pos,
iwad, &iwad.name,
)) ))
.clicked() .clicked()
{ {
self.selected_iwad = Some(iwad.to_string()); self.selected_iwad = Some(pos);
} }
} }
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("PWADs"); ui.label("PWADs and Mods");
if ui.button("Add PWAD").clicked() { if ui.button("Add PWAD or Mod").clicked() {
if let Some(paths) = rfd::FileDialog::new().pick_files() { if let Some(path) = rfd::FileDialog::new().pick_file() {
for path in paths { self.all_pwads.push(WadInfo {
self.all_pwads.push(path.display().to_string()); path: path.clone(),
} name: path.display().to_string(),
});
} }
} }
}); });
for pwad in &self.all_pwads { for (pos, pwad) in self.all_pwads.iter().enumerate() {
if ui if ui
.add(egui::SelectableLabel::new( .add(egui::SelectableLabel::new(
self.selected_pwads.contains(pwad), self.selected_pwads.contains(&pos),
pwad, &pwad.name,
)) ))
.clicked() .clicked()
{ {
self.selected_pwads.push(pwad.to_string()); self.selected_pwads.push(pos);
} }
} }
ui.label("Command"); ui.label("Command");
@@ -111,7 +127,14 @@ impl eframe::App for RustDoomLauncher {
ui.label("Select a launcher plz"); ui.label("Select a launcher plz");
} }
if let Some(i) = &self.selected_iwad { if let Some(i) = &self.selected_iwad {
if ui.add(egui::SelectableLabel::new(false, i)).clicked() { if let Some(iwad) = &self.all_iwads.get(*i) {
if ui
.add(egui::SelectableLabel::new(false, &iwad.name))
.clicked()
{
self.selected_iwad = None;
}
} else {
self.selected_iwad = None; self.selected_iwad = None;
} }
} else { } else {
@@ -123,7 +146,7 @@ impl eframe::App for RustDoomLauncher {
if ui if ui
.add(egui::SelectableLabel::new( .add(egui::SelectableLabel::new(
false, false,
self.selected_pwads[pos].clone(), &self.all_pwads.get(*self.selected_pwads.get(pos).unwrap()).unwrap().name,
)) ))
.clicked() .clicked()
{ {