Archived Legacy DynamicHUD
  • Dynamic HUD
  • Import using Gradle
  • Widgets
    • Adding Widgets
      • TextWidget
      • ItemWidget
      • ArmorWidget
    • Creating your own Widget class
    • Widget class
    • WidgetManager
    • WidgetBox
  • Saving and Loading
    • Saving and Loading
    • Changing save file name and directory
  • Moveable Screen
    • AbstractMoveableScreen
    • Default Moveable Screen
    • Using the default Moveable Screen
    • Creating and using your own MoveableScreen class
  • ContextMenu
    • ContextMenu class
    • ContextMenuOptionsProvider interface
    • DoubleInputScreen
    • DataInputScreen
  • Slider Widget
    • Slider
  • SliderWidgetBuilder
  • ColorPicker
    • ColorGradientPicker
      • ColorPickerButton
      • GradientBox
      • GradientSlider
  • Helpers
    • ColorHelper
    • DrawHelper
    • TextureHelper
Powered by GitBook
On this page
  • SliderWidget
  • Fields
  • Constructors
  • Methods
  • Building the slider
  • To build the slider Widget, I recommend using SliderWidgetBuilder but this is the alternative method:
  1. Slider Widget

Slider

This page talks about the SliderWidget class

SliderWidget

The SliderWidget class provides functionality for creating and displaying sliders in Minecraft Fabric.

Fields

  • client: The MinecraftClient instance.

  • width: The width of the widget.

  • height: The height of the widget.

  • label: The label displayed above the slider.

  • minValue: The minimum value of the slider.

  • maxValue: The maximum value of the slider.

  • x: The x position of the widget.

  • y: The y position of the widget.

  • value: The current value of the slider.

  • selectedWidget: The widget that this slider is associated with.

Constructors

SliderWidget(MinecraftClient client, int x, int y, int width, int height, String label, float value, float minValue, float maxValue, Widget selectedWidget)

Constructs a SliderWidget object with the given client instance, position, size, label, initial value, minimum value, maximum value, and selected widget.

Methods

tick()

Updates the scale of the slider for animation.

render(MatrixStack matrices)

Renders this widget on screen.

setPosition(int x, int y)

Sets the position of this widget.

mouseClicked(double mouseX, double mouseY, int button)

Handles mouse clicks on this widget. This method updates the value of the slider based on the mouse position if the mouse is over the slider.

mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY)

Handles mouse dragging on this widget. This method updates the value of the slider based on the mouse position if the mouse is over the slider.

getValue()

Returns the current value of the slider.

setValue(float value)

Sets the value of the slider.

Building the slider

Any class extending AbstractMoveableScreen
// Create a new SliderWidget
SliderWidget slider = new SliderWidget(client, 10, 10, 100, 20, "My Slider", 0.5f, 0.0f, 1.0f);

// Get and set the value of the slider
float value = slider.getValue();
slider.setValue(value);

In this example, we create a new SliderWidget with a width of 100 pixels and a height of 20 pixels. We set its label to "My Slider" and its initial value to 0.5. We also set its minimum and maximum values to 0.0 and 1.0 respectively.

The MouseHandler and AbstractMoveableScreen Class automatically takes care of the slider mouse inputs, but if you are thinking of using it differently somewhere else then you need to add the following to the mouse handler events

// Render the SliderWidget
slider.render(matrices);


// Mouse Handler Events
// Handle mouse input
if (slider.mouseClicked(mouseX, mouseY)) {
    // The mouse was clicked on the slider
    return true;
}
if (slider.mouseDragged(mouseX, mouseY)) {
    // The mouse was dragged on the slider
    return true;
}

Finally, we get and set the value of the slider using its getValue and setValue methods.

I hope this helps! Let me know if you need any further assistance. 😊

PreviousDataInputScreenNextSliderWidgetBuilder

To build the slider Widget, I recommend using but this is the alternative method:

SliderWidgetBuilder