]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/gui/AbstractSlider.java
Setup for 1.16.3 Fabric
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / gui / AbstractSlider.java
1 package com.irtimaled.bbor.client.gui;
2
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;
7
8 abstract class AbstractSlider extends AbstractControl {
9     private final int optionCount;
10     private final int total;
11     int position = 0;
12
13     AbstractSlider(int width, int optionCount) {
14         super(0, 0, width, "");
15         this.optionCount = optionCount;
16         total = this.width - 8;
17     }
18
19     @Override
20     protected void renderBackground(MatrixStack matrixStack, int mouseX, int mouseY) {
21         this.minecraft.getTextureManager().bindTexture(WIDGETS_LOCATION);
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);
25     }
26
27     private double getProgressPercentage() {
28         return (this.position / (double) this.optionCount) * (double) total;
29     }
30
31     private void changeProgress(double mouseX) {
32         double progress = (mouseX - (double) (this.x + 4)) / (double) total;
33         setPosition((int) (progress * optionCount));
34     }
35
36     protected int getPosition() {
37         return position;
38     }
39
40     protected boolean setPosition(int position) {
41         position = MathHelper.clamp(position, 0, optionCount);
42         if (this.position == position) return false;
43
44         this.position = position;
45         onProgressChanged();
46         return true;
47     }
48
49     @Override
50     protected int getYImage(boolean hovered) {
51         return 0;
52     }
53
54     @Override
55     protected void onDrag(double mouseX, double mouseY, double deltaX, double deltaY) {
56         changeProgress(mouseX);
57     }
58
59     @Override
60     public void onClick(double mouseX, double mouseY) {
61         changeProgress(mouseX);
62     }
63
64     protected abstract void onProgressChanged();
65
66     @Override
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);
71     }
72
73     @Override
74     public void playDownSound(SoundManager soundHandler) {
75     }
76
77     @Override
78     public void onRelease(double mouseX, double mouseY) {
79         super.playDownSound(MinecraftClient.getInstance().getSoundManager());
80     }
81 }