1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
package com.arslaancodes.arslaansmagichax.screens.clickgui.setting;
import com.arslaancodes.arslaansmagichax.modules.settings.NumberSetting;
import com.arslaancodes.arslaansmagichax.modules.settings.Setting;
import com.arslaancodes.arslaansmagichax.screens.clickgui.ModuleButton;
import java.awt.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.network.chat.Component;
public class Slider extends Component {
public NumberSetting numberSetting = (NumberSetting) setting;
private boolean sliding = false;
public Slider(Setting setting, ModuleButton parent, int offset) {
super(setting, parent, offset);
this.numberSetting = (NumberSetting) setting;
}
@Override
public void render(DrawContext context, int mouseX, int mouseY, float deltaTicks) {
String displayText = numberSetting.getName() + ": " + numberSetting.getValue();
int defaultPadding = 5;
int boxWidth = parent.parent.width;
int alpha = isHovered(mouseX, mouseY) ? 230 : 160;
context.fill(
parent.parent.x,
parent.parent.y + parent.offset + offset,
parent.parent.x + boxWidth,
parent.parent.y + parent.offset + offset + parent.parent.height,
new Color(30, 30, 47, alpha).getRGB()
);
double diff = Math.min(boxWidth, Math.max(0, mouseX - parent.parent.x));
int renderWidth = (int) (boxWidth * (numberSetting.getValue() - numberSetting.getMin()) / (numberSetting.getMax() - numberSetting.getMin()));
context.fill(
parent.parent.x,
parent.parent.y + parent.offset + offset,
parent.parent.x + renderWidth,
parent.parent.y + parent.offset + offset + parent.parent.height,
new Color(252, 210, 77, 255).getRGB()
);
if (sliding) {
if (diff == 0) {
numberSetting.setValue(numberSetting.getMin());
} else {
numberSetting.setValue(roundToPlace((diff / boxWidth) * (numberSetting.getMax() - numberSetting.getMin()) + numberSetting.getMin(), 1));
}
}
int textWidth = mc.font.width(displayText);
int leftPadding = defaultPadding;
if (textWidth + 2 * defaultPadding > boxWidth) {
leftPadding = Math.max(2, boxWidth - textWidth - 2);
}
context.drawString(
mc.font,
Component.literal(displayText),
parent.parent.x + leftPadding,
parent.parent.y + parent.offset + offset + (parent.parent.height - mc.font.lineHeight) / 2,
Color.white.getRGB()
);
}
@Override
public void mouseClicked(double mouseX, double mouseY, int button) {
if (isHovered(mouseX, mouseY)) sliding = true;
}
@Override
public void mouseReleased(double mouseX, double mouseY, int button) {
sliding = false;
}
private double roundToPlace(double value, int place) {
if (place < 0) {
return value;
}
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(place, RoundingMode.HALF_UP);
return bd.doubleValue();
}
}
|