GradientSlider
This page talks about GradientSlider
GradientSlider
The GradientSlider
class provides functionality for creating and displaying gradient sliders in Minecraft Fabric.
Fields
width
: The width of the gradient slider.height
: The height of the gradient slider.selectedWidget
: The widget that this gradient slider is associated with.x
: The x position of the gradient slider.y
: The y position of the gradient slider.hue
: The hue of the gradient slider.isDragging
: Whether the handle of the gradient slider is currently being dragged.
Constructors
GradientSlider(int x, int y, int width, int height, Widget selectedWidget)
Constructs a GradientSlider
object with the given position, size, and selected widget.
Methods
tick()
Updates the progress and alpha of the gradient slider.
render(MatrixStack matrices)
Renders this gradient slider on screen.
setPosition(int x, int y)
Sets the position of this gradient slider.
onClick(double mouseX, double mouseY, int button)
Handles mouse clicks on this gradient slider. This method sets the dragging state to true if the mouse is over the handle and the left mouse button was clicked. It also updates the hue of the gradient slider based on the mouse position if the mouse is over the gradient slider.
isMouseOver(double mouseX, double mouseY)
Returns whether the mouse is currently over this gradient slider.
onRelease(double mouseX, double mouseY, int button)
Handles mouse release events on this gradient slider. This method stops dragging or scaling the handle.
onDrag(double mouseX, double mouseY, int button)
Handles mouse dragging on this gradient slider. This method updates the hue of the gradient slider based on the mouse position if the handle is being dragged.
getHue()
Returns the current hue of this gradient slider.
setHue(float hue)
Sets the hue of this gradient slider.
The GradientSlider is already called and present in ColorGradientPicker. But if you still want to add it yourself somewhere you can do it by:Here's an example of how to use a GradientSlider
in your Minecraft Fabric mod:
// Create a new GradientSlider
GradientSlider slider = new GradientSlider(10, 10, 100, 20);
// Render the GradientSlider
slider.render(matrices);
// Handle mouse input
if (slider.onClick(mouseX, mouseY)) {
// The mouse was clicked on the slider
}
if (slider.onDrag(mouseX, mouseY)) {
// The mouse was dragged on the slider
}
if (slider.onRelease(mouseX, mouseY)) {
// The mouse was released on the slider
}
// Get and set values for the GradientSlider
float hue = slider.getHue();
slider.setHue(0.5f);
In this example, we create a new GradientSlider
with a position of (10, 10) and a size of (100, 20). We then render the GradientSlider
on screen using its render
method. We also handle mouse input by calling its onClick
, onDrag
, and onRelease
methods.
Finally, we get and set values for hue using its getter and setter methods.