Adding flags

This commit is contained in:
2023-06-07 22:22:13 +01:00
parent f8fa25a2fb
commit 5e0ed2feb8
3 changed files with 87 additions and 8 deletions

View File

@@ -8,7 +8,9 @@ edition = "2021"
[dependencies] [dependencies]
directories = "5.0.1" directories = "5.0.1"
eframe = "0.21.3" eframe = "0.21.3"
native-dialog = "0.6.3"
rfd = "0.11.4" rfd = "0.11.4"
serde = "1.0.163" serde = "1.0.163"
serde_derive = "1.0.163" serde_derive = "1.0.163"
shell-escape = "0.1.5"
toml = "0.7.4" toml = "0.7.4"

View File

@@ -55,4 +55,14 @@ impl Difficulty {
]; ];
DIFFICULTIES.iter() DIFFICULTIES.iter()
} }
pub fn flag_number(&self) -> &str {
match self {
Difficulty::None => "0",
Difficulty::Baby => "1",
Difficulty::Easy => "2",
Difficulty::Medium => "3",
Difficulty::Hard => "4",
Difficulty::Nightmare => "5",
}
}
} }

View File

@@ -1,6 +1,7 @@
use config::{default_save_filename, load_config, save_config, Config}; use config::{default_save_filename, load_config, save_config, Config};
use eframe::egui; use eframe::egui;
use infos::{Difficulty, LauncherInfo, WadInfo}; use infos::{Difficulty, LauncherInfo, WadInfo};
use native_dialog::{MessageDialog, MessageType};
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
@@ -32,6 +33,13 @@ struct RustDoomLauncher {
difficulty: Difficulty, difficulty: Difficulty,
fast_monsters: bool, fast_monsters: bool,
respawning_monsters: bool, respawning_monsters: bool,
display_command: bool,
}
// Great name, I know
enum MyErrors {
NoLauncher,
NoIwad,
} }
impl Default for RustDoomLauncher { impl Default for RustDoomLauncher {
@@ -50,6 +58,7 @@ impl Default for RustDoomLauncher {
difficulty: Difficulty::None, difficulty: Difficulty::None,
fast_monsters: false, fast_monsters: false,
respawning_monsters: false, respawning_monsters: false,
display_command: false,
} }
} }
} }
@@ -76,14 +85,34 @@ impl RustDoomLauncher {
} }
} }
fn launch_doom(&self) -> () { fn get_launcher_and_command(&self, show_dialogs: bool) -> Result<(&str, Vec<&str>), MyErrors> {
let launcher = match self.selected_launcher { let launcher = match self.selected_launcher {
Some(l) => self.all_launchers.get(l).unwrap(), Some(l) => self.all_launchers.get(l).unwrap(),
None => panic!("Gotta pick a launcher!"), None => {
if show_dialogs {
MessageDialog::new()
.set_type(MessageType::Error)
.set_title("I can't let you do that")
.set_text("You didn't pick a launcher!\nPlease do that")
.show_alert()
.unwrap();
}
return Err(MyErrors::NoLauncher);
}
}; };
let iwad = match self.selected_iwad { let iwad = match self.selected_iwad {
Some(i) => self.all_iwads.get(i).unwrap(), Some(i) => self.all_iwads.get(i).unwrap(),
None => panic!("Gotta pick and iwad"), None => {
if show_dialogs {
MessageDialog::new()
.set_type(MessageType::Error)
.set_title("I can't let you do that")
.set_text("You didn't pick an IWAD!\nPlease do that")
.show_alert()
.unwrap();
}
return Err(MyErrors::NoIwad);
}
}; };
let mut command = vec!["-iwad", iwad.path.to_str().unwrap()]; let mut command = vec!["-iwad", iwad.path.to_str().unwrap()];
for pwad_index in &self.selected_pwads { for pwad_index in &self.selected_pwads {
@@ -91,9 +120,37 @@ impl RustDoomLauncher {
let pwad = self.all_pwads.get(*pwad_index).unwrap(); let pwad = self.all_pwads.get(*pwad_index).unwrap();
command.push(pwad.path.to_str().unwrap()); command.push(pwad.path.to_str().unwrap());
} }
Command::new(launcher.path.to_str().unwrap()) if self.difficulty != Difficulty::None {
.args(command) command.push("-skill");
.spawn(); command.push(self.difficulty.flag_number());
}
if !self.warp_string.is_empty() {
command.push("-warp");
command.push(&self.warp_string);
}
if self.respawning_monsters {
command.push("-respawn");
}
Ok((launcher.path.to_str().unwrap(), command))
}
fn form_command_string(&self) -> String {
match self.get_launcher_and_command(false) {
Ok((launch, comms)) => {
//TODO - escape all the strings
let c = comms.join(" ");
format!("{} {}", launch, c)
}
Err(MyErrors::NoLauncher) => "Error: No launcher".to_string(),
Err(MyErrors::NoIwad) => "Error: No IWAD".to_string(),
}
}
fn launch_doom(&self) -> () {
if let Ok((launcher, command)) = self.get_launcher_and_command(true) {
// Note to self, don't hit launch button when running from emacs
Command::new(launcher).args(command).spawn();
}
} }
} }
@@ -108,7 +165,7 @@ impl eframe::App for RustDoomLauncher {
true true
} }
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
if self.config_file_loaded == false { if self.config_file_loaded == false {
self.get_config_file_stuff(); self.get_config_file_stuff();
self.config_file_loaded = true; self.config_file_loaded = true;
@@ -247,7 +304,10 @@ impl eframe::App for RustDoomLauncher {
}); });
}); });
ui.separator(); ui.separator();
ui.horizontal(|ui| {
ui.label("Command"); ui.label("Command");
ui.checkbox(&mut self.display_command, "");
});
ui.horizontal_wrapped(|ui| { ui.horizontal_wrapped(|ui| {
// I don't actually think using SelectableLabel is correct here - // I don't actually think using SelectableLabel is correct here -
// but it'll at least do the highlighting when hovered nicely // but it'll at least do the highlighting when hovered nicely
@@ -300,6 +360,13 @@ impl eframe::App for RustDoomLauncher {
pos += 1; pos += 1;
} }
}); });
if self.display_command {
ui.horizontal(|ui| {
let mut text = self.form_command_string();
let window_size = frame.info().window_info.size;
ui.add(egui::TextEdit::multiline(&mut text).desired_width(window_size[0] * 0.9));
});
}
}); });
} }
} }