-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
SliderPrecision component #20032
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
Merged
+75
−12
Merged
SliderPrecision component #20032
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,6 +19,7 @@ use bevy_input::keyboard::{KeyCode, KeyboardInput}; | |||||
use bevy_input::ButtonState; | ||||||
use bevy_input_focus::FocusedInput; | ||||||
use bevy_log::warn_once; | ||||||
use bevy_math::ops; | ||||||
use bevy_picking::events::{Drag, DragEnd, DragStart, Pointer, Press}; | ||||||
use bevy_ui::{ComputedNode, ComputedNodeTarget, InteractionDisabled, UiGlobalTransform, UiScale}; | ||||||
|
||||||
|
@@ -38,7 +39,8 @@ pub enum TrackClick { | |||||
|
||||||
/// A headless slider widget, which can be used to build custom sliders. Sliders have a value | ||||||
/// (represented by the [`SliderValue`] component) and a range (represented by [`SliderRange`]). An | ||||||
/// optional step size can be specified via [`SliderStep`]. | ||||||
/// optional step size can be specified via [`SliderStep`], and you can control the rounding | ||||||
/// during dragging with [`SliderPrecision`]. | ||||||
/// | ||||||
/// You can also control the slider remotely by triggering a [`SetSliderValue`] event on it. This | ||||||
/// can be useful in a console environment for controlling the value gamepad inputs. | ||||||
|
@@ -187,6 +189,25 @@ impl Default for SliderStep { | |||||
} | ||||||
} | ||||||
|
||||||
/// A component which controls the rounding of the slider value during dragging. | ||||||
/// | ||||||
/// Stepping is not affected, although presumably the step size will be an integer multiple of the | ||||||
/// rounding factor. This also doesn't prevent the slider value from being set to non-rounded values | ||||||
/// by other means, such as manually entering digits via a numeric input field. | ||||||
/// | ||||||
/// The value in this component represents the number of decimal places of desired precision, so a | ||||||
/// value of 2 would round to the nearest 1/100th. A value of -3 would round to the nearest | ||||||
/// thousand. | ||||||
#[derive(Component, Debug, Default, Clone, Copy)] | ||||||
pub struct SliderPrecision(pub i32); | ||||||
|
||||||
impl SliderPrecision { | ||||||
fn round(&self, value: f32) -> f32 { | ||||||
let factor = ops::powf(10.0_f32, self.0 as f32); | ||||||
(value * factor).round() / factor | ||||||
} | ||||||
} | ||||||
|
||||||
/// Component used to manage the state of a slider during dragging. | ||||||
#[derive(Component, Default)] | ||||||
pub struct CoreSliderDragState { | ||||||
|
@@ -204,6 +225,7 @@ pub(crate) fn slider_on_pointer_down( | |||||
&SliderValue, | ||||||
&SliderRange, | ||||||
&SliderStep, | ||||||
Option<&SliderPrecision>, | ||||||
&ComputedNode, | ||||||
&ComputedNodeTarget, | ||||||
&UiGlobalTransform, | ||||||
|
@@ -217,8 +239,17 @@ pub(crate) fn slider_on_pointer_down( | |||||
if q_thumb.contains(trigger.target()) { | ||||||
// Thumb click, stop propagation to prevent track click. | ||||||
trigger.propagate(false); | ||||||
} else if let Ok((slider, value, range, step, node, node_target, transform, disabled)) = | ||||||
q_slider.get(trigger.target()) | ||||||
} else if let Ok(( | ||||||
slider, | ||||||
value, | ||||||
range, | ||||||
step, | ||||||
precision, | ||||||
node, | ||||||
node_target, | ||||||
transform, | ||||||
disabled, | ||||||
)) = q_slider.get(trigger.target()) | ||||||
{ | ||||||
// Track click | ||||||
trigger.propagate(false); | ||||||
|
@@ -257,7 +288,9 @@ pub(crate) fn slider_on_pointer_down( | |||||
value.0 + step.0 | ||||||
} | ||||||
} | ||||||
TrackClick::Snap => click_val, | ||||||
TrackClick::Snap => precision | ||||||
.map(|prec| prec.round(click_val)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is already clamped, see previous lines. |
||||||
.unwrap_or(click_val), | ||||||
}); | ||||||
|
||||||
if matches!(slider.on_change, Callback::Ignore) { | ||||||
|
@@ -296,6 +329,7 @@ pub(crate) fn slider_on_drag( | |||||
&ComputedNode, | ||||||
&CoreSlider, | ||||||
&SliderRange, | ||||||
Option<&SliderPrecision>, | ||||||
&UiGlobalTransform, | ||||||
&mut CoreSliderDragState, | ||||||
Has<InteractionDisabled>, | ||||||
|
@@ -305,7 +339,8 @@ pub(crate) fn slider_on_drag( | |||||
mut commands: Commands, | ||||||
ui_scale: Res<UiScale>, | ||||||
) { | ||||||
if let Ok((node, slider, range, transform, drag, disabled)) = q_slider.get_mut(trigger.target()) | ||||||
if let Ok((node, slider, range, precision, transform, drag, disabled)) = | ||||||
q_slider.get_mut(trigger.target()) | ||||||
{ | ||||||
trigger.propagate(false); | ||||||
if drag.dragging && !disabled { | ||||||
|
@@ -320,17 +355,22 @@ pub(crate) fn slider_on_drag( | |||||
let slider_width = ((node.size().x - thumb_size) * node.inverse_scale_factor).max(1.0); | ||||||
let span = range.span(); | ||||||
let new_value = if span > 0. { | ||||||
range.clamp(drag.offset + (distance.x * span) / slider_width) | ||||||
drag.offset + (distance.x * span) / slider_width | ||||||
} else { | ||||||
range.start() + span * 0.5 | ||||||
}; | ||||||
let rounded_value = range.clamp( | ||||||
precision | ||||||
.map(|prec| prec.round(new_value)) | ||||||
.unwrap_or(new_value), | ||||||
); | ||||||
|
||||||
if matches!(slider.on_change, Callback::Ignore) { | ||||||
commands | ||||||
.entity(trigger.target()) | ||||||
.insert(SliderValue(new_value)); | ||||||
.insert(SliderValue(rounded_value)); | ||||||
} else { | ||||||
commands.notify_with(&slider.on_change, new_value); | ||||||
commands.notify_with(&slider.on_change, rounded_value); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
@@ -491,3 +531,24 @@ impl Plugin for CoreSliderPlugin { | |||||
.add_observer(slider_on_set_value); | ||||||
} | ||||||
} | ||||||
|
||||||
#[cfg(test)] | ||||||
mod tests { | ||||||
use super::*; | ||||||
|
||||||
#[test] | ||||||
fn test_slider_precision_rounding() { | ||||||
// Test positive precision values (decimal places) | ||||||
let precision_2dp = SliderPrecision(2); | ||||||
assert_eq!(precision_2dp.round(1.234567), 1.23); | ||||||
assert_eq!(precision_2dp.round(1.235), 1.24); | ||||||
|
||||||
// Test zero precision (rounds to integers) | ||||||
let precision_0dp = SliderPrecision(0); | ||||||
assert_eq!(precision_0dp.round(1.4), 1.0); | ||||||
|
||||||
// Test negative precision (rounds to tens, hundreds, etc.) | ||||||
let precision_neg1 = SliderPrecision(-1); | ||||||
assert_eq!(precision_neg1.round(14.0), 10.0); | ||||||
} | ||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.