1
0
Fork 0

Wrap ParamSlider granular dragging in a struct

This commit is contained in:
Robbert van der Helm 2022-11-11 23:13:18 +01:00
parent a2251509ee
commit 910afe7ec4

View file

@ -25,7 +25,7 @@ pub struct ParamSlider {
/// We keep track of the start coordinate and normalized value when holding down Shift while /// We keep track of the start coordinate and normalized value when holding down Shift while
/// dragging for higher precision dragging. This is a `None` value when granular dragging is not /// dragging for higher precision dragging. This is a `None` value when granular dragging is not
/// active. /// active.
granular_drag_start_x_value: Option<(f32, f32)>, granular_drag_status: Option<GranularDragStatus>,
// These fields are set through modifiers: // These fields are set through modifiers:
/// Whether or not to listen to scroll events for changing the parameter's value in steps. /// Whether or not to listen to scroll events for changing the parameter's value in steps.
@ -67,6 +67,15 @@ enum ParamSliderEvent {
TextInput(String), TextInput(String),
} }
// TODO: Vizia's lens derive macro requires this to be marked as pub
#[derive(Debug, Clone, Copy)]
pub struct GranularDragStatus {
/// The mouse's X-coordinate when the granular drag was started.
pub starting_x_coordinate: f32,
/// The normalized value when the granular drag was started.
pub starting_value: f32,
}
impl ParamSlider { impl ParamSlider {
/// Creates a new [`ParamSlider`] for the given parameter. To accommodate VIZIA's mapping system, /// Creates a new [`ParamSlider`] for the given parameter. To accommodate VIZIA's mapping system,
/// you'll need to provide a lens containing your `Params` implementation object (check out how /// you'll need to provide a lens containing your `Params` implementation object (check out how
@ -95,7 +104,7 @@ impl ParamSlider {
text_input_active: false, text_input_active: false,
drag_active: false, drag_active: false,
granular_drag_start_x_value: None, granular_drag_status: None,
use_scroll_wheel: true, use_scroll_wheel: true,
scrolled_lines: 0.0, scrolled_lines: 0.0,
@ -477,12 +486,12 @@ impl View for ParamSlider {
// edit the parameter without jumping to a new value // edit the parameter without jumping to a new value
self.param_base.begin_set_parameter(cx); self.param_base.begin_set_parameter(cx);
if cx.modifiers.shift() { if cx.modifiers.shift() {
self.granular_drag_start_x_value = Some(( self.granular_drag_status = Some(GranularDragStatus {
cx.mouse.cursorx, starting_x_coordinate: cx.mouse.cursorx,
self.param_base.unmodulated_normalized_value(), starting_value: self.param_base.unmodulated_normalized_value(),
)); });
} else { } else {
self.granular_drag_start_x_value = None; self.granular_drag_status = None;
self.set_normalized_value_drag( self.set_normalized_value_drag(
cx, cx,
util::remap_current_entity_x_coordinate(cx, cx.mouse.cursorx), util::remap_current_entity_x_coordinate(cx, cx.mouse.cursorx),
@ -518,25 +527,28 @@ impl View for ParamSlider {
// If shift is being held then the drag should be more granular instead of // If shift is being held then the drag should be more granular instead of
// absolute // absolute
if cx.modifiers.shift() { if cx.modifiers.shift() {
let (drag_start_x, drag_start_value) = let granular_drag_status =
*self.granular_drag_start_x_value.get_or_insert_with(|| { *self
( .granular_drag_status
cx.mouse.cursorx, .get_or_insert_with(|| GranularDragStatus {
self.param_base.unmodulated_normalized_value(), starting_x_coordinate: *x,
) starting_value: self.param_base.unmodulated_normalized_value(),
}); });
self.set_normalized_value_drag( self.set_normalized_value_drag(
cx, cx,
util::remap_current_entity_x_coordinate( util::remap_current_entity_x_coordinate(
cx, cx,
// This can be optimized a bit // This can be optimized a bit
util::remap_current_entity_x_t(cx, drag_start_value) util::remap_current_entity_x_t(
+ (*x - drag_start_x) * GRANULAR_DRAG_MULTIPLIER, cx,
granular_drag_status.starting_value,
) + ((*x - granular_drag_status.starting_x_coordinate)
* GRANULAR_DRAG_MULTIPLIER),
), ),
); );
} else { } else {
self.granular_drag_start_x_value = None; self.granular_drag_status = None;
self.set_normalized_value_drag( self.set_normalized_value_drag(
cx, cx,
@ -548,8 +560,8 @@ impl View for ParamSlider {
WindowEvent::KeyUp(_, Some(Key::Shift)) => { WindowEvent::KeyUp(_, Some(Key::Shift)) => {
// If this happens while dragging, snap back to reality uh I mean the current screen // If this happens while dragging, snap back to reality uh I mean the current screen
// position // position
if self.drag_active && self.granular_drag_start_x_value.is_some() { if self.drag_active && self.granular_drag_status.is_some() {
self.granular_drag_start_x_value = None; self.granular_drag_status = None;
self.param_base.set_normalized_value( self.param_base.set_normalized_value(
cx, cx,
util::remap_current_entity_x_coordinate(cx, cx.mouse.cursorx), util::remap_current_entity_x_coordinate(cx, cx.mouse.cursorx),