Skip to content

Commit

Permalink
Fixed ScrollPosition to use logical coordinates.
Browse files Browse the repository at this point in the history
  • Loading branch information
ickshonpe committed Dec 2, 2024
1 parent 56d5591 commit 72b094b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 9 additions & 3 deletions crates/bevy_ui/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,14 +433,20 @@ with UI components as a child of an entity without UI components, your UI layout

let content_size = Vec2::new(layout.content_size.width, layout.content_size.height);
let max_possible_offset = (content_size - layout_size).max(Vec2::ZERO);
let clamped_scroll_position = scroll_position.clamp(Vec2::ZERO, max_possible_offset);
let clamped_scroll_position = scroll_position.clamp(
Vec2::ZERO,
max_possible_offset * inverse_target_scale_factor,
);

if clamped_scroll_position != scroll_position {
commands
.entity(entity)
.insert(ScrollPosition::from(&clamped_scroll_position));
.insert(ScrollPosition::from(clamped_scroll_position));
}

let physical_scroll_position =
(clamped_scroll_position / inverse_target_scale_factor).round();

for child_uinode in ui_children.iter_ui_children(entity) {
update_uinode_geometry_recursive(
commands,
Expand All @@ -451,7 +457,7 @@ with UI components as a child of an entity without UI components, your UI layout
ui_children,
inverse_target_scale_factor,
layout_size,
clamped_scroll_position,
physical_scroll_position,
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ impl Default for ComputedNode {
#[derive(Component, Debug, Clone, Reflect)]
#[reflect(Component, Default)]
pub struct ScrollPosition {
/// How far across the node is scrolled, in pixels. (0 = not scrolled / scrolled to right)
/// How far across the node is scrolled, in logical pixels. (0 = not scrolled / scrolled to right)
pub offset_x: f32,
/// How far down the node is scrolled, in pixels. (0 = not scrolled / scrolled to top)
/// How far down the node is scrolled, in logical pixels. (0 = not scrolled / scrolled to top)
pub offset_y: f32,
}

Expand All @@ -263,8 +263,8 @@ impl From<&ScrollPosition> for Vec2 {
}
}

impl From<&Vec2> for ScrollPosition {
fn from(vec: &Vec2) -> Self {
impl From<Vec2> for ScrollPosition {
fn from(vec: Vec2) -> Self {
ScrollPosition {
offset_x: vec.x,
offset_y: vec.y,
Expand Down

0 comments on commit 72b094b

Please sign in to comment.