Tutorial: Creating a slider

In this tutorial you learn how to create a slider in Kanzi Studio.

Complete this tutorial if you want to understand the structure of Kanzi sliders and create your own slider. If you want to use and modify a ready-made slider, use the slider in the Kanzi Studio factory content. See Slider.

This video shows the result of the tutorial.

../../_images/slider-value-indicator1.gif

This tutorial assumes that you understand the basics of working with Kanzi Studio. The best entry points for getting familiar with Kanzi Studio are:

Assets for the tutorial

You can find the completed tutorial in the <KanziWorkspace>/Tutorials/Slider/Completed directory.

Create a slider

In this section you learn how to create a slider.

To create a slider:

  1. In Kanzi Studio create a project and in the New Project window set:

    • Name to Slider

    • Template to Kanzi Studio project

      Use this template to create a Kanzi application without using the Kanzi Engine API.

    ../../_images/create-slider-project.png
  2. In the Node Tree delete the Viewport 2D node.

    You can delete the Viewport 2D node because you do not create any 3D content in this tutorial.

    ../../_images/delete-viewport-2d.png
  3. In the Prefabs click create-prefab and create a Slider 2D node.

    You create the slider as a prefab so that you can reuse it.

    ../../_images/prefabs-slider-only.png
  4. Drag the Slider 2D prefab to the Node Tree and drop it on the RootPage node.

    This way you instantiate the prefab in the node tree so that you can follow the creation of the slider in the Preview.

    ../../_images/drag-prefab-to-rootpage.png ../../_images/node-tree-slider-2d.png
  5. In the Preview click analyze-off to enter the Analyze mode, right-click analyze-on, and select:

    • Debug objects shows visualization nodes, such as light, camera, and layout nodes.

    • Borders of 2D nodes outlines the borders of 2D nodes with striped lines.

    This way you can see in the Preview how the properties and their values affect the nodes as you create the slider.

    ../../_images/visualize-borders-of-2d-nodes.png
  6. In the Prefabs select the Slider 2D node, and in the Properties add and set:

    • Layout Width to 600

    • Layout Height to 60

    • Horizontal Alignment to Center

    • Vertical Alignment to Center

    This way you set the size and alignment of the slider, including the rail, the knob, and the position of the knob on both extremes of the slider.

    ../../_images/prefabs-slider-only.png ../../_images/properties-slider.png ../../_images/preview-slider-2d.png
  7. Create the visual representation of the rail along which the slider knob moves:

    1. In the Prefabs in the Slider 2D prefab create a Content Layout 2D node, and name it Rail.

      The Rail node visually represents the rail along which the slider knob moves. For example, to create a visual representation of a rail, you can use a Content Layout 2D node, an Image node, or several Image or Empty Node 2D nodes in a layout.

      ../../_images/prefabs-rail.png
    2. In the Prefabs select the Rail node, and in the Properties add and set:

      • Layout Width to 500

      • Layout Height to 25

      • Horizontal Alignment to Center

      • Vertical Alignment to Center

      This way you set the size and alignment of the slider rail. It is narrower and shorter than its parent Slider 2D node so that the center of the slider knob can move to the edge of the rail on both sides.

      ../../_images/prefabs-rail.png ../../_images/properties-rail.png ../../_images/preview-rail-size.png
    3. In the Prefabs select the Rail node, in the Properties add the Background Brush property, select + Color Brush, and name the brush Gray Brush.

      ../../_images/prefabs-rail.png ../../_images/properties-rail-background-brush.png
    4. Next to the Background Brush property click quick-edit-properties and set the Lightness (L) property field to 105.

      ../../_images/create-gray-brush.png ../../_images/preview-rail-color.png
  8. Create a trajectory that Kanzi uses to move the slider knob along the visual representation of the rail:

    1. In the Prefabs in the Slider 2D prefab, create a Trajectory Layout 2D node, and in the Properties add and set:

      • Layout Width to 500

      • Layout Height to 25

      • Horizontal Alignment to Center

      • Vertical Alignment to Center

      The values of these properties must match those of the visual representation of the slider rail.

      ../../_images/prefabs-trajectory-layout.png ../../_images/properties-trajectory-layout.png ../../_images/preview-trajectory-layout.png
    2. In the Prefabs select the Trajectory Layout 2D node and in the Trajectory property select + Line Trajectory.

      ../../_images/prefabs-trajectory-layout.png ../../_images/create-line-trajectory.png
    3. Next to the Trajectory property click quick-edit-properties and set:

      • Length to 500

        Set the Length property to the same value as the Layout Width property of the visual representation of the rail.

      • Direction X property field to 1, all the other property fields to 0

        This way you set the trajectory of the slider to be the x axis so that the slider knob moves horizontally on the rail.

      ../../_images/properties-line-trajectory.png ../../_images/preview-trajectory-layout-linear-trajectory.png
    4. Create an alias that Kanzi uses to tell the slider which trajectory to use as the rail of the slider:

      1. In the Prefabs select the Slider 2D prefab, in the Dictionaries window click + Add Resource, and select Create > Alias.

        ../../_images/create-new-alias-resource1.png
      2. Name the alias $layout and set it to point to the Trajectory Layout 2D node in the Slider 2D prefab.

        Kanzi uses this alias to tell the slider along which trajectory to move the slider knob.

        ../../_images/slider-alias.png
  9. Create the slider knob:

    1. In the Prefabs in the Trajectory Layout 2D create a Content Layout 2D node, name it Knob, and in the Properties add and set:

      • Layout Width to 40

      • Layout Height to 60

      You use this node as the visual representation of the slider knob.

      ../../_images/prefabs-knob-border.png ../../_images/properties-knob-border.png ../../_images/preview-knob.png
    2. In the Knob node add the Background Brush property, set it to + Color Brush, and name the brush White Brush.

      ../../_images/prefabs-knob-border.png ../../_images/properties-knob-border-background-brush.png
    3. Next to the Background Brush property click quick-edit-properties and set the Lightness (L) property field to 255.

      ../../_images/create-white-brush.png ../../_images/preview-knob-color.png
    4. In the Prefabs in the Knob node create an Empty Node 2D, add the Background Brush property, set it to + Color Brush, and name the brush Blue Brush.

      ../../_images/prefabs-empty-node.png ../../_images/properties-empty-node.png
    5. Next to the Background Brush property click quick-edit-properties and set these property fields:

      • Hue (H) to 155

      • Saturation (S) to 216

      • Lightness (L) to 156

      ../../_images/create-blue-brush.png ../../_images/preview-empty-node.png
    6. In the Prefabs select the Knob node, and in the Properties add and set:

      • Horizontal Padding Left to 3

      • Horizontal Padding Right to 3

      • Vertical Padding Top to 3

      • Vertical Padding Bottom to 3

      ../../_images/prefabs-knob-all.png ../../_images/properties-empty-node-padding.png ../../_images/preview-empty-node-margin.png
    7. In the Prefabs select the Knob node, in the Properties click + Add Binding, and in the Binding Editor set:

      • Property to Trajectory Override Offset

      • Expression to

        {@../../RangeConcept.NormalizedValue}
        

      Click Save.

      This binding makes the slider knob move on the trajectory that defines the slider rail.

      ../../_images/prefabs-knob-all.png ../../_images/properties-knob-add-binding.png ../../_images/knob-binding.png
  10. In the Preview click:

    • analyze-on to exit the Analyze mode

    • interact to enter the Interact mode

In the Preview you can now drag the slider knob along the rail.

../../_images/slider-tutorial-result.gif

Display the slider value

In this section you learn how to set the minimum, maximum, step, and starting values for a slider, and display the value of a slider.

To display the slider value:

  1. In the Prefabs select the Slider 2D prefab, in the Properties from the Range category add and set:

    • Minimum Value to 0

      This way you set the lowest value for a slider.

    • Maximum Value to 20

      This way you set the highest value for a slider.

    • Step Value to 1

      This way you set the increment by which the slider knob moves.

    • Value to 0

      This way you set the initial position of the slider knob. If you do not set the Minimum Value and the Maximum Value properties, the range is between 0 and 1.

    ../../_images/prefabs-slider1.png ../../_images/slider-range-properties.png
  2. In the Prefabs in the Slider 2D prefab create a Text Block 2D node, name it Slider Value, and in the Properties add the Vertical Alignment property and set it to Center.

    ../../_images/prefabs-text-block.png ../../_images/properties-text-block1.png
  3. In the Prefabs select the Slider Value node, in the Properties, click + Add Binding, and in the Binding Editor set:

    • Property to Text

    • Expression to

      INT({@../RangeConcept.Value})
      

    Click Save.

    This way you set the Slider Value node to display the current value of a slider.

    ../../_images/text-value-binding-editor.png

In the Preview when you drag the slider knob, the Slider Value node shows the current value of the slider.

../../_images/slider-value-indicator2.gif

Tip

You can control the Value property of a slider with another property and a to-source or two-way binding. See Updating the slider value with a property.

What’s next?

In this tutorial you learned how to create a slider. Now you can:

See also

To learn more about using sliders, see Using the Slider nodes.

To learn more about trajectories, see Trajectories.

To learn more about using bindings, see Using bindings and Bindings expressions reference.

To learn more about prefabs, see Using node prefabs.

To learn more about creating Kanzi applications, see Tutorials.