1 package com.irtimaled.bbor.client.gui;
3 import com.irtimaled.bbor.common.MathHelper;
4 import net.minecraft.client.Minecraft;
5 import net.minecraft.client.audio.SoundHandler;
7 abstract class AbstractSlider extends AbstractControl {
8 private final int optionCount;
9 private final int total;
12 AbstractSlider(int width, int optionCount) {
13 super(0, 0, width, "");
14 this.optionCount = optionCount;
15 total = this.width - 8;
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);
26 private double getProgressPercentage() {
27 return (this.position / (double) this.optionCount) * (double) total;
30 private void changeProgress(double mouseX) {
31 double progress = (mouseX - (double) (this.x + 4)) / (double) total;
32 setPosition((int) (progress * optionCount));
35 protected int getPosition() {
39 protected boolean setPosition(int position) {
40 position = MathHelper.clamp(position, 0, optionCount);
41 if (this.position == position) return false;
43 this.position = position;
49 protected int getYImage(boolean hovered) {
54 protected void onDrag(double mouseX, double mouseY, double deltaX, double deltaY) {
55 changeProgress(mouseX);
59 public void onClick(double mouseX, double mouseY) {
60 changeProgress(mouseX);
63 protected abstract void onProgressChanged();
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);
73 public void playDownSound(SoundHandler soundHandler) {
77 public void onRelease(double mouseX, double mouseY) {
78 super.playDownSound(Minecraft.getInstance().getSoundHandler());