Skip to content

Event ​

Event is an object that is generated and sent based on some user action (e.g. a mouse click, a key press, etc.). The dispatch method is used to distribute an event.

The event either occurs (which is equivalent to bool==true) or is absent. The event can be triggered by various conditions, and both parents and children and siblings can catch (or listen to) the event. To do this, you need to declare the same event in the scopes of all elements where you plan to catch them.

Example ​

python
(event valChanged)
python
(event evKilled)
...
(dispatch evKilled args={} dir="EventDirection.DOWN" (event $datahub.getEntity(entityId).health.evKilled))

event is the event on which dispatch occurs.

dir specifies the direction of the event in the hierarchy. By default, dispatch sends the event only within the element itself. To send an event to a parent, you need to specify EventDirection.UP in dir. To a child, specify EventDirection.DOWN. To siblings, you just need to declare an event in the desired sibling, and you can omit dir. A dict with values is created for dir in hud_replaces. By default, dir=0.

python
(def constant EventDirection {  NONE:  0,
                                UP:    1,
                                DOWN:  2
})

Generate event ​

The event can be generated:

  • in scalefom, for example, mouse click event or keyboard keyDown event
python
(block
    (style
        (width = 100)
        (height = 100)
        (backgroundColor = 0xff00ff00)
    )
    (bind alpha "0.5" init=false on='click')
)
  • in core c++ parts of unbound. For example, in slider block, valueChanged event is generated when its value changes.position changes
python
(dispatch valChanged on='valueChanged' dir=1)
  • in the layout, i.e. the event is declared in scope of the element and is generated in its definition.
python
(def element TestView() layout = true entrance=true
    (scope
        (event eventScope)
    )
  
    (element Button
        (dispatch eventScope on='click')
    )
)

But in essence, this is not the generation of an event, but a transformation from scalefom/core c++ event (in this example click) into scope event (in this example eventScope).

Bind construct ​

Binding constructs can subscribe to events in two ways:

  • Pass the event name to on argument.
python
(bind eventArgs "$event" init=false on='eventScope')
  • Pass a nested event object.
python
(trace "$event" init=false (event "eventScope"))

Differences between on and event ​

If the event is generated in scaleform or in core c++ unbound, then you can only subscribe using on argument.

Details
python
(def element TestView() layout = true entrance=true
    (scope
        (event eventScope)
    )
  
    (block
        (style
            (width = 100px)
            (height = 100px)
            (backgroundColor = 0xffff0000)
        )
        (bind alpha "0.5" init=false on='click')
    )
  
    (video original_widht=1920 original_height=1080
        (style
            (width = 320px)
            (height = 180px)
        )
  
        (source = "R.videos.Logo_All" )
        (trace "$event" init=false on='metaDataChanged')
    )
)

If an event is declared in an element scope, there are 2 subscription cases depending on the location of the subscription.

  • Subscription to scope can be done both via on and via event.
Details
python
(def element TestView() layout = true entrance=true
    (scope
        (event eventScope)
  
        (var eventArgs:dict = null)
        (bind eventArgs "$event" init=false (event "eventScope"))
        # (bind eventArgs "$event" init=false on='eventScope')
    )
  
    (element Button
        (scope
            (label = 'dispatch scope event')
        )
        (dispatch eventScope on='click')
    )
  
    (trace "eventArgs" init=false)
)
  • Subscription outside scope can only be done via event.
Details
python
(def element TestView() layout = true entrance=true
    (scope
        (event eventScope)
    )
  
    (element Button
        (scope
            (label = 'dispatch scope event')
        )
        (dispatch eventScope on='click')
    )
  
    (trace "$event" init=false (event "eventScope"))
)

Thus, you can subscribe via on argument to those events that are generated by target-object. Let's separately consider the case of subscribing to scope events when creating an element.

Details
python
(element Button
    (scope
        (label = 'default text')
        (bind label 'leftClick' init=false on='evBtnLeftClickEvent')
        (bind label 'rightClick' init=false on='evBtnRightClickEvent')
    )
)

The events evBtnLeftClickEvent and evBtnRightClickEvent are generated in Button element definition. If you use event construct , events must be declared in parent scope.

Details
python
(def element TestView() layout = true entrance=true
    (scope
        (event eventScope)
    )
  
    (element Button
        (scope
            (label = 'default text')
            (bind label "'keyCode: ' + $event.keyCode" init=false (event "eventScope"))
        )
    )
  
    (dispatch eventScope on='stageKeyUp')
)