Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only use physical coords internally in bevy_ui #16375

Merged
merged 31 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
06e9915
Store physical coords in Node and use them in rendering.
ickshonpe Nov 13, 2024
4b711b7
clean up
ickshonpe Nov 13, 2024
9cfb164
Use physical coords in focus
ickshonpe Nov 13, 2024
c6f831b
use physical viewport in picking_backend
ickshonpe Nov 13, 2024
a3d2e2c
convert pointer position to physical coords in picking_backend
ickshonpe Nov 13, 2024
f7ad8d1
Removed some comments, updated some others.
ickshonpe Nov 13, 2024
456ef4d
fixed `ui_rounding_test`
ickshonpe Nov 13, 2024
c922960
Remove unneeded `map` and associated comment.
ickshonpe Nov 13, 2024
5112975
Merge branch 'main' into ui-physical-coords
ickshonpe Nov 18, 2024
1d088aa
removed scaling vertex attribute from `ui.wgsl`
ickshonpe Nov 18, 2024
5617e40
Removed `inverse_scale_factor` fields from UiVertex and TransparentUI
ickshonpe Nov 18, 2024
6ad21ca
remove unused aa type
ickshonpe Nov 18, 2024
bd3d67d
Merge branch 'main' into ui-physical-coords
ickshonpe Nov 21, 2024
2303fd7
Update crates/bevy_ui/src/render/mod.rs
ickshonpe Nov 21, 2024
d603d25
Update crates/bevy_ui/src/render/mod.rs
ickshonpe Nov 21, 2024
5812de2
Removed scaling for textbounds
ickshonpe Nov 21, 2024
644ba3e
Added inverse scale factor field to ComputedNode
ickshonpe Nov 21, 2024
68e413f
Added Doc comments for inverse scale factor field and accessor method.
ickshonpe Nov 21, 2024
0f89d74
scale node size before computing texture slices
ickshonpe Nov 21, 2024
f500017
added extra cases to ui_texture_slice_and_flip example
ickshonpe Nov 21, 2024
6d91c00
Merge branch 'ui-physical-coords' of https://github.com/ickshonpe/bev…
ickshonpe Nov 21, 2024
02419d6
Merge branch 'ui-physical-coords-node-sf' into ui-physical-coords
ickshonpe Nov 21, 2024
7047e34
apply scale factor to overflow clip margin
ickshonpe Nov 21, 2024
7722ce8
Fixed outlines resolver
ickshonpe Nov 21, 2024
56f3313
cleanup outline calculations
ickshonpe Nov 21, 2024
e701a78
Fixed inner corner radius clamping
ickshonpe Nov 21, 2024
a0fe4f8
Fixed imports
ickshonpe Nov 21, 2024
d9afb58
fix shadow scalings
ickshonpe Nov 21, 2024
0ea34ae
Fixed `overflow_debug` example
ickshonpe Nov 21, 2024
81c189b
Merge branch 'main' into ui-physical-coords
ickshonpe Nov 21, 2024
68134e4
Removed unneeded import
ickshonpe Nov 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
CalculatedClip, ComputedNode, DefaultUiCamera, ResolvedBorderRadius, TargetCamera, UiScale,
UiStack,
CalculatedClip, ComputedNode, DefaultUiCamera, ResolvedBorderRadius, TargetCamera, UiStack,
};
use bevy_ecs::{
change_detection::DetectChangesMut,
Expand Down Expand Up @@ -158,7 +157,6 @@ pub fn ui_focus_system(
windows: Query<&Window>,
mouse_button_input: Res<ButtonInput<MouseButton>>,
touches_input: Res<Touches>,
ui_scale: Res<UiScale>,
ui_stack: Res<UiStack>,
mut node_query: Query<NodeQuery>,
) {
Expand Down Expand Up @@ -201,19 +199,16 @@ pub fn ui_focus_system(
};

let viewport_position = camera
.logical_viewport_rect()
.map(|rect| rect.min)
.physical_viewport_rect()
.map(|rect| rect.min.as_vec2())
.unwrap_or_default();
windows
.get(window_ref.entity())
.ok()
.and_then(Window::cursor_position)
.and_then(Window::physical_cursor_position)
.or_else(|| touches_input.first_pressed_position())
.map(|cursor_position| (entity, cursor_position - viewport_position))
})
// The cursor position returned by `Window` only takes into account the window scale factor and not `UiScale`.
// To convert the cursor position to logical UI viewport coordinates we have to divide it by `UiScale`.
.map(|(entity, cursor_position)| (entity, cursor_position / ui_scale.0))
.collect();

// prepare an iterator that contains all the nodes that have the cursor in their rect,
Expand Down
28 changes: 10 additions & 18 deletions crates/bevy_ui/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,6 @@ with UI components as a child of an entity without UI components, your UI layout
});

for (camera_id, mut camera) in camera_layout_info.drain() {
let inverse_target_scale_factor = camera.scale_factor.recip();

ui_surface.compute_camera_layout(camera_id, camera.size, text_buffers, &mut font_system);

for root in &camera.root_nodes {
Expand All @@ -305,7 +303,6 @@ with UI components as a child of an entity without UI components, your UI layout
None,
&mut node_transform_query,
&ui_children,
inverse_target_scale_factor,
Vec2::ZERO,
Vec2::ZERO,
);
Expand All @@ -330,7 +327,6 @@ with UI components as a child of an entity without UI components, your UI layout
Option<&ScrollPosition>,
)>,
ui_children: &UiChildren,
inverse_target_scale_factor: f32,
parent_size: Vec2,
parent_scroll_position: Vec2,
) {
Expand All @@ -343,15 +339,13 @@ with UI components as a child of an entity without UI components, your UI layout
maybe_scroll_position,
)) = node_transform_query.get_mut(entity)
{
let Ok((layout, unrounded_unscaled_size)) = ui_surface.get_layout(entity) else {
let Ok((layout, unrounded_size)) = ui_surface.get_layout(entity) else {
return;
};

let layout_size =
inverse_target_scale_factor * Vec2::new(layout.size.width, layout.size.height);
let unrounded_size = inverse_target_scale_factor * unrounded_unscaled_size;
let layout_location =
inverse_target_scale_factor * Vec2::new(layout.location.x, layout.location.y);
let layout_size = Vec2::new(layout.size.width, layout.size.height);

let layout_location = Vec2::new(layout.location.x, layout.location.y);

// The position of the center of the node, stored in the node's transform
let node_center =
Expand All @@ -364,10 +358,10 @@ with UI components as a child of an entity without UI components, your UI layout
}

let taffy_rect_to_border_rect = |rect: taffy::Rect<f32>| BorderRect {
left: rect.left * inverse_target_scale_factor,
right: rect.right * inverse_target_scale_factor,
top: rect.top * inverse_target_scale_factor,
bottom: rect.bottom * inverse_target_scale_factor,
left: rect.left,
right: rect.right,
top: rect.top,
bottom: rect.bottom,
};

node.bypass_change_detection().border = taffy_rect_to_border_rect(layout.border);
Expand Down Expand Up @@ -422,8 +416,7 @@ with UI components as a child of an entity without UI components, your UI layout
})
.unwrap_or_default();

let content_size = Vec2::new(layout.content_size.width, layout.content_size.height)
* inverse_target_scale_factor;
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);

Expand All @@ -441,7 +434,6 @@ with UI components as a child of an entity without UI components, your UI layout
Some(viewport_size),
node_transform_query,
ui_children,
inverse_target_scale_factor,
layout_size,
clamped_scroll_position,
);
Expand Down Expand Up @@ -1131,7 +1123,7 @@ mod tests {
.sum();
let parent_width = world.get::<ComputedNode>(parent).unwrap().size.x;
assert!((width_sum - parent_width).abs() < 0.001);
assert!((width_sum - 320.).abs() <= 1.);
assert!((width_sum - 320. * s).abs() <= 1.);
s += r;
}
}
Expand Down
11 changes: 5 additions & 6 deletions crates/bevy_ui/src/picking_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ pub fn ui_picking(
camera_query: Query<(Entity, &Camera, Has<IsDefaultUiCamera>)>,
default_ui_camera: DefaultUiCamera,
primary_window: Query<Entity, With<PrimaryWindow>>,
ui_scale: Res<UiScale>,
ui_stack: Res<UiStack>,
node_query: Query<NodeQuery>,
mut output: EventWriter<PointerHits>,
Expand Down Expand Up @@ -95,15 +94,15 @@ pub fn ui_picking(
let Ok((_, camera_data, _)) = camera_query.get(camera) else {
continue;
};
let mut pointer_pos = pointer_location.position;
if let Some(viewport) = camera_data.logical_viewport_rect() {
pointer_pos -= viewport.min;
let mut pointer_pos =
pointer_location.position * camera_data.target_scaling_factor().unwrap_or(1.);
if let Some(viewport) = camera_data.physical_viewport_rect() {
pointer_pos -= viewport.min.as_vec2();
}
let scaled_pointer_pos = pointer_pos / **ui_scale;
pointer_pos_by_camera
.entry(camera)
.or_default()
.insert(pointer_id, scaled_pointer_pos);
.insert(pointer_id, pointer_pos);
}
}

Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ui/src/render/box_shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ pub fn queue_shadows(
),
batch_range: 0..0,
extra_index: PhaseItemExtraIndex::NONE,
inverse_scale_factor: 1.,
});
}
}
Expand Down
Loading
Loading