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
Modifier and TypeClassDescriptionstatic enum
The direction enum represents all four directions a slider can face. -
Field Summary
Modifier and TypeFieldDescriptionprotected final Axis
protected float
A coordinate:value ratio.protected WAbstractSlider.Direction
protected boolean
True if the user is currently dragging the thumb.protected int
protected int
protected int
protected float
A value:coordinate ratio. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addNarrations
(NarrationMessageBuilder builder) Adds the narrations of this widget to a narration builder.boolean
canFocus()
Tests whether this widget can have the focus in the GUI.boolean
Checks whether this widget can be resized usingWWidget.setSize(int, int)
.getAxis()
Gets the direction of this slider.int
int
protected abstract int
Returns the thumb size along the slider axis.int
getValue()
static boolean
isDecreasingKey
(int ch, WAbstractSlider.Direction direction) Tests if the key should decrease sliders with the specified direction.boolean
Tests whether the user is dragging this slider.static boolean
isIncreasingKey
(int ch, WAbstractSlider.Direction direction) Tests if the key should increase sliders with the specified direction.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.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 void
onValueChanged
(int value) void
setDirection
(WAbstractSlider.Direction direction) Sets the direction of this slider.void
setDraggingFinishedListener
(@Nullable IntConsumer draggingFinishedListener) void
setMaxValue
(int max) void
setMinValue
(int min) void
setSize
(int x, int y) Sets the size of this widget.void
setValue
(int value) Sets the slider value without calling listeners.void
setValue
(int value, boolean callListeners) Sets the slider value.void
setValueChangeListener
(@Nullable IntConsumer valueChangeListener) void
tick()
Executes a client-side tick for this widget.protected void
UpdatescoordToValueRatio
andvalueToCoordRatio
.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()UpdatescoordToValueRatio
andvalueToCoordRatio
. 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:WWidget
Sets 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:WWidget
Checks whether this widget can be resized usingWWidget.setSize(int, int)
. -
canFocus
public boolean canFocus()Description copied from class:WWidget
Tests whether this widget can have the focus in the GUI. -
onMouseDown
Description copied from class:WWidget
Notifies this widget that the mouse has been pressed while inside its bounds- Overrides:
onMouseDown
in 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.PROCESSED
if the event is handled,InputResult.IGNORED
otherwise.
-
onMouseDrag
@Environment(CLIENT) public InputResult onMouseDrag(int x, int y, int button, double deltaX, double deltaY) Description copied from class:WWidget
Notifies this widget that the mouse has been moved while pressed and inside its bounds.- Overrides:
onMouseDrag
in 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.PROCESSED
if the event is handled,InputResult.IGNORED
otherwise.
-
onClick
Description copied from class:WWidget
Notifies this widget that the mouse has been pressed and released, both while inside its bounds.- Overrides:
onClick
in 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.PROCESSED
if the event is handled,InputResult.IGNORED
otherwise.
-
onMouseUp
Description copied from class:WWidget
Notifies this widget that the mouse has been released while inside its bounds- Overrides:
onMouseUp
in 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.PROCESSED
if the event is handled,InputResult.IGNORED
otherwise.
-
onMouseScroll
@Environment(CLIENT) public InputResult onMouseScroll(int x, int y, double horizontalAmount, double verticalAmount) Description copied from class:WWidget
Notifies this widget that the mouse has been scrolled inside its bounds.- Overrides:
onMouseScroll
in 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.PROCESSED
if the event is handled,InputResult.IGNORED
otherwise.
-
tick
@Environment(CLIENT) public void tick()Description copied from class:WWidget
Executes 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:WWidget
Notifies this widget that a key has been pressed.- Overrides:
onKeyPressed
in classWWidget
- Parameters:
key
- the GLFW scancode of the key- Returns:
InputResult.PROCESSED
if the event is handled,InputResult.IGNORED
otherwise.
-
onKeyReleased
Description copied from class:WWidget
Notifies this widget that a key has been released- Overrides:
onKeyReleased
in classWWidget
- Parameters:
key
- the GLFW scancode of the key- Returns:
InputResult.PROCESSED
if the event is handled,InputResult.IGNORED
otherwise.
-
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:WWidget
Adds 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:
addNarrations
in 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
-