Merge pull request #1931 from levzzz5154/fix/draggable-scrollbars

🤖 Agent: Thanks levzzz5154! The Game Rules and Statistics scroll bars can be pressed, dragged and released like vanilla's; I merged main and pushed a41381236 (Statistics uses the GUI height; #1913's drag test is folded in). Tested together with the other wave 1 contributor PRs on current main: fmt, clippy -D warnings and the horizon-client tests (1,120 lib tests and the rest) pass; the 114 harnesses the batch affects pass; lan_e2e anvil, grindstone, replant, instant_digging, digging, frames and leads pass with the host in-frame and on its server thread.
This commit is contained in:
Ludovic Marie
2026-10-26 03:24:52 +01:00
committed by GitHub
4 changed files with 147 additions and 23 deletions
+10
View File
@@ -11090,6 +11090,12 @@ impl ApplicationHandler for Game<'_> {
if self.screen == Screen::Advancements {
self.advancements_drag();
}
if self.screen == Screen::Stats {
self.stats_drag();
}
if self.screen == Screen::WorldOptions {
self.world_options_drag();
}
if self.screen == Screen::Creative {
let (_, oy) = Self::creative_origin(&b);
self.creative_screen.drag_scrollbar(self.cursor.1 - oy);
@@ -11369,6 +11375,9 @@ impl ApplicationHandler for Game<'_> {
Screen::WorldOptions if pressed && button == MouseButton::Left => {
self.world_options_click()
}
Screen::WorldOptions if button == MouseButton::Left => {
self.world_options_release()
}
Screen::Friends if pressed && button == MouseButton::Left => {
self.friends_screen_click()
}
@@ -11392,6 +11401,7 @@ impl ApplicationHandler for Game<'_> {
.map_or([1, 1], |r| [r.config.width, r.config.height]);
self.stats_click(size);
}
Screen::Stats if button == MouseButton::Left => self.stats_release(),
Screen::Mod if pressed && button == MouseButton::Left => {
self.mod_screen_click()
}
@@ -45,7 +45,8 @@ pub(super) struct RulesScreen {
initial: GameRules,
/// IntegerRuleEntry's boxes, by rule.
numbers: Vec<(&'static str, EditBox)>,
scroll: f32,
/// RuleList's scrolling: the wheel and the held scrollbar.
scroll: ui::Scroll,
/// ConfirmScreen "Game Rule Changes" in its place.
confirm: bool,
}
@@ -120,7 +121,7 @@ impl RulesScreen {
values: rules.clone(),
initial: rules.clone(),
numbers,
scroll: 0.0,
scroll: ui::Scroll::default(),
confirm: false,
}
}
@@ -156,12 +157,17 @@ impl RulesScreen {
})
.collect()
}
fn max_scroll(rows: usize, h: f32) -> f32 {
(ROW * rows as f32 + 4.0 - (h - HEADER - FOOTER)).max(0.0)
}
/// Entry `i`'s top: 2 under the list's top, less the scroll.
fn row_top(&self, i: usize) -> f32 {
HEADER + 2.0 - self.scroll.trunc() + ROW * i as f32
HEADER + 2.0 - self.scroll.amount.trunc() + ROW * i as f32
}
/// The list's scrollbar column (AbstractSelectionList: 6 pixels right of
/// the entries) and the pixels it scrolls.
fn bar(w: f32) -> f32 {
row_left(w) + ROW_WIDTH + 6.0 + 2.0
}
fn content(&self) -> f32 {
ROW * self.rows().len() as f32 + 4.0
}
/// getEntryAtPosition: the row under the pointer, inside the list.
fn hovered(&self, rows: usize, (x, y): (f32, f32), (w, h): (f32, f32)) -> Option<usize> {
@@ -212,10 +218,28 @@ impl RulesScreen {
/// The mouse wheel (AbstractSelectionList: half an entry a step).
pub(super) fn wheel(&mut self, delta: f32, h: f32) {
if !self.confirm {
let max = Self::max_scroll(self.rows().len(), h);
self.scroll = (self.scroll - delta * ROW / 2.0).clamp(0.0, max);
let content = self.content();
self.scroll
.wheel(delta, ROW / 2.0, HEADER, h - FOOTER, content);
}
}
/// A press on the scrollbar holds it (AbstractSelectionList.updateScrolling).
pub(super) fn press_scrollbar(&mut self, cursor: (f32, f32), (w, h): (f32, f32)) -> bool {
let content = self.content();
self.scroll
.press(cursor, Self::bar(w), HEADER, h - FOOTER, content)
}
/// The pointer moved while the scrollbar is held.
pub(super) fn drag(&mut self, y: f32, h: f32) {
if self.scroll.held() {
let content = self.content();
self.scroll.drag(y, HEADER, h - FOOTER, content);
}
}
/// The button came up: the scrollbar lets go.
pub(super) fn release(&mut self) {
self.scroll.release();
}
fn focused(&mut self) -> Option<(Option<&'static str>, &mut EditBox)> {
if self.search.focused {
return Some((None, &mut self.search));
@@ -229,7 +253,7 @@ impl RulesScreen {
/// number sets its rule when valid (IntegerRuleEntry's responder).
fn edited(&mut self, rule: Option<&'static str>) {
let Some(name) = rule else {
self.scroll = 0.0;
self.scroll.amount = 0.0;
return;
};
let Some(r) = gamerules::rule(name) else {
@@ -286,6 +310,11 @@ impl RulesScreen {
if on(cx, cy, 150.0, 20.0) {
return self.close();
}
// A press on the scrollbar holds it and clicks nothing else.
if self.press_scrollbar(cursor, size) {
self.unfocus();
return None;
}
// A click moves the focus to the widget under it.
let (sx, sy) = search_at(w);
if on(sx, sy, SEARCH_WIDTH, 15.0) {
@@ -360,7 +389,8 @@ impl RulesScreen {
return;
}
let rows = self.rows();
self.scroll = self.scroll.clamp(0.0, Self::max_scroll(rows.len(), h));
let content = ROW * rows.len() as f32 + 4.0;
self.scroll.clamp(HEADER, h - FOOTER, content);
// RuleList: the in-world list background, its entries clipped to it,
// then the separators and scrollbar.
ui::list_background(batch, atlas, HEADER, h - FOOTER, true);
@@ -421,16 +451,14 @@ impl RulesScreen {
}
batch.clip_since(first, 0.0, HEADER, w, h - FOOTER);
ui::separators(batch, atlas, HEADER, h - FOOTER, true);
let content = ROW * rows.len() as f32 + 4.0;
let bar_x = left + ROW_WIDTH + 6.0 + 2.0;
ui::scrollbar(
batch,
atlas,
bar_x,
Self::bar(w),
HEADER,
h - FOOTER,
content,
self.scroll.trunc(),
self.scroll.amount.trunc(),
);
// The header: the title centred over the search box.
let title = Text::translate("editGamerule.title", &[]);
@@ -536,6 +564,29 @@ mod tests {
);
}
/// AbstractScrollArea: a press on the bar holds it, a drag moves the
/// list (to the ends past them), a release lets go.
#[test]
fn the_scroll_bar_can_be_held_dragged_and_let_go() {
let mut s = RulesScreen::new(&GameRules::default());
let (w, h) = (427.0, 240.0);
let (top, bottom, content) = (HEADER, h - FOOTER, s.content());
assert!(
content > bottom - top,
"the rules overflow a 240-high screen"
);
let bar = RulesScreen::bar(w);
assert!(!s.press_scrollbar((bar - 4.0, top + 5.0), (w, h)));
assert!(s.press_scrollbar((bar + 3.0, top + 5.0), (w, h)));
s.drag(top + 40.0, h);
assert!(s.scroll.amount > 0.0);
s.drag(bottom + 10.0, h);
assert_eq!(s.scroll.amount, content - (bottom - top));
s.release();
s.drag(top, h);
assert_eq!(s.scroll.amount, content - (bottom - top));
}
#[test]
fn edits_change_rules_and_bad_numbers_hold_done() {
let mut s = RulesScreen::new(&GameRules::default());
@@ -802,6 +802,27 @@ impl Game<'_> {
}
}
/// The pointer moved while a list's scrollbar is held: the game rules'
/// list follows it (AbstractSelectionList.mouseDragged).
pub(super) fn world_options_drag(&mut self) {
let h = self.gui_size().1;
if let Some(o) = self.world_options.as_mut()
&& o.view == View::GameRules
&& let Some(rules) = o.rules.as_mut()
{
rules.drag(self.cursor.1, h);
}
}
/// The button came up: a held scrollbar lets go (onRelease).
pub(super) fn world_options_release(&mut self) {
if let Some(o) = self.world_options.as_mut()
&& let Some(rules) = o.rules.as_mut()
{
rules.release();
}
}
pub(super) fn world_options_click(&mut self) {
let Some(mut o) = self.world_options.take() else {
return;
+51 -9
View File
@@ -158,7 +158,8 @@ const COLUMNS: [(&str, &str); 6] = [
#[derive(Default)]
pub(super) struct StatsUi {
tab: usize,
scroll: f32,
/// The list's scrolling: the wheel and the held scrollbar.
scroll: horizon_client::ui::Scroll,
sort: Option<(usize, i32)>,
}
/// `MenuTabBar` geometry: three tabs over at most 400 pixels.
@@ -182,6 +183,15 @@ impl Game<'_> {
self.stats_ui = StatsUi::default();
self.screen = Screen::Stats;
}
/// The list's scrollbar column (right of the 280 pixel list) and the
/// pixels it scrolls.
fn stats_bar(w: f32) -> f32 {
((w - 280.0) / 2.0).floor() + 280.0 + 6.0
}
fn stats_content(&self) -> f32 {
let (row_h, rows) = self.stats_rows();
row_h * rows as f32 + 4.0
}
fn stats_rows(&self) -> (f32, usize) {
match self.stats_ui.tab {
0 => (14.0, CUSTOM.len()),
@@ -260,10 +270,9 @@ impl Game<'_> {
// The list.
let (row_h, rows) = self.stats_rows();
let content = row_h * rows as f32 + 4.0;
let max_scroll = (content - (bottom - top)).max(0.0);
self.stats_ui.scroll = self.stats_ui.scroll.clamp(0.0, max_scroll);
self.stats_ui.scroll.clamp(top, bottom, content);
let left = ((w - 280.0) / 2.0).floor();
let scroll = self.stats_ui.scroll;
let scroll = self.stats_ui.scroll.amount;
let row_y = |i: usize| top + 4.0 + i as f32 * row_h - scroll;
let visible = |y: f32| y >= top && y + row_h <= bottom;
match self.stats_ui.tab {
@@ -408,11 +417,11 @@ impl Game<'_> {
ui::scrollbar(
batch,
atlas,
left + 280.0 + 6.0,
Self::stats_bar(w),
top,
bottom,
content,
self.stats_ui.scroll,
self.stats_ui.scroll.amount,
);
self.draw_stats_footer(batch, w, h);
}
@@ -433,6 +442,10 @@ impl Game<'_> {
);
}
pub(super) fn stats_click(&mut self, size: [u32; 2]) {
// A press on the scrollbar holds it and clicks nothing else.
if self.stats_press_scrollbar(size) {
return;
}
let b = HudBatch::new(size);
let (w, h) = (b.width, b.height);
let (x0, tw) = tabs(w);
@@ -441,7 +454,7 @@ impl Game<'_> {
&& self.stats_ui.tab != i
{
self.stats_ui.tab = i;
self.stats_ui.scroll = 0.0;
self.stats_ui.scroll.amount = 0.0;
self.audio
.play("minecraft:ui.button.click", None, 0.25, 1.0);
return;
@@ -461,7 +474,7 @@ impl Game<'_> {
}
if self.stats_ui.tab == 1 {
let left = ((w - 280.0) / 2.0).floor();
let y = TAB_H + 4.0 - self.stats_ui.scroll;
let y = TAB_H + 4.0 - self.stats_ui.scroll.amount;
for c in 0..COLUMNS.len() {
let x = left + 75.0 + 40.0 * c as f32 - 18.0;
if ui::hovered(self.cursor, x, y + 1.0, 18.0, 18.0) {
@@ -479,6 +492,35 @@ impl Game<'_> {
}
pub(super) fn stats_scroll(&mut self, amount: f32) {
let (row_h, _) = self.stats_rows();
self.stats_ui.scroll -= amount * row_h / 2.0;
let h = self.gui_size().1;
let content = self.stats_content();
self.stats_ui
.scroll
.wheel(amount, row_h / 2.0, TAB_H, h - FOOTER, content);
}
/// A press on the statistics list's scrollbar holds it
/// (`AbstractSelectionList.updateScrolling`).
pub(super) fn stats_press_scrollbar(&mut self, size: [u32; 2]) -> bool {
let b = HudBatch::new(size);
let (w, h) = (b.width, b.height);
let content = self.stats_content();
self.stats_ui
.scroll
.press(self.cursor, Self::stats_bar(w), TAB_H, h - FOOTER, content)
}
/// The pointer moved while the scrollbar is held.
pub(super) fn stats_drag(&mut self) {
if !self.stats_ui.scroll.held() {
return;
}
let h = self.gui_size().1;
let content = self.stats_content();
self.stats_ui
.scroll
.drag(self.cursor.1, TAB_H, h - FOOTER, content);
}
/// The button came up: the scrollbar lets go.
pub(super) fn stats_release(&mut self) {
self.stats_ui.scroll.release();
}
}