Step 4 - Create states for the Car screen

In this step, you use the Kanzi state machine to show the car in your application from different angles and to animate the transition between those states.

Create the car shadow

In this section, in the Car Settings Activity you adjust the material used for the car shadow.

To create the car shadow:

  1. In the Activity Browser, in the Car Settings Activity, click activate.

    Under the car model you can see a black plane. This is because the alpha blending mode is incorrect for the material used to render the shadow texture.

    ../../_images/activity-browser-car-settings-active.png ../../_images/preview-car-settings.png
  2. In the Prefabs, select the Car > RootNode > Shadowplane node. In the Properties, next to the Cluster Material property, click go-to.

    This way you go to the properties of the Shadow material.

    ../../_images/shadowplane-in-the-project.png ../../_images/shadow-material.png
  3. In the Shadow material Properties, set the Blend Mode property to Alpha: Automatic.

    ../../_images/blend-mode-automatic.png ../../_images/car-alpha-shadow.png

Create states

In this section, you create different states for the car, each state focusing on a different part of the car. In the next section, you create the UI controls to move between these states.

In Kanzi, the state manager is one of the most important features for creating rich user interfaces.

To create states:

  1. In the Prefabs, double-click the Car Settings Activity to open it in a new Preview tab.

    When you open Car Settings on its own in the Preview, the background is black because by default the background of Activities is not set. In this tutorial, the Activities show the background of the Screen > RootNode.

    ../../_images/node-tree-car-settings-tab.png ../../_images/preview-tab-car-settings.png
  2. In the Node Tree, select the Car Settings > Viewport 2D > Car > RootNode > Camera_Root node.

    ../../_images/car-camera_root-node.png
  3. In the State Tools window, click Create State Manager to create a state manager in the Camera_Root node.

    You can find the State Tools window below the Preview window.

    ../../_images/create-state-manager1.png

    Kanzi Studio creates a new state manager and assigns it to the Camera_Root node.

    When the State Tools are active, the Preview tab is orange and you can create states using the State Tools. In this state, Kanzi Studio tracks all property value changes and you can use the State Tools to store these values to states.

    ../../_images/state-tools-active.png
  4. In the State Tools click Create State to create a state.

    Create three states.

    ../../_images/create-states.png
  5. Double-click the name of each state and name the states Brakes, Roof, Suspension.

    ../../_images/states-with-names.png
  6. In the Node Tree, select the Car Settings > Viewport 2D > Car > RootNode > Camera_Root > Camera node.

    ../../_images/car-camera.png
  7. In the Preview, select the Camera tool camera-tool and click set-position.

    When you enable this option, Kanzi Studio stores the current position of the free camera to the Camera node you select in the Camera tool dropdown menu.

    In this case, this is the Camera node that you selected in the Node Tree.

    ../../_images/camera-tool-store-current-position-on.png
  8. In the Node Tree, select the Car Settings > Viewport 2D > Car > RootNode > Car node. In the Preview Camera tool, click bring-camera to bring the camera closer to the car.

    ../../_images/car-node.png ../../_images/bring-camera-closer-to-car.png ../../_images/preview-car-closer.png
  9. In the Preview, in the Camera tool, use the orbit camera image0 to set the position of the car. In the State Tools, click image1 to save the current camera position to that state.

    For example, when you are satisfied with the position, in the State Tools in the Brakes state click image2 to store the current position of the Camera node to that state.

    ../../_images/store-values-to-state.png

    Orbit Camera lets you move around:

    • The nodes that you select in the Node Tree.

    • The node that you set with the Constraints > Look At property of the camera.

    To move with the orbit camera, use these controls:

    Control

    Description

    Click and drag the left mouse button.

    Rotate

    • Click and drag the middle mouse button.

    • Press the Space key, and click and drag the left mouse button.

    Pan

    • Scroll the mouse wheel.

    • Press the Shift and Alt keys, and click and drag the left mouse button.

    Zoom

    Hold down Ctrl and click the left mouse button.

    Select or deselect a node.

  10. Repeat the previous step until you create positions for all states.

    ../../_images/state1.png ../../_images/state2.png ../../_images/state3.png
  11. In the Preview, click image3 to enter the Interact mode. In the State Tools, click each state to see what your application looks like in that state.

    ../../_images/first-tutorial-step-4-car-states.gif
  12. In the State Tools, click Edit State Manager to deactivate the State Tools.

    ../../_images/state-tools-off.png

Create the controls to transition between states

In this section, you create the controls to transition between the states that you created in the previous section.

To create the controls to transition between states:

  1. In the Node Tree, press Alt and right-click the Car Settings > Viewport 2D node, select Stack Layout 2D, and name it CarControl.

    ../../_images/car-page-stack-layout-2d.png
  2. In the Node Tree, select the CarControl node. In the Properties, set the Direction property to Y.

    Use the Direction property to set the axis along which you want the Stack Layout node to stack its child nodes.

    ../../_images/stack-layout-direction-y.png
  3. In the Prefabs, click image4, create a Button 2D prefab, and in the Button 2D create an Image node.

    You use this prefab to control the state manager you created earlier in this step.

    ../../_images/button-2d-prefab.png
  4. In the Prefabs, select the Button 2D > Image node. In the Properties, add and set:

    • Layout Width and Layout Height properties to 100

    • Layout.Item > Vertical Margin property Top and Bottom property fields to 5

    • Next to the Image property click image5.

      This enables you to show a different image for each instance of that prefab in your project.

    ../../_images/button-2d-image-properties.png

    Tip

    To add a property, in the Properties, right-click, select Add Property, and select the property that you want to add.

    ../../_images/properties-right-click-to-add-vertical-margin.png
  5. From the Prefabs, drag the Button 2D prefab to the Node Tree and drop it on the Car Settings > Viewport 2D > CarControl node. Drag the prefab three times to create three buttons.

    Kanzi Studio instantiates the Button 2D prefabs in the CarControl node.

    ../../_images/carcontrol-with-button-2d-prefabs.png ../../_images/preview-carcontrol-buttons-start.png
  6. Select each Button 2D prefab instance, press F2, and rename them to Brakes, Roof, and Suspension.

    ../../_images/renamed-carcontrol-nodes.png
  7. In the Node Tree, in the CarControl node, select the first of the instantiated Button 2D prefabs:

    1. In the Properties, add the FirstApplication.Button2D.Image property and set it to the StateBrakes image.

      ../../_images/button2dimage-property.png
    2. In the Node Components, press Alt and right-click Triggers, and select Message Trigger > Button > Click.

      ../../_images/create-button-click-trigger.png
    3. In the Node Components, press Alt and right-click the Button: Click trigger, select Dispatch Message Action > State Manager > Go to State, and in the State Manager: Go to State action set:

      • Target Item to Prefabs/Car/Car/RootNode/Camera_Root

      • State to Camera_Root State Manager/StateGroup/Brakes

      ../../_images/click-add-go-to-state-action.png ../../_images/button-2d-button-click-action-go-to-state-brakes.png
  8. Repeat the previous step to set the remaining two Button 2D prefabs:

    • Use the StateRoof and StateSuspension images.

    • Set the Go to State action to point to the states Roof and Suspension.

    Tip

    To add triggers that have a similar setup, in the Node Components:

    1. Right-click a trigger and select Copy.

    2. Select the node where you want to add the trigger, in the Node Components, right-click Triggers, and select Paste.

    3. Edit the trigger and its actions.

    ../../_images/node-components-copy-trigger.png ../../_images/node-components-paste-trigger.png

In the Preview, when you click the buttons in the CarControl node, Kanzi Studio transitions to that state using the default linear transition. You can adjust the transitions using the State Transition Editor. See Using state transitions.

../../_images/step-4-mid.gif

Adjust the position of the buttons

In this section, you adjust the position of the buttons that control the states of the car.

To adjust the position of the buttons:

  1. Click the Screen tab under the main menu to show the entire application in the Preview.

    ../../_images/screen-tab.png
  2. In the Activity Browser, activate the Car Settings Activity. In the Prefabs, select the Car Settings > Viewport 2D > CarControl node. In the Properties, add the Transform 2D > Render Transformation property and set:

    • Translation X to 20

    • Translation Y to 75

    ../../_images/prefabs-carcontrol.png ../../_images/properties-carcontrol.png ../../_images/carcontrol-positioned.png

See also

To find out more about working with the state managers, see Using state managers.

To find out more about working with triggers and actions, see Triggers.

To find out more about working with the Preview Camera tool, see Using the Camera node.