Fix floating point conversion errors for float settings

Not a pretty fix. This also fixes a visual bug with sliders and minimum values that are not equal to 0.
Closes #145
This commit is contained in:
Ridan Vandenbergh 2019-11-23 13:22:20 +01:00
parent d6f789f305
commit 0a7630c6c7
3 changed files with 18 additions and 11 deletions

View File

@ -81,17 +81,26 @@ public class SettingsPanel extends OrganisedContainer {
if (numberSetting.getMin() != null) slider.setMin(numberSetting.getMin().doubleValue());
addChild(slider);
} else {
Slider slider = new Slider(numberSetting.getValue().doubleValue(), numberSetting.getMin().doubleValue(), numberSetting.getMax().doubleValue(), Slider.getDefaultStep(numberSetting.getMin().doubleValue(), numberSetting.getMax().doubleValue()), name, setting instanceof IntegerSetting);
// Terrible terrible bug fix.
// I know, these parseDoubles look awful, but any conversions I tried here would end up with weird floating point conversion errors.
// This is really the easiest solution..
double value = Double.parseDouble(numberSetting.getValue().toString());
double min = Double.parseDouble(numberSetting.getMin().toString());
double max = Double.parseDouble(numberSetting.getMax().toString());
Slider slider = new Slider(
value, min, max,
Slider.getDefaultStep(min, max),
name,
setting instanceof IntegerSetting);
slider.addPoof(new Slider.SliderPoof<Slider, Slider.SliderPoof.SliderPoofInfo>() {
@Override
public void execute(Slider component, SliderPoofInfo info) {
if (setting instanceof IntegerSetting)
setting.setValue(new Integer((int) info.getNewValue()));
setting.setValue((int) info.getNewValue());
else if (setting instanceof FloatSetting)
setting.setValue(new Float(info.getNewValue()));
setting.setValue((float) info.getNewValue());
else if (setting instanceof DoubleSetting)
setting.setValue(info.getNewValue());
setModule(module);
}
});
addChild(slider);

View File

@ -21,7 +21,8 @@ public class RootSliderUI extends AbstractComponentUI<Slider> {
glColor4f(1,0.33f,0.33f,component.getOpacity());
glLineWidth(2.5f);
int height = component.getHeight();
double w = component.getWidth() * (component.getValue() / component.getMaximum());
double value = component.getValue();
double w = component.getWidth() * ((value - component.getMinimum()) / (component.getMaximum() - component.getMinimum()));
float downscale = 1.1f;
glBegin(GL_LINES);
{
@ -37,11 +38,7 @@ public class RootSliderUI extends AbstractComponentUI<Slider> {
glColor3f(1,0.33f,0.33f);
RenderHelper.drawCircle((int)w,height/downscale,2f);
String s = "";
if (Math.floor(component.getValue())==component.getValue())
s += (int) component.getValue();
else
s += component.getValue();
String s = value + "";
if (component.isPressed()){
w -= smallFontRenderer.getStringWidth(s)/2;
w = Math.max(0,Math.min(w, component.getWidth()-smallFontRenderer.getStringWidth(s)));

View File

@ -102,7 +102,8 @@ public class Slider extends AbstractComponent {
public void setValue(double value) {
SliderPoof.SliderPoofInfo info = new SliderPoof.SliderPoofInfo(this.value, value);
callPoof(SliderPoof.class, info);
this.value = integer ? (int) info.getNewValue() : info.getNewValue();
double newValue = info.getNewValue();
this.value = integer ? (int) newValue : newValue;
}
public static abstract class SliderPoof<T extends Component, S extends SliderPoof.SliderPoofInfo> extends Poof<T,S> {