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
(event valChanged)(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.
(def constant EventDirection { NONE: 0,
UP: 1,
DOWN: 2
})Generate event
The event can be generated:
- in
scalefom, for example, mouseclickevent or keyboardkeyDownevent
(block
(style
(width = 100)
(height = 100)
(backgroundColor = 0xff00ff00)
)
(bind alpha "0.5" init=false on='click')
)- in
core c++parts ofunbound. For example, insliderblock,valueChangedevent is generated when its value changes.position changes
(dispatch valChanged on='valueChanged' dir=1)- in the layout, i.e. the event is declared in
scopeof the element and is generated in its definition.
(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
onargument.
(bind eventArgs "$event" init=false on='eventScope')- Pass a nested
eventobject.
(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
(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
scopecan be done both viaonand viaevent.
Details
(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
scopecan only be done viaevent.
Details
(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
(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
(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')
)