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