add an API for buttons on ports (#96)

closes #91

Reviewed-on: https://codeberg.org/StreamGraph/StreamGraph/pulls/96
Co-authored-by: Lera Elvoé <yagich@poto.cafe>
Co-committed-by: Lera Elvoé <yagich@poto.cafe>
This commit is contained in:
Lera Elvoé 2024-03-06 07:50:23 +00:00 committed by yagich
parent 715086f204
commit 677e7b36c5
19 changed files with 82 additions and 95 deletions

View file

@ -80,8 +80,8 @@ func add_input_port(
type: DeckType.Types, type: DeckType.Types,
label: String, label: String,
descriptor: String = "", descriptor: String = "",
usage: Port.UsageType = Port.UsageType.BOTH) -> void: usage: Port.UsageType = Port.UsageType.BOTH) -> Port:
add_port(type, label, PortType.INPUT, get_input_ports().size(), descriptor, usage) 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. ## Add an output port to this node. Usually only used at initialization.
@ -89,8 +89,8 @@ func add_output_port(
type: DeckType.Types, type: DeckType.Types,
label: String, label: String,
descriptor: String = "", descriptor: String = "",
usage: Port.UsageType = Port.UsageType.BOTH) -> void: usage: Port.UsageType = Port.UsageType.BOTH) -> Port:
add_port(type, label, PortType.OUTPUT, get_output_ports().size(), descriptor, usage) 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. ## Add a virtual port to this node. Usually only used at initialization.
@ -98,8 +98,8 @@ func add_virtual_port(
type: DeckType.Types, type: DeckType.Types,
label: String, label: String,
descriptor: String = "", descriptor: String = "",
usage: Port.UsageType = Port.UsageType.BOTH) -> void: usage: Port.UsageType = Port.UsageType.BOTH) -> Port:
add_port(type, label, PortType.VIRTUAL, get_virtual_ports().size(), descriptor, usage) return add_port(type, label, PortType.VIRTUAL, get_virtual_ports().size(), descriptor, usage)
## Add a port to this node. Usually only used at initialization. ## Add a port to this node. Usually only used at initialization.
@ -108,7 +108,7 @@ func add_port(type: DeckType.Types,
port_type: PortType, port_type: PortType,
index_of_type: int, index_of_type: int,
descriptor: String = "", 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) var port := Port.new(type, label, ports.size(), port_type, index_of_type, descriptor, usage)
ports.append(port) ports.append(port)
port_added.emit(ports.size() - 1) port_added.emit(ports.size() - 1)
@ -117,6 +117,7 @@ func add_port(type: DeckType.Types,
port_value_updated.emit(port.index, new_value) port_value_updated.emit(port.index, new_value)
) )
ports_updated.emit() ports_updated.emit()
return port
## Remove a port from this node. ## Remove a port from this node.
@ -305,6 +306,12 @@ func get_all_ports() -> Array[Port]:
return ports 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. ## Get a sibling node by its' ID.
func get_node(uuid: String) -> DeckNode: func get_node(uuid: String) -> DeckNode:
return _belonging_to.get_node(uuid) return _belonging_to.get_node(uuid)

View file

@ -9,8 +9,11 @@ func _init() -> void:
node_type = "button" node_type = "button"
description = "A button to trigger certain nodes that have a trigger input." description = "A button to trigger certain nodes that have a trigger input."
add_output_port( var port := add_output_port(
DeckType.Types.BOOL, DeckType.Types.ANY,
"Press me", "Press me",
"button" "button",
Port.UsageType.TRIGGER,
) )
port.button_pressed.connect(send.bind(port.index_of_type, null))

View file

@ -3,16 +3,12 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
extends DeckNode extends DeckNode
var times_activated := 0
func _init() -> void: func _init() -> void:
name = "Print" name = "Print"
node_type = name.to_snake_case() node_type = name.to_snake_case()
description = "Print a value to the console." description = "Print a value to the console."
props_to_serialize = [&"times_activated"]
add_input_port( add_input_port(
DeckType.Types.ANY, DeckType.Types.ANY,
"Data to print", "Data to print",
@ -20,32 +16,24 @@ func _init() -> void:
) )
add_input_port( add_input_port(
DeckType.Types.BOOL, DeckType.Types.ANY,
"Trigger", "Trigger",
"button" "button",
) Port.UsageType.TRIGGER,
).button_pressed.connect(_receive.bind(1, null))
add_output_port(
DeckType.Types.BOOL,
"On Trigger",
"label"
)
func _receive(to_input_port: int, data: Variant) -> void: func _receive(to_input_port: int, data: Variant) -> void:
if to_input_port != 1: var data_to_print := ""
return 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 == null:
if data_to_print == null or (data_to_print as String).is_empty(): data_to_print = "<nothing>"
else:
data_to_print = str(data) data_to_print = str(data)
if (data_to_print as String).is_empty():
if data_to_print.is_empty():
data_to_print = "<nothing>" data_to_print = "<nothing>"
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) DeckHolder.logger.log_node(data_to_print)
send(0, true)

View file

@ -22,11 +22,11 @@ func _init() -> void:
) )
add_input_port( add_input_port(
DeckType.Types.BOOL, DeckType.Types.ANY,
"Set", "Set",
"button", "button",
Port.UsageType.TRIGGER, Port.UsageType.TRIGGER,
) ).button_pressed.connect(_receive.bind(2, null))
add_output_port( add_output_port(
DeckType.Types.ANY, DeckType.Types.ANY,

View file

@ -21,16 +21,13 @@ func _init() -> void:
) )
add_virtual_port( add_virtual_port(
DeckType.Types.BOOL, DeckType.Types.ANY,
"Refresh", "Refresh",
"button" "button"
) ).button_pressed.connect(refresh)
func _receive(on_virtual_port: int, _data: Variant) -> void: func refresh() -> void:
if on_virtual_port != 0:
return
if noobs == null: if noobs == null:
noobs = Connections.obs_websocket noobs = Connections.obs_websocket

View file

@ -28,7 +28,7 @@ func _init() -> void:
"Set", "Set",
"button", "button",
Port.UsageType.TRIGGER, Port.UsageType.TRIGGER,
) ).button_pressed.connect(_receive.bind(2, null))

View file

@ -40,11 +40,11 @@ func _init() -> void:
) )
add_input_port( add_input_port(
DeckType.Types.BOOL, DeckType.Types.ANY,
"Set", "Set",
"button", "button",
Port.UsageType.TRIGGER Port.UsageType.TRIGGER
) ).button_pressed.connect(_receive.bind(InputPorts.SET, null))
func _receive(to_input_port: int, data: Variant) -> void: func _receive(to_input_port: int, data: Variant) -> void:

View file

@ -41,7 +41,7 @@ func _init() -> void:
"Set", "Set",
"button", "button",
Port.UsageType.TRIGGER, Port.UsageType.TRIGGER,
) ).button_pressed.connect(_receive.bind(InputPorts.SET, null))
get_input_ports()[2].set_value(true) get_input_ports()[2].set_value(true)

View file

@ -19,11 +19,11 @@ func _init() -> void:
) )
add_input_port( add_input_port(
DeckType.Types.BOOL, DeckType.Types.ANY,
"Switch", "Switch",
"button", "button",
Port.UsageType.TRIGGER, Port.UsageType.TRIGGER,
) ).button_pressed.connect(_receive.bind(1, null))
func _receive(on_input_port: int, data: Variant) -> void: func _receive(on_input_port: int, data: Variant) -> void:

View file

@ -31,16 +31,13 @@ func _init() -> void:
) )
add_virtual_port( add_virtual_port(
DeckType.Types.BOOL, DeckType.Types.ANY,
"Request", "Request",
"button" "button"
) ).button_pressed.connect(request)
func _receive(on_virtual_port: int, _data: Variant) -> void: func request() -> void:
if on_virtual_port != 2:
return
if noobs == null: if noobs == null:
noobs = Connections.obs_websocket noobs = Connections.obs_websocket

View file

@ -16,10 +16,10 @@ func _init() -> void:
add_output_port(DeckType.Types.DICTIONARY, "dictionary", "", Port.UsageType.TRIGGER) add_output_port(DeckType.Types.DICTIONARY, "dictionary", "", Port.UsageType.TRIGGER)
add_output_port(DeckType.Types.DICTIONARY, "vector", "", 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() var id := UUID.v4()
send(0, get_output_ports()[0].value, id) send(0, get_output_ports()[0].value, id)
send(1, get_output_ports()[1].value, id) send(1, get_output_ports()[1].value, id)

View file

@ -25,19 +25,18 @@ func _init() -> void:
add_output_port(DeckType.Types.DICTIONARY, "DICTIONARY") add_output_port(DeckType.Types.DICTIONARY, "DICTIONARY")
add_output_port(DeckType.Types.ANY, "ANY") 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 _receive(to_input_port: int, data: Variant) -> void: func do() -> void:
if to_input_port == 6:
send(0, false) send(0, false)
send(1, 1.0) send(1, 1.0)
send(2, "string") send(2, "string")
send(3, ["foo", "bar", 5.3]) send(3, ["foo", "bar", 5.3])
send(4, {"foo": "bar", 1: 2}) send(4, {"foo": "bar", 1: 2})
send(5, null) send(5, null)
return
func _receive(to_input_port: int, data: Variant) -> void:
print("received data %s of type %s on port %s, expected type %s, converted v %s, converted type %s" % [ 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], 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), DeckType.convert_value(data, get_input_ports()[to_input_port].type),

View file

@ -36,9 +36,7 @@ func _init():
DeckType.Types.ANY, DeckType.Types.ANY,
"Add Subscription", "button", "Add Subscription", "button",
Port.UsageType.TRIGGER Port.UsageType.TRIGGER
) ).button_pressed.connect(_receive.bind(inputs.add, null))
func _receive(to_input_port, data: Variant) -> void: func _receive(to_input_port, data: Variant) -> void:

View file

@ -15,8 +15,12 @@ func _init():
add_input_port(DeckType.Types.STRING, "Channel", "field") add_input_port(DeckType.Types.STRING, "Channel", "field")
# Adds Trigger for leaving the specified channel # 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): func _receive(to_input_port, data: Variant):

View file

@ -15,11 +15,16 @@ func _init():
add_input_port(DeckType.Types.STRING, "Channel", "field") add_input_port(DeckType.Types.STRING, "Channel", "field")
# Adds Trigger for leaving the specified channel # 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 var channel

View file

@ -22,10 +22,7 @@ func _init():
DeckType.Types.ANY, DeckType.Types.ANY,
"Remove Subscription", "Remove Subscription",
"button" "button"
) ).button_pressed.connect(_receive.bind(1, null))
func _receive(to_input_port, data: Variant): func _receive(to_input_port, data: Variant):

View file

@ -27,14 +27,14 @@ func _init():
"field" "field"
) )
add_input_port( add_input_port(
DeckType.Types.BOOL, DeckType.Types.ANY,
"Send", "Send",
"button" "button",
) Port.UsageType.TRIGGER,
).button_pressed.connect(_receive.bind(inputs.send, null))
func _receive(to_input_port: int, data: Variant) -> void:
func _receive(to_input_port, data: Variant):
data = str(data) data = str(data)

View file

@ -39,6 +39,7 @@ var index: int
var value: Variant var value: Variant
signal value_updated(new_value: Variant) signal value_updated(new_value: Variant)
signal button_pressed()
func _init( func _init(

View file

@ -9,13 +9,4 @@ extends DescriptorContainer
func _setup(port: Port, node: DeckNode) -> void: func _setup(port: Port, node: DeckNode) -> void:
button.text = port.label button.text = port.label
if port.port_type == DeckNode.PortType.OUTPUT: button.pressed.connect(node.press_button.bind(port.index))
button.pressed.connect(
func():
node.send(port.index_of_type, true)
)
else:
button.pressed.connect(
func():
node._receive(port.index_of_type, true)
)