Skip to content

Commit

Permalink
Preventing editing while dialogs are open
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Sep 12, 2024
1 parent f7fb23c commit a6f115e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 37 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ required-features = ["editor"]
[dependencies]
serde = { version = "1.0.208", optional = true, features = ["derive"] }
easing-function = "0.1.0"
rsn = { version = "0.1", optional = true }
tempfile = { version = "3.12.0", optional = true }
rfd = { version = "0.14.1", optional = true }

# Editor dependencies
cushy = { git = "https://github.com/khonsulabs/cushy", optional = true, features = [
"serde",
] }
rsn = { version = "0.1", optional = true }
tempfile = { version = "3.12.0", optional = true }
rfd = { version = "0.14.1", optional = true }

[dev-dependencies]
pot = "3.0.0"
Expand Down
70 changes: 47 additions & 23 deletions src/funnybones.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use cushy::{
kludgine::app::winit::keyboard::ModifiersState,
value::{Destination, Dynamic, DynamicRead, Source, Switchable, Watcher},
widget::{MakeWidget, SharedCallback, WidgetList, HANDLED},
widgets::{checkbox::Checkable, input::InputValue, slider::Slidable, Space},
window::{MakeWindow, PendingWindow, Window},
widgets::{checkbox::Checkable, input::InputValue, layers::Modal, slider::Slidable, Space},
window::{MakeWindow, PendingWindow, Window, WindowHandle},
App, Application, ModifiersStateExt, Open, PendingApp, WithClone,
};
use funnybones::{
Expand All @@ -31,6 +31,7 @@ fn main() -> cushy::Result {
}

fn skeleton_window(path: Option<PathBuf>) -> Window {
let modals = Modal::new();
let editing_skeleton = if let Some(path) = path.as_ref() {
match EditingSkeleton::read_from(path) {
Ok(skeleton) => skeleton,
Expand Down Expand Up @@ -85,14 +86,18 @@ fn skeleton_window(path: Option<PathBuf>) -> Window {
});

bones_editor
.vertical_scroll()
.expand()
.and(canvas.expand().and(zoom).into_rows().expand())
.into_columns()
.expand()
.and(modals.clone())
.into_layers()
.with_shortcut("s", ModifiersState::PRIMARY, {
(&path, &editing_skeleton, &on_error).with_clone(
|(path, editing_skeleton, on_error)| {
(&path, &editing_skeleton, &on_error, &modals).with_clone(
|(path, editing_skeleton, on_error, modals)| {
move |_| {
if let Err(err) = save(&path, &editing_skeleton, &on_error) {
if let Err(err) = save(&path, &editing_skeleton, &on_error, &modals) {
on_error.invoke(err);
}
HANDLED
Expand All @@ -102,7 +107,7 @@ fn skeleton_window(path: Option<PathBuf>) -> Window {
})
.with_shortcut("s", ModifiersState::PRIMARY | ModifiersState::SHIFT, {
move |_| {
save_as(&path, &editing_skeleton, &on_error);
save_as(&path, &editing_skeleton, &on_error, &modals);
HANDLED
}
})
Expand All @@ -113,12 +118,13 @@ fn save(
path: &Dynamic<Option<PathBuf>>,
skeleton: &EditingSkeleton,
on_error: &SharedCallback<SaveError>,
modals: &Modal,
) -> Result<(), SaveError> {
let current_path = path.read();
if let Some(path) = &*current_path {
skeleton.write_to(path)
} else {
save_as(path, skeleton, on_error);
save_as(path, skeleton, on_error, modals);
Ok(())
}
}
Expand All @@ -127,13 +133,16 @@ fn save_as(
path: &Dynamic<Option<PathBuf>>,
skeleton: &EditingSkeleton,
on_error: &SharedCallback<SaveError>,
modals: &Modal,
) {
(path, skeleton, on_error).with_clone(|(path, skeleton, on_error)| {
(path, skeleton, on_error, modals).with_clone(|(path, skeleton, on_error, modals)| {
std::thread::spawn(move || {
if let Some(new_path) = rfd::FileDialog::new()
modals.present("Please dismiss the save file dialog to continue editing.");
let new_path = rfd::FileDialog::new()
.add_filter("FunnyBones Skeleton (.fbs)", &["fbs"])
.save_file()
{
.save_file();
modals.dismiss();
if let Some(new_path) = new_path {
match skeleton.write_to(&new_path) {
Ok(()) => {
path.set(Some(new_path));
Expand All @@ -148,6 +157,7 @@ fn save_as(
fn main_menu_window(app: &impl Application) -> Window {
let window = PendingWindow::default();
let handle = window.handle();
let visible = Dynamic::new(true);

window
.with_root(
Expand All @@ -164,16 +174,19 @@ fn main_menu_window(app: &impl Application) -> Window {
.and("New Animation".into_button())
.and("Open Existing...".into_button().on_click({
let mut app = app.as_app();
let handle = handle.clone();
let visible = visible.clone();
move |_| {
open_file(&mut app);
handle.request_close();
visible.set(false);
open_file(&mut app, &handle, true);
}
}))
.into_rows()
.pad(),
)
.resize_to_fit(true)
.resizable(false)
.visible(visible)
}

fn add_bones_to_skeleton(
Expand Down Expand Up @@ -668,15 +681,26 @@ fn bone_property_editor(bone: SkeletalBone, watcher: &Watcher, is_root: bool) ->
.into_rows()
}

fn open_file(app: &mut App) {
if let Some(file) = rfd::FileDialog::new()
.add_filter("FunnyBones Skeleton (.fbs)", &["fbs"])
.pick_file()
{
if file.extension().map_or(false, |ext| ext == "fbs") {
let _ = skeleton_window(Some(file)).open(app);
} else {
todo!("unknown file type");
fn open_file(app: &mut App, parent_window: &WindowHandle, close: bool) {
parent_window.execute({
let mut app = app.clone();
let parent_window = parent_window.clone();
move |context| {
let dialog = rfd::FileDialog::new()
.add_filter("FunnyBones Skeleton (.fbs)", &["fbs"])
.set_parent(context.winit().expect("running on winit"));
std::thread::spawn(move || {
if let Some(file) = dialog.pick_file() {
if file.extension().map_or(false, |ext| ext == "fbs") {
let _ = skeleton_window(Some(file)).open(&mut app);
} else {
todo!("unknown file type");
}
}
if close {
parent_window.request_close();
}
});
}
}
});
}

0 comments on commit a6f115e

Please sign in to comment.