1 package com.irtimaled.bbor.client.gui;
3 import com.irtimaled.bbor.common.MathHelper;
4 import net.minecraft.client.MinecraftClient;
5 import net.minecraft.client.sound.SoundManager;
6 import net.minecraft.client.util.math.MatrixStack;
8 abstract class AbstractSlider extends AbstractControl {
9 private final int optionCount;
10 private final int total;
13 AbstractSlider(int width, int optionCount) {
14 super(0, 0, width, "");
15 this.optionCount = optionCount;
16 total = this.width - 8;
20 protected void renderBackground(MatrixStack matrixStack, int mouseX, int mouseY) {
21 this.minecraft.getTextureManager().bindTexture(WIDGETS_TEXTURE);
22 int hoverState = super.getYImage(this.isHovered());
23 this.drawTexture(matrixStack, this.x + (int) getProgressPercentage(), this.y, 0, 46 + hoverState * 20, 4, this.height);
24 this.drawTexture(matrixStack, this.x + (int) getProgressPercentage() + 4, this.y, 196, 46 + hoverState * 20, 4, 20);
27 private double getProgressPercentage() {
28 return (this.position / (double) this.optionCount) * (double) total;
31 private void changeProgress(double mouseX) {
32 double progress = (mouseX - (double) (this.x + 4)) / (double) total;
33 setPosition((int) Math.round(progress * optionCount));
36 protected int getPosition() {
40 protected boolean setPosition(int position) {
41 position = MathHelper.clamp(position, 0, optionCount);
42 if (this.position == position) return false;
44 this.position = position;
50 protected int getYImage(boolean hovered) {
55 protected void onDrag(double mouseX, double mouseY, double deltaX, double deltaY) {
56 changeProgress(mouseX);
60 public void onClick(double mouseX, double mouseY) {
61 changeProgress(mouseX);
64 protected abstract void onProgressChanged();
67 public boolean keyPressed(int key, int scanCode, int modifiers) {
68 if (key != 262 && key != 263) return false;
69 int position = getPosition();
70 return key == 263 ? setPosition(position - 1) : setPosition(position + 1);
74 public void playDownSound(SoundManager soundHandler) {
78 public void onRelease(double mouseX, double mouseY) {
79 super.playDownSound(MinecraftClient.getInstance().getSoundManager());