plate-tool/plate-tool-eframe/src/modals.rs

249 lines
8.3 KiB
Rust

use eframe::egui;
#[non_exhaustive]
#[derive(Debug)]
pub enum ModalState {
NewPlate(NewPlateModalState),
NewPlateComplete(NewPlateModalComplete),
EditPlate(EditPlateModalState),
EditPlateComplete(EditPlateModalStateComplete),
None,
}
impl Default for ModalState {
fn default() -> Self {
Self::None
}
}
#[non_exhaustive]
#[derive(Debug)]
pub struct NewPlateModalState {
pub name: String,
pub plate_type: plate_tool_lib::plate::PlateType,
pub plate_format: plate_tool_lib::plate::PlateFormat,
window_open: bool,
}
impl Default for NewPlateModalState {
fn default() -> Self {
Self {
name: String::default(),
plate_type: plate_tool_lib::plate::PlateType::default(),
plate_format: plate_tool_lib::plate::PlateFormat::default(),
window_open: true,
}
}
}
#[non_exhaustive]
#[derive(Debug, Default)]
pub struct NewPlateModalComplete(pub Option<plate_tool_lib::plate_instances::PlateInstance>);
pub fn show_modal_if_open(ctx: &egui::Context, modal_state: &mut ModalState) {
match modal_state {
ModalState::NewPlate(_) => show_new_plate_modal(ctx, modal_state),
ModalState::EditPlate(_) => show_edit_plate_modal(ctx, modal_state),
_ => (),
}
}
fn show_new_plate_modal(ctx: &egui::Context, modal_state: &mut ModalState) {
let state: &mut NewPlateModalState = {
if let ModalState::NewPlate(x) = modal_state {
x
} else {
*modal_state = ModalState::NewPlate(NewPlateModalState::default());
if let ModalState::NewPlate(y) = modal_state {
y
} else {
unreachable!()
}
}
};
let mut output: Option<plate_tool_lib::plate_instances::PlateInstance> = None;
egui::Window::new("New Plate")
.order(egui::Order::Foreground)
.collapsible(false)
.resizable(false)
.open(&mut state.window_open)
.show(ctx, |ui| {
ui.vertical(|ui| {
ui.add(egui::TextEdit::singleline(&mut state.name).hint_text("Plate Name"));
egui::ComboBox::from_label("Plate Type")
.selected_text(format!("{:?}", state.plate_type))
.show_ui(ui, |ui| {
ui.selectable_value(
&mut state.plate_type,
plate_tool_lib::plate::PlateType::Source,
"Source",
);
ui.selectable_value(
&mut state.plate_type,
plate_tool_lib::plate::PlateType::Destination,
"Destination",
);
});
egui::ComboBox::from_label("Plate Format")
.selected_text(format!("{:?}", state.plate_format))
.show_ui(ui, |ui| {
ui.selectable_value(
&mut state.plate_format,
plate_tool_lib::plate::PlateFormat::W96,
"96W",
);
ui.selectable_value(
&mut state.plate_format,
plate_tool_lib::plate::PlateFormat::W384,
"384W",
);
ui.selectable_value(
&mut state.plate_format,
plate_tool_lib::plate::PlateFormat::W1536,
"1536W",
);
});
if ui.button("Add").clicked() {
output = Some(plate_tool_lib::plate_instances::PlateInstance::new(
state.plate_type,
state.plate_format,
state.name.clone(),
));
}
});
});
if state.window_open == false {
*modal_state = ModalState::NewPlateComplete(NewPlateModalComplete(None));
return;
}
if output.is_some() {
*modal_state = ModalState::NewPlateComplete(NewPlateModalComplete(output));
}
}
pub fn open_new_plate_modal(modal_state: &mut ModalState) {
// Do not close another modal
if matches!(modal_state, ModalState::None) {
*modal_state = ModalState::NewPlate(NewPlateModalState::default());
}
}
pub fn open_new_plate_modal_with_default_type(
modal_state: &mut ModalState,
plate_type: plate_tool_lib::plate::PlateType,
) {
// Do not close another modal
if matches!(modal_state, ModalState::None) {
*modal_state = ModalState::NewPlate(NewPlateModalState {
plate_type: plate_type,
..Default::default()
});
}
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct EditPlateModalState {
pub name: String,
pub plate_type: plate_tool_lib::plate::PlateType,
pub plate_format: plate_tool_lib::plate::PlateFormat,
pub id: plate_tool_lib::uuid::Uuid,
window_open: bool,
}
impl EditPlateModalState {
fn new(
name: &str,
plate_type: plate_tool_lib::plate::PlateType,
plate_format: plate_tool_lib::plate::PlateFormat,
id: plate_tool_lib::uuid::Uuid,
) -> Self {
Self {
name: name.to_owned(),
plate_type,
plate_format,
id,
window_open: true,
}
}
}
#[non_exhaustive]
#[derive(Debug)]
pub struct EditPlateModalStateComplete(pub Option<EditPlateModalState>);
fn show_edit_plate_modal(ctx: &egui::Context, modal_state: &mut ModalState) {
let state: &mut EditPlateModalState = {
if let ModalState::EditPlate(x) = modal_state {
x
} else {
return;
}
};
let mut should_complete: bool = false;
egui::Window::new("Edit Plate")
.order(egui::Order::Foreground)
.collapsible(false)
.resizable(false)
.open(&mut state.window_open)
.show(ctx, |ui| {
ui.vertical(|ui| {
ui.add(egui::TextEdit::singleline(&mut state.name).hint_text("Plate Name"));
egui::ComboBox::from_label("Plate Format")
.selected_text(format!("{:?}", state.plate_format))
.show_ui(ui, |ui| {
ui.selectable_value(
&mut state.plate_format,
plate_tool_lib::plate::PlateFormat::W96,
"96W",
);
ui.selectable_value(
&mut state.plate_format,
plate_tool_lib::plate::PlateFormat::W384,
"384W",
);
ui.selectable_value(
&mut state.plate_format,
plate_tool_lib::plate::PlateFormat::W1536,
"1536W",
);
});
if ui.button("Save").clicked() {
should_complete = true;
}
});
});
if state.window_open == false {
*modal_state = ModalState::EditPlateComplete(EditPlateModalStateComplete(None));
return;
}
if should_complete {
*modal_state =
ModalState::EditPlateComplete(EditPlateModalStateComplete(Some(state.to_owned())));
return;
}
}
pub fn open_edit_plate_modal(
modal_state: &mut ModalState,
name: &str,
plate_type: plate_tool_lib::plate::PlateType,
plate_format: plate_tool_lib::plate::PlateFormat,
id: plate_tool_lib::uuid::Uuid,
) {
// Do not close another modal
if matches!(modal_state, ModalState::None) {
*modal_state =
ModalState::EditPlate(EditPlateModalState::new(name, plate_type, plate_format, id))
}
}
pub fn open_edit_plate_modal_plateinstance(
modal_state: &mut ModalState,
plate_instance: &plate_tool_lib::plate_instances::PlateInstance,
) {
// Do not close another modal
if matches!(modal_state, ModalState::None) {
*modal_state = ModalState::EditPlate(EditPlateModalState::new(
&plate_instance.name,
plate_instance.plate.plate_type,
plate_instance.plate.plate_format,
plate_instance.get_uuid(),
));
}
}