@@ -13,7 +13,7 @@ use miniquad::{EventHandler, MouseButton};
1313use once_cell:: sync:: Lazy ;
1414use sasa:: { PlaySfxParams , Sfx } ;
1515use serde:: Serialize ;
16- use std:: { cell:: RefCell , collections:: HashMap , num:: FpCategory } ;
16+ use std:: { cell:: RefCell , collections:: HashMap , mem , num:: FpCategory } ;
1717use tracing:: debug;
1818
1919pub const FLICK_SPEED_THRESHOLD : f32 = 0.8 ;
@@ -276,9 +276,21 @@ pub struct Judge {
276276 pub judgements : RefCell < Judgements > ,
277277}
278278
279+ #[ derive( Default ) ]
280+ struct TouchStatus {
281+ touches : Vec < Touch > ,
282+ key_delta : i32 ,
283+ keys_down : u32 ,
284+ }
285+
279286static SUBSCRIBER_ID : Lazy < usize > = Lazy :: new ( register_input_subscriber) ;
280287thread_local ! {
281- static TOUCHES : RefCell <( Vec <Touch >, i32 , u32 ) > = RefCell :: default ( ) ;
288+ static TOUCHES : RefCell <TouchStatus > = RefCell :: default ( ) ;
289+ static WHEEL : RefCell <( f32 , f32 ) > = RefCell :: default ( ) ;
290+ }
291+
292+ pub fn take_wheel ( ) -> ( f32 , f32 ) {
293+ WHEEL . with ( |it| mem:: take ( & mut * it. borrow_mut ( ) ) )
282294}
283295
284296impl Judge {
@@ -332,11 +344,17 @@ impl Judge {
332344 }
333345
334346 pub ( crate ) fn on_new_frame ( ) {
335- let mut handler = Handler ( Vec :: new ( ) , 0 , 0 ) ;
347+ let mut handler = Handler {
348+ status : TouchStatus :: default ( ) ,
349+ wheel : ( 0. , 0. ) ,
350+ } ;
336351 repeat_all_miniquad_input ( & mut handler, * SUBSCRIBER_ID ) ;
337352 handler. finalize ( ) ;
338353 TOUCHES . with ( |it| {
339- * it. borrow_mut ( ) = ( handler. 0 , handler. 1 , handler. 2 ) ;
354+ * it. borrow_mut ( ) = handler. status ;
355+ } ) ;
356+ WHEEL . with ( |it| {
357+ * it. borrow_mut ( ) = handler. wheel ;
340358 } ) ;
341359 }
342360
@@ -359,7 +377,7 @@ impl Judge {
359377 let guard = it. borrow ( ) ;
360378 let tr = Self :: touch_transform ( false ) ;
361379 guard
362- . 0
380+ . touches
363381 . iter ( )
364382 . cloned ( )
365383 . map ( |mut it| {
@@ -422,9 +440,9 @@ impl Judge {
422440 } ;
423441 let ( events, keys_down) = TOUCHES . with ( |it| {
424442 let guard = it. borrow ( ) ;
425- ( guard. 0 . clone ( ) , guard. 2 )
443+ ( guard. touches . clone ( ) , guard. keys_down )
426444 } ) ;
427- self . key_down_count = self . key_down_count . saturating_add_signed ( TOUCHES . with ( |it| it. borrow ( ) . 1 ) ) ;
445+ self . key_down_count = self . key_down_count . saturating_add_signed ( TOUCHES . with ( |it| it. borrow ( ) . key_delta ) ) ;
428446 {
429447 fn to_local ( Vec2 { x, y } : Vec2 ) -> Point {
430448 Point :: new ( x / screen_width ( ) * 2. - 1. , y / screen_height ( ) * 2. - 1. )
@@ -892,11 +910,14 @@ impl Judge {
892910 }
893911}
894912
895- struct Handler ( Vec < Touch > , i32 , u32 ) ;
913+ struct Handler {
914+ status : TouchStatus ,
915+ wheel : ( f32 , f32 ) ,
916+ }
896917impl Handler {
897918 fn finalize ( & mut self ) {
898919 if is_mouse_button_down ( MouseButton :: Left ) {
899- self . 0 . push ( Touch {
920+ self . status . touches . push ( Touch {
900921 id : button_to_id ( MouseButton :: Left ) ,
901922 phase : TouchPhase :: Moved ,
902923 position : mouse_position ( ) . into ( ) ,
@@ -920,16 +941,21 @@ impl EventHandler for Handler {
920941 fn update ( & mut self , _: & mut miniquad:: Context ) { }
921942 fn draw ( & mut self , _: & mut miniquad:: Context ) { }
922943 fn touch_event ( & mut self , _: & mut miniquad:: Context , phase : miniquad:: TouchPhase , id : u64 , x : f32 , y : f32 , time : f64 ) {
923- self . 0 . push ( Touch {
944+ self . status . touches . push ( Touch {
924945 id,
925946 phase : phase. into ( ) ,
926947 position : vec2 ( x, y) ,
927948 time,
928949 } ) ;
929950 }
930951
952+ fn mouse_wheel_event ( & mut self , _ctx : & mut miniquad:: Context , x : f32 , y : f32 ) {
953+ self . wheel . 0 += x;
954+ self . wheel . 1 += y;
955+ }
956+
931957 fn mouse_button_down_event ( & mut self , _ctx : & mut miniquad:: Context , button : MouseButton , x : f32 , y : f32 ) {
932- self . 0 . push ( Touch {
958+ self . status . touches . push ( Touch {
933959 id : button_to_id ( button) ,
934960 phase : TouchPhase :: Started ,
935961 position : vec2 ( x, y) ,
@@ -938,7 +964,7 @@ impl EventHandler for Handler {
938964 }
939965
940966 fn mouse_button_up_event ( & mut self , _ctx : & mut miniquad:: Context , button : MouseButton , x : f32 , y : f32 ) {
941- self . 0 . push ( Touch {
967+ self . status . touches . push ( Touch {
942968 id : button_to_id ( button) ,
943969 phase : TouchPhase :: Ended ,
944970 position : vec2 ( x, y) ,
@@ -948,13 +974,13 @@ impl EventHandler for Handler {
948974
949975 fn key_down_event ( & mut self , _ctx : & mut miniquad:: Context , _keycode : KeyCode , _keymods : miniquad:: KeyMods , repeat : bool ) {
950976 if !repeat {
951- self . 1 += 1 ;
952- self . 2 += 1 ;
977+ self . status . key_delta += 1 ;
978+ self . status . keys_down += 1 ;
953979 }
954980 }
955981
956982 fn key_up_event ( & mut self , _ctx : & mut miniquad:: Context , _keycode : KeyCode , _keymods : miniquad:: KeyMods ) {
957- self . 1 -= 1 ;
983+ self . status . key_delta -= 1 ;
958984 }
959985}
960986
0 commit comments