Skip to content

Commit ffd0539

Browse files
authored
Use normalize_point helper method in widgets (#24020)
# Objective Closes #23909 ## Solution A simple refactoring. ## Testing Ran `examples/ui/widgets/vertical_slider.rs` and `examples/ui/widgets/feathers_gallery.rs` to see whether color plane and scrollbar still work as intended
1 parent c3dab1a commit ffd0539

3 files changed

Lines changed: 68 additions & 43 deletions

File tree

crates/bevy_feathers/src/controls/color_plane.rs

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,30 @@ fn update_plane_color(
289289
}
290290
}
291291

292+
fn emit_color_plane_value_change(
293+
commands: &mut Commands,
294+
source: Entity,
295+
node: &ComputedNode,
296+
node_target: &ComputedUiRenderTargetInfo,
297+
transform: &UiGlobalTransform,
298+
pointer_position: Vec2,
299+
ui_scale: f32,
300+
is_final: bool,
301+
) {
302+
let Some(pos) = node.normalize_point(
303+
*transform,
304+
pointer_position * node_target.scale_factor() / ui_scale,
305+
) else {
306+
return;
307+
};
308+
309+
commands.trigger(ValueChange {
310+
source,
311+
value: (pos + Vec2::splat(0.5)).clamp(Vec2::ZERO, Vec2::ONE),
312+
is_final,
313+
});
314+
}
315+
292316
fn on_pointer_press(
293317
mut press: On<Pointer<Press>>,
294318
q_color_planes: Query<Has<InteractionDisabled>, With<ColorPlane>>,
@@ -309,16 +333,16 @@ fn on_pointer_press(
309333
{
310334
press.propagate(false);
311335
if !disabled {
312-
let local_pos = transform.try_inverse().unwrap().transform_point2(
313-
press.pointer_location.position * node_target.scale_factor() / ui_scale.0,
336+
emit_color_plane_value_change(
337+
&mut commands,
338+
parent.0,
339+
node,
340+
node_target,
341+
transform,
342+
press.pointer_location.position,
343+
ui_scale.0,
344+
false,
314345
);
315-
let pos = local_pos / node.size() + Vec2::splat(0.5);
316-
let new_value = pos.clamp(Vec2::ZERO, Vec2::ONE);
317-
commands.trigger(ValueChange {
318-
source: parent.0,
319-
value: new_value,
320-
is_final: false,
321-
});
322346
}
323347
}
324348
}
@@ -361,16 +385,16 @@ fn on_drag(
361385
{
362386
drag.propagate(false);
363387
if state.0 && !disabled {
364-
let local_pos = transform.try_inverse().unwrap().transform_point2(
365-
drag.pointer_location.position * node_target.scale_factor() / ui_scale.0,
388+
emit_color_plane_value_change(
389+
&mut commands,
390+
parent.0,
391+
node,
392+
node_target,
393+
transform,
394+
drag.pointer_location.position,
395+
ui_scale.0,
396+
false,
366397
);
367-
let pos = local_pos / node.size() + Vec2::splat(0.5);
368-
let new_value = pos.clamp(Vec2::ZERO, Vec2::ONE);
369-
commands.trigger(ValueChange {
370-
source: parent.0,
371-
value: new_value,
372-
is_final: false,
373-
});
374398
}
375399
}
376400
}
@@ -398,16 +422,16 @@ fn on_drag_end(
398422
{
399423
drag_end.propagate(false);
400424
if state.0 && !disabled {
401-
let local_pos = transform.try_inverse().unwrap().transform_point2(
402-
drag_end.pointer_location.position * node_target.scale_factor() / ui_scale.0,
425+
emit_color_plane_value_change(
426+
&mut commands,
427+
parent.0,
428+
node,
429+
node_target,
430+
transform,
431+
drag_end.pointer_location.position,
432+
ui_scale.0,
433+
true,
403434
);
404-
let pos = local_pos / node.size() + Vec2::splat(0.5);
405-
let new_value = pos.clamp(Vec2::ZERO, Vec2::ONE);
406-
commands.trigger(ValueChange {
407-
source: parent.0,
408-
value: new_value,
409-
is_final: true,
410-
});
411435
}
412436
state.0 = false;
413437
}

crates/bevy_ui_widgets/src/scrollbar.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,12 @@ fn scrollbar_on_pointer_down(
145145
// If they click on the scrollbar track, page up or down.
146146
ev.propagate(false);
147147

148-
// Convert to widget-local coordinates.
149-
let local_pos = transform.try_inverse().unwrap().transform_point2(
148+
let Some(normalized_pos) = node.normalize_point(
149+
*transform,
150150
ev.event().pointer_location.position * node_target.scale_factor() / ui_scale.0,
151-
) + node.size() * 0.5;
151+
) else {
152+
return;
153+
};
152154

153155
// Bail if we don't find the target entity.
154156
let Ok((mut scroll_pos, scroll_content)) = q_scroll_pos.get_mut(scrollbar.target) else {
@@ -170,16 +172,12 @@ fn scrollbar_on_pointer_down(
170172

171173
match scrollbar.orientation {
172174
ControlOrientation::Horizontal => {
173-
if node.size().x > 0. {
174-
let click_pos = local_pos.x * content_size.x / node.size().x;
175-
adjust_scroll_pos(&mut scroll_pos.x, click_pos, visible_size.x, max_range.x);
176-
}
175+
let click_pos = (normalized_pos.x + 0.5) * content_size.x;
176+
adjust_scroll_pos(&mut scroll_pos.x, click_pos, visible_size.x, max_range.x);
177177
}
178178
ControlOrientation::Vertical => {
179-
if node.size().y > 0. {
180-
let click_pos = local_pos.y * content_size.y / node.size().y;
181-
adjust_scroll_pos(&mut scroll_pos.y, click_pos, visible_size.y, max_range.y);
182-
}
179+
let click_pos = (normalized_pos.y + 0.5) * content_size.y;
180+
adjust_scroll_pos(&mut scroll_pos.y, click_pos, visible_size.y, max_range.y);
183181
}
184182
}
185183
}

crates/bevy_ui_widgets/src/slider.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,12 @@ pub(crate) fn slider_on_pointer_down(
306306
.unwrap_or(0.0);
307307

308308
// Detect track click.
309-
let local_pos = transform.try_inverse().unwrap().transform_point2(
309+
let Some(normalized_pos) = node.normalize_point(
310+
*transform,
310311
press.pointer_location.position * node_target.scale_factor() / ui_scale.0,
311-
);
312+
) else {
313+
return;
314+
};
312315
let track_size = if is_vertical {
313316
node.size().y - thumb_size
314317
} else {
@@ -319,13 +322,13 @@ pub(crate) fn slider_on_pointer_down(
319322
let click_val = if track_size > 0. {
320323
if is_vertical {
321324
// For vertical sliders: bottom-to-top (0 at bottom, max at top)
322-
// local_pos.y ranges from -height/2 (top) to +height/2 (bottom)
323-
let y_from_bottom = (node.size().y / 2.0) - local_pos.y;
325+
// normalized_pos.y ranges from -0.5 (top) to +0.5 (bottom)
326+
let y_from_bottom = (0.5 - normalized_pos.y) * node.size().y;
324327
let adjusted_y = y_from_bottom - thumb_size / 2.0;
325328
adjusted_y * range.span() / track_size + range.start()
326329
} else {
327330
// For horizontal sliders: convert from center-origin to left-origin
328-
let x_from_left = local_pos.x + node.size().x / 2.0;
331+
let x_from_left = (normalized_pos.x + 0.5) * node.size().x;
329332
let adjusted_x = x_from_left - thumb_size / 2.0;
330333
adjusted_x * range.span() / track_size + range.start()
331334
}

0 commit comments

Comments
 (0)