Tutorial: Creating a slider

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

This video shows the result of the tutorial.

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

The starting point of this tutorial is the <KanziWorkspace>/Tutorials/Slider/Start/Slider.kzproj Kanzi Studio project.

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

Create the slider structure

In this section you create the structure for the slider.

To create the slider structure:

  1. In Kanzi Studio open the project stored in the <KanziWorkspace>/Tutorials/Slider/Start directory.

  2. In the Prefabs create a Slider 2D prefab and inside it create:

    • A node that visually represents the rail along which the slider knob moves. You can use an Image node or several Image nodes inside a layout.

      For example, create an Image node and name it Rail.

    • A Trajectory Layout 2D node that acts as the rail along which you want to move the slider knob.

    • Inside the Trajectory Layout 2D create a node that you want to use as the visual representation of the slider knob. For example, create an Image node and name it Knob.

    ../../_images/creating-your-own-2d-slider.png
  3. In the Prefabs select the Rail node and in the Properties set the Image property to the Rail image.

    ../../_images/rail-image.png
  4. In the Prefabs select the Knob node and in the Properties set the Image property to the Knob image.

    ../../_images/knob-image.png
  5. Create a line trajectory that acts as the rail along which the knob of the slider moves:

    1. In the Library press Alt and right-click Trajectories and select the Line Trajectory.

      ../../_images/create-line-trajectory.png
    2. In the Properties set:

      • Length to 256.

        Set the Length property to the same length as is the length in pixels of the visual representation of the rail.

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

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

      ../../_images/line-trajectory-properties.png
  6. In the Prefabs select the Trajectory Layout 2D node and in the Properties set the Trajectory property to the Line Trajectory you created in the previous step.

    ../../_images/trajectory-layout-2d-in-prefabs.png ../../_images/trajecotry-property-line-trajectory.png
  7. 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

Set the slider size and make the knob move along the trajectory

In this section you set the layout sizes of the slider and the visual representation of the slider rail to meet the size of the visual representation and the trajectory along which the knob on the slider moves.

To set the slider sizes and make the knob move along the trajectory:

  1. From the Prefabs drag the Slider 2D to the Node Tree and drop it on the Frame node to instantiate the prefab.

    ../../_images/instantiate-slider-prefab.png
  2. In the Preview click image0 to enter the Analyze mode, right-click image1, deselect 3D Grid, and select:

    • Debug objects

      This way you see the shape of the Trajectory Layout 2D node.

    • Borders of 2D nodes

      This way you see the size of different elements in your slider.

    ../../_images/visualize-borders-of-2d-nodes.png
  3. In the Node Tree select the instantiated Slider 2D prefab, in the Preview select the Node tool image2 and click the center square image3 to align the Slider 2D prefab instance to the center of the Frame node.

    ../../_images/slider-2d-centered.png
  4. In the Prefabs select the Slider 2D prefab, and in the Properties add and set:

    • Layout Width to 280

    • Layout Height to 30

    ../../_images/creating-your-own-2d-slider.png ../../_images/slider-2d-layout-height.png
  5. In the Prefabs select the Rail node, and in the Properties add and set:

    • Horizontal Alignment to Center

    • Vertical Alignment to Center

    ../../_images/rail-in-prefabs.png ../../_images/rail-horizontal-and-vertical-alignment.png
  6. In the Prefabs select the Trajectory Layout 2D node, and in the Properties add and set:

    • Layout Width to 256

    • Layout Height to 4

    • Horizontal Alignment to Center

    • Vertical Alignment to Center

    ../../_images/trajectory-layout-2d-in-prefabs.png ../../_images/trajectory-layout-2d-width-and-height.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.

    ../../_images/knob-binding.png
  8. In the Preview click image4 to exit the Analyze mode, click image5 to enter the Interact mode, and click and drag the slider knob along the rail.

See also

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

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

To learn more about creating Kanzi applications, see Tutorials.