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

@@ -1,6 +1,7 @@
use config::{default_save_filename, load_config, save_config, Config};
use eframe::egui;
use infos::{Difficulty, LauncherInfo, WadInfo};
use native_dialog::{MessageDialog, MessageType};
use std::path::PathBuf;
use std::process::Command;
@@ -32,6 +33,13 @@ struct RustDoomLauncher {
difficulty: Difficulty,
fast_monsters: bool,
respawning_monsters: bool,
display_command: bool,
}
// Great name, I know
enum MyErrors {
NoLauncher,
NoIwad,
}
impl Default for RustDoomLauncher {
@@ -50,6 +58,7 @@ impl Default for RustDoomLauncher {
difficulty: Difficulty::None,
fast_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 {
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 {
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()];
for pwad_index in &self.selected_pwads {
@@ -91,9 +120,37 @@ impl RustDoomLauncher {
let pwad = self.all_pwads.get(*pwad_index).unwrap();
command.push(pwad.path.to_str().unwrap());
}
Command::new(launcher.path.to_str().unwrap())
.args(command)
.spawn();
if self.difficulty != Difficulty::None {
command.push("-skill");
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
}
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 {
self.get_config_file_stuff();
self.config_file_loaded = true;
@@ -247,7 +304,10 @@ impl eframe::App for RustDoomLauncher {
});
});
ui.separator();
ui.label("Command");
ui.horizontal(|ui| {
ui.label("Command");
ui.checkbox(&mut self.display_command, "");
});
ui.horizontal_wrapped(|ui| {
// I don't actually think using SelectableLabel is correct here -
// but it'll at least do the highlighting when hovered nicely
@@ -300,6 +360,13 @@ impl eframe::App for RustDoomLauncher {
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));
});
}
});
}
}