Skip to content

Commit

Permalink
Add RoundingPrecisionSetting from #801
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander01998 committed Apr 4, 2024
1 parent 583e57e commit eb43316
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2014-2024 Wurst-Imperium and contributors.
*
* This source code is subject to the terms of the GNU General Public
* License, version 3. If a copy of the GPL was not distributed with this
* file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt
*/
package net.wurstclient.settings;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

public final class RoundingPrecisionSetting extends SliderSetting
{
private static final DecimalFormatSymbols SYMBOLS =
new DecimalFormatSymbols(Locale.ENGLISH);

private final DecimalFormat[] FORMATS;

public RoundingPrecisionSetting(String name, String description, int value,
int min, int max)
{
super(name, description, value, min, max, 1,
ValueDisplay.ROUNDING_PRECISION);

if(min < 0)
throw new IllegalArgumentException(
"min must be greater than or equal to 0");

FORMATS = new DecimalFormat[max + 1];
}

public DecimalFormat getFormat()
{
int value = getValueI();

if(FORMATS[value] == null)
{
String pattern = "0";
if(value > 0)
pattern += "." + "#".repeat(value);

FORMATS[value] = new DecimalFormat(pattern, SYMBOLS);
}

return FORMATS[value];
}

public String format(double value)
{
return getFormat().format(value);
}
}
3 changes: 3 additions & 0 deletions src/main/java/net/wurstclient/settings/SliderSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ public String getValueString(double v)

public static final ValueDisplay DEGREES = INTEGER.withSuffix("\u00b0");

public static final ValueDisplay ROUNDING_PRECISION =
v -> (int)v == 0 ? "1" : "0." + "0".repeat((int)v - 1) + "1";

public static final ValueDisplay NONE = v -> "";

public String getValueString(double value);
Expand Down

0 comments on commit eb43316

Please sign in to comment.