
com.parzivail.util.client.screen.SimpleSliderWidget Maven / Gradle / Ivy
package com.parzivail.util.client.screen;
import I;
import com.mojang.blaze3d.systems.RenderSystem;
import org.lwjgl.glfw.GLFW;
import java.util.function.Consumer;
import net.minecraft.class_2561;
import net.minecraft.class_2960;
import net.minecraft.class_332;
import net.minecraft.class_3532;
import net.minecraft.class_4185;
public class SimpleSliderWidget extends class_4185
{
private final int trackU;
private final int trackV;
private final int thumbU;
private final int thumbV;
private final int thumbHoverU;
private final int thumbHoverV;
private final int thumbWidth;
private final int thumbHeight;
private final int textureWidth;
private final int textureHeight;
private final Consumer onChange;
private class_2960 texture = null;
private boolean dirty;
private float value;
public SimpleSliderWidget(int x, int y, int width, int height, int trackU, int trackV, int thumbU, int thumbV, int thumbHoverU, int thumbHoverV, int thumbWidth, int thumbHeight, int textureWidth, int textureHeight, Consumer onChange)
{
super(x, y, width, height, class_2561.method_30163(""), button -> {
}, field_40754);
this.trackU = trackU;
this.trackV = trackV;
this.thumbU = thumbU;
this.thumbV = thumbV;
this.thumbHoverU = thumbHoverU;
this.thumbHoverV = thumbHoverV;
this.thumbWidth = thumbWidth;
this.thumbHeight = thumbHeight;
this.textureWidth = textureWidth;
this.textureHeight = textureHeight;
this.onChange = onChange;
}
public void setTexture(class_2960 texture)
{
this.texture = texture;
}
public float getValue()
{
return value;
}
private void setValueFromMouseX(double mouseX)
{
this.value = class_3532.method_15363((float)((mouseX - this.method_46426()) / (this.field_22758 - 2)), 0, 1);
}
public void setValue(float v)
{
this.value = v;
}
@Override
public boolean method_25402(double mouseX, double mouseY, int button)
{
if (button == GLFW.GLFW_MOUSE_BUTTON_LEFT && clientAreaContains(mouseX, mouseY))
{
setValueFromMouseX(mouseX);
dirty = true;
return true;
}
return super.method_25402(mouseX, mouseY, button);
}
private boolean clientAreaContains(double x, double y)
{
return x >= this.method_46426() && x <= this.method_46426() + field_22758 && y >= this.method_46427() && y <= this.method_46427() + field_22759;
}
@Override
public boolean method_25403(double mouseX, double mouseY, int button, double deltaX, double deltaY)
{
if (button == GLFW.GLFW_MOUSE_BUTTON_LEFT && dirty)
{
setValueFromMouseX(mouseX);
dirty = true;
return true;
}
return super.method_25403(mouseX, mouseY, button, deltaX, deltaY);
}
@Override
public boolean method_25406(double mouseX, double mouseY, int button)
{
if (button == GLFW.GLFW_MOUSE_BUTTON_LEFT && dirty)
{
commit();
return true;
}
return super.method_25406(mouseX, mouseY, button);
}
@Override
public void method_48579(class_332 context, int mouseX, int mouseY, float delta)
{
var tU = this.thumbU;
var tV = this.thumbV;
if (this.method_49606() || dirty)
{
tU = this.thumbHoverU;
tV = this.thumbHoverV;
}
RenderSystem.enableDepthTest();
context.method_25290(texture, this.method_46426(), this.method_46427(), this.trackU, this.trackV, this.field_22758, this.field_22759, this.textureWidth, this.textureHeight);
context.method_25290(texture, this.method_46426() + 1 + Math.round(value * (field_22758 - 2)) - this.thumbWidth / 2, this.method_46427() - (this.thumbHeight - this.field_22759) / 2, tU, tV, this.thumbWidth, this.thumbHeight, this.textureWidth, this.textureHeight);
}
public void commit()
{
if (!dirty)
return;
onChange.accept(this);
dirty = false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy