Intro to sliders
Sliders are a powerful way to vary numeric values between a minimum and a maximum value. They have a bar or line (a "rail") over which a "handle" slides left and right (or up and down), and the position of this handle relative to the rail determines the value.
Constructor
The constructor for this widget is UISlider and its signature is the following:
UISlider(_id, _x, _y, _length, _sprite, _sprite_handle, _value, _min_value, _max_value, _orientation=UI_ORIENTATION.HORIZONTAL, _relative_to=UI_DEFAULT_ANCHOR_POINT)
idis the string ID.xandyare the offsets with respect to the anchor point. Note that, as directed in the Fundamentals page, your sprites should have its origin point set to top left, to allow for correct positioning in gooey.lengthis the desired width (if horizontal) or height (if vertical) of the slider rail.spriteis the sprite used to render the slider rail.sprite_handleis the sprite used to render the handle.valueis the starting value of the slider.min_valueandmax_valueare the minimum and maximum possible values of the control.orientationdetermines whether the slider is rendered horizontally or vertically, according to theUI_ORIENTATIONenum.relative_tois the anchor point, as usual from theUI_RELATIVE_TOenum.
Setting the value interactively
In sliders you have three ways of interacting with the slider to change a value:
- Dragging the handle;
- Clicking in the "rail"; and
- Scrolling with the mouse wheel
Dragging the handle
You can click and drag the handle to change the value. The value will change in multiples of what is set using setDragChange method (by default, 1 unit).
Clicking the rail
You can click the rail either at the left (or bottom, if vertical) of the handle (to decrease the value) or at the right (or top, if vertical) of the handle (to increase the value). The amount changed per click can be set using the setClickChange method - by default, 2 units.
You can also change the default behavior so that, instead of decreasing or increasing the value each time you click, you directly set the slider to the value at the click position. This is configured by the setClickToSet method.
Scrolling the mouse wheel
You can scroll the mouse wheel while over the slider to increase or decrease the value. The amount changed per scroll will be set using the setScrollChange method - by default, 1 unit.
Setting the value programmatically
You can use setValue to set the value of a slider programatically. This method will clamp the provided value between the minimum and maximum allowed.
Setting up the sprites
Remember that the sprites need to have their origin set at top left. However, this implies the handle will be drawn from the top of the rail down. We can adjust where the handle "intersects" the rail by configuring its offset, with setHandleOffset.
Apart from the base "rail" sprite, you can also configure a "progress" sprite, which shows the sprite overlaid from the start of the slider from the "left" or "bottom" of the rail up to the handle. This is optional and off by default - you can set it using the setSpriteProgress and setImageProgress methods.
Configuring text
By default, the slider will show both the min/max values (at each end of the "rail") and the current value (at the top of the handle). You can enable or disable minimum, maximum and handle text (setShowMinMaxText and setShowHandleText), and you can control the format of this string with setTextFormat, as well as it offset with setHandleTextOffset.
Example
Let's create an example using the included Kenney's UI pack. I will create a Settings menu with three sliders: one for sound volume, one for music volume and one for FOV correction1.
For the Sounds Volume slider, I want it to be a regular slider that goes from 0 to 100. I want it to be able to change by 5 when clicked, by 1 when dragged and by 5 when scrolled. Let's take a look at the code:
I want to change things up a bit for the Music Volume slider. Let's make it so it shows only the handle text, sets the change unit to 10 no matter if it's click, scroll or drag, and enables "click to set", so the slider changes to the value corresponding to the position I click:
Finally, I want the third slider to vary between -3 and 3, with different increments for click, change and drag.
The final interactive example looks like so:
-
No idea what this is, I couldn't think of a good example that varies between negative and positive units and relates to a game setting. If something occurs to me, I will change it. ↩