Skip to content

Commit 9857bec

Browse files
committed
feat(ui): add mouse wheel scroll
1 parent 41adbbc commit 9857bec

2 files changed

Lines changed: 71 additions & 24 deletions

File tree

prpr/src/judge.rs

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use miniquad::{EventHandler, MouseButton};
1313
use once_cell::sync::Lazy;
1414
use sasa::{PlaySfxParams, Sfx};
1515
use serde::Serialize;
16-
use std::{cell::RefCell, collections::HashMap, num::FpCategory};
16+
use std::{cell::RefCell, collections::HashMap, mem, num::FpCategory};
1717
use tracing::debug;
1818

1919
pub 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+
279286
static SUBSCRIBER_ID: Lazy<usize> = Lazy::new(register_input_subscriber);
280287
thread_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

284296
impl 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+
}
896917
impl 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

prpr/src/ui/scroll.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
use super::{clip_rounded_rect, Ui};
2-
use crate::core::{Matrix, Point, Vector};
3-
use macroquad::prelude::{Rect, Touch, TouchPhase, Vec2};
2+
use crate::{
3+
core::{Matrix, Point, Vector},
4+
judge::take_wheel,
5+
};
6+
use macroquad::{
7+
input::mouse_position,
8+
prelude::{Rect, Touch, TouchPhase, Vec2},
9+
window::{screen_height, screen_width},
10+
};
411
use nalgebra::Translation2;
512
use std::collections::VecDeque;
613

714
const THRESHOLD: f32 = 0.03;
15+
const WHEEL_STEP: f32 = 0.1;
816

917
pub struct VelocityTracker {
1018
movements: VecDeque<(f32, Point)>,
@@ -179,12 +187,9 @@ impl Scroller {
179187
self.touch.map(|it| it.3).unwrap_or_default()
180188
}
181189

182-
pub fn update(&mut self, t: f32) {
183-
// if !self.frame_touched {
184-
// if let Some((id, ..)) = self.touch {
185-
// self.touch(id, TouchPhase::Cancelled, 0., 0.);
186-
// }
187-
// }
190+
pub fn update(&mut self, t: f32, extra_scroll: f32) {
191+
self.offset += extra_scroll * WHEEL_STEP;
192+
188193
let dt = t - self.last_time;
189194
self.offset += self.speed * dt;
190195
const K: f32 = 4.;
@@ -308,7 +313,23 @@ impl Scroll {
308313
}
309314

310315
pub fn update(&mut self, t: f32) {
311-
(if self.horizontal { &mut self.x_scroller } else { &mut self.y_scroller }).update(t)
316+
let extra_scroll = if let Some(matrix) = self.matrix {
317+
let pt = mouse_position();
318+
let pt = matrix.transform_point(&Point::new(pt.0 / screen_width() * 2. - 1., pt.1 / screen_height() * 2. - 1.));
319+
if pt.x < 0. || pt.y < 0. || pt.x > self.size.0 || pt.y > self.size.1 {
320+
0.
321+
} else {
322+
let (x, y) = take_wheel();
323+
if self.horizontal {
324+
-x
325+
} else {
326+
-y
327+
}
328+
}
329+
} else {
330+
0.
331+
};
332+
(if self.horizontal { &mut self.x_scroller } else { &mut self.y_scroller }).update(t, extra_scroll)
312333
}
313334

314335
pub fn contains(&self, touch: &Touch) -> bool {

0 commit comments

Comments
 (0)