|
| 1 | +package me.flashyreese.mods.sodiumextra.client.gui.options.control; |
| 2 | + |
| 3 | +import net.caffeinemc.mods.sodium.client.gui.options.Option; |
| 4 | +import net.caffeinemc.mods.sodium.client.gui.options.control.Control; |
| 5 | +import net.caffeinemc.mods.sodium.client.gui.options.control.ControlElement; |
| 6 | +import net.caffeinemc.mods.sodium.client.gui.options.control.ControlValueFormatter; |
| 7 | +import net.caffeinemc.mods.sodium.client.util.Dim2i; |
| 8 | +import net.minecraft.ChatFormatting; |
| 9 | +import net.minecraft.client.gui.GuiGraphics; |
| 10 | +import net.minecraft.client.renderer.Rect2i; |
| 11 | +import net.minecraft.network.chat.MutableComponent; |
| 12 | +import net.minecraft.network.chat.Style; |
| 13 | +import net.minecraft.util.Mth; |
| 14 | +import org.apache.commons.lang3.Validate; |
| 15 | + |
| 16 | +import java.util.function.Supplier; |
| 17 | + |
| 18 | +public class DynamicSliderControl implements Control<Integer> { |
| 19 | + private final Option<Integer> option; |
| 20 | + private final Supplier<Integer> min; |
| 21 | + private final Supplier<Integer> max; |
| 22 | + private final int interval; |
| 23 | + private final ControlValueFormatter mode; |
| 24 | + |
| 25 | + public DynamicSliderControl(Option<Integer> option, Supplier<Integer> min, Supplier<Integer> max, int interval, ControlValueFormatter mode) { |
| 26 | + Validate.isTrue(interval > 0, "The slider interval must be greater than zero"); |
| 27 | + Validate.notNull(mode, "The slider mode must not be null"); |
| 28 | + this.option = option; |
| 29 | + this.min = min; |
| 30 | + this.max = max; |
| 31 | + this.interval = interval; |
| 32 | + this.mode = mode; |
| 33 | + } |
| 34 | + |
| 35 | + public ControlElement<Integer> createElement(Dim2i dim) { |
| 36 | + int min = this.min.get(); |
| 37 | + int max = this.max.get(); |
| 38 | + int interval = this.interval; |
| 39 | + |
| 40 | + Validate.isTrue(max > min, "The maximum value must be greater than the minimum value"); |
| 41 | + Validate.isTrue(((max - min) % interval) == 0, "The maximum value must be divisable by the interval"); |
| 42 | + |
| 43 | + return new DynamicSliderControl.Button(this.option, dim, min, max, this.interval, this.mode); |
| 44 | + } |
| 45 | + |
| 46 | + public Option<Integer> getOption() { |
| 47 | + return this.option; |
| 48 | + } |
| 49 | + |
| 50 | + public int getMaxWidth() { |
| 51 | + return 170; |
| 52 | + } |
| 53 | + |
| 54 | + private static class Button extends ControlElement<Integer> { |
| 55 | + private static final int THUMB_WIDTH = 2; |
| 56 | + private static final int TRACK_HEIGHT = 1; |
| 57 | + private final Rect2i sliderBounds; |
| 58 | + private final ControlValueFormatter formatter; |
| 59 | + private final int min; |
| 60 | + private final int max; |
| 61 | + private final int range; |
| 62 | + private final int interval; |
| 63 | + private int contentWidth; |
| 64 | + private double thumbPosition; |
| 65 | + private boolean sliderHeld; |
| 66 | + |
| 67 | + public Button(Option<Integer> option, Dim2i dim, int min, int max, int interval, ControlValueFormatter formatter) { |
| 68 | + super(option, dim); |
| 69 | + this.min = min; |
| 70 | + this.max = max; |
| 71 | + this.range = max - min; |
| 72 | + this.interval = interval; |
| 73 | + this.thumbPosition = this.getThumbPositionForValue(option.getValue()); |
| 74 | + this.formatter = formatter; |
| 75 | + this.sliderBounds = new Rect2i(dim.getLimitX() - 96, dim.getCenterY() - 5, 90, 10); |
| 76 | + this.sliderHeld = false; |
| 77 | + } |
| 78 | + |
| 79 | + public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) { |
| 80 | + int sliderX = this.sliderBounds.getX(); |
| 81 | + int sliderY = this.sliderBounds.getY(); |
| 82 | + int sliderWidth = this.sliderBounds.getWidth(); |
| 83 | + int sliderHeight = this.sliderBounds.getHeight(); |
| 84 | + MutableComponent label = this.formatter.format(this.option.getValue()).copy(); |
| 85 | + if (!this.option.isAvailable()) { |
| 86 | + label.setStyle(Style.EMPTY.withColor(ChatFormatting.GRAY).withItalic(true)); |
| 87 | + } |
| 88 | + |
| 89 | + int labelWidth = this.font.width(label); |
| 90 | + boolean drawSlider = this.option.isAvailable() && (this.hovered || this.isFocused()); |
| 91 | + if (drawSlider) { |
| 92 | + this.contentWidth = sliderWidth + labelWidth; |
| 93 | + } else { |
| 94 | + this.contentWidth = labelWidth; |
| 95 | + } |
| 96 | + |
| 97 | + super.render(graphics, mouseX, mouseY, delta); |
| 98 | + if (drawSlider) { |
| 99 | + this.thumbPosition = this.getThumbPositionForValue(this.option.getValue()); |
| 100 | + double thumbOffset = Mth.clamp((double) (this.getIntValue() - this.min) / (double) this.range * (double) sliderWidth, 0.0F, sliderWidth); |
| 101 | + int thumbX = (int) ((double) sliderX + thumbOffset - (double) 2.0F); |
| 102 | + int trackY = (int) ((double) ((float) sliderY + (float) sliderHeight / 2.0F) - (double) 0.5F); |
| 103 | + this.drawRect(graphics, thumbX, sliderY, thumbX + 4, sliderY + sliderHeight, -1); |
| 104 | + this.drawRect(graphics, sliderX, trackY, sliderX + sliderWidth, trackY + 1, -1); |
| 105 | + this.drawString(graphics, label, sliderX - labelWidth - 6, sliderY + sliderHeight / 2 - 4, -1); |
| 106 | + } else { |
| 107 | + this.drawString(graphics, label, sliderX + sliderWidth - labelWidth, sliderY + sliderHeight / 2 - 4, -1); |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + public int getContentWidth() { |
| 113 | + return this.contentWidth; |
| 114 | + } |
| 115 | + |
| 116 | + public int getIntValue() { |
| 117 | + return this.min + this.interval * (int) Math.round(this.getSnappedThumbPosition() / (double) this.interval); |
| 118 | + } |
| 119 | + |
| 120 | + public double getSnappedThumbPosition() { |
| 121 | + return this.thumbPosition / ((double) 1.0F / (double) this.range); |
| 122 | + } |
| 123 | + |
| 124 | + public double getThumbPositionForValue(int value) { |
| 125 | + return (double) (value - this.min) * ((double) 1.0F / (double) this.range); |
| 126 | + } |
| 127 | + |
| 128 | + public boolean mouseClicked(double mouseX, double mouseY, int button) { |
| 129 | + this.sliderHeld = false; |
| 130 | + if (this.option.isAvailable() && button == 0 && this.dim.containsCursor(mouseX, mouseY)) { |
| 131 | + if (this.sliderBounds.contains((int) mouseX, (int) mouseY)) { |
| 132 | + this.setValueFromMouse(mouseX); |
| 133 | + this.sliderHeld = true; |
| 134 | + } |
| 135 | + |
| 136 | + return true; |
| 137 | + } else { |
| 138 | + return false; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private void setValueFromMouse(double d) { |
| 143 | + this.setValue((d - (double) this.sliderBounds.getX()) / (double) this.sliderBounds.getWidth()); |
| 144 | + } |
| 145 | + |
| 146 | + public void setValue(double d) { |
| 147 | + this.thumbPosition = Mth.clamp(d, 0.0F, 1.0F); |
| 148 | + int value = this.getIntValue(); |
| 149 | + if (this.option.getValue() != value) { |
| 150 | + this.option.setValue(value); |
| 151 | + } |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { |
| 156 | + if (!this.isFocused()) { |
| 157 | + return false; |
| 158 | + } else if (keyCode == 263) { |
| 159 | + this.option.setValue(Mth.clamp(this.option.getValue() - this.interval, this.min, this.max)); |
| 160 | + return true; |
| 161 | + } else if (keyCode == 262) { |
| 162 | + this.option.setValue(Mth.clamp(this.option.getValue() + this.interval, this.min, this.max)); |
| 163 | + return true; |
| 164 | + } else { |
| 165 | + return false; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { |
| 170 | + if (this.option.isAvailable() && button == 0) { |
| 171 | + if (this.sliderHeld) { |
| 172 | + this.setValueFromMouse(mouseX); |
| 173 | + } |
| 174 | + |
| 175 | + return true; |
| 176 | + } else { |
| 177 | + return false; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments