Added RPC Node Scope (#107)

Reviewed-on: https://codeberg.org/StreamGraph/StreamGraph/pulls/107
Co-authored-by: Eroax <eroaxebusiness@duck.com>
Co-committed-by: Eroax <eroaxebusiness@duck.com>
This commit is contained in:
Eroax 2024-03-16 03:54:11 +00:00 committed by Eroax
parent 3f5a441972
commit d2d55297a9
3 changed files with 272 additions and 5 deletions

View file

@ -7,18 +7,240 @@ extends RPCScope
func _init() -> void: func _init() -> void:
name = "node" name = "node"
operation_types = {
"send" : {"callable" : send, "event_name" : ""},
"press_button" : {"callable" : press_button, "event_name" : ""},
"get_input_ports" : {"callable" : get_input_ports, "event_name" : ""},
"get_output_ports" : {"callable" : get_output_ports, "event_name" : ""},
"get_virtual_ports" : {"callable" : get_virtual_ports, "event_name" : ""},
"get_all_ports" : {"callable" : get_all_ports, "event_name" : ""},
"get_port_value" : {"callable" : get_port_value, "event_name" : ""},
"set_port_value" : {"callable" : set_port_value, "event_name" : "node_port_value_updated"},
"get_position" : {"callable" : get_position, "event_name" : ""},
"set_position" : {"callable" : set_position, "event_name" : "node_position_updated"},
"rename" : {"callable" : rename, "event_name" : "node_renamed"},
"get_group_id" : {"callable" : get_group_id, "event_name" : ""},
}
RPCSignalLayer.signals.node_position_updated.connect(_on_node_position_updated) RPCSignalLayer.signals.node_position_updated.connect(_on_node_position_updated)
RPCSignalLayer.signals.node_renamed.connect(_on_node_renamed) RPCSignalLayer.signals.node_renamed.connect(_on_node_renamed)
RPCSignalLayer.signals.node_port_value_updated.connect(_on_node_port_value_updated) RPCSignalLayer.signals.node_port_value_updated.connect(_on_node_port_value_updated)
# Get Node workflow = Get Deck from ID > Get Node in Deck from ID
# Condition -> Target
# Payload -> Data
func send(req : RPCRequest) -> void:
# Condition
# deck_id, id, output_port
var node := _get_deck_node(
req.operation.condition.deck_id,
req.operation.condition.id
)
node.send(req.operation.condition.output_port, req.operation.payload.data)
var resp := create_generic_success(req)
response.emit(resp)
func press_button(req : RPCRequest) -> void:
# Condition
# deck_id, id, global_port
var node := _get_deck_node(
req.operation.condition.deck_id,
req.operation.condition.id
)
node.press_button(req.operation.condition.global_port)
var resp := create_generic_success(req)
response.emit(resp)
#region Port Getters
func get_input_ports(req : RPCRequest) -> void:
var node := _get_deck_node(
req.operation.condition.deck_id,
req.operation.condition.id
)
var ports : Array[RPCPort]
for all in node.get_input_ports():
ports.append(RPCPort.new(all))
var resp = create_response(req, ports)
response.emit(resp)
func get_output_ports(req : RPCRequest) -> void:
var node := _get_deck_node(
req.operation.condition.deck_id,
req.operation.condition.id
)
var ports : Array[RPCPort]
for all in node.get_output_ports():
ports.append(RPCPort.new(all))
var resp = create_response(req, ports)
response.emit(resp)
func get_virtual_ports(req : RPCRequest) -> void:
var node := _get_deck_node(
req.operation.condition.deck_id,
req.operation.condition.id
)
var ports : Array[RPCPort]
for all in node.get_virtual_ports():
ports.append(RPCPort.new(all))
var resp = create_response(req, ports)
response.emit(resp)
func get_all_ports(req : RPCRequest) -> void:
var node := _get_deck_node(
req.operation.condition.deck_id,
req.operation.condition.id
)
var ports : Array[RPCPort]
for all in node.get_all_ports():
ports.append(RPCPort.new(all))
var resp = create_response(req, ports)
response.emit(resp)
#endregion
#region Port Values
func get_port_value(req : RPCRequest) -> void:
var node := _get_deck_node(
req.operation.condition.deck_id,
req.operation.condition.id
)
var value = node.get_all_ports()[req.operation.condition.global_port].value
var resp = create_response(req, value)
response.emit(resp)
func set_port_value(req : RPCRequest) -> void:
reconnect(
RPCSignalLayer.signals.node_port_value_updated,
_on_node_port_value_updated,
func():
var node := _get_deck_node(
req.operation.condition.deck_id,
req.operation.condition.id
)
var port = node.get_all_ports()[req.operation.condition.global_port]
# Doesn't show in the Renderer
port.set_value(req.operation.payload.port_value)
var resp = create_generic_success(req)
resp.create_event_counterpart({"new_value" : req.operation.payload.port_value}, operation_types)
response.emit(resp)
)
#endregion
func get_position(req : RPCRequest) -> void:
var node := _get_deck_node(
req.operation.condition.deck_id,
req.operation.condition.id
)
var resp = create_response(req, node.position)
response.emit(resp)
func set_position(req : RPCRequest) -> void:
reconnect(
RPCSignalLayer.signals.node_position_updated,
_on_node_position_updated,
func():
var node := _get_deck_node(
req.operation.condition.deck_id,
req.operation.condition.id
)
node.position = req.operation.payload.position
var resp := create_generic_success(req)
resp.create_event_counterpart({"new_position" : req.operation.payload.position}, operation_types)
response.emit(resp)
)
func rename(req : RPCRequest) -> void:
reconnect(
RPCSignalLayer.signals.node_renamed,
_on_node_renamed,
func():
var node := _get_deck_node(
req.operation.condition.deck_id,
req.operation.condition.id
)
node.name = req.operation.payload.name
var resp := create_generic_success(req)
resp.create_event_counterpart({"new_name" :req.operation.payload.name}, operation_types)
response.emit(resp)
)
# Note, Check if Node IS a group node.
func get_group_id(req : RPCRequest) -> void:
var node := _get_deck_node(
req.operation.condition.deck_id,
req.operation.condition.id
)
var group_id = node.group_id
var resp := create_response(req, group_id)
response.emit(resp)
func _on_node_position_updated(deck_id: String, node_id: String, position: Dictionary) -> void: func _on_node_position_updated(deck_id: String, node_id: String, position: Dictionary) -> void:
event.emit(create_event("position_updated", position, {"deck_id": deck_id, "node_id": node_id})) event.emit(create_event("position_updated", position, {"deck_id": deck_id, "id": node_id}))
func _on_node_renamed(deck_id: String, node_id: String, new_name: String) -> void: func _on_node_renamed(deck_id: String, node_id: String, new_name: String) -> void:
event.emit(create_event("renamed", {"new_name": new_name}, {"deck_id": deck_id, "node_id": node_id})) event.emit(create_event("renamed", {"new_name": new_name}, {"deck_id": deck_id, "id": node_id}))
func _on_node_port_value_updated(deck_id: String, node_id: String, port_idx: int, new_value: Variant) -> void: func _on_node_port_value_updated(deck_id: String, node_id: String, port_idx: int, new_value: Variant) -> void:
event.emit(create_event( event.emit(create_event(
@ -29,7 +251,17 @@ func _on_node_port_value_updated(deck_id: String, node_id: String, port_idx: int
}, },
{ {
"deck_id": deck_id, "deck_id": deck_id,
"node_id": node_id, "id": node_id,
} }
) )
) )
#region Utility Functions
## Utility Functions for getting a Deck from a node_id and a deck_id.
func _get_deck_node(deck_id, node_id) -> DeckNode:
var deck := DeckHolder.get_deck(deck_id)
var node := deck.get_node(node_id)
return node
#endregion

View file

@ -0,0 +1,35 @@
# (c) 2023-present Eroax
# (c) 2023-present Yagich
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
extends RPCFrame
class_name RPCPort
var type: DeckType.Types
var label: String
var index: int
var port_type: DeckNode.PortType
var index_of_type: int
var descriptor: String
var usage_type: Port.UsageType
func _init(port : Port):
frame_name = "port"
_props = [
&"type",
&"label",
&"descriptor",
&"port_type",
&"index_of_type",
&"index",
&"usage_type"
]
type = port.type
label = port.label
descriptor = port.descriptor
port_type = port.port_type
index_of_type = port.index_of_type
index = port.index
usage_type = port.usage_type

View file

@ -15,7 +15,7 @@ var scope: String
## If non-[code]null[/code], the error that this request represents. ## If non-[code]null[/code], the error that this request represents.
var errors: Array[RPCError] var errors: Array[RPCError]
## Additional data associated with the response. Mandatory, but can be empty. ## Additional data associated with the response. Mandatory, but can be empty.
var data: Dictionary var data: Variant
## See [member RPCRequest.keep]. ## See [member RPCRequest.keep].
var kept: Dictionary var kept: Dictionary