Skip to content
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

Add sensitivity #377

Merged
merged 15 commits into from
Aug 31, 2023
25 changes: 25 additions & 0 deletions src/axislike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub struct SingleAxis {
pub negative_low: f32,
/// Whether to invert output values from this axis.
pub inverted: bool,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can replace inverted with a negative sensitivity value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do this mathematically, but I think it makes the API a lot less clear. I think we should keep them separate, and add some kind of non-negative constraint on sensitivity (or at least a warning).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, fair. Let's do that then.

Copy link
Collaborator Author

@100-TomatoJuice 100-TomatoJuice Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but what does "that" refer to? I'm guessing merging sensitivity and inverted as you said below, but what to do about making the API less clear, as plof27 said. I'm guessing just write useful docs on sensitivity to explain how inverting works now? I think this is addressed by leaving invert() functions for use, as it uses the same API as before. It's just inverted means reversing the sign instead of flipping a bool.

Copy link
Collaborator Author

@100-TomatoJuice 100-TomatoJuice Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a side note, instead of removing the invert functions, I can just invert the sensitivity to do the same thing.

/// Returns this [`SingleAxis`] inverted.
#[must_use]
pub fn inverted(mut self) -> Self {
    self.sensitivity = -self.sensitivity;
    self
}

This helps to keep the ergonomics of the invert bool without having it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I like that :)

Copy link
Collaborator Author

@100-TomatoJuice 100-TomatoJuice Aug 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I like that :)

So, would you like that re-merged into one value? Or, close this as resolved and keep them separate. I am good with either solution.


pub sensitivity: f32,
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
/// The target value for this input, used for input mocking.
///
/// WARNING: this field is ignored for the sake of [`Eq`] and [`Hash`](std::hash::Hash)
Expand All @@ -45,6 +47,7 @@ impl SingleAxis {
positive_low: threshold,
negative_low: -threshold,
inverted: false,
sensitivity: 1.0,
value: None,
}
}
Expand All @@ -60,6 +63,7 @@ impl SingleAxis {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
value: Some(value),
}
}
Expand All @@ -72,6 +76,7 @@ impl SingleAxis {
positive_low: 0.,
negative_low: 0.,
inverted: false,
sensitivity: 1.0,
value: None,
}
}
Expand All @@ -84,6 +89,7 @@ impl SingleAxis {
positive_low: 0.,
negative_low: 0.,
inverted: false,
sensitivity: 1.0,
value: None,
}
}
Expand All @@ -96,6 +102,7 @@ impl SingleAxis {
positive_low: 0.,
negative_low: 0.,
inverted: false,
sensitivity: 1.0,
value: None,
}
}
Expand All @@ -108,6 +115,7 @@ impl SingleAxis {
positive_low: 0.,
negative_low: 0.,
inverted: false,
sensitivity: 1.0,
value: None,
}
}
Expand All @@ -121,6 +129,7 @@ impl SingleAxis {
negative_low: threshold,
positive_low: f32::MAX,
inverted: false,
sensitivity: 1.0,
value: None,
}
}
Expand All @@ -134,6 +143,7 @@ impl SingleAxis {
negative_low: f32::MIN,
positive_low: threshold,
inverted: false,
sensitivity: 1.0,
value: None,
}
}
Expand All @@ -146,6 +156,12 @@ impl SingleAxis {
self
}

#[must_use]
pub fn with_sensitivity(mut self, sensitivity: f32) -> SingleAxis{
self.sensitivity = sensitivity;
self
}

/// Returns this [`SingleAxis`] inverted.
#[must_use]
pub fn inverted(mut self) -> Self {
Expand All @@ -159,6 +175,7 @@ impl PartialEq for SingleAxis {
self.axis_type == other.axis_type
&& FloatOrd(self.positive_low) == FloatOrd(other.positive_low)
&& FloatOrd(self.negative_low) == FloatOrd(other.negative_low)
&& FloatOrd(self.sensitivity) == FloatOrd(other.sensitivity)
}
}
impl Eq for SingleAxis {}
Expand All @@ -167,6 +184,7 @@ impl std::hash::Hash for SingleAxis {
self.axis_type.hash(state);
FloatOrd(self.positive_low).hash(state);
FloatOrd(self.negative_low).hash(state);
FloatOrd(self.sensitivity).hash(state);
}
}

Expand Down Expand Up @@ -281,6 +299,13 @@ impl DualAxis {
self
}

#[must_use]
pub fn with_sensitivity(mut self, x_sensitivity: f32, y_sensitivity: f32) -> DualAxis{
self.x.sensitivity = x_sensitivity;
self.y.sensitivity = y_sensitivity;
self
}

/// Returns this [`DualAxis`] with an inverted X-axis.
#[must_use]
pub fn inverted_x(mut self) -> DualAxis {
Expand Down
4 changes: 2 additions & 2 deletions src/input_streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ impl<'a> InputStreams<'a> {
if value >= axis.negative_low && value <= axis.positive_low && include_deadzone {
0.0
} else if axis.inverted {
-value
-value * axis.sensitivity
} else {
value
value * axis.sensitivity
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![forbid(missing_docs)]
//#![forbid(missing_docs)]
#![forbid(unsafe_code)]
100-TomatoJuice marked this conversation as resolved.
Show resolved Hide resolved
#![warn(clippy::doc_markdown)]
#![doc = include_str!("../README.md")]
Expand Down
9 changes: 9 additions & 0 deletions tests/gamepad_axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ fn game_pad_single_axis_mocking() {
value: Some(-1.),
positive_low: 0.0,
negative_low: 0.0,
sensitivity: 1.0,
inverted: false,
};

Expand All @@ -99,13 +100,15 @@ fn game_pad_dual_axis_mocking() {
value: Some(1.),
positive_low: 0.0,
negative_low: 0.0,
sensitivity: 1.0,
inverted: false,
},
y: SingleAxis {
axis_type: AxisType::Gamepad(GamepadAxisType::LeftStickY),
value: Some(0.),
positive_low: 0.0,
negative_low: 0.0,
sensitivity: 1.0,
inverted: false,
},
deadzone: DualAxis::DEFAULT_DEADZONE_SHAPE,
Expand Down Expand Up @@ -137,6 +140,7 @@ fn game_pad_single_axis() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand All @@ -150,6 +154,7 @@ fn game_pad_single_axis() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand All @@ -163,6 +168,7 @@ fn game_pad_single_axis() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand All @@ -176,6 +182,7 @@ fn game_pad_single_axis() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand All @@ -190,6 +197,7 @@ fn game_pad_single_axis() {
positive_low: 0.1,
negative_low: 0.1,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand All @@ -203,6 +211,7 @@ fn game_pad_single_axis() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand Down
9 changes: 9 additions & 0 deletions tests/mouse_motion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ fn mouse_motion_single_axis_mocking() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};

app.send_input(input);
Expand All @@ -95,13 +96,15 @@ fn mouse_motion_dual_axis_mocking() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
},
y: SingleAxis {
axis_type: AxisType::MouseMotion(MouseMotionAxisType::Y),
value: Some(0.),
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
},
deadzone: DualAxis::DEFAULT_DEADZONE_SHAPE,
};
Expand Down Expand Up @@ -171,6 +174,7 @@ fn mouse_motion_single_axis() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand All @@ -184,6 +188,7 @@ fn mouse_motion_single_axis() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand All @@ -197,6 +202,7 @@ fn mouse_motion_single_axis() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand All @@ -210,6 +216,7 @@ fn mouse_motion_single_axis() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand All @@ -224,6 +231,7 @@ fn mouse_motion_single_axis() {
positive_low: 0.1,
negative_low: 0.1,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand All @@ -237,6 +245,7 @@ fn mouse_motion_single_axis() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand Down
9 changes: 9 additions & 0 deletions tests/mouse_wheel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ fn mouse_wheel_single_axis_mocking() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};

app.send_input(input);
Expand All @@ -96,13 +97,15 @@ fn mouse_wheel_dual_axis_mocking() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
},
y: SingleAxis {
axis_type: AxisType::MouseWheel(MouseWheelAxisType::Y),
value: Some(0.),
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
},
deadzone: DualAxis::DEFAULT_DEADZONE_SHAPE,
};
Expand Down Expand Up @@ -172,6 +175,7 @@ fn mouse_wheel_single_axis() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand All @@ -185,6 +189,7 @@ fn mouse_wheel_single_axis() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand All @@ -198,6 +203,7 @@ fn mouse_wheel_single_axis() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand All @@ -211,6 +217,7 @@ fn mouse_wheel_single_axis() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand All @@ -225,6 +232,7 @@ fn mouse_wheel_single_axis() {
positive_low: 0.1,
negative_low: 0.1,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand All @@ -238,6 +246,7 @@ fn mouse_wheel_single_axis() {
positive_low: 0.0,
negative_low: 0.0,
inverted: false,
sensitivity: 1.0,
};
app.send_input(input);
app.update();
Expand Down
Loading