Working OK

This commit is contained in:
Pcornat 2024-07-18 21:16:24 +02:00
parent 09c02c8783
commit fa6aca08ef
Signed by: Pcornat
GPG Key ID: E0326CC678A00BDD
5 changed files with 33 additions and 37 deletions

View File

@ -6,9 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
eframe = { version = "*", features = ["dark-light"] }
egui = { version = "*", features = ["deadlock_detection"]}
egui_file = "*"
eframe = { version = "*" }
rfd = "*"
json = "*"
serde = { version = "*", features = ["derive"] }
serde_json = "*"

View File

@ -1,10 +0,0 @@
pub struct FileDialog {
dialog: egui_file::FileDialog,
}
impl super::Dialog for FileDialog {
fn open(&mut self) {
self.dialog = egui_file::FileDialog::open_file(None);
self.dialog.open();
}
}

View File

@ -1,9 +0,0 @@
pub mod file_dialog;
pub trait View {
fn ui(&mut self, ui: &mut egui::Ui);
}
pub trait Dialog {
fn open(&mut self);
fn show(&mut self, ctx: &egui::Context);
}

View File

@ -1,3 +1,2 @@
pub mod gui;
pub mod sheet;
pub mod dialog;

View File

@ -1,20 +1,25 @@
use std::path::PathBuf;
use eframe::egui;
use eframe::egui::KeyboardShortcut;
use billy_sheet::gui::SheetGui;
fn main() {
let options = eframe::NativeOptions::default();
fn main() -> eframe::Result {
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_drag_and_drop(true),
..Default::default()
};
eframe::run_native(
"Billy Sheet editor",
options,
Box::new(|_ctx| Box::<Gui>::default()),
);
Box::new(|_ctx| Ok(Box::<Gui>::default())),
)
}
#[derive(Debug)]
struct Gui {
sheet: billy_sheet::sheet::CharacterSheet,
dialogs: Vec<impl billy_sheet::dialog::Dialog>,
picked_path: Option<PathBuf>,
edit_mode: bool,
}
@ -22,7 +27,7 @@ impl Default for Gui {
fn default() -> Self {
Self {
sheet: billy_sheet::sheet::CharacterSheet::default(),
dialogs: Vec::new(),
picked_path: None,
edit_mode: true,
}
}
@ -34,9 +39,17 @@ impl eframe::App for Gui {
egui::menu::bar(ui, |ui| {
egui::widgets::global_dark_light_mode_buttons(ui);
ui.menu_button("File", |ui| {
if ui.button("Open (Ctrl + O)").clicked() {}
if ui.button("Open (Ctrl + O)").clicked() {
if let Some(path) = rfd::FileDialog::new()
.set_directory(std::env::current_dir().unwrap_or_default())
.add_filter(".json", &["json"])
.pick_file()
{
self.picked_path = Some(path);
}
}
if ui.button("Quit (Ctrl + Q)").clicked() {
_frame.close();
ui.ctx().send_viewport_cmd(egui::ViewportCommand::Close);
}
if ui.button("Save in json").clicked() {
let path = std::path::Path::new("./sheet.json");
@ -79,11 +92,15 @@ impl eframe::App for Gui {
});
});
});
if ctx
.input_mut()
.consume_key(egui::Modifiers::CTRL, egui::Key::Q)
{
_frame.close();
ctx.input_mut(|i_state| {
let shortcut = KeyboardShortcut::new(egui::Modifiers::CTRL, egui::Key::Q);
if i_state.consume_shortcut(&shortcut) {
i_state.viewport().close_requested();
}
});
if let Some(file_path) = &self.picked_path {
println!("{}", file_path.to_str().unwrap_or_default());
self.picked_path = None;
}
}
}