Skip to content

Commit

Permalink
ui scale fix (#279)
Browse files Browse the repository at this point in the history
* ui scale fix

* Update changelog

* Fix debug ui not being scale aware and add test to example

---------

Co-authored-by: Aevyrie <[email protected]>
  • Loading branch information
awtterpip and aevyrie authored Feb 23, 2024
1 parent a318858 commit e26601c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# UNRELEASED

- Added: support for `bevy_ui` `UiScale`.

# 0.17.0

- Update for bevy 0.12
Expand Down
14 changes: 13 additions & 1 deletion backends/bevy_picking_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use bevy_ui::{prelude::*, RelativeCursorPosition, UiStack};
use bevy_window::PrimaryWindow;

use bevy_picking_core::backend::prelude::*;
use bevy_picking_core::pointer::Location;

/// Commonly used imports for the [`bevy_picking_ui`](crate) crate.
pub mod prelude {
Expand Down Expand Up @@ -68,9 +69,11 @@ pub fn ui_picking(
cameras: Query<(Entity, &Camera, Option<&UiCameraConfig>)>,
primary_window: Query<Entity, With<PrimaryWindow>>,
ui_stack: Res<UiStack>,
ui_scale: Option<Res<UiScale>>,
mut node_query: Query<NodeQuery>,
mut output: EventWriter<PointerHits>,
) {
let ui_scale = ui_scale.map(|f| f.0).unwrap_or(1.0) as f32;
for (pointer, location) in pointers.iter().filter_map(|(pointer, pointer_location)| {
pointer_location
.location()
Expand All @@ -83,7 +86,16 @@ pub fn ui_picking(
}
false
})
.map(|loc| (pointer, loc))
.cloned()
.map(|loc| {
(
pointer,
Location {
position: loc.position / ui_scale,
..loc
},
)
})
}) {
let window_entity = primary_window.single();

Expand Down
1 change: 1 addition & 0 deletions examples/bevy_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn main() {
.add_plugins(DefaultPickingPlugins)
.add_systems(Startup, (setup, setup_3d))
.add_systems(Update, update_button_colors)
.insert_resource(UiScale(1.5))
.run();
}

Expand Down
5 changes: 3 additions & 2 deletions src/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ impl Debug for DebugName {
pub fn debug_draw(
mut commands: Commands,
pointers: Query<(Entity, &pointer::PointerId, &PointerDebug)>,
scale: Res<bevy_ui::UiScale>,
) {
use bevy_text::prelude::*;
use bevy_ui::prelude::*;
Expand All @@ -377,8 +378,8 @@ pub fn debug_draw(
),
style: Style {
position_type: PositionType::Absolute,
left: Val::Px(location.position.x + 5.0),
top: Val::Px(location.position.y + 5.0),
left: Val::Px(location.position.x + 5.0) / scale.0 as f32,
top: Val::Px(location.position.y + 5.0) / scale.0 as f32,
..Default::default()
},
..Default::default()
Expand Down

0 comments on commit e26601c

Please sign in to comment.