Skip to content

Commit

Permalink
Take into account rounding errors during conversion from float to int
Browse files Browse the repository at this point in the history
  • Loading branch information
bwRavencl committed Dec 31, 2024
1 parent a7678dc commit f759e64
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ public void doAction(final Input input, final int component, final Float value)

var d = Input.normalize(Math.signum(value) * (float) Math.pow((absValue - deadZone) * 100f, exponent), -inMax,
inMax, -maxRelativeSpeed, maxRelativeSpeed) * input.getRateMultiplier();

d += remainingD;

if (Math.abs(d) < input.getPlanckLength()) {
remainingD = d;
} else {
remainingD = 0f;
return;
}

final var runMode = input.getRunMode();
final var oldValue = Input.normalize(input.getAxes().get(virtualAxis), runMode.getMinAxisValue(),
runMode.getMaxAxisValue(), -1f, 1f);
final var runMode = input.getRunMode();
final var oldValue = Input.normalize(input.getAxes().get(virtualAxis), runMode.getMinAxisValue(),
runMode.getMaxAxisValue(), -1f, 1f);

final var newValue = Math.min(Math.max(oldValue + (invert ? -d : d), minValue), maxValue);
input.setAxis(virtualAxis, newValue, hapticFeedback, detentValue);
}
final var newValue = Math.min(Math.max(oldValue + (invert ? -d : d), minValue), maxValue);
input.setAxis(virtualAxis, newValue, hapticFeedback, detentValue);

remainingD = 0f;
}

public Float getDetentValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,21 @@ public String getDescription(final Input input) {

void moveCursor(final Input input, float d) {
d = invert ? -d : d;

d += remainingD;

if (d >= -1f && d <= 1f) {
final var roundedD = Math.round(d);
if (roundedD == 0) {
remainingD = d;
} else {
remainingD = 0f;

final var intD = Math.round(d);
return;
}

if (axis == MouseAxis.X) {
input.setCursorDeltaX(input.getCursorDeltaX() + intD);
} else {
input.setCursorDeltaY(input.getCursorDeltaY() + intD);
}
if (axis == MouseAxis.X) {
input.setCursorDeltaX(input.getCursorDeltaX() + roundedD);
} else {
input.setCursorDeltaY(input.getCursorDeltaY() + roundedD);
}

remainingD = d - roundedD;
}

public void setAxis(final MouseAxis axis) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ public String getDescription(final Input input) {

void scroll(final Input input, float d) {
d = invert ? -d : d;

d += remainingD;

if (d >= -1f && d <= 1f) {
final var roundedD = Math.round(d);
if (roundedD == 0) {
remainingD = d;
} else {
remainingD = 0f;

input.setScrollClicks(Math.round(d));
return;
}

input.setScrollClicks(roundedD);
remainingD = d - roundedD;
}

public void setClicks(final int clicks) {
Expand Down

0 comments on commit f759e64

Please sign in to comment.