Skip to content

Commit de0319e

Browse files
committed
fixes #118, use egui with rfd to show open file dialog to load tab from disk
1 parent ec3412d commit de0319e

File tree

9 files changed

+380
-22
lines changed

9 files changed

+380
-22
lines changed

Cargo.lock

Lines changed: 283 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/notation_viewer/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ fn main() {
99
#[cfg(not(target_arch = "wasm32"))]
1010
let tabs = vec![
1111
"songs/misc/scarborough_fair.ron".to_owned(),
12+
/*
1213
"songs/misc/stand_by_me.ron".to_owned(),
1314
"songs/jay/long_juan_feng.ron".to_owned(),
1415
"songs/pu_shu/bai_hua_lin.ron".to_owned(),
1516
"beginner/1_right_hand.ron".to_owned(),
1617
"test.ron".to_owned(),
18+
*/
1719
];
1820
notation_bevy::prelude::NotationViewer::run(tabs);
1921
}

crates/notation_bevy/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ bevy_easings = "0.4.1"
6060
# bevy_svg_map = "0.2"
6161

6262
bevy-inspector-egui = { version = "0.5.1", optional = true }
63+
rfd = "0.6.2"
6364

6465
#[target."cfg(not(target_arch = "wasm32"))".dependencies]
6566
# Can not enable bevy_wgpu in this way, so the actually binary crate

crates/notation_bevy/src/app/app.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ impl PluginGroup for NotationPlugins {
4242
pub struct NotationApp;
4343

4444
impl NotationApp {
45+
pub const TITLE:&'static str = "Notation Viewer";
46+
4547
pub fn new_builder(title: &str) -> AppBuilder {
4648
let mut app = App::build();
4749
AssetLoader::new(NotationAssetsStates::Loading, NotationAssetsStates::Loaded)
@@ -137,6 +139,7 @@ fn setup_camera(mut commands: Commands) {
137139
fn load_tab(
138140
mut commands: Commands,
139141
time: Res<Time>,
142+
mut windows: ResMut<Windows>,
140143
mut state: ResMut<NotationAppState>,
141144
mut theme: ResMut<NotationTheme>,
142145
entities: Query<Entity, With<GlobalTransform>>,
@@ -178,6 +181,10 @@ fn load_tab(
178181
match Tab::try_parse_arc(asset.tab.clone()) {
179182
Ok(tab) => {
180183
state.tab = Some(tab.clone());
184+
if let Some(window) = windows.get_primary_mut() {
185+
let title = format!("{} - {}", NotationApp::TITLE, state.tab_path);
186+
window.set_title(title);
187+
}
181188
theme._bypass_systems = false;
182189
evts.send(AddTabEvent(tab));
183190
}

crates/notation_bevy/src/app/app_state.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ impl NotationAppState {
3737
_load_tab_delay_seconds: 0.0,
3838
}
3939
}
40-
pub fn change_tab(&mut self, asset_server: &AssetServer, tab_path: String) {
40+
pub fn change_tab(
41+
&mut self,
42+
asset_server: &AssetServer,
43+
theme: &mut NotationTheme,
44+
tab_path: String
45+
) {
46+
theme._bypass_systems = true;
4147
self.tab_path = tab_path;
4248
self.tab_asset = asset_server.load(self.tab_path.as_str());
4349
self.parse_error = None;

crates/notation_bevy/src/viewer/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl NotationViewer {
159159
}
160160
}
161161
pub fn run(tab_pathes: Vec<String>) {
162-
NotationApp::run("Notation Viewer", tab_pathes, |app| {
162+
NotationApp::run(NotationApp::TITLE, tab_pathes, |app| {
163163
app.add_system_set(
164164
SystemSet::on_update(NotationAssetsStates::Loaded)
165165
.with_system(ControlView::control_ui.system())

crates/notation_bevy/src/viewer/control_view.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -427,29 +427,48 @@ impl ControlView {
427427
pub fn tab_ui(
428428
ui: &mut Ui,
429429
asset_server: &AssetServer,
430+
pathes: &mut TabPathes,
430431
state: &mut NotationAppState,
431432
_settings: &mut NotationSettings,
432433
theme: &mut NotationTheme,
433-
tab_pathes: &TabPathes,
434434
) {
435-
if tab_pathes.0.len() > 1 {
435+
if theme._bypass_systems {
436+
ui.label("Loading Tab ...");
437+
return;
438+
}
439+
ui.horizontal(|ui| {
440+
if ui.button("Reset Tab").clicked() {
441+
Control::reload_tab(state, theme);
442+
}
443+
#[cfg(not(target_arch = "wasm32"))]
444+
if ui.button("Open Tab").clicked() {
445+
if let Some(path) = rfd::FileDialog::new()
446+
.add_filter("Fun Notation", &["ron"])
447+
.pick_file() {
448+
let path_str = path.clone().into_os_string().into_string();
449+
if let Ok(path_str) = path_str {
450+
pathes.0.insert(0, path_str.clone());
451+
state.change_tab(asset_server, theme, path_str.clone());
452+
} else {
453+
println!("Failed to convert path to string: {:?} -> {:?}", path, path_str);
454+
}
455+
}
456+
}
457+
});
458+
if pathes.0.len() > 1 {
436459
let width = Self::calc_width(state.window_width);
437460
egui::ComboBox::from_label("")
438461
.selected_text(state.tab_path.clone())
439462
.width(width - 24.0)
440463
.show_ui(ui, |ui| {
441-
for path in tab_pathes.0.iter() {
464+
for path in pathes.0.iter() {
442465
if ui.selectable_label(*path == state.tab_path, path).clicked()
443466
{
444-
theme._bypass_systems = true;
445-
state.change_tab(asset_server, path.clone());
467+
state.change_tab(asset_server, theme, path.clone());
446468
}
447469
}
448470
});
449471
}
450-
if ui.button("Reset Tab").clicked() {
451-
Control::reload_tab(state, theme);
452-
}
453472
}
454473
pub fn guitar_tab_display_ui(
455474
ui: &mut Ui,
@@ -622,10 +641,10 @@ impl ControlView {
622641
egui_ctx: Res<EguiContext>,
623642
mut windows: ResMut<Windows>,
624643
asset_server: Res<AssetServer>,
644+
mut pathes: ResMut<TabPathes>,
625645
mut state: ResMut<NotationAppState>,
626646
mut settings: ResMut<NotationSettings>,
627647
mut theme: ResMut<NotationTheme>,
628-
tab_pathes: Res<TabPathes>,
629648
mut midi_settings: ResMut<MidiSettings>,
630649
mut midi_state: ResMut<MidiState>,
631650
mut play_control_evts: EventWriter<PlayControlEvent>,
@@ -649,7 +668,7 @@ impl ControlView {
649668
}
650669
ui.separator();
651670
*/
652-
Self::tab_ui(ui, &asset_server, &mut state, &mut settings, &mut theme, &tab_pathes);
671+
Self::tab_ui(ui, &asset_server, &mut pathes, &mut state, &mut settings, &mut theme);
653672
ui.separator();
654673
Self::play_control_ui(ui, &mut settings, &mut midi_state, &mut play_control_evts);
655674
ui.separator();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod misc;
22
pub mod text;
3+
pub mod path;
34

45
pub struct BevyUtil();
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::path::PathBuf;
2+
3+
use super::BevyUtil;
4+
5+
impl BevyUtil {
6+
pub fn check_assets_path(root: PathBuf) -> Option<PathBuf> {
7+
let mut path = root.clone();
8+
path.push("assets");
9+
if !path.exists() && path.is_dir() {
10+
println!("BevyUtil check_assets_path() not exist: {:?} -> {:?}", root, path);
11+
} else if !path.is_dir() {
12+
println!("BevyUtil check_assets_path() is not dir: {:?} -> {:?}", root, path);
13+
} else {
14+
return Some(path);
15+
}
16+
None
17+
}
18+
pub fn get_assets_path() -> Option<PathBuf> {
19+
let mut path = None;
20+
if let Ok(root) = std::env::current_exe() {
21+
if let Some(root) = root.parent() {
22+
path = Self::check_assets_path(root.to_path_buf());
23+
}
24+
}
25+
if path.is_none() {
26+
if let Ok(root) = std::env::current_dir() {
27+
path = Self::check_assets_path(root.to_path_buf());
28+
}
29+
}
30+
path
31+
}
32+
fn _get_asset_path(root: PathBuf, name: &str, extension: &str) -> Option<PathBuf> {
33+
let mut path = root.clone();
34+
path.push(name);
35+
path.set_extension(extension);
36+
if path.exists() {
37+
Some(path)
38+
} else {
39+
println!("BevyUtil check_asset_path() not exist: {:?} {}.{} -> {:?}", root, name, extension, path);
40+
None
41+
}
42+
}
43+
pub fn get_asset_path(name: &str, extension: &str) -> Option<PathBuf> {
44+
match Self::get_assets_path() {
45+
Some(root) => Self::_get_asset_path(root, name, extension),
46+
None => None
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)