World list and pack lists: vanilla scroller, dragged with the pointer

SelectWorldScreen's list now sits at vanilla's 49..h-60 with the list
background, header and footer separators, white selection outline and
the widget/scroller sprites at the row's right + 8. The scroller follows
the pointer while held (AbstractScrollArea.mouseDragged), the wheel moves
half a row, and the pack screen's lists can be dragged the same way.

Part of #282

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-15 10:04:37 +02:00
parent 587695e108
commit a8bd497132
4 changed files with 140 additions and 66 deletions
+8 -1
View File
@@ -1699,7 +1699,11 @@ impl OptionsUi {
}
if self.page() == Page::Packs {
let mut events = Vec::new();
if drag || right {
if drag {
self.packs(atlas, size).drag(y as f64);
return events;
}
if right {
return events;
}
let m = crate::screen_draw::Measure(&atlas.font);
@@ -1862,6 +1866,9 @@ impl OptionsUi {
}
pub fn release(&mut self) -> Vec<Event> {
self.scrollbar_drag_y = None;
if let Some(p) = &mut self.pack_screen {
p.release();
}
// AbstractSliderButton suppresses the press sound and plays once on
// release, even if the pointer has left the widget or its value stayed.
let slider = self.dragging.take().is_some();
+38 -1
View File
@@ -136,6 +136,8 @@ struct List {
selected: Option<usize>,
/// The list has a focused child (possibly an entry it no longer holds).
focused: bool,
/// The pointer's last y while the scrollbar is held (`scrolling`).
held: Option<f64>,
}
impl List {
fn new(selected_side: bool, height: i32) -> Self {
@@ -149,6 +151,7 @@ impl List {
entries: Vec::new(),
selected: None,
focused: false,
held: None,
}
}
fn bottom(&self) -> i32 {
@@ -244,6 +247,21 @@ impl List {
fn scroller_height(&self) -> i32 {
((self.height * self.height) as f32 / self.content_height() as f32) as i32
}
/// `AbstractScrollArea.mouseDragged` while the scrollbar is held.
fn drag(&mut self, y: f64) {
let Some(last) = self.held else { return };
if y < self.y as f64 {
self.set_scroll(0.0);
} else if y > self.bottom() as f64 {
self.set_scroll(self.max_scroll() as f64);
} else {
let max = self.max_scroll().max(1) as f64;
let bar = self.scroller_height().clamp(32, self.height - 8);
let scale = (max / (self.height - bar) as f64).max(1.0);
self.set_scroll(self.scroll + (y - last) * scale);
}
self.held = Some(y);
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -490,6 +508,13 @@ impl PackScreen {
&& x <= (l.scroll_bar_x() + 6) as f64
&& y >= l.y as f64
&& y < l.bottom() as f64;
// updateScrolling: a press on the scroller holds it.
if scrolling {
self.lists[side].held = Some(y);
self.set_focus(Focus::List(side));
return Action::None;
}
let l = &self.lists[side];
let hit = l.entry_at(x as i32, y as i32);
let mut took = false;
if let Some(i) = hit {
@@ -516,7 +541,7 @@ impl PackScreen {
}
}
}
if took || scrolling {
if took {
self.set_focus(Focus::List(side));
}
return Action::None;
@@ -669,6 +694,18 @@ impl PackScreen {
false
}
/// The mouse wheel (`AbstractScrollArea.mouseScrolled`).
/// The pointer moved with the button held: a held scrollbar follows it.
pub fn drag(&mut self, y: f64) {
for l in &mut self.lists {
l.drag(y);
}
}
/// `onRelease`.
pub fn release(&mut self) {
for l in &mut self.lists {
l.held = None;
}
}
pub fn scroll(&mut self, x: f64, y: f64, amount: f64) {
if self.confirm.is_some() || self.search.contains(x, y) {
return;
+70 -63
View File
@@ -16,8 +16,9 @@ use std::{
mod tests;
mod world_management;
const LIST_TOP: f32 = 48.0;
const LIST_BOTTOM: f32 = 58.0;
/// SelectWorldScreen's `HeaderAndFooterLayout(8 + 9 + 8 + 20 + 4, 60)`.
const LIST_TOP: f32 = 49.0;
const LIST_BOTTOM: f32 = 60.0;
const ROW_HEIGHT: f32 = 36.0;
const ROW_WIDTH: f32 = 270.0;
@@ -124,7 +125,7 @@ pub struct Title {
pub started: std::time::Instant,
search: String,
search_focused: bool,
scroll: Cell<f32>,
scroll: Cell<crate::ui::Scroll>,
create_return_to_main: bool,
message_lines: Cell<usize>,
backup: Option<mpsc::Receiver<Result<u64, String>>>,
@@ -228,7 +229,7 @@ impl Title {
started: now,
search: String::new(),
search_focused: false,
scroll: Cell::new(0.0),
scroll: Cell::default(),
create_return_to_main: false,
message_lines: Cell::new(1),
backup: None,
@@ -252,7 +253,7 @@ impl Title {
pub fn enter_singleplayer(&mut self) {
self.search.clear();
self.search_focused = true;
self.scroll.set(0.0);
self.scroll.set(crate::ui::Scroll::default());
let worlds = list_worlds(&self.saves);
self.create_return_to_main = worlds.is_empty();
self.mode = if worlds.is_empty() {
@@ -324,10 +325,23 @@ impl Title {
.collect::<Vec<_>>()
.into_iter()
}
/// The world list's top, bottom, rows' left edge and content height.
fn world_list(&self, worlds: &[WorldEntry], (w, h): (f32, f32)) -> (f32, f32, f32, f32) {
let (w, h) = (w.ceil(), h.ceil());
let content = self.visible_worlds(worlds).count() as f32 * ROW_HEIGHT + 4.0;
let left = (w / 2.0).floor() - ROW_WIDTH / 2.0;
(LIST_TOP, h - LIST_BOTTOM, left, content)
}
/// The pointer moved with the button held (`mouseDragged`).
pub fn drag(&mut self, cursor: (f32, f32), size: (f32, f32)) {
if let Mode::Multiplayer(screen) = &mut self.mode {
screen.drag(cursor.1, size);
return screen.drag(cursor.1, size);
}
if let Mode::Select { worlds, .. } = &self.mode {
let (top, bottom, _, content) = self.world_list(worlds, size);
let mut scroll = self.scroll.get();
scroll.drag(cursor.1, top, bottom, content);
self.scroll.set(scroll);
}
}
/// The button was released (`onRelease`).
@@ -335,6 +349,7 @@ impl Title {
if let Mode::Multiplayer(screen) = &mut self.mode {
screen.release();
}
self.scroll.get_mut().release();
}
/// Mouse wheel over the classic world and server lists.
pub fn scroll_worlds(&mut self, delta: f32, cursor: (f32, f32), size: (f32, f32)) {
@@ -345,14 +360,13 @@ impl Title {
let Mode::Select { worlds, .. } = &self.mode else {
return;
};
if cursor.1 < LIST_TOP || cursor.1 >= size.1 - LIST_BOTTOM {
let (top, bottom, _, content) = self.world_list(worlds, size);
if cursor.1 < top || cursor.1 >= bottom {
return;
}
let max = (self.visible_worlds(worlds).count() as f32 * ROW_HEIGHT + 4.0
- (size.1 - LIST_TOP - LIST_BOTTOM))
.max(0.0);
self.scroll
.set((self.scroll.get() - delta * ROW_HEIGHT / 2.0).clamp(0.0, max));
let mut scroll = self.scroll.get();
scroll.wheel(delta, ROW_HEIGHT / 2.0, top, bottom, content);
self.scroll.set(scroll);
}
fn failed(&mut self, error: impl std::fmt::Display) {
let return_to = Box::new(self.mode.clone());
@@ -802,44 +816,33 @@ impl Title {
true,
);
}
let bottom = h - LIST_BOTTOM;
batch.fill(
atlas,
0.0,
LIST_TOP,
w,
bottom - LIST_TOP,
rgba(0, 0, 0, 100),
);
batch.fill(atlas, 0.0, LIST_TOP - 1.0, w, 1.0, rgba(160, 160, 160, 255));
batch.fill(atlas, 0.0, bottom, w, 1.0, rgba(160, 160, 160, 255));
let count = self.visible_worlds(worlds).count();
let max = (count as f32 * ROW_HEIGHT + 4.0 - (bottom - LIST_TOP)).max(0.0);
self.scroll.set(self.scroll.get().clamp(0.0, max));
let (top, bottom, left, content) = self.world_list(worlds, (w, h));
let mut scroll = self.scroll.get();
scroll.clamp(top, bottom, content);
self.scroll.set(scroll);
let offset = scroll.amount.trunc();
// AbstractSelectionList: background, rows clipped to the
// list, separators, scrollbar.
crate::ui::list_background(batch, atlas, top, bottom, false);
let first = batch.vertices.len();
let x = ((w - ROW_WIDTH) / 2.0).floor() + 2.0;
// Entry content sits 2 pixels inside its row.
let x = left + 2.0;
let mut slot = 0;
for (row, (i, world)) in self.visible_worlds(worlds).enumerate() {
let y = LIST_TOP + 5.0 + row as f32 * ROW_HEIGHT - self.scroll.get();
if y + 32.0 <= LIST_TOP || y >= bottom {
let y = top + 4.0 + row as f32 * ROW_HEIGHT - offset;
if y + 32.0 <= top || y >= bottom {
continue;
}
let mid = y - 2.0 + ROW_HEIGHT / 2.0;
if mid > LIST_TOP && mid < bottom {
if mid > top && mid < bottom {
crate::widgets::target_as(
[x - 2.0, y - 2.0, ROW_WIDTH, ROW_HEIGHT],
crate::widgets::Kind::Row,
);
}
// extractSelection: white once the list has focus.
if *selected == Some(i) {
batch.fill(
atlas,
x - 2.0,
y - 2.0,
ROW_WIDTH,
ROW_HEIGHT,
rgba(128, 128, 128, 255),
);
batch.fill(atlas, x - 2.0, y - 2.0, ROW_WIDTH, ROW_HEIGHT, u32::MAX);
batch.fill(
atlas,
x - 1.0,
@@ -888,8 +891,8 @@ impl Title {
}
// Clip list quads, including their UVs, to the content area.
for quad in batch.vertices[first..].as_chunks_mut::<6>().0 {
let top = quad.iter().map(|v| v.pos[1]).fold(f32::INFINITY, f32::min);
let bot = quad
let low = quad.iter().map(|v| v.pos[1]).fold(f32::INFINITY, f32::min);
let high = quad
.iter()
.map(|v| v.pos[1])
.fold(f32::NEG_INFINITY, f32::max);
@@ -899,27 +902,22 @@ impl Title {
.map(|v| v.uv[1])
.fold(f32::NEG_INFINITY, f32::max);
for v in quad {
v.pos[1] = v.pos[1].clamp(LIST_TOP, bottom);
if bot > top {
v.uv[1] = uv_top + (uv_bot - uv_top) * (v.pos[1] - top) / (bot - top);
v.pos[1] = v.pos[1].clamp(top, bottom);
if high > low {
v.uv[1] = uv_top + (uv_bot - uv_top) * (v.pos[1] - low) / (high - low);
}
}
}
if max > 0.0 {
let track = bottom - LIST_TOP;
let thumb =
(track * track / (count as f32 * ROW_HEIGHT + 4.0)).clamp(32.0, track);
let sx = (w / 2.0).floor() + ROW_WIDTH / 2.0 + 6.0;
batch.fill(atlas, sx, LIST_TOP, 6.0, track, rgba(0, 0, 0, 255));
batch.fill(
atlas,
sx,
LIST_TOP + self.scroll.get() / max * (track - thumb),
6.0,
thumb,
rgba(128, 128, 128, 255),
);
}
crate::ui::separators(batch, atlas, top, bottom, false);
crate::ui::scrollbar(
batch,
atlas,
left + ROW_WIDTH + 8.0,
top,
bottom,
content,
scroll.amount,
);
}
Mode::Create {
name, seed, focus, ..
@@ -1246,11 +1244,20 @@ impl Title {
(Mode::Select { .. }, None) => {
self.search_focused =
(21.0..41.0).contains(&y) && (x - (w / 2.0).floor()).abs() < 100.0;
if y >= LIST_TOP
&& y < h - LIST_BOTTOM
&& (x - (w / 2.0).floor()).abs() < ROW_WIDTH / 2.0
let Mode::Select { worlds, .. } = &self.mode else {
unreachable!()
};
let (top, bottom, left, content) = self.world_list(worlds, (w, h));
if self
.scroll
.get_mut()
.press((x, y), left + ROW_WIDTH + 8.0, top, bottom, content)
{
let row = ((y - LIST_TOP - 5.0 + self.scroll.get()) / ROW_HEIGHT).floor();
return Action::None;
}
if y >= top && y < bottom && x >= left && x < left + ROW_WIDTH {
let offset = self.scroll.get().amount.trunc();
let row = ((y - top - 2.0 + offset) / ROW_HEIGHT).floor();
if row >= 0.0 {
let Mode::Select { worlds, selected } = &self.mode else {
unreachable!()
@@ -1383,7 +1390,7 @@ impl Title {
&& let Mode::Select { selected, .. } = &mut self.mode
{
*selected = None;
self.scroll.set(0.0);
self.scroll.set(crate::ui::Scroll::default());
}
}
pub fn confirm_edit(&mut self) {
+24 -1
View File
@@ -414,7 +414,7 @@ fn recreate_uses_existing_name_seed_and_mode_and_scroll_reaches_later_rows() {
f.click("Cancel");
f.title
.scroll_worlds(-100.0, (240.0, 100.0), (480.0, 262.0));
assert!(f.title.scroll.get() > 0.0);
assert!(f.title.scroll.get().amount > 0.0);
let Mode::Select { worlds, .. } = &f.title.mode else {
panic!()
};
@@ -561,3 +561,26 @@ fn delete_removes_links_without_following_them() {
assert!(!f.title.saves.join("World").exists());
assert!(outside.join("keep.txt").is_file());
}
#[test]
fn scrolled_world_list_draws_inside_the_list_and_drags_its_scroller() {
let mut f = Fixture::new();
for i in 0..10 {
f.world(&format!("World{i}"), &format!("World {i}"));
}
f.select();
let size = (480.0, 262.0);
// SelectWorldScreen's list: 49 to h - 60, scroller at the row's right + 8.
let bar = (480.0 / 2.0 - ROW_WIDTH / 2.0 + ROW_WIDTH + 8.0 + 3.0, 60.0);
f.title.click(bar.0, bar.1, size);
f.title.drag((bar.0, 120.0), size);
let moved = f.title.scroll.get().amount;
assert!(moved > 0.0);
f.title.release();
f.title.drag((bar.0, 60.0), size);
assert_eq!(f.title.scroll.get().amount, moved);
let atlas = crate::hud::HudAtlas::build(Path::new("/nonexistent/horizon-test-assets")).unwrap();
let mut batch = HudBatch::new([480, 262]);
f.title.draw(&atlas, &mut batch, (0.0, 0.0), true);
assert!(!batch.vertices.is_empty());
}