1 package com.irtimaled.bbor.client.gui;
3 import com.irtimaled.bbor.config.Setting;
4 import net.minecraft.client.Minecraft;
5 import net.minecraft.client.renderer.GlStateManager;
6 import net.minecraft.util.math.MathHelper;
8 import java.util.HashMap;
11 class IntSettingSlider extends Button implements IRenderableControl {
12 private final int maxValue;
13 private final String prefix;
14 private boolean dragging;
15 private Map<Integer, String> displayValues = new HashMap<>();
17 final Setting<Integer> setting;
22 IntSettingSlider(int id, int x, int y, int width, int minValue, int maxValue, String prefix, Setting<Integer> setting) {
23 super(id, x, y, width, "");
24 this.setting = setting;
25 this.minValue = minValue;
26 this.maxValue = maxValue;
28 this.range = maxValue - minValue;
29 this.sliderValue = getSliderValue();
30 this.displayString = getDisplayValue();
33 IntSettingSlider addDisplayValue(int value, String displayValue) {
34 displayValues.put(value, displayValue);
35 if(setting.get() == value) {
36 this.displayString = getDisplayValue();
41 private String getDisplayValue() {
42 Integer value = setting.get();
43 return prefix + ": " + displayValues.getOrDefault(value, value.toString());
46 protected Integer getSettingValue() {
47 return MathHelper.clamp(minValue + (int) (range * sliderValue), minValue, maxValue);
50 protected double getSliderValue() {
51 return MathHelper.clamp((setting.get() - minValue) / (double) range, 0d, 1d);
55 protected int getHoverState(boolean p_getHoverState_1_) {
60 protected void renderBg(Minecraft minecraft, int mouseX, int mouseY) {
65 minecraft.getTextureManager().bindTexture(BUTTON_TEXTURES);
66 GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
67 this.drawTexturedModalRect(this.x + (int) (this.sliderValue * (double) (this.width - 8)), this.y, 0, 66, 4, 20);
68 this.drawTexturedModalRect(this.x + (int) (this.sliderValue * (double) (this.width - 8)) + 4, this.y, 196, 66, 4, 20);
71 public final void onClick(double mouseX, double mouseY) {
76 private void changeSlider(double mouseX) {
77 double proportion = (mouseX - (double) (this.x + 4)) / (double) (this.width - 8);
78 this.sliderValue = MathHelper.clamp(proportion, 0d, 1d);
79 this.setting.set(this.getSettingValue());
80 this.displayString = this.getDisplayValue();
83 public void onRelease(double p_onRelease_1_, double p_onRelease_3_) {
84 this.dragging = false;