Skip to content

Commit c0fa10b

Browse files
authored
slider widget track click position UiScale fix (#19676)
# Objective The position for track clicks in `core_slider` is calculated incorrectly when using `UiScale`. ## Solution `trigger.event().pointer_location.position` uses logical window coordinates, that is: `position = physical_position / window_scale_factor` while `ComputedNodeTarget::scale_factor` returns the window scale factor multiplied by Ui Scale: `target_scale_factor = window_scale_factor * ui_scale` So to get the physical position need to divide by the `UiScale`: ``` position * target_scale_factor / ui_scale = (physical_postion / window_scale_factor) * (window_scale_factor * ui_scale) / ui_scale = physical_position ``` I thought this was fixed during the slider PR review, but must have got missed somewhere or lost in a merge. ## Testing Can test using the `core_widgets` example` with `.insert_resource(UiScale(2.))` added to the bevy app.
1 parent 209866c commit c0fa10b

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

crates/bevy_core_widgets/src/core_slider.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ pub(crate) fn slider_on_pointer_down(
211211
focus: Option<ResMut<InputFocus>>,
212212
focus_visible: Option<ResMut<InputFocusVisible>>,
213213
mut commands: Commands,
214+
ui_scale: Res<UiScale>,
214215
) {
215216
if q_thumb.contains(trigger.target()) {
216217
// Thumb click, stop propagation to prevent track click.
@@ -255,7 +256,7 @@ pub(crate) fn slider_on_pointer_down(
255256

256257
// Detect track click.
257258
let local_pos = transform.try_inverse().unwrap().transform_point2(
258-
trigger.event().pointer_location.position * node_target.scale_factor(),
259+
trigger.event().pointer_location.position * node_target.scale_factor() / ui_scale.0,
259260
);
260261
let track_width = node.size().x - thumb_size;
261262
// Avoid division by zero

0 commit comments

Comments
 (0)