# Behavior Definition

#### Behavior Definition

While SceneComposer can be used to compose the static structure and properties of a scene, with Behaviors it is possible to add dynamics to a static scene structure.

<div class="contents" id="bkmrk-a-behavior-is-define"><div class="contents"><div class="textblock">- A Behavior is defined by behavior attributes: Name, Type, Properties
- Behavior attributes are expressed in the <span style="color: rgb(230, 126, 35);">[Behavior Meta Information](https://doc316en.candera.eu/link/568#bkmrk-behavior-meta-inform)</span>
- Behaviors are assembled together in a widget set
- Behaviors and the widget set can be assembled in several Behavior libraries (lib), to be linked to the application

</div></div></div>#### Behavior Generation

If *cgi\_studio\_cit* is within the package, the base classes for Behaviors can be generated. Therefore an .xhcdl-file has to be written. The base class can include properties that can be defined in the .xhcdl-file too. The xhcdl-files can be found for each behavior in the same folder where the source-code lies. The following example shows an .xhcdl-file for a behavior.

```
<?xml version="1.0" encoding="utf-8"?>
<definition
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="../../../../cgi_studio_cit/schema/HmiContract.xsd">
    <include href="Behaviors/Behavior.xhcdl" addIncludeToCode="false" />
    <generator location="Behaviors/Transition/generated">
        <namespace namespace="Candera" />
        <header>
            <include file="Behaviors/ActionBehavior/ActionBehavior.h" />
            <include file="CanderaWidget/WidgetBase/StringDataTypes.h" />
        </header>
    </generator>
    <widgets setPolicy="onChange" notifierPolicy="onUpdate" viewInvalidationPolicy="wakeup">
        <group baseClassRef="ActionBehavior">
            <widget name="SimpleTransitionActionBehavior" readableName="" uid="{889EF594-E702-4846-930D-BA377121BBA0}">
                <property name="ActivatedSceneAssetID" description="The scene to activate." type="FeatStd::UInt32" uid="{7DB722C4-5543-43D8-BEB0-DF38729C8F5C}" />
                <property name="IsActivatedScene3D" description="If the activated scene is 3D or not." type="bool" uid="{ED8607D7-1F37-49FD-B689-CBF444DFF708}" />
                <property name="DeactivatedSceneAssetID" description="The scene to deactivate." type="FeatStd::UInt32" uid="{9A0E2E92-AAF6-4460-A2D9-7F40FED3210E}" />
                <property name="IsDeactivatedScene3D" description="If the deactivated scene is 3D or not." type="bool" uid="{31F35C65-6E87-4EAF-A660-709C8FB67F1A}" />
                <property name="Variant" description="The transition variant." type="FeatStd::String" uid="{E3A46266-73DE-4F1D-BD5F-224E3ABB91E3}" />
            </widget>
        </group>
    </widgets>
</definition>
```

Some important tags and attributes:

<div class="contents" id="bkmrk-generator-location%3A-"><div class="contents"><div class="textblock">- Generator location: Defines in which folder the base class should be generated.
- Group baseClassRef: The base class of the generated base class.
- Widget name: The name of the generated base class. The suffix "Base" will be added to the class-name.
- Property: The properties of the Behavior. For the String-type the include file *CanderaWidget/WidgetBase/StringDataTypes.h* has to be added for instance. If the property can't be generated it can still be added in the source code.

</div></div></div>After the .xhcdl-file is written the behavior can be generated by calling *GenerateBehaviors.bat* which can be found in *cgi\_studio\_controls/src/Behaviors*. The files are a base class with a header- and cpp-file. It is meant to derive from that class. All the properties and its getter- and setter-methods are already in the class.

#### Behavior Meta Information

**Behavior Name and Type**

To publish a behavior to SceneComposer, it must register for the widget with its name and type:

<div class="contents" id="bkmrk-cdabehaviordef-behav"><div class="contents"><div class="textblock"><table class="doxtable"><tbody><tr><td>CdaBehaviorDef</td><td>Behavior class name and its base class</td></tr><tr><td>CdaDescription</td><td>Behavior description for the SceneComposer tooltip</td></tr><tr><td>CdaReadableName</td><td>Behavior name</td></tr></tbody></table>

</div></div></div><p class="callout info">Note that the example above explains how to publish a behavior for a 3D-scene. For 2D-scenes the macro *CdaBehavior2DDef* must be used instead of *CdaBehaviorDef*.</p>

**Behavior Properties**

For linking a behavior with scene content in SceneComposer, behavior properties are used. Usually the properties should be defined in the .xhcdl-file of a behavior. Anyway, the properties can be defined directly in the behavior-class by using the appropriate macros in the header-file. The behavior implements a property by providing a getter and a setter method.

<div class="contents" id="bkmrk-cdaproperty-specifie"><div class="contents"><div class="textblock"><table class="doxtable" style="width: 100%;"><tbody><tr><td style="width: 14.5822%;">CdaProperty</td><td style="width: 85.4178%;">Specifies \[property\_name\], \[property\_type\], \[property\_getter\], \[property\_setter\]</td></tr><tr><td style="width: 14.5822%;">CdaDescription</td><td style="width: 85.4178%;">Behavior property description for the SceneComposer tooltip</td></tr><tr><td style="width: 14.5822%;">CdaCategory</td><td style="width: 85.4178%;">Defines a category for the property. SceneComposer displays all properties of one category in a related Category section in the property grid.</td></tr></tbody></table>

</div></div></div><p class="callout info">The Behavior property must be initialized correctly, e.g. in the widget constructor!  
Take care that the getter and setter methods are safe (prevent null pointer accesses)!</p>

#### Usage of the introduced macro CGI\_BEHAVIOR\_FORWARD\_AssetLoaderDataTypeDef

This macro is used to be able to set a certain behavior as property of another behavior.

Therefore following code has to be added to the xhcdl file to generate the macro in the base class of the behavior:

```
<postNamespace text="CGI_BEHAVIOR_FORWARD_AssetLoaderDataTypeDef(::CgiStudioControl::ValueBehavior)" />
```

This is an example code to be able to use a ValueBehavior as property. The generated code in the base class of your behavior will look like this:

```
<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___behavior_base.html#ga16abcb418e53e14ab4e909538ffb4c57">CGI_BEHAVIOR_FORWARD_AssetLoaderDataTypeDef</a>(::<a class="code" href="http://dev.doc.cgistudio.at/APILINK/class_cgi_studio_control_1_1_value_behavior.html" title="Receives a ChangeValueEvent and passes its changed value to the receivers (Value Processing Behaviors...">CgiStudioControl::ValueBehavior</a>)
```

<div class="contents" id="bkmrk-"><div class="contents"><div class="textblock"><div class="fragment">---

</div></div></div></div>