]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/gui/IntSettingSlider.java
Change how keyboard events are handled
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / gui / IntSettingSlider.java
1 package com.irtimaled.bbor.client.gui;
2
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;
7
8 import java.util.HashMap;
9 import java.util.Map;
10
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<>();
16
17     final Setting<Integer> setting;
18     final int minValue;
19     final int range;
20     double sliderValue;
21
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;
27         this.prefix = prefix;
28         this.range = maxValue - minValue;
29         this.sliderValue = getSliderValue();
30         this.displayString = getDisplayValue();
31     }
32
33     IntSettingSlider addDisplayValue(int value, String displayValue) {
34         displayValues.put(value, displayValue);
35         if(setting.get() == value) {
36             this.displayString = getDisplayValue();
37         }
38         return this;
39     }
40
41     private String getDisplayValue() {
42         Integer value = setting.get();
43         return prefix + ": " + displayValues.getOrDefault(value, value.toString());
44     }
45
46     protected Integer getSettingValue() {
47         return MathHelper.clamp(minValue + (int) (range * sliderValue), minValue, maxValue);
48     }
49
50     protected double getSliderValue() {
51         return MathHelper.clamp((setting.get() - minValue) / (double) range, 0d, 1d);
52     }
53
54     @Override
55     protected int getHoverState(boolean p_getHoverState_1_) {
56         return 0;
57     }
58
59     @Override
60     protected void renderBg(Minecraft minecraft, int mouseX, int mouseY) {
61         if (this.dragging) {
62             changeSlider(mouseX);
63         }
64
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);
69     }
70
71     public final void onClick(double mouseX, double mouseY) {
72         changeSlider(mouseX);
73         this.dragging = true;
74     }
75
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();
81     }
82
83     public void onRelease(double p_onRelease_1_, double p_onRelease_3_) {
84         this.dragging = false;
85     }
86 }