From 677e7b36c532092333c1c0f047c2bcea74b353ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Wed, 6 Mar 2024 07:50:23 +0000 Subject: [PATCH] add an API for buttons on ports (#96) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #91 Reviewed-on: https://codeberg.org/StreamGraph/StreamGraph/pulls/96 Co-authored-by: Lera ElvoƩ Co-committed-by: Lera ElvoƩ --- classes/deck/deck_node.gd | 21 +++++++---- classes/deck/nodes/general/button.gd | 9 +++-- classes/deck/nodes/general/print.gd | 36 +++++++------------ classes/deck/nodes/general/set_deck_var.gd | 4 +-- classes/deck/nodes/obs/obs_scene_list.gd | 9 ++--- .../deck/nodes/obs/obs_set_input_settings.gd | 2 +- .../nodes/obs/obs_set_source_transform.gd | 4 +-- .../deck/nodes/obs/obs_set_source_visible.gd | 2 +- classes/deck/nodes/obs/obs_switch_scene.gd | 4 +-- .../obs/obs_websocket_generic_request.gd | 9 ++--- classes/deck/nodes/test/test_type_sends.gd | 4 +-- classes/deck/nodes/test/test_types.gd | 19 +++++----- .../twitch_add_eventsub_subscription.gd | 8 ++--- classes/deck/nodes/twitch/twitch_join_chat.gd | 8 +++-- .../deck/nodes/twitch/twitch_leave_chat.gd | 9 +++-- .../twitch_remove_eventsub_subscription.gd | 7 ++-- classes/deck/nodes/twitch/twitch_send_chat.gd | 10 +++--- classes/deck/port.gd | 1 + .../descriptors/button_descriptor.gd | 11 +----- 19 files changed, 82 insertions(+), 95 deletions(-) diff --git a/classes/deck/deck_node.gd b/classes/deck/deck_node.gd index 11cdfa7..d8d5a56 100644 --- a/classes/deck/deck_node.gd +++ b/classes/deck/deck_node.gd @@ -80,8 +80,8 @@ func add_input_port( type: DeckType.Types, label: String, descriptor: String = "", - usage: Port.UsageType = Port.UsageType.BOTH) -> void: - add_port(type, label, PortType.INPUT, get_input_ports().size(), descriptor, usage) + usage: Port.UsageType = Port.UsageType.BOTH) -> Port: + return add_port(type, label, PortType.INPUT, get_input_ports().size(), descriptor, usage) ## Add an output port to this node. Usually only used at initialization. @@ -89,8 +89,8 @@ func add_output_port( type: DeckType.Types, label: String, descriptor: String = "", - usage: Port.UsageType = Port.UsageType.BOTH) -> void: - add_port(type, label, PortType.OUTPUT, get_output_ports().size(), descriptor, usage) + usage: Port.UsageType = Port.UsageType.BOTH) -> Port: + return add_port(type, label, PortType.OUTPUT, get_output_ports().size(), descriptor, usage) ## Add a virtual port to this node. Usually only used at initialization. @@ -98,8 +98,8 @@ func add_virtual_port( type: DeckType.Types, label: String, descriptor: String = "", - usage: Port.UsageType = Port.UsageType.BOTH) -> void: - add_port(type, label, PortType.VIRTUAL, get_virtual_ports().size(), descriptor, usage) + usage: Port.UsageType = Port.UsageType.BOTH) -> Port: + return add_port(type, label, PortType.VIRTUAL, get_virtual_ports().size(), descriptor, usage) ## Add a port to this node. Usually only used at initialization. @@ -108,7 +108,7 @@ func add_port(type: DeckType.Types, port_type: PortType, index_of_type: int, descriptor: String = "", - usage: Port.UsageType = Port.UsageType.BOTH) -> void: + usage: Port.UsageType = Port.UsageType.BOTH) -> Port: var port := Port.new(type, label, ports.size(), port_type, index_of_type, descriptor, usage) ports.append(port) port_added.emit(ports.size() - 1) @@ -117,6 +117,7 @@ func add_port(type: DeckType.Types, port_value_updated.emit(port.index, new_value) ) ports_updated.emit() + return port ## Remove a port from this node. @@ -305,6 +306,12 @@ func get_all_ports() -> Array[Port]: return ports +func press_button(port_idx: int) -> void: + var port := ports[port_idx] + if port.descriptor.split(":")[0] == "button": + port.button_pressed.emit() + + ## Get a sibling node by its' ID. func get_node(uuid: String) -> DeckNode: return _belonging_to.get_node(uuid) diff --git a/classes/deck/nodes/general/button.gd b/classes/deck/nodes/general/button.gd index 03dbce6..85b5a07 100644 --- a/classes/deck/nodes/general/button.gd +++ b/classes/deck/nodes/general/button.gd @@ -9,8 +9,11 @@ func _init() -> void: node_type = "button" description = "A button to trigger certain nodes that have a trigger input." - add_output_port( - DeckType.Types.BOOL, + var port := add_output_port( + DeckType.Types.ANY, "Press me", - "button" + "button", + Port.UsageType.TRIGGER, ) + + port.button_pressed.connect(send.bind(port.index_of_type, null)) diff --git a/classes/deck/nodes/general/print.gd b/classes/deck/nodes/general/print.gd index 2a2630c..7ad57e0 100644 --- a/classes/deck/nodes/general/print.gd +++ b/classes/deck/nodes/general/print.gd @@ -3,16 +3,12 @@ # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) extends DeckNode -var times_activated := 0 - func _init() -> void: name = "Print" node_type = name.to_snake_case() description = "Print a value to the console." - props_to_serialize = [&"times_activated"] - add_input_port( DeckType.Types.ANY, "Data to print", @@ -20,32 +16,24 @@ func _init() -> void: ) add_input_port( - DeckType.Types.BOOL, + DeckType.Types.ANY, "Trigger", - "button" - ) - - add_output_port( - DeckType.Types.BOOL, - "On Trigger", - "label" - ) + "button", + Port.UsageType.TRIGGER, + ).button_pressed.connect(_receive.bind(1, null)) func _receive(to_input_port: int, data: Variant) -> void: - if to_input_port != 1: - return + var data_to_print := "" + if to_input_port == 1: + data = await resolve_input_port_value_async(0) - var data_to_print = str(await resolve_input_port_value_async(0)) - if data_to_print == null or (data_to_print as String).is_empty(): + if data == null: + data_to_print = "" + else: data_to_print = str(data) - if (data_to_print as String).is_empty(): + + if data_to_print.is_empty(): data_to_print = "" - times_activated += 1 - -# var data_to_print = input_ports[0].value_callback.call() - #print(data_to_print) - #print("extra data: ", extra_data) DeckHolder.logger.log_node(data_to_print) - send(0, true) diff --git a/classes/deck/nodes/general/set_deck_var.gd b/classes/deck/nodes/general/set_deck_var.gd index a01ac8f..3ca6e19 100644 --- a/classes/deck/nodes/general/set_deck_var.gd +++ b/classes/deck/nodes/general/set_deck_var.gd @@ -22,11 +22,11 @@ func _init() -> void: ) add_input_port( - DeckType.Types.BOOL, + DeckType.Types.ANY, "Set", "button", Port.UsageType.TRIGGER, - ) + ).button_pressed.connect(_receive.bind(2, null)) add_output_port( DeckType.Types.ANY, diff --git a/classes/deck/nodes/obs/obs_scene_list.gd b/classes/deck/nodes/obs/obs_scene_list.gd index 4493929..cb5dbbb 100644 --- a/classes/deck/nodes/obs/obs_scene_list.gd +++ b/classes/deck/nodes/obs/obs_scene_list.gd @@ -21,16 +21,13 @@ func _init() -> void: ) add_virtual_port( - DeckType.Types.BOOL, + DeckType.Types.ANY, "Refresh", "button" - ) + ).button_pressed.connect(refresh) -func _receive(on_virtual_port: int, _data: Variant) -> void: - if on_virtual_port != 0: - return - +func refresh() -> void: if noobs == null: noobs = Connections.obs_websocket diff --git a/classes/deck/nodes/obs/obs_set_input_settings.gd b/classes/deck/nodes/obs/obs_set_input_settings.gd index 3e5097b..10b51d0 100644 --- a/classes/deck/nodes/obs/obs_set_input_settings.gd +++ b/classes/deck/nodes/obs/obs_set_input_settings.gd @@ -28,7 +28,7 @@ func _init() -> void: "Set", "button", Port.UsageType.TRIGGER, - ) + ).button_pressed.connect(_receive.bind(2, null)) diff --git a/classes/deck/nodes/obs/obs_set_source_transform.gd b/classes/deck/nodes/obs/obs_set_source_transform.gd index 8e963dc..89f75fe 100644 --- a/classes/deck/nodes/obs/obs_set_source_transform.gd +++ b/classes/deck/nodes/obs/obs_set_source_transform.gd @@ -40,11 +40,11 @@ func _init() -> void: ) add_input_port( - DeckType.Types.BOOL, + DeckType.Types.ANY, "Set", "button", Port.UsageType.TRIGGER - ) + ).button_pressed.connect(_receive.bind(InputPorts.SET, null)) func _receive(to_input_port: int, data: Variant) -> void: diff --git a/classes/deck/nodes/obs/obs_set_source_visible.gd b/classes/deck/nodes/obs/obs_set_source_visible.gd index 139ba17..a610987 100644 --- a/classes/deck/nodes/obs/obs_set_source_visible.gd +++ b/classes/deck/nodes/obs/obs_set_source_visible.gd @@ -41,7 +41,7 @@ func _init() -> void: "Set", "button", Port.UsageType.TRIGGER, - ) + ).button_pressed.connect(_receive.bind(InputPorts.SET, null)) get_input_ports()[2].set_value(true) diff --git a/classes/deck/nodes/obs/obs_switch_scene.gd b/classes/deck/nodes/obs/obs_switch_scene.gd index 325f98d..4e99387 100644 --- a/classes/deck/nodes/obs/obs_switch_scene.gd +++ b/classes/deck/nodes/obs/obs_switch_scene.gd @@ -19,11 +19,11 @@ func _init() -> void: ) add_input_port( - DeckType.Types.BOOL, + DeckType.Types.ANY, "Switch", "button", Port.UsageType.TRIGGER, - ) + ).button_pressed.connect(_receive.bind(1, null)) func _receive(on_input_port: int, data: Variant) -> void: diff --git a/classes/deck/nodes/obs/obs_websocket_generic_request.gd b/classes/deck/nodes/obs/obs_websocket_generic_request.gd index b2732fb..7bfb493 100644 --- a/classes/deck/nodes/obs/obs_websocket_generic_request.gd +++ b/classes/deck/nodes/obs/obs_websocket_generic_request.gd @@ -31,16 +31,13 @@ func _init() -> void: ) add_virtual_port( - DeckType.Types.BOOL, + DeckType.Types.ANY, "Request", "button" - ) + ).button_pressed.connect(request) -func _receive(on_virtual_port: int, _data: Variant) -> void: - if on_virtual_port != 2: - return - +func request() -> void: if noobs == null: noobs = Connections.obs_websocket diff --git a/classes/deck/nodes/test/test_type_sends.gd b/classes/deck/nodes/test/test_type_sends.gd index 3539865..5b5aa60 100644 --- a/classes/deck/nodes/test/test_type_sends.gd +++ b/classes/deck/nodes/test/test_type_sends.gd @@ -16,10 +16,10 @@ func _init() -> void: add_output_port(DeckType.Types.DICTIONARY, "dictionary", "", Port.UsageType.TRIGGER) add_output_port(DeckType.Types.DICTIONARY, "vector", "", Port.UsageType.TRIGGER) - add_input_port(DeckType.Types.ANY, "Send", "button") + add_input_port(DeckType.Types.ANY, "Send", "button").button_pressed.connect(do) -func _receive(_to_input_port: int, _data: Variant) -> void: +func do() -> void: var id := UUID.v4() send(0, get_output_ports()[0].value, id) send(1, get_output_ports()[1].value, id) diff --git a/classes/deck/nodes/test/test_types.gd b/classes/deck/nodes/test/test_types.gd index 5f55db0..dd55db5 100644 --- a/classes/deck/nodes/test/test_types.gd +++ b/classes/deck/nodes/test/test_types.gd @@ -25,19 +25,18 @@ func _init() -> void: add_output_port(DeckType.Types.DICTIONARY, "DICTIONARY") add_output_port(DeckType.Types.ANY, "ANY") - add_input_port(DeckType.Types.BOOL, "Send", "button") + add_input_port(DeckType.Types.ANY, "Send", "button").button_pressed.connect(do) +func do() -> void: + send(0, false) + send(1, 1.0) + send(2, "string") + send(3, ["foo", "bar", 5.3]) + send(4, {"foo": "bar", 1: 2}) + send(5, null) + func _receive(to_input_port: int, data: Variant) -> void: - if to_input_port == 6: - send(0, false) - send(1, 1.0) - send(2, "string") - send(3, ["foo", "bar", 5.3]) - send(4, {"foo": "bar", 1: 2}) - send(5, null) - return - print("received data %s of type %s on port %s, expected type %s, converted v %s, converted type %s" % [ data, typeof(data), to_input_port, DeckType.GODOT_TYPES_MAP[get_input_ports()[to_input_port].type], DeckType.convert_value(data, get_input_ports()[to_input_port].type), diff --git a/classes/deck/nodes/twitch/twitch_add_eventsub_subscription.gd b/classes/deck/nodes/twitch/twitch_add_eventsub_subscription.gd index 70dcc72..7cb4cfe 100644 --- a/classes/deck/nodes/twitch/twitch_add_eventsub_subscription.gd +++ b/classes/deck/nodes/twitch/twitch_add_eventsub_subscription.gd @@ -25,20 +25,18 @@ func _init(): add_input_port( DeckType.Types.STRING, "Event Name", "field" - ) + ) #Subscription Data add_input_port( DeckType.Types.DICTIONARY, "Subscription Data" - ) + ) #Trigger add_input_port( DeckType.Types.ANY, "Add Subscription", "button", Port.UsageType.TRIGGER - ) - - + ).button_pressed.connect(_receive.bind(inputs.add, null)) func _receive(to_input_port, data: Variant) -> void: diff --git a/classes/deck/nodes/twitch/twitch_join_chat.gd b/classes/deck/nodes/twitch/twitch_join_chat.gd index d587def..b128757 100644 --- a/classes/deck/nodes/twitch/twitch_join_chat.gd +++ b/classes/deck/nodes/twitch/twitch_join_chat.gd @@ -15,8 +15,12 @@ func _init(): add_input_port(DeckType.Types.STRING, "Channel", "field") # Adds Trigger for leaving the specified channel - add_input_port(DeckType.Types.ANY, "Join Channel", "button", Port.UsageType.TRIGGER) - + add_input_port( + DeckType.Types.ANY, + "Join Channel", + "button", + Port.UsageType.TRIGGER + ).button_pressed.connect(_receive.bind(1, null)) func _receive(to_input_port, data: Variant): diff --git a/classes/deck/nodes/twitch/twitch_leave_chat.gd b/classes/deck/nodes/twitch/twitch_leave_chat.gd index 39640e8..609aeb0 100644 --- a/classes/deck/nodes/twitch/twitch_leave_chat.gd +++ b/classes/deck/nodes/twitch/twitch_leave_chat.gd @@ -15,11 +15,16 @@ func _init(): add_input_port(DeckType.Types.STRING, "Channel", "field") # Adds Trigger for leaving the specified channel - add_input_port(DeckType.Types.ANY, "Leave Channel", "button", Port.UsageType.TRIGGER) + add_input_port( + DeckType.Types.ANY, + "Leave Channel", + "button", + Port.UsageType.TRIGGER + ).button_pressed.connect(_receive.bind(1, null)) -func _receive(to_input_port, data: Variant): +func _receive(to_input_port: int, data: Variant) -> void: var channel diff --git a/classes/deck/nodes/twitch/twitch_remove_eventsub_subscription.gd b/classes/deck/nodes/twitch/twitch_remove_eventsub_subscription.gd index b5b39d0..2b75401 100644 --- a/classes/deck/nodes/twitch/twitch_remove_eventsub_subscription.gd +++ b/classes/deck/nodes/twitch/twitch_remove_eventsub_subscription.gd @@ -16,16 +16,13 @@ func _init(): DeckType.Types.STRING, "Subscription Type", "field" - ) + ) add_input_port( DeckType.Types.ANY, "Remove Subscription", "button" - ) - - - + ).button_pressed.connect(_receive.bind(1, null)) func _receive(to_input_port, data: Variant): diff --git a/classes/deck/nodes/twitch/twitch_send_chat.gd b/classes/deck/nodes/twitch/twitch_send_chat.gd index 2220672..95e579a 100644 --- a/classes/deck/nodes/twitch/twitch_send_chat.gd +++ b/classes/deck/nodes/twitch/twitch_send_chat.gd @@ -27,14 +27,14 @@ func _init(): "field" ) add_input_port( - DeckType.Types.BOOL, + DeckType.Types.ANY, "Send", - "button" - ) - + "button", + Port.UsageType.TRIGGER, + ).button_pressed.connect(_receive.bind(inputs.send, null)) -func _receive(to_input_port, data: Variant): +func _receive(to_input_port: int, data: Variant) -> void: data = str(data) diff --git a/classes/deck/port.gd b/classes/deck/port.gd index 3b4ea53..487882a 100644 --- a/classes/deck/port.gd +++ b/classes/deck/port.gd @@ -39,6 +39,7 @@ var index: int var value: Variant signal value_updated(new_value: Variant) +signal button_pressed() func _init( diff --git a/graph_node_renderer/descriptors/button_descriptor.gd b/graph_node_renderer/descriptors/button_descriptor.gd index 7ba66f5..dead1bd 100644 --- a/graph_node_renderer/descriptors/button_descriptor.gd +++ b/graph_node_renderer/descriptors/button_descriptor.gd @@ -9,13 +9,4 @@ extends DescriptorContainer func _setup(port: Port, node: DeckNode) -> void: button.text = port.label - if port.port_type == DeckNode.PortType.OUTPUT: - button.pressed.connect( - func(): - node.send(port.index_of_type, true) - ) - else: - button.pressed.connect( - func(): - node._receive(port.index_of_type, true) - ) + button.pressed.connect(node.press_button.bind(port.index))