]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/gui/AbstractSlider.java
db1431c57b270cd2614e593498f4b87e9a6cf999
[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.Minecraft;
5 import net.minecraft.client.audio.SoundHandler;
6
7 abstract class AbstractSlider extends AbstractControl {
8     private final int optionCount;
9     private final int total;
10     int position = 0;
11
12     AbstractSlider(int width, int optionCount) {
13         super(0, 0, width, "");
14         this.optionCount = optionCount;
15         total = this.width - 8;
16     }
17
18     @Override
19     protected void renderBackground(int mouseX, int mouseY) {
20         this.minecraft.getTextureManager().bindTexture(WIDGETS_LOCATION);
21         int hoverState = super.getYImage(this.isHovered());
22         this.blit(this.x + (int) getProgressPercentage(), this.y, 0, 46 + hoverState * 20, 4, this.height);
23         this.blit(this.x + (int) getProgressPercentage() + 4, this.y, 196, 46 + hoverState * 20, 4, 20);
24     }
25
26     private double getProgressPercentage() {
27         return (this.position / (double) this.optionCount) * (double) total;
28     }
29
30     private void changeProgress(double mouseX) {
31         double progress = (mouseX - (double) (this.x + 4)) / (double) total;
32         setPosition((int) (progress * optionCount));
33     }
34
35     protected int getPosition() {
36         return position;
37     }
38
39     protected boolean setPosition(int position) {
40         position = MathHelper.clamp(position, 0, optionCount);
41         if (this.position == position) return false;
42
43         this.position = position;
44         onProgressChanged();
45         return true;
46     }
47
48     @Override
49     protected int getYImage(boolean hovered) {
50         return 0;
51     }
52
53     @Override
54     protected void onDrag(double mouseX, double mouseY, double deltaX, double deltaY) {
55         changeProgress(mouseX);
56     }
57
58     @Override
59     public void onClick(double mouseX, double mouseY) {
60         changeProgress(mouseX);
61     }
62
63     protected abstract void onProgressChanged();
64
65     @Override
66     public boolean keyPressed(int key, int scanCode, int modifiers) {
67         if (key != 262 && key != 263) return false;
68         int position = getPosition();
69         return key == 263 ? setPosition(position - 1) : setPosition(position + 1);
70     }
71
72     @Override
73     public void playDownSound(SoundHandler soundHandler) {
74     }
75
76     @Override
77     public void onRelease(double mouseX, double mouseY) {
78         super.playDownSound(Minecraft.getInstance().getSoundHandler());
79     }
80 }