Slider Controller

A slider controller is a user-interface element that we are probably all familiar with. How can we create our own custom slider controller? What is the structure, function, behavior of a slider control?

Structure:

Typically we imagine some horizontal rectangle, with an interactive identifier that show the current position of the slider indicator.

Function:

A one dimensional slider is used to determine a value within a given range of values.

Behavior:

When we press down on the slider, we expect the indicator to move to our mouse position if we are currently positioned over the bar. This is similar to button behavior, the button and slider only change values when they are directly under the mouse and there is some mouse movement.

Map:

Our slider will use the processing map() function because it's core functionality is to provide users with the ability to select a value within a range. The map function allows us to provide a visual range and an interactive indicator for the user to modify. In code, we create the mapping from the slider tool geometry: w, h, position, to determine the intended variation in the controlled value, such as hue or length.

Simple Linear Slider

The code below creates a slider that controls the length of a rectangle. It uses the map function. When determining the slider rectangle's range of values, this is determined by the pixel width of the rectangle: w. In order to determine the values we use to determine the slider's current position and current mapping output value, we need to also consider the x-offset of the slider. In other words, if the slider's left-side is positioned along the left side of the canvas, then x offset is 0. This would mean that if the indicator position: sliderX had a value of 20, and if the width of the slider was 40, we'd know that the slider indicator was at the middle of the range. However, if sliderX had a value of 20, but the x coordinate of the slider rectangle was also 20, we'd know that the slider indicator would be at it's minimum value in the range. Since we want flexibility to position the slider anywhere on the canvas, we need to consider the x offset of the slider when determining the slider's value. So the possible values for the slider Indicator will fall in the range of: ( min: x, max: x+w ), and we'll use these ranges in the map function as the slider representation's min, max value.

Slider, Base-Class

How can we define a Slider base class that allows us to create a slider of any size, at any position, that allows selection of values within any range? The map( ) function will allow us to define a generalized Slider base class.

Main Tab Code:

In the code below, we use a slider instance to control the scale of an ellipse

Can you make a slider to control Hue? Can we use the Slider base class to create a HueSlider class, how do we create a ROYGBIV Gradient Rectangle? Below is the HueSlider class. How would we create a Saturation and Brightness slider?

HueSlider, Child-class

To create Saturation and Brightness sliders, we need to consider that their display may be dependent on the Hue slider. So, in the main tab of our project, When we check the HueSlider, and before we display the Bright or Sat Sliders, we can use the value of the HueSlider to set their hue attribute.

//in main tab:

void CheckSliders(){

}

//SAT SLIDER

Main Tab Code

Here's how we check the sliders and use the Hue Slider to set the hue value for the SatSlider before we display the SatSlider. The sizeSlider is a base-class type slider. Notice that the reference variable data type for all sliders is of the base-class type.

Last updated

Was this helpful?