Class WAbstractSlider
java.lang.Object
io.github.cottonmc.cotton.gui.widget.WWidget
io.github.cottonmc.cotton.gui.widget.WAbstractSlider
- Direct Known Subclasses:
WLabeledSlider,WSlider
A base class for slider widgets that can be used to select int values.
You can set two listeners on a slider:
- A value change listener that gets all value changes.
- A dragging finished listener that gets called when the player stops dragging the slider or modifies the value with the keyboard. For example, this can be used for sending sync packets to the server when the player has selected a value.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe direction enum represents all four directions a slider can face. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Axisprotected floatA coordinate:value ratio.protected WAbstractSlider.Directionprotected booleanTrue if the user is currently dragging the thumb.protected intprotected intprotected intprotected floatA value:coordinate ratio. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddNarrations(NarrationMessageBuilder builder) Adds the narrations of this widget to a narration builder.booleancanFocus()Tests whether this widget can have the focus in the GUI.booleanChecks whether this widget can be resized usingWWidget.setSize(int, int).getAxis()Gets the direction of this slider.intintprotected abstract intReturns the thumb size along the slider axis.intgetValue()static booleanisDecreasingKey(int ch, WAbstractSlider.Direction direction) Tests if the key should decrease sliders with the specified direction.booleanTests whether the user is dragging this slider.static booleanisIncreasingKey(int ch, WAbstractSlider.Direction direction) Tests if the key should increase sliders with the specified direction.protected abstract booleanisMouseInsideBounds(int x, int y) Checks if the mouse cursor is close enough to the slider that the user can start dragging.onClick(int x, int y, int button) Notifies this widget that the mouse has been pressed and released, both while inside its bounds.onKeyPressed(int ch, int key, int modifiers) Notifies this widget that a key has been pressed.onKeyReleased(int ch, int key, int modifiers) Notifies this widget that a key has been releasedonMouseDown(int x, int y, int button) Notifies this widget that the mouse has been pressed while inside its boundsonMouseDrag(int x, int y, int button, double deltaX, double deltaY) Notifies this widget that the mouse has been moved while pressed and inside its bounds.onMouseScroll(int x, int y, double horizontalAmount, double verticalAmount) Notifies this widget that the mouse has been scrolled inside its bounds.onMouseUp(int x, int y, int button) Notifies this widget that the mouse has been released while inside its boundsprotected voidonValueChanged(int value) voidsetDirection(WAbstractSlider.Direction direction) Sets the direction of this slider.voidsetDraggingFinishedListener(@Nullable IntConsumer draggingFinishedListener) voidsetMaxValue(int max) voidsetMinValue(int min) voidsetSize(int x, int y) Sets the size of this widget.voidsetValue(int value) Sets the slider value without calling listeners.voidsetValue(int value, boolean callListeners) Sets the slider value.voidsetValueChangeListener(@Nullable IntConsumer valueChangeListener) voidtick()Executes a client-side tick for this widget.protected voidUpdatescoordToValueRatioandvalueToCoordRatio.Methods inherited from class io.github.cottonmc.cotton.gui.widget.WWidget
addPainters, addTooltip, canHover, getAbsoluteX, getAbsoluteY, getFocusModel, getHeight, getHost, getParent, getWidth, getX, getY, hit, hoveredProperty, isActivationKey, isFocused, isHovered, isNarratable, isWithinBounds, onCharTyped, onFocusGained, onFocusLost, onHidden, onMouseMove, onShown, paint, releaseFocus, renderTooltip, requestFocus, setHost, setHovered, setLocation, setParent, shouldRenderInDarkMode, validate
-
Field Details
-
min
protected int min -
max
protected int max -
axis
-
direction
-
value
protected int value -
dragging
protected boolean draggingTrue if the user is currently dragging the thumb. Used for visuals. -
valueToCoordRatio
protected float valueToCoordRatioA value:coordinate ratio. Used for converting user input into values.- See Also:
-
coordToValueRatio
protected float coordToValueRatioA coordinate:value ratio. Used for rendering the thumb.- See Also:
-
-
Constructor Details
-
WAbstractSlider
-
-
Method Details
-
getThumbWidth
protected abstract int getThumbWidth()Returns the thumb size along the slider axis.- Returns:
- the thumb size along the slider axis
-
isMouseInsideBounds
protected abstract boolean isMouseInsideBounds(int x, int y) Checks if the mouse cursor is close enough to the slider that the user can start dragging.- Parameters:
x- the mouse x positiony- the mouse y position- Returns:
- if the cursor is inside dragging bounds
-
updateValueCoordRatios
protected void updateValueCoordRatios()UpdatescoordToValueRatioandvalueToCoordRatio. This method should be called whenever this widget resizes or changes it min/max value boundaries.- Since:
- 5.1.0
-
setSize
public void setSize(int x, int y) Description copied from class:WWidgetSets the size of this widget.Overriding methods may restrict one of the dimensions to be a constant value, for example
super.setSize(x, 20). -
canResize
public boolean canResize()Description copied from class:WWidgetChecks whether this widget can be resized usingWWidget.setSize(int, int). -
canFocus
public boolean canFocus()Description copied from class:WWidgetTests whether this widget can have the focus in the GUI. -
onMouseDown
Description copied from class:WWidgetNotifies this widget that the mouse has been pressed while inside its bounds- Overrides:
onMouseDownin classWWidget- Parameters:
x- The X coordinate of the event, in widget-space (0 is the left edge of this widget)y- The Y coordinate of the event, in widget-space (0 is the top edge of this widget)button- The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click)- Returns:
InputResult.PROCESSEDif the event is handled,InputResult.IGNOREDotherwise.
-
onMouseDrag
@Environment(CLIENT) public InputResult onMouseDrag(int x, int y, int button, double deltaX, double deltaY) Description copied from class:WWidgetNotifies this widget that the mouse has been moved while pressed and inside its bounds.- Overrides:
onMouseDragin classWWidget- Parameters:
x- The X coordinate of the event, in widget-space (0 is the left edge of this widget)y- The Y coordinate of the event, in widget-space (0 is the top edge of this widget)button- The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click)deltaX- The amount of dragging on the X axisdeltaY- The amount of dragging on the Y axis- Returns:
InputResult.PROCESSEDif the event is handled,InputResult.IGNOREDotherwise.
-
onClick
Description copied from class:WWidgetNotifies this widget that the mouse has been pressed and released, both while inside its bounds.- Overrides:
onClickin classWWidget- Parameters:
x- The X coordinate of the event, in widget-space (0 is the left edge of this widget)y- The Y coordinate of the event, in widget-space (0 is the top edge of this widget)button- The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click)- Returns:
InputResult.PROCESSEDif the event is handled,InputResult.IGNOREDotherwise.
-
onMouseUp
Description copied from class:WWidgetNotifies this widget that the mouse has been released while inside its bounds- Overrides:
onMouseUpin classWWidget- Parameters:
x- The X coordinate of the event, in widget-space (0 is the left edge of this widget)y- The Y coordinate of the event, in widget-space (0 is the top edge of this widget)button- The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click)- Returns:
InputResult.PROCESSEDif the event is handled,InputResult.IGNOREDotherwise.
-
onMouseScroll
@Environment(CLIENT) public InputResult onMouseScroll(int x, int y, double horizontalAmount, double verticalAmount) Description copied from class:WWidgetNotifies this widget that the mouse has been scrolled inside its bounds.- Overrides:
onMouseScrollin classWWidget- Parameters:
x- The X coordinate of the event, in widget-space (0 is the left edge of this widget)y- The Y coordinate of the event, in widget-space (0 is the top edge of this widget)horizontalAmount- The scrolled horizontal amount. Positive values are right and negative values are left.verticalAmount- The scrolled vertical amount. Positive values are up and negative values are down.- Returns:
InputResult.PROCESSEDif the event is handled,InputResult.IGNOREDotherwise.
-
tick
@Environment(CLIENT) public void tick()Description copied from class:WWidgetExecutes a client-side tick for this widget. -
getValue
public int getValue() -
setValue
public void setValue(int value) Sets the slider value without calling listeners.- Parameters:
value- the new value
-
setValue
public void setValue(int value, boolean callListeners) Sets the slider value.- Parameters:
value- the new valuecallListeners- if true, call all slider listeners
-
getValueChangeListener
-
setValueChangeListener
-
getDraggingFinishedListener
-
setDraggingFinishedListener
-
getMinValue
public int getMinValue() -
getMaxValue
public int getMaxValue() -
setMinValue
public void setMinValue(int min) -
setMaxValue
public void setMaxValue(int max) -
getAxis
-
getDirection
Gets the direction of this slider.- Returns:
- the direction
- Since:
- 2.0.0
-
setDirection
Sets the direction of this slider.- Parameters:
direction- the new direction- Throws:
IllegalArgumentException- if the direction axis is not equal toaxis.- Since:
- 2.0.0
-
onValueChanged
protected void onValueChanged(int value) -
onKeyPressed
Description copied from class:WWidgetNotifies this widget that a key has been pressed.- Overrides:
onKeyPressedin classWWidget- Parameters:
key- the GLFW scancode of the key- Returns:
InputResult.PROCESSEDif the event is handled,InputResult.IGNOREDotherwise.
-
onKeyReleased
Description copied from class:WWidgetNotifies this widget that a key has been released- Overrides:
onKeyReleasedin classWWidget- Parameters:
key- the GLFW scancode of the key- Returns:
InputResult.PROCESSEDif the event is handled,InputResult.IGNOREDotherwise.
-
isDragging
public boolean isDragging()Tests whether the user is dragging this slider.- Returns:
- true if this slider is being dragged, false otherwise
- Since:
- 4.0.0
-
addNarrations
Description copied from class:WWidgetAdds the narrations of this widget to a narration builder. Narrations will only apply if this widget is narratable.The widget needs to be focusable or hoverable, and also be focused/hovered for narrations to be added.
- Overrides:
addNarrationsin classWWidget- Parameters:
builder- the narration builder, cannot be null
-
isDecreasingKey
Tests if the key should decrease sliders with the specified direction.- Parameters:
ch- the key codedirection- the direction- Returns:
- true if the key should decrease sliders with the direction, false otherwise
- Since:
- 2.0.0
-
isIncreasingKey
Tests if the key should increase sliders with the specified direction.- Parameters:
ch- the key codedirection- the direction- Returns:
- true if the key should increase sliders with the direction, false otherwise
- Since:
- 2.0.0
-