Skip to content

Commit 55acf74

Browse files
committed
fixes #80, bugfix with init size on wasm target
1 parent f18d72e commit 55acf74

File tree

10 files changed

+73
-31
lines changed

10 files changed

+73
-31
lines changed

apps/notation_viewer/Makefile.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ command = "cargo"
5757
[tasks.release-to-amateurguitar-www]
5858
script_runner = "@shell"
5959
script = ["""
60-
cp ./target/wasm.js ../../../../amateurguitar/www/static/notation/
61-
cp ./target/wasm_bg.wasm ../../../../amateurguitar/www/static/notation/
62-
cp ./assets/fonts/* ../../../../amateurguitar/www/static/notation/assets/fonts/
63-
cp ./assets/png/* ../../../../amateurguitar/www/static/notation/assets/png/
64-
cp ./assets/beginner/* ../../../../amateurguitar/www/static/notation/assets/beginner/
65-
cp ./assets/songs/jay/* ../../../../amateurguitar/www/static/notation/assets/songs/jay/
66-
cp ./assets/songs/pu_shu/* ../../../../amateurguitar/www/static/notation/assets/songs/pu_shu/
60+
cp ./target/wasm.js ../../../../amateurguitar/www/themes/juice/static/notation/
61+
cp ./target/wasm_bg.wasm ../../../../amateurguitar/themes/juice/www/static/notation/
62+
cp ./assets/fonts/* ../../../../amateurguitar/www/themes/juice/static/notation/assets/fonts/
63+
cp ./assets/png/* ../../../../amateurguitar/www/themes/juice/static/notation/assets/png/
64+
cp ./assets/beginner/* ../../../../amateurguitar/www/themes/juice/static/notation/assets/beginner/
65+
cp ./assets/songs/jay/* ../../../../amateurguitar/www/themes/juice/static/notation/assets/songs/jay/
66+
cp ./assets/songs/pu_shu/* ../../../../amateurguitar/www/themes/juice/static/notation/assets/songs/pu_shu/
6767
"""]
6868
dependencies = ["build-web"]

crates/notation_bevy/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ console_error_panic_hook = "0.1"
7171
gloo-events = "0.1.1"
7272
futures = "0.3"
7373
web-sys = { version = "0.3.45", features = [
74-
"Element", "Document", "Window", "Location",
74+
"Element", "Document", "Window", "Location", "console",
7575
#https://rustwasm.github.io/docs/wasm-bindgen/examples/web-audio.html
7676
"AudioContext", "AudioDestinationNode", "AudioNode", "AudioParam", "GainNode", "OscillatorNode", "OscillatorType",
7777
]}

crates/notation_bevy/src/app/app.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
use bevy::app::PluginGroupBuilder;
24
use bevy::input::mouse::MouseMotion;
35
use bevy::prelude::*;
@@ -64,7 +66,7 @@ impl NotationApp {
6466
console_error_panic_hook::set_once();
6567

6668
#[cfg(target_arch = "wasm32")]
67-
app.add_plugin(crate::ext::bevy_web_fullscreen::FullViewportPlugin);
69+
app.add_plugin(crate::wasm::bevy_web_fullscreen::FullViewportPlugin);
6870

6971
app.add_plugin(bevy_egui::EguiPlugin);
7072
app.add_plugin(NotationUiPlugin);
@@ -101,13 +103,13 @@ impl NotationApp {
101103

102104
app.add_system_set(
103105
SystemSet::on_enter(NotationAssetsStates::Loaded)
104-
.with_system(setup_window_size.system()),
106+
.with_system(setup_window_size.system())
105107
);
106108
app.add_system_set(
107109
SystemSet::on_update(NotationAssetsStates::Loaded)
108110
.with_system(on_window_resized.system())
109111
.with_system(handle_inputs.system())
110-
.with_system(load_tab.system()),
112+
.with_system(load_tab.system())
111113
);
112114

113115
extra(&mut app);
@@ -131,7 +133,7 @@ fn load_tab(
131133
assets: ResMut<Assets<TabAsset>>,
132134
mut evts: EventWriter<AddTabEvent>,
133135
) {
134-
if state.tab.is_none() && state.parse_error.is_none() {
136+
if state.window_width > 0.0 && state.window_height > 0.0 && state.tab.is_none() && state.parse_error.is_none() {
135137
if let Some(asset) = assets.get(&state.tab_asset) {
136138
match Tab::try_parse_arc(asset.tab.clone()) {
137139
Ok(tab) => {
@@ -148,7 +150,7 @@ fn load_tab(
148150
}
149151

150152
fn handle_inputs(
151-
_keyboard_input: Res<Input<KeyCode>>,
153+
mut commands: Commands,
152154
windows: Res<Windows>,
153155
keyboard_input: Res<Input<KeyCode>>,
154156
mouse_input: Res<Input<MouseButton>>,
@@ -160,6 +162,7 @@ fn handle_inputs(
160162
mut mouse_clicked: EventWriter<MouseClickedEvent>,
161163
mut mouse_dragged: EventWriter<MouseDraggedEvent>,
162164
mut window_resized_evts: EventWriter<WindowResizedEvent>,
165+
viewer_query: Query<(Entity, &Arc<NotationViewer>), With<Arc<NotationViewer>>>,
163166
) {
164167
if keyboard_input.pressed(KeyCode::LControl) {
165168
settings.mouse_dragged_panning = true;
@@ -174,8 +177,9 @@ fn handle_inputs(
174177
crate::viewer::control::ControlView::play_or_pause(&mut midi_state, &mut play_control_evts);
175178
} else if keyboard_input.just_released(KeyCode::Return) {
176179
crate::viewer::control::ControlView::play_or_stop(&mut midi_state, &mut play_control_evts);
177-
}
178-
if mouse_input.just_released(MouseButton::Left) {
180+
} else if keyboard_input.just_released(KeyCode::Backslash) {
181+
crate::viewer::control::ControlView::toggle_layout_mode(&mut commands, &mut state, &mut settings, &viewer_query);
182+
} else if mouse_input.just_released(MouseButton::Left) {
179183
windows
180184
.get_primary()
181185
.and_then(|x| x.cursor_position())
@@ -193,9 +197,19 @@ fn handle_inputs(
193197
}
194198
}
195199

196-
fn setup_window_size(window: Res<WindowDescriptor>, mut app_state: ResMut<NotationAppState>) {
197-
app_state.window_width = window.width;
198-
app_state.window_height = window.height;
200+
fn setup_window_size(
201+
window: Res<WindowDescriptor>,
202+
mut app_state: ResMut<NotationAppState>,
203+
) {
204+
#[cfg(target_arch = "wasm32")]
205+
let (width, height) = crate::wasm::bevy_web_fullscreen::get_viewport_size();
206+
207+
#[cfg(not(target_arch = "wasm32"))]
208+
let (width, height) = (window.width, window.height);
209+
210+
println!("setup_window_size(): {} {} ", width, height);
211+
app_state.window_width = width;
212+
app_state.window_height = height;
199213
}
200214

201215
fn on_window_resized(
@@ -208,6 +222,7 @@ fn on_window_resized(
208222
if evt.width as usize != window.width as usize
209223
|| evt.height as usize != window.height as usize
210224
{
225+
println!("on_window_resized(): {} {} -> {} {} ", window.width, window.height, evt.width, evt.height);
211226
window.width = evt.width;
212227
window.height = evt.height;
213228
app_state.window_width = evt.width;

crates/notation_bevy/src/app/app_state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ impl NotationAppState {
2222
pub fn new(asset_server: &AssetServer, tab_path: String) -> Self {
2323
let tab_asset = asset_server.load(tab_path.as_str());
2424
Self {
25-
window_width: 1280.0,
26-
window_height: 720.0,
25+
window_width: 0.0,
26+
window_height: 0.0,
2727
tab_path,
2828
tab_asset,
2929
tab: None,

crates/notation_bevy/src/ext/mod.rs

Lines changed: 0 additions & 2 deletions
This file was deleted.

crates/notation_bevy/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ pub mod settings;
3131
pub mod theme;
3232

3333
pub mod app;
34-
pub mod ext;
3534
pub mod ui;
3635
pub mod viewer;
3736

37+
#[cfg(target_arch = "wasm32")]
38+
pub mod wasm;
39+
3840
#[cfg(feature = "inspector")]
3941
pub mod inspector;
4042

crates/notation_bevy/src/tab/tab_bars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl TabBars {
3737
}
3838
impl<'a> View<NotationLayout<'a>> for TabBars {
3939
fn log_layout_changed(&self) -> bool {
40-
true
40+
false
4141
}
4242
}
4343

crates/notation_bevy/src/viewer/control.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ impl ControlView {
150150
}
151151
}
152152

153+
pub fn toggle_layout_mode(
154+
commands: &mut Commands,
155+
state: &mut ResMut<NotationAppState>,
156+
settings: &mut ResMut<NotationSettings>,
157+
viewer_query: &Query<(Entity, &Arc<NotationViewer>), With<Arc<NotationViewer>>>,
158+
) {
159+
if settings.layout.mode == LayoutMode::Grid {
160+
settings.layout.mode = LayoutMode::Line;
161+
} else {
162+
settings.layout.mode = LayoutMode::Grid;
163+
}
164+
Self::reload_tab(commands, state, viewer_query);
165+
}
166+
153167
pub fn control_ui(
154168
mut commands: Commands,
155169
egui_ctx: Res<EguiContext>,
@@ -210,12 +224,7 @@ impl ControlView {
210224
"Grid Mode"
211225
};
212226
if ui.button(mode_text).clicked() {
213-
if settings.layout.mode == LayoutMode::Grid {
214-
settings.layout.mode = LayoutMode::Line;
215-
} else {
216-
settings.layout.mode = LayoutMode::Grid;
217-
}
218-
Self::reload_tab(&mut commands, &mut state, &viewer_query);
227+
Self::toggle_layout_mode(&mut commands, &mut state, &mut settings, &viewer_query);
219228
}
220229
if ui.button("Reload Tab").clicked() {
221230
Self::reload_tab(&mut commands, &mut state, &viewer_query);

crates/notation_bevy/src/ext/bevy_web_fullscreen.rs renamed to crates/notation_bevy/src/wasm/bevy_web_fullscreen.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl Plugin for FullViewportPlugin {
2323
}
2424
}
2525

26-
fn get_viewport_size() -> (f32, f32) {
26+
pub fn get_viewport_size() -> (f32, f32) {
2727
let web_window = web_sys::window().expect("could not get window");
2828
let document_element = web_window
2929
.document()
@@ -33,6 +33,7 @@ fn get_viewport_size() -> (f32, f32) {
3333

3434
let width = document_element.client_width();
3535
let height = document_element.client_height();
36+
web_log!("bevy_web_fullscreen::get_viewport_size() -> {}, {}", width, height);
3637

3738
(width as f32, height as f32)
3839
}
@@ -44,6 +45,7 @@ fn setup_viewport_resize_system(resize_sender: Res<Mutex<OnResizeSender>>) {
4445
local_sender.send(()).unwrap();
4546

4647
gloo_events::EventListener::new(&web_window, "resize", move |_event| {
48+
web_log!("bevy_web_fullscreen::setup_viewport_resize_system() {:?}", _event);
4749
local_sender.send(()).unwrap();
4850
})
4951
.forget();
@@ -56,7 +58,9 @@ fn viewport_resize_system(
5658
if resize_receiver.lock().unwrap().try_recv().is_ok() {
5759
if let Some(window) = windows.get_primary_mut() {
5860
let size = get_viewport_size();
61+
web_log!("bevy_web_fullscreen::viewport_resize_system() {}, {}", size.0, size.1);
5962
window.set_resolution(size.0, size.1);
6063
}
6164
}
6265
}
66+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
extern crate web_sys;
2+
3+
#[macro_use]
4+
pub mod web_utils {
5+
// A macro to provide `println!(..)`-style syntax for `console.log` logging.
6+
macro_rules! web_log {
7+
( $( $t:tt )* ) => {
8+
web_sys::console::log_1(&format!( $( $t )* ).into());
9+
}
10+
}
11+
}
12+
13+
pub mod bevy_web_fullscreen;
14+

0 commit comments

Comments
 (0)