@@ -180,7 +180,7 @@ fn handle_keyboard_inputs(
180180fn handle_mouse_inputs (
181181 windows : Res < Windows > ,
182182 mouse_input : Res < Input < MouseButton > > ,
183- settings : ResMut < NotationSettings > ,
183+ settings : Res < NotationSettings > ,
184184 mut mouse_motion_events : EventReader < MouseMotion > ,
185185 mut mouse_wheel_input : EventReader < bevy:: input:: mouse:: MouseWheel > ,
186186 mut mouse_clicked : EventWriter < MouseClickedEvent > ,
@@ -218,22 +218,40 @@ fn handle_mouse_inputs(
218218}
219219
220220fn handle_touch_inputs (
221+ windows : Res < Windows > ,
221222 touch_input : Res < Touches > ,
223+ mut app_state : ResMut < NotationAppState > ,
222224 mut mouse_clicked : EventWriter < MouseClickedEvent > ,
223- mut mouse_dragged : EventWriter < MouseDraggedEvent > ,
225+ // mut mouse_dragged: EventWriter<MouseDraggedEvent>,
224226) {
225- for ( index, finger) in touch_input. iter ( ) . enumerate ( ) {
226- if index == 0 {
227- if touch_input. just_pressed ( finger. id ( ) ) {
228- mouse_clicked. send ( MouseClickedEvent { cursor_position : finger. position ( ) } ) ;
229- }
230- } else if index == 1 {
231- if touch_input. just_pressed ( finger. id ( ) ) {
232- } else if touch_input. just_released ( finger. id ( ) ) {
233- } else {
234- let delta = finger. position ( ) - finger. previous_position ( ) ;
235- mouse_dragged. send ( MouseDraggedEvent { delta : delta } ) ;
236- }
227+ for ( _index, finger) in touch_input. iter ( ) . enumerate ( ) {
228+ if touch_input. just_pressed ( finger. id ( ) ) {
229+ windows
230+ . get_primary ( )
231+ . map ( |w| ( w. physical_width ( ) as f32 , w. physical_height ( ) as f32 ) )
232+ . map ( | ( physical_width, physical_height) | {
233+ /*
234+ Super hacky way to get the touch input in mobile browsers (WASM).
235+ winit not support it yet, using a pull request version, which seems to have some issues
236+ as well, also the touch event triggering is very unreliable during my test, but at least
237+ it's better than no touch at all.
238+ */
239+ let dpi_x = physical_width / app_state. window_width ;
240+ let dpi_y = physical_height / app_state. window_height ;
241+ let x = finger. position ( ) . x * dpi_x;
242+ let y = app_state. window_height - finger. position ( ) . y * dpi_y;
243+ app_state. debug_str = Some ( format ! ( "Touch: {} {:?} -> {} {}" , _index, finger. position( ) , x, y) ) ;
244+ mouse_clicked. send ( MouseClickedEvent { cursor_position : Vec2 :: new ( x, y) } ) ;
245+ } ) ;
246+ } else if touch_input. just_released ( finger. id ( ) ) {
247+ app_state. debug_str = None ;
248+ } else {
249+ app_state. debug_str = Some ( format ! ( "Touch: {} - {:?}" , _index, finger. position( ) ) ) ;
250+ /*
251+ let delta = finger.position() - finger.previous_position();
252+ app_state.debug_str = Some(format!("Dragged: {}, {:?}", _index, delta));
253+ mouse_dragged.send(MouseDraggedEvent { delta: delta });
254+ */
237255 }
238256 }
239257}
@@ -268,6 +286,7 @@ fn on_window_resized(
268286 window. height = evt. height ;
269287 app_state. window_width = evt. width ;
270288 app_state. window_height = evt. height ;
289+ app_state. scale_factor_override = window. scale_factor_override ;
271290 window_resized_evts. send ( WindowResizedEvent ( ) ) ;
272291 }
273292 }
0 commit comments