Node
¶
Base class for Kanzi nodes.
Use the interface provided by Node to dispatch messages and enable the nodes in the node tree to react to messages.
When you dispatch a message it travels in two phases: first tunneling, then bubbling. During the tunneling phase the message travels from the root node to the node where the message originated. From there the message bubbles back to the root node.
When you want a node to react to a message when that message passes, add message handlers to those nodes. A message handler listens only to messages from a specific source node. To register message handlers, use addMessageHandler() API.
To remove all types of handlers and filters use removeMessageHandler().
To dispatch a single message, use dispatchMessage().
-- Define a message arguments class for the message.
CustomMessageArguments = MessageArguments.createMessageArgumentsClass({
CaptionArgument = PropertyType:find("project.CaptionPropertyType")
})
-- Create an instance of MessageType for the message type from the project.
CustomMessage = MessageType:find("CustomMessage", CustomMessageArguments)
-- Define message handler function.
function handleMessage(arguments)
-- Kanzi creates the instance of correct message arguments class and passes it to the message handler.
print(arguments:getCaptionArgument())
end
-- Add the message handler to a node.
local token = contextNode:addMessageHandler(CustomMessage, handleMessage)
-- Dispatch message.
local args = CustomMessage:newMessageArguments()
args:setCaptionArgument("Hello Lua world!")
contextNode:dispatchMessage(CustomMessage, args)
-- Remove message handler.
contextNode:removeMessageHandler(token)
Use node components to add functionality to a node.
-- Use ClickManipulatorComponent to allow users to click nodes in your application.
local clickManipulator = ClickManipulatorComponent:create("ClickComponent")
contextNode:addNodeComponent(clickManipulator)
-- Enable HitTestable property for the Node to receive click events.
contextNode:setProperty(Node.HitTestableProperty, true)
-- Add click message handler.
contextNode:addMessageHandler(ClickManipulator.ClickMessage,
function(args)
print("Click!")
end
)
To access node components owned by the node, use lookupNodeComponent(). To remove node component from a node, use removeNodeComponent().
-- Find the click manipulator component previously added to the node.
local manipulatorComponent = contextNode:lookupNodeComponent("ClickComponent")
assert(manipulatorComponent)
-- Remove the click manipulator component from the node to remove the click functionality.
contextNode:removeNodeComponent(manipulatorComponent)
To iterate through all the node components owned by the node, use iterateNodeComponents().
-- Iterate through node components.
for component in contextNode:iterateNodeComponents() do
print(component:getProperty(NodeComponent.NameProperty))
end
To set node properties see KanziObject:setProperty() and KanziObject:getProperty().
local objectNode = EmptyNode2D:create("TestNode")
objectNode:setProperty(Node.NameProperty, "NewTestNodeName");
objectNode:setProperty(Node.WidthProperty, 25);
objectNode:setProperty(Node.HorizontalAlignmentProperty, Node.HorizontalAlignment.HorizontalAlignmentCenter)
objectNode:setProperty(Node.HorizontalMarginProperty, Vector2(5, 7))
objectNode:setProperty(Node.VisibleProperty, false)
local name = objectNode:getProperty(Node.NameProperty)
local width = objectNode:getProperty(Node.WidthProperty)
local hAlign = objectNode:getProperty(Node.HorizontalAlignmentProperty)
local hMargin = objectNode:getProperty(Node.HorizontalMarginProperty)
local visible = objectNode:getProperty(Node.VisibleProperty)
Inherits properties and message types from NodeMetadata.
| getName() | Sets the value of NodeMetadata.NameProperty |
| lookupNode() | Returns a node by looking it up using a path or an alias |
| lookupObject() | Lookup for an object from a node |
| trySetFocus() | Tries to move the focus to this node |
| getParent() | Returns the parent of the node |
| setHorizontalAlignment() | Sets the value of NodeMetadata.HorizontalAlignmentProperty |
| tryAcquireResource() | Acquires a resource from the node or its closest ancestor that has the resource ID in the resource dictionary |
| dispatchMessage() | Dispatches a message from this node with specified arguments |
| addMessageHandler() | Adds a message handler where the handler is a function |
| addMessageHandlerFromSource() | Adds a message handler where the handler is a function and you explicitly define the accepted source |
| addMessageFilter() | Adds a message filter where the filter is a function |
| addTunnelingMessageHandler() | Adds a message handler where the handler is a function and you explicitly define the accepted source |
| addTunnelingMessageFilter() | Adds a message filter where the filter is a function |
| removeMessageHandler() | Removes a message subscription |
| addNodeComponent() | Transfers the ownership of a node component to an object node |
| removeNodeComponent() | Removes the ownership of a node component from an object node |
| lookupNodeComponent() | Returns a node component with the specified name that is owned by the node |
| iterateNodeComponents() | Returns an iterator used to iterate over node components owned by this node |
| addInputManipulator() | Transfers the ownership and attaches an input manipulator to an object node |
| removeInputManipulator() | Removes the ownership and detaches an input manipulator from an object node |
| isNamed() | Checks if the node has the given name |
| isEffectivelyVisible() | Returns whether a node and its ancestor nodes are visible |
| isEffectivelyEnabled() | Returns the value of NodeMetadata.EffectivelyEnabledProperty |
| isEffectivelyFocusable() | Returns whether a node is focusable |
| isInitialized() | Gets the initialized status |
| isAttached() | Gets the attached status |
| isAttaching() | Gets the attaching status |
| isDetaching() | Gets the detaching status |
| addAbstractChild() | Transfers the ownership and attaches an input manipulator to an object node |
| getAbstractChildCount() | |
| getAbstractChildIndex() | |
| getAbstractChild() | |
| removeAbstractChild() | |
| getChangeFlags() | Get current change flags |
| getChildChangeFlags() | Get current child change flags |
| setChangeFlag() | Sets a change flag |
| clearChangeFlag() | Clears a change flag |
| isValidLayoutValue() | Indicates whether a value is valid layout value |
| unboundedLayoutValue() | Returns an unbounded layout value |
| isUnboundedLayoutValue() | Indicates whether a value is an unbound layout value |
Sets the value of NodeMetadata.NameProperty.
| (string) | Node name. |
Returns a node by looking it up using a path or an alias. Kanzi Lua deduces the correct node type and returns an instance of the corresponding type.
-- Look up a sibling node named "Text".
local siblingTextNode = contextNode:lookupNode("../Text")
-- If found, Kanzi instantiates the node, so that you can access its methods.
if siblingTextNode ~= nil then
siblingTextNode:setText("Hello Lua world!")
end
| path | (string) | Relative path to use for lookup. |
| (Node or nil) | An instance of the cooresponding node type. If node is not found, nil. |
Lookup for an object from a node. Object found may be another node or a resource within a node. Kanzi Lua deduces the correct node type and returns an instance of the corresponding type.
| path | (string) | Relative path to use for lookup. |
| (Node or nil) | Shared pointer referencing object. May be empty if no object found. |
Tries to move the focus to this node. If this node is a focus scope node, this method tries to focus the preserved focus of the focus scope, or tries to focus on the first focusable descendant node of this node.
| (Node) | If this method successfully moved focus, returns this node or a descendant node of this node. If this method did not move focus, returns nil. |
Returns the parent of the node.
| (Node or nil) | Pointer to the parent node or nil. |
Sets the value of NodeMetadata.HorizontalAlignmentProperty.
| value | (Node.HorizontalAlignment) | The new value of NodeMetadata.HorizontalAlignmentProperty. |
Acquires a resource from the node or its closest ancestor that has the resource ID in the resource dictionary. Kanzi Lua deduces the correct type of the Resource and returns an ObjectRef that holds an owning reference to the instance of corresponding type.
-- Acquire the prefab resource and then instantiate the prefab.
local prefab = contextNode:tryAcquireResource("kzb://project/Prefabs/Caption")
local node = prefab:instantiate("Caption Node")
-- Acquire the color brush and set the background of the instantiated prefab.
local brush = contextNode:tryAcquireResource("kzb://project/Brushes/Color Brush")
node:setProperty(Node2D.BackgroundBrushProperty, brush)
| resourceID | (string) | Name of the resource. |
| (ObjectRef or nil) | An ObjectRef that holds an owning reference to the instance of the corresponding type of the Resource. If the resource ID or URL do not map to any resource, nil. |
Dispatches a message from this node with specified arguments.
| messageType | (MessageType) | The type of the dispatched message. |
| messageArguments | (MessageArguments) | The arguments of the dispatched message. |
Adds a message handler where the handler is a function. The handler is invoked when a message of messageType is signaled in the node to which you add the handler.
Message handler function accepts as parameter only the instance of the MessageArguments specific for a given messageType. To later manage message subscription, store the returned MessageSubscriptionToken.
| messageType | (MessageType) | The message type to listen to. |
| handler | (function) | The function invoked when the node that owns the handler receives a matching message. |
| (MessageSubscriptionToken) | The MessageSubscriptionToken that identifies the added handler. |
Adds a message handler where the handler is a function and you explicitly define the accepted source. The handler is invoked when a message of messageType bubbles through the node to which you add the handler, but only when you dispatch it from the given source node.
| messageType | (MessageType) | The message type to listen to. |
| messageSourceFilter | (Node) | Source node of the messages. |
| handler | (function) |
| (MessageSubscriptionToken) | The MessageSubscriptionToken which identifies the added handler. |
Adds a message filter where the filter is a function. The filter is invoked when a message of messageType bubbles through the node the filter is added to.
| messageType | (MessageType) | The message type to listen to. |
| handler | (function) |
| (MessageSubscriptionToken) | The MessageSubscriptionToken which identifies the added filter. |
Adds a message handler where the handler is a function and you explicitly define the accepted source. The handler is invoked when a message of messageType tunnels through the node to which you add the handler, but only when you dispatch it from the given source node.
| messageType | (MessageType) | The message type to listen to. |
| messageSourceFilter | (Node) | Source node of the messages. |
| handler | (function) |
| (MessageSubscriptionToken) | The MessageSubscriptionToken which identifies the added handler. |
Adds a message filter where the filter is a function. The filter is invoked when a message of messageType tunnels through the node to which you add this filter.
| messageType | (MessageType) | The message type to listen to. |
| handler | (function) |
| (MessageSubscriptionToken) | The MessageSubscriptionToken which identifies the added filter. |
Removes a message subscription.
| token | (MessageSubscriptionToken) | The message subscription token that identifies the handler. |
Transfers the ownership of a node component to an object node.
| nodeComponent | (NodeComponent) | The node component to be added to the node. |
Removes the ownership of a node component from an object node.
| nodeComponent | (NodeComponent) | The node component to be removed from the node. |
Returns a node component with the specified name that is owned by the node.
| name | (string) | The name of the node component. |
| (NodeComponent or nil) | The NodeComponent with the specified name that is owned by the node, or nil if none is found. |
Returns an iterator used to iterate over node components owned by this node.
Transfers the ownership and attaches an input manipulator to an object node.
| inputManipulator | (InputManipulator) | The input manipulator to add. |
Removes the ownership and detaches an input manipulator from an object node.
| inputManipulator | (InputManipulator) | The input manipulator to remove. |
Checks if the node has the given name. \param name Name to compare against.
| name | (string) | Name to compare against. |
| (boolean) | If the name matches true, false otherwise. |
Returns whether a node and its ancestor nodes are visible.
| (boolean) |
Returns the value of NodeMetadata.EffectivelyEnabledProperty.
| (boolean) |
Returns whether a node is focusable. A node is focusable when it is a focus scope node or has FocusableProperty attached and set to true, or both.
| (boolean) | If the node is focusable, returns true, otherwise false. |
Gets the initialized status.
| (boolean) | true if initialize() has been called, false otherwise. |
Gets the attached status.
| (boolean) | true if the node is attached to a screen or one of its descendants, false otherwise. |
Gets the attaching status.
| (boolean) | true if node is currently in the process of attaching, false otherwise. |
Gets the detaching status.
| (boolean) | true if the node currently in the process of detaching, false otherwise. |
Transfers the ownership and attaches an input manipulator to an object node.
| child | (Node) | The node to add as a child. |
| (boolean) |
| (number) |
| child | (Node) |
| (number) |
| index | (number) |
| (Node) |
| child | (Node) |
| (boolean) |
Get current change flags.
| (number) | Change flags directly. |
Get current child change flags.
| (number) | Child change flags directly. |
Sets a change flag.
| flag | (number) |
Clears a change flag.
| flag | (number) |
Indicates whether a value is valid layout value. Valid layout value is a non-negative floating point number. Valid layout value can be positive infinity (unbounded layout value).
| value | (number) | Value to test if it is unbound layout value. |
| (boolean) | true if the value if valid layout value, false otherwise. |
Returns an unbounded layout value.
| (number) | Unbounded layout value. |
Indicates whether a value is an unbound layout value.
| layoutValue | (number) | Value to test if it is unbound layout value. |
| (boolean) | true if the layoutValue is unbound layout value, false otherwise. |