From cae9c8957d1ac767edcb106e838e2d379be416b9 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Thu, 6 Apr 2023 02:01:34 +0200 Subject: [PATCH] Cope for negative multiplier values for slider/spinner --- .../openrocket/gui/adaptors/DoubleModel.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/swing/src/net/sf/openrocket/gui/adaptors/DoubleModel.java b/swing/src/net/sf/openrocket/gui/adaptors/DoubleModel.java index 7aee2b8a6..6442c6b4a 100644 --- a/swing/src/net/sf/openrocket/gui/adaptors/DoubleModel.java +++ b/swing/src/net/sf/openrocket/gui/adaptors/DoubleModel.java @@ -123,7 +123,8 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat @Override public Object getNextValue() { double d = currentUnit.toUnit(DoubleModel.this.getValue()); - double max = currentUnit.toUnit(maxValue); + boolean inverted = DoubleModel.this.currentUnit.getMultiplier() < 0; + double max = inverted ? currentUnit.toUnit(minValue) : currentUnit.toUnit(maxValue); if (MathUtil.equals(d, max)) return null; d = currentUnit.getNextValue(d); @@ -135,7 +136,8 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat @Override public Object getPreviousValue() { double d = currentUnit.toUnit(DoubleModel.this.getValue()); - double min = currentUnit.toUnit(minValue); + boolean inverted = DoubleModel.this.currentUnit.getMultiplier() < 0; + double min = inverted ? currentUnit.toUnit(maxValue) : currentUnit.toUnit(minValue); if (MathUtil.equals(d, min)) return null; d = currentUnit.getPreviousValue(d); @@ -315,11 +317,12 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat @Override public int getValue() { double value = DoubleModel.this.getValue(); + boolean inverted = DoubleModel.this.getCurrentUnit().getMultiplier() < 0; if (value <= min.getValue()) - return 0; + return inverted ? MAX : 0; if (value >= max.getValue()) - return MAX; - + return inverted ? 0 : MAX; + double x; if ((value <= mid.getValue()) || (quad2 == 0)) { // If quad 2 is 0, the midpoint is perfectly in center // Use linear scale @@ -333,6 +336,9 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat // a*x^2 + b*x + c-value == 0 x = (MathUtil.safeSqrt(quad1 * quad1 - 4 * quad2 * (quad0 - value)) - quad1) / (2 * quad2); } + if (inverted) { + x = 1 - x; + } return (int) (x * MAX); } @@ -345,8 +351,12 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat " value=" + newValue + ", currently firing events"); return; } - + + boolean inverted = DoubleModel.this.getCurrentUnit().getMultiplier() < 0; double x = (double) newValue / MAX; + if (inverted) { + x = 1 - x; + } double scaledValue; if (x <= linearPosition) {