|
| 1 | +package com.mrbysco.armorposer.client.gui.widgets; |
| 2 | + |
| 3 | +import com.mrbysco.armorposer.client.gui.ArmorStandScreen; |
| 4 | +import net.minecraft.client.gui.Font; |
| 5 | +import net.minecraft.client.gui.GuiGraphics; |
| 6 | +import net.minecraft.client.gui.components.AbstractSliderButton; |
| 7 | +import net.minecraft.client.gui.screens.Screen; |
| 8 | +import net.minecraft.network.chat.CommonComponents; |
| 9 | +import net.minecraft.network.chat.Component; |
| 10 | +import net.minecraft.util.Mth; |
| 11 | + |
| 12 | +import java.text.DecimalFormat; |
| 13 | + |
| 14 | +public class SizeSlider extends AbstractSliderButton { |
| 15 | + private final ArmorStandScreen screen; |
| 16 | + private final double minValue; |
| 17 | + private final double maxValue; |
| 18 | + protected double stepSize; |
| 19 | + private final DecimalFormat format = new DecimalFormat("0.00"); |
| 20 | + |
| 21 | + public SizeSlider(int x, int y, int width, double value, double minValue, double maxValue, ArmorStandScreen screen) { |
| 22 | + super(x, y, width, 16, CommonComponents.EMPTY, 0.0); |
| 23 | + this.minValue = minValue; |
| 24 | + this.maxValue = maxValue; |
| 25 | + this.screen = screen; |
| 26 | + this.stepSize = 0.01D; |
| 27 | + this.value = (Mth.clamp((float) value, minValue, maxValue) - minValue) / (maxValue - minValue); |
| 28 | + |
| 29 | + this.updateMessage(); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + protected void onDrag(double mouseX, double mouseY, double dragX, double dragY) { |
| 34 | + super.onDrag(mouseX, mouseY, dragX, dragY); |
| 35 | + this.setValueFromMouse(mouseX); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public boolean mouseScrolled(double mouseX, double mouseY, double xScroll, double yScroll) { |
| 40 | + if (isHovered()) { |
| 41 | + double increment = Screen.hasShiftDown() ? 0.01D : 0.001D; |
| 42 | + this.setSliderValue(this.value + (yScroll < 0 ? -increment : increment)); |
| 43 | + } |
| 44 | + return super.mouseScrolled(mouseX, mouseY, xScroll, yScroll); |
| 45 | + } |
| 46 | + |
| 47 | + private void setValueFromMouse(double mouseX) { |
| 48 | + this.setSliderValue((mouseX - (this.getX() + 4)) / (this.width - 8)); |
| 49 | + } |
| 50 | + |
| 51 | + private void setSliderValue(double value) { |
| 52 | + double oldValue = this.value; |
| 53 | + this.value = this.snapToNearest(value); |
| 54 | + if (!Mth.equal(oldValue, this.value)) |
| 55 | + this.applyValue(); |
| 56 | + |
| 57 | + this.updateMessage(); |
| 58 | + } |
| 59 | + |
| 60 | + private double snapToNearest(double value) { |
| 61 | + if (stepSize <= 0D) |
| 62 | + return Mth.clamp(value, 0D, 1D); |
| 63 | + |
| 64 | + value = Mth.lerp(Mth.clamp(value, 0D, 1D), this.minValue, this.maxValue); |
| 65 | + |
| 66 | + value = (stepSize * Math.round(value / stepSize)); |
| 67 | + |
| 68 | + if (this.minValue > this.maxValue) { |
| 69 | + value = Mth.clamp(value, this.maxValue, this.minValue); |
| 70 | + } else { |
| 71 | + value = Mth.clamp(value, this.minValue, this.maxValue); |
| 72 | + } |
| 73 | + |
| 74 | + return Mth.map(value, this.minValue, this.maxValue, 0D, 1D); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected void applyValue() { |
| 79 | + this.screen.updateScale(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onRelease(double d, double e) { |
| 84 | + super.onRelease(d, e); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected void renderScrollingString(GuiGraphics guiGraphics, Font font, int i, int j) { |
| 89 | + super.renderScrollingString(guiGraphics, font, i, j); |
| 90 | + } |
| 91 | + |
| 92 | + public double getValue() { |
| 93 | + return this.value * (maxValue - minValue) + minValue; |
| 94 | + } |
| 95 | + |
| 96 | + public String getValueString() { |
| 97 | + return this.format.format(this.getValue()); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + protected void updateMessage() { |
| 102 | + this.setMessage(Component.literal("").append(this.getValueString())); |
| 103 | +// this.setMessage( |
| 104 | +// CommonComponents.optionNameValue( |
| 105 | +// RealmsSlotOptionsScreen.SPAWN_PROTECTION_TEXT, |
| 106 | +// (Component)(RealmsSlotOptionsScreen.this.spawnProtection == 0 |
| 107 | +// ? CommonComponents.OPTION_OFF |
| 108 | +// : Component.literal(String.valueOf(RealmsSlotOptionsScreen.this.spawnProtection))) |
| 109 | +// ) |
| 110 | +// ); |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments