mirror of
https://codeberg.org/StreamGraph/StreamGraph.git
synced 2024-11-13 19:49:55 +01:00
types system simplification (#8)
no longer using classes for every type. the type system has been greatly simplified, with the added bonus that it hooks directly into GraphEdit's slot type system. connections will still fail if the type conversion fails, which may be used by other renderers. the type conversion map is straightforward to understand, and easy to extend should the need arise (hopefully it shouldn't). Reviewed-on: https://codeberg.org/Eroax/Re-DotDeck/pulls/8 Co-authored-by: Lera Elvoé <yagich@poto.cafe> Co-committed-by: Lera Elvoé <yagich@poto.cafe>
This commit is contained in:
parent
14ecc1087a
commit
c4e35043df
17 changed files with 146 additions and 235 deletions
|
@ -7,25 +7,6 @@ class_name Deck
|
||||||
## is the [DeckNode] instance.
|
## is the [DeckNode] instance.
|
||||||
var nodes: Dictionary
|
var nodes: Dictionary
|
||||||
|
|
||||||
## The list of [DeckType] for easy port creation.
|
|
||||||
enum Types{
|
|
||||||
ERROR = -1,
|
|
||||||
BOOL,
|
|
||||||
NUMERIC,
|
|
||||||
STRING,
|
|
||||||
ARRAY,
|
|
||||||
DICTIONARY,
|
|
||||||
}
|
|
||||||
|
|
||||||
## A dictionary mapping [enum Types] to [DeckType] subclasses.
|
|
||||||
static var type_assoc: Dictionary = {
|
|
||||||
Types.ERROR: DeckType.DeckTypeError,
|
|
||||||
Types.BOOL: DeckType.DeckTypeBool,
|
|
||||||
Types.NUMERIC: DeckType.DeckTypeNumeric,
|
|
||||||
Types.STRING: DeckType.DeckTypeString,
|
|
||||||
Types.ARRAY: DeckType.DeckTypeArray,
|
|
||||||
Types.DICTIONARY: DeckType.DeckTypeDictionary,
|
|
||||||
}
|
|
||||||
|
|
||||||
## A map of variables set on this deck.
|
## A map of variables set on this deck.
|
||||||
var variable_stack: Dictionary = {}
|
var variable_stack: Dictionary = {}
|
||||||
|
@ -91,12 +72,11 @@ func get_node(uuid: String) -> DeckNode:
|
||||||
|
|
||||||
## Attempt to connect two nodes. Returns [code]true[/code] if the connection succeeded.
|
## Attempt to connect two nodes. Returns [code]true[/code] if the connection succeeded.
|
||||||
func connect_nodes(from_node: DeckNode, to_node: DeckNode, from_output_port: int, to_input_port: int) -> bool:
|
func connect_nodes(from_node: DeckNode, to_node: DeckNode, from_output_port: int, to_input_port: int) -> bool:
|
||||||
# first, check that we can do the type conversion.
|
# check that we can do the type conversion
|
||||||
var type_a: Types = from_node.get_output_ports()[from_output_port].type
|
var type_a: DeckType.Types = from_node.get_output_ports()[from_output_port].type
|
||||||
var type_b: Types = to_node.get_input_ports()[to_input_port].type
|
var type_b: DeckType.Types = to_node.get_input_ports()[to_input_port].type
|
||||||
var err: DeckType = (type_assoc[type_b]).from(type_assoc[type_a].new())
|
if !DeckType.can_convert(type_a, type_b):
|
||||||
if err is DeckType.DeckTypeError:
|
print("Can not convert from %s to %s." % [DeckType.type_str(type_a), DeckType.type_str(type_b)])
|
||||||
print(err.error_message)
|
|
||||||
return false
|
return false
|
||||||
|
|
||||||
# TODO: prevent duplicate connections
|
# TODO: prevent duplicate connections
|
||||||
|
|
|
@ -66,22 +66,22 @@ signal incoming_connection_removed(from_port: int)
|
||||||
|
|
||||||
|
|
||||||
## Add an input port to this node. Usually only used at initialization.
|
## Add an input port to this node. Usually only used at initialization.
|
||||||
func add_input_port(type: Deck.Types, label: String, descriptor: String = "") -> void:
|
func add_input_port(type: DeckType.Types, label: String, descriptor: String = "") -> void:
|
||||||
add_port(type, label, PortType.INPUT, get_input_ports().size(), descriptor)
|
add_port(type, label, PortType.INPUT, get_input_ports().size(), descriptor)
|
||||||
|
|
||||||
|
|
||||||
## Add an output port to this node. Usually only used at initialization.
|
## Add an output port to this node. Usually only used at initialization.
|
||||||
func add_output_port(type: Deck.Types, label: String, descriptor: String = "") -> void:
|
func add_output_port(type: DeckType.Types, label: String, descriptor: String = "") -> void:
|
||||||
add_port(type, label, PortType.OUTPUT, get_output_ports().size(), descriptor)
|
add_port(type, label, PortType.OUTPUT, get_output_ports().size(), descriptor)
|
||||||
|
|
||||||
|
|
||||||
## Add a virtual port to this node. Usually only used at initialization.
|
## Add a virtual port to this node. Usually only used at initialization.
|
||||||
func add_virtual_port(type: Deck.Types, label: String, descriptor: String = "") -> void:
|
func add_virtual_port(type: DeckType.Types, label: String, descriptor: String = "") -> void:
|
||||||
add_port(type, label, PortType.VIRTUAL, get_virtual_ports().size(), descriptor)
|
add_port(type, label, PortType.VIRTUAL, get_virtual_ports().size(), descriptor)
|
||||||
|
|
||||||
|
|
||||||
## Add a port to this node. Usually only used at initialization.
|
## Add a port to this node. Usually only used at initialization.
|
||||||
func add_port(type: Deck.Types, label: String, port_type: PortType, index_of_type: int, descriptor: String = "") -> void:
|
func add_port(type: DeckType.Types, label: String, port_type: PortType, index_of_type: int, descriptor: String = "") -> void:
|
||||||
var port := Port.new(type, label, ports.size(), port_type, index_of_type, descriptor)
|
var port := Port.new(type, label, ports.size(), port_type, index_of_type, descriptor)
|
||||||
ports.append(port)
|
ports.append(port)
|
||||||
port_added.emit(ports.size() - 1)
|
port_added.emit(ports.size() - 1)
|
||||||
|
@ -97,7 +97,7 @@ func remove_port(port_idx: int) -> void:
|
||||||
|
|
||||||
|
|
||||||
## Send data to all outgoing connections on port [param from_output_port].
|
## Send data to all outgoing connections on port [param from_output_port].
|
||||||
func send(from_output_port: int, data: DeckType, extra_data: Array = []) -> void:
|
func send(from_output_port: int, data: Variant, extra_data: Array = []) -> void:
|
||||||
if outgoing_connections.get(from_output_port) == null:
|
if outgoing_connections.get(from_output_port) == null:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -110,7 +110,7 @@ func send(from_output_port: int, data: DeckType, extra_data: Array = []) -> void
|
||||||
|
|
||||||
|
|
||||||
## Virtual function that's called when this node receives data from another node's [method send] call.
|
## Virtual function that's called when this node receives data from another node's [method send] call.
|
||||||
func _receive(to_input_port: int, data: DeckType, extra_data: Array = []) -> void:
|
func _receive(to_input_port: int, data: Variant, extra_data: Array = []) -> void:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ func _init() -> void:
|
||||||
category = "general"
|
category = "general"
|
||||||
|
|
||||||
add_output_port(
|
add_output_port(
|
||||||
Deck.Types.BOOL,
|
DeckType.Types.BOOL,
|
||||||
"Press me",
|
"Press me",
|
||||||
"button"
|
"button"
|
||||||
)
|
)
|
||||||
|
|
|
@ -8,7 +8,7 @@ func _init() -> void:
|
||||||
category = "general"
|
category = "general"
|
||||||
|
|
||||||
add_output_port(
|
add_output_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.STRING,
|
||||||
"Variable",
|
"Variable",
|
||||||
"field"
|
"field"
|
||||||
)
|
)
|
||||||
|
|
|
@ -12,7 +12,7 @@ func _init() -> void:
|
||||||
appears_in_search = false
|
appears_in_search = false
|
||||||
|
|
||||||
add_output_port(
|
add_output_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.ANY,
|
||||||
"Input 0"
|
"Input 0"
|
||||||
)
|
)
|
||||||
outgoing_connection_added.connect(_on_outgoing_connection_added)
|
outgoing_connection_added.connect(_on_outgoing_connection_added)
|
||||||
|
@ -24,7 +24,7 @@ func _on_outgoing_connection_added(port_idx: int) -> void:
|
||||||
return
|
return
|
||||||
|
|
||||||
add_output_port(
|
add_output_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.ANY,
|
||||||
"Input %s" % (get_all_ports().size())
|
"Input %s" % (get_all_ports().size())
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ func _on_outgoing_connection_removed(port_idx: int) -> void:
|
||||||
func _pre_connection() -> void:
|
func _pre_connection() -> void:
|
||||||
for i in output_count + 1:
|
for i in output_count + 1:
|
||||||
add_output_port(
|
add_output_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.ANY,
|
||||||
"Input %s" % (i + 1)
|
"Input %s" % (i + 1)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ func _post_load() -> void:
|
||||||
if ports.size() <= last_connected_port:
|
if ports.size() <= last_connected_port:
|
||||||
for i in last_connected_port:
|
for i in last_connected_port:
|
||||||
add_output_port(
|
add_output_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.ANY,
|
||||||
"Input %s" % get_output_ports().size()
|
"Input %s" % get_output_ports().size()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -19,9 +19,9 @@ func _pre_connection() -> void:
|
||||||
var index_of_type: int
|
var index_of_type: int
|
||||||
match port_type:
|
match port_type:
|
||||||
PortType.OUTPUT:
|
PortType.OUTPUT:
|
||||||
add_output_port(Deck.Types.STRING, "Output %s" % get_output_ports().size())
|
add_output_port(DeckType.Types.ANY, "Output %s" % get_output_ports().size())
|
||||||
PortType.INPUT:
|
PortType.INPUT:
|
||||||
add_input_port(Deck.Types.STRING, "Input %s" % get_input_ports().size())
|
add_input_port(DeckType.Types.ANY, "Input %s" % get_input_ports().size())
|
||||||
|
|
||||||
|
|
||||||
func init_io() -> void:
|
func init_io() -> void:
|
||||||
|
@ -46,13 +46,13 @@ func recalculate_ports() -> void:
|
||||||
|
|
||||||
for output_port: Port in output_node.get_input_ports():
|
for output_port: Port in output_node.get_input_ports():
|
||||||
add_output_port(
|
add_output_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.ANY,
|
||||||
"Output %s" % output_port.index
|
"Output %s" % output_port.index
|
||||||
)
|
)
|
||||||
|
|
||||||
for input_port: Port in input_node.get_output_ports():
|
for input_port: Port in input_node.get_output_ports():
|
||||||
add_input_port(
|
add_input_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.ANY,
|
||||||
"Input %s" % input_port.index
|
"Input %s" % input_port.index
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ func recalculate_ports() -> void:
|
||||||
extra_ports.append(port.port_type)
|
extra_ports.append(port.port_type)
|
||||||
|
|
||||||
|
|
||||||
func _receive(to_input_port: int, data: DeckType, extra_data: Array = []):
|
func _receive(to_input_port: int, data: Variant, extra_data: Array = []):
|
||||||
input_node.send(get_input_ports()[to_input_port].index_of_type, data, extra_data)
|
input_node.send(get_input_ports()[to_input_port].index_of_type, data, extra_data)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ func _init() -> void:
|
||||||
appears_in_search = false
|
appears_in_search = false
|
||||||
|
|
||||||
add_input_port(
|
add_input_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.ANY,
|
||||||
"Output 0"
|
"Output 0"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ func _on_incoming_connection_added(port_idx: int) -> void:
|
||||||
return
|
return
|
||||||
|
|
||||||
add_input_port(
|
add_input_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.ANY,
|
||||||
"Output %s" % (get_all_ports().size())
|
"Output %s" % (get_all_ports().size())
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ func _on_incoming_connection_removed(port_idx: int) -> void:
|
||||||
func _pre_connection() -> void:
|
func _pre_connection() -> void:
|
||||||
for i in input_count + 1:
|
for i in input_count + 1:
|
||||||
add_input_port(
|
add_input_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.ANY,
|
||||||
"Output %s" % (i + 1)
|
"Output %s" % (i + 1)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -63,11 +63,11 @@ func _post_load() -> void:
|
||||||
if ports.size() <= last_connected_port:
|
if ports.size() <= last_connected_port:
|
||||||
for i in last_connected_port:
|
for i in last_connected_port:
|
||||||
add_input_port(
|
add_input_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.ANY,
|
||||||
"Output %s" % get_input_ports().size()
|
"Output %s" % get_input_ports().size()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func _receive(to_input_port: int, data: DeckType, extra_data: Array = []) -> void:
|
func _receive(to_input_port: int, data: Variant, extra_data: Array = []) -> void:
|
||||||
var group_node := _belonging_to._belonging_to.get_node(_belonging_to.group_node)
|
var group_node := _belonging_to._belonging_to.get_node(_belonging_to.group_node)
|
||||||
group_node.send(group_node.get_output_ports()[to_input_port].index_of_type, data, extra_data)
|
group_node.send(group_node.get_output_ports()[to_input_port].index_of_type, data, extra_data)
|
||||||
|
|
|
@ -12,25 +12,25 @@ func _init() -> void:
|
||||||
category = "general"
|
category = "general"
|
||||||
|
|
||||||
add_input_port(
|
add_input_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.STRING,
|
||||||
"Text to print",
|
"Text to print",
|
||||||
"field"
|
"field"
|
||||||
)
|
)
|
||||||
|
|
||||||
add_input_port(
|
add_input_port(
|
||||||
Deck.Types.BOOL,
|
DeckType.Types.BOOL,
|
||||||
"Trigger",
|
"Trigger",
|
||||||
"button"
|
"button"
|
||||||
)
|
)
|
||||||
|
|
||||||
add_output_port(
|
add_output_port(
|
||||||
Deck.Types.BOOL,
|
DeckType.Types.BOOL,
|
||||||
"On Trigger",
|
"On Trigger",
|
||||||
"label"
|
"label"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func _receive(to_input_port: int, data: DeckType, extra_data: Array = []) -> void:
|
func _receive(to_input_port: int, data: Variant, extra_data: Array = []) -> void:
|
||||||
if to_input_port != 1:
|
if to_input_port != 1:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -40,11 +40,11 @@ func _receive(to_input_port: int, data: DeckType, extra_data: Array = []) -> voi
|
||||||
elif get_input_ports()[0].value_callback.get_object() && get_input_ports()[0].value_callback.call() != "":
|
elif get_input_ports()[0].value_callback.get_object() && get_input_ports()[0].value_callback.call() != "":
|
||||||
data_to_print = get_input_ports()[0].value_callback.call()
|
data_to_print = get_input_ports()[0].value_callback.call()
|
||||||
else:
|
else:
|
||||||
data_to_print = data.get_value()
|
data_to_print = data
|
||||||
|
|
||||||
times_activated += 1
|
times_activated += 1
|
||||||
|
|
||||||
# var data_to_print = input_ports[0].value_callback.call()
|
# var data_to_print = input_ports[0].value_callback.call()
|
||||||
print(data_to_print)
|
print(data_to_print)
|
||||||
print("extra data: ", extra_data)
|
print("extra data: ", extra_data)
|
||||||
send(0, DeckType.DeckTypeBool.new(true))
|
send(0, true)
|
||||||
|
|
|
@ -8,48 +8,47 @@ func _init() -> void:
|
||||||
category = "general"
|
category = "general"
|
||||||
|
|
||||||
add_input_port(
|
add_input_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.STRING,
|
||||||
"Variable Name",
|
"Variable Name",
|
||||||
"field"
|
"field"
|
||||||
)
|
)
|
||||||
|
|
||||||
add_input_port(
|
add_input_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.ANY,
|
||||||
"Value",
|
"Value",
|
||||||
"field"
|
"field"
|
||||||
)
|
)
|
||||||
|
|
||||||
add_input_port(
|
add_input_port(
|
||||||
Deck.Types.BOOL,
|
DeckType.Types.BOOL,
|
||||||
"Set",
|
"Set",
|
||||||
"button"
|
"button"
|
||||||
)
|
)
|
||||||
|
|
||||||
add_output_port(
|
add_output_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.ANY,
|
||||||
"Value",
|
"Value",
|
||||||
"label"
|
"label"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func _receive(to_input_port: int, data: DeckType, extra_data: Array = []) -> void:
|
func _receive(to_input_port: int, data: Variant, extra_data: Array = []) -> void:
|
||||||
if to_input_port != 2:
|
if to_input_port != 2:
|
||||||
return
|
return
|
||||||
|
|
||||||
var var_name: String = get_value_for_port(0, data)
|
var var_name: String = get_value_for_port(0, data)
|
||||||
# String for now, until i figure out an Any type
|
var var_value: Variant = get_value_for_port(1, data)
|
||||||
var var_value: String = get_value_for_port(1, data)
|
|
||||||
|
|
||||||
_belonging_to.variable_stack[var_name] = var_value
|
_belonging_to.variable_stack[var_name] = var_value
|
||||||
|
|
||||||
send(0, DeckType.DeckTypeString.new(var_value))
|
send(0, var_value)
|
||||||
|
|
||||||
# this can probably go into DeckNode with a different name that makes it clear
|
# this can probably go into DeckNode with a different name that makes it clear
|
||||||
# that it prioritizes call-time resolution
|
# that it prioritizes call-time resolution
|
||||||
func get_value_for_port(port: int, data: DeckType) -> Variant:
|
func get_value_for_port(port: int, data: Variant) -> Variant:
|
||||||
if request_value(port) != null:
|
if request_value(port) != null:
|
||||||
return request_value(port)
|
return request_value(port)
|
||||||
elif ports[port].value_callback.call() != "":
|
elif ports[port].value_callback.call() != "":
|
||||||
return ports[port].value_callback.call()
|
return ports[port].value_callback.call()
|
||||||
else:
|
else:
|
||||||
return data.get_value()
|
return data
|
||||||
|
|
|
@ -7,7 +7,7 @@ func _init() -> void:
|
||||||
category = "general"
|
category = "general"
|
||||||
|
|
||||||
add_output_port(
|
add_output_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.STRING,
|
||||||
"Text",
|
"Text",
|
||||||
"field"
|
"field"
|
||||||
)
|
)
|
||||||
|
|
|
@ -8,10 +8,10 @@ func _init() -> void:
|
||||||
|
|
||||||
for i in 4:
|
for i in 4:
|
||||||
add_output_port(
|
add_output_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.STRING,
|
||||||
"Test"
|
"Test"
|
||||||
)
|
)
|
||||||
add_input_port(
|
add_input_port(
|
||||||
Deck.Types.STRING,
|
DeckType.Types.STRING,
|
||||||
"Test"
|
"Test"
|
||||||
)
|
)
|
||||||
|
|
43
classes/deck/nodes/test_types.gd
Normal file
43
classes/deck/nodes/test_types.gd
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
extends DeckNode
|
||||||
|
|
||||||
|
|
||||||
|
func _init() -> void:
|
||||||
|
name = "Types Test"
|
||||||
|
node_type = name.to_snake_case()
|
||||||
|
description = ""
|
||||||
|
category = "test"
|
||||||
|
|
||||||
|
props_to_serialize = []
|
||||||
|
|
||||||
|
add_input_port(DeckType.Types.BOOL, "BOOL")
|
||||||
|
add_input_port(DeckType.Types.NUMERIC, "NUMERIC")
|
||||||
|
add_input_port(DeckType.Types.STRING, "STRING")
|
||||||
|
add_input_port(DeckType.Types.ARRAY, "ARRAY")
|
||||||
|
add_input_port(DeckType.Types.DICTIONARY, "DICTIONARY")
|
||||||
|
add_input_port(DeckType.Types.ANY, "ANY")
|
||||||
|
|
||||||
|
add_output_port(DeckType.Types.BOOL, "BOOL")
|
||||||
|
add_output_port(DeckType.Types.NUMERIC, "NUMERIC")
|
||||||
|
add_output_port(DeckType.Types.STRING, "STRING")
|
||||||
|
add_output_port(DeckType.Types.ARRAY, "ARRAY")
|
||||||
|
add_output_port(DeckType.Types.DICTIONARY, "DICTIONARY")
|
||||||
|
add_output_port(DeckType.Types.ANY, "ANY")
|
||||||
|
|
||||||
|
add_input_port(DeckType.Types.BOOL, "Send", "button")
|
||||||
|
|
||||||
|
|
||||||
|
func _receive(to_input_port: int, data: Variant, extra_data: Array = []) -> 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),
|
||||||
|
typeof(DeckType.convert_value(data, get_input_ports()[to_input_port].type))
|
||||||
|
])
|
|
@ -5,7 +5,7 @@ class_name Port
|
||||||
## them on a node.
|
## them on a node.
|
||||||
|
|
||||||
## The type index of this port.
|
## The type index of this port.
|
||||||
var type: Deck.Types
|
var type: DeckType.Types
|
||||||
## The label of this port. Used by the renderer to display. How it's displayed depends on the renderer
|
## The label of this port. Used by the renderer to display. How it's displayed depends on the renderer
|
||||||
## and the [member descriptor].
|
## and the [member descriptor].
|
||||||
var label: String
|
var label: String
|
||||||
|
@ -29,7 +29,7 @@ var value: Variant: set = set_value
|
||||||
|
|
||||||
|
|
||||||
func _init(
|
func _init(
|
||||||
p_type: Deck.Types,
|
p_type: DeckType.Types,
|
||||||
p_label: String,
|
p_label: String,
|
||||||
p_index: int,
|
p_index: int,
|
||||||
p_port_type: DeckNode.PortType,
|
p_port_type: DeckNode.PortType,
|
||||||
|
|
|
@ -1,161 +1,45 @@
|
||||||
class_name DeckType
|
class_name DeckType
|
||||||
## Base class for defining the types that can be used on a [Port].
|
|
||||||
|
|
||||||
var _value: Variant
|
enum Types{
|
||||||
var _success: bool = true
|
BOOL,
|
||||||
|
NUMERIC,
|
||||||
|
STRING,
|
||||||
|
ARRAY,
|
||||||
|
DICTIONARY,
|
||||||
|
ANY,
|
||||||
|
}
|
||||||
|
|
||||||
|
const CONVERSION_MAP := {
|
||||||
|
Types.BOOL: [Types.NUMERIC, Types.STRING, Types.ANY],
|
||||||
|
Types.NUMERIC: [Types.BOOL, Types.STRING, Types.ANY],
|
||||||
|
Types.STRING: [Types.BOOL, Types.NUMERIC, Types.ANY],
|
||||||
|
Types.ARRAY: [Types.STRING, Types.BOOL, Types.ANY],
|
||||||
|
Types.DICTIONARY: [Types.STRING, Types.BOOL, Types.ANY],
|
||||||
|
Types.ANY: [Types.BOOL, Types.NUMERIC, Types.STRING, Types.ARRAY, Types.DICTIONARY]
|
||||||
|
}
|
||||||
|
|
||||||
|
const GODOT_TYPES_MAP := {
|
||||||
|
Types.BOOL: TYPE_BOOL,
|
||||||
|
Types.NUMERIC: TYPE_FLOAT,
|
||||||
|
Types.STRING: TYPE_STRING,
|
||||||
|
Types.ARRAY: TYPE_ARRAY,
|
||||||
|
Types.DICTIONARY: TYPE_DICTIONARY,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
## Returns [code]true[/code] if the type is valid and can be used.
|
static func can_convert(from: Types, to: Types) -> bool:
|
||||||
func is_valid() -> bool:
|
if from == to:
|
||||||
return _success
|
return true
|
||||||
|
|
||||||
|
return (to in (CONVERSION_MAP[from] as Array))
|
||||||
|
|
||||||
|
|
||||||
func get_value() -> Variant:
|
static func convert_value(value: Variant, to: Types) -> Variant:
|
||||||
return _value
|
if to == Types.ANY:
|
||||||
|
return value
|
||||||
|
|
||||||
|
return type_convert(value, GODOT_TYPES_MAP[to])
|
||||||
|
|
||||||
|
|
||||||
func set_value(new_value: Variant) -> void:
|
static func type_str(type: Types) -> String:
|
||||||
_value = new_value
|
return str(Types.keys()[type])
|
||||||
|
|
||||||
|
|
||||||
## Virtual function. Used to convert [param other] to the overriding class' type.
|
|
||||||
static func from(other: DeckType) -> DeckType:
|
|
||||||
return null
|
|
||||||
|
|
||||||
|
|
||||||
## Generic Error type.
|
|
||||||
##
|
|
||||||
## Always returns [code]null[/code] as the value, and [code]false[/code] as success.[br]
|
|
||||||
## Always returns a new [DeckType.DeckTypeError] on conversion from any other type.
|
|
||||||
class DeckTypeError extends DeckType:
|
|
||||||
## An optional error message.
|
|
||||||
var error_message: String
|
|
||||||
|
|
||||||
|
|
||||||
func _init(p_error_message: String = ""):
|
|
||||||
_value = self
|
|
||||||
_success = false
|
|
||||||
|
|
||||||
error_message = p_error_message
|
|
||||||
|
|
||||||
|
|
||||||
static func from(other: DeckType) -> DeckTypeError:
|
|
||||||
return DeckTypeError.new()
|
|
||||||
|
|
||||||
|
|
||||||
## Numeric type. Corresponds to the JSON number type, so only supports the float primitive type.
|
|
||||||
class DeckTypeNumeric extends DeckType:
|
|
||||||
func _init(value: float = 0.0) -> void:
|
|
||||||
_value = value
|
|
||||||
|
|
||||||
## Converts either a [DeckType.DeckTypeString] or [DeckType.DeckTypeBool] to numeric.
|
|
||||||
## In the case of [DeckType.DeckTypeBool], [code]1.0[/code] is used for [code]true[/code],
|
|
||||||
## [code]0.0[/code] for [code]false[/code].[br]
|
|
||||||
## In the case of [DeckType.DeckTypeString], converts to String if it is a valid number
|
|
||||||
## or [DeckType.DeckTypeError] otherwise.
|
|
||||||
static func from(other: DeckType) -> DeckType:
|
|
||||||
if other is DeckTypeNumeric:
|
|
||||||
return other
|
|
||||||
|
|
||||||
if other is DeckTypeString:
|
|
||||||
if (other.get_value() as String).is_valid_float():
|
|
||||||
var value: float = float(other.get_value() as String)
|
|
||||||
var inst := DeckTypeNumeric.new()
|
|
||||||
inst._value = value
|
|
||||||
return inst
|
|
||||||
else:
|
|
||||||
var err: DeckTypeError = DeckTypeError.from(other)
|
|
||||||
err.error_message = "Conversion from String to Numeric failed, check the number"
|
|
||||||
return err
|
|
||||||
|
|
||||||
if other is DeckTypeBool:
|
|
||||||
var inst := DeckTypeNumeric.new()
|
|
||||||
inst._value = float(other.get_value() as bool)
|
|
||||||
return inst
|
|
||||||
|
|
||||||
var err: DeckTypeError = DeckTypeError.from(other)
|
|
||||||
err.error_message = "Conversion to Numeric is only possible from String or Bool"
|
|
||||||
return err
|
|
||||||
|
|
||||||
|
|
||||||
## String type. Corresponds to the JSON string type. Any type can be converted to String.
|
|
||||||
class DeckTypeString extends DeckType:
|
|
||||||
func _init(value: String = "") -> void:
|
|
||||||
_value = value
|
|
||||||
|
|
||||||
|
|
||||||
static func from(other: DeckType) -> DeckTypeString:
|
|
||||||
if other is DeckTypeString:
|
|
||||||
return other
|
|
||||||
|
|
||||||
var inst := DeckTypeString.new()
|
|
||||||
inst._value = var_to_str(other.get_value())
|
|
||||||
return inst
|
|
||||||
|
|
||||||
|
|
||||||
## Boolean type. Corresponds to the JSON bool type.
|
|
||||||
class DeckTypeBool extends DeckType:
|
|
||||||
func _init(value: bool = false) -> void:
|
|
||||||
_value = value
|
|
||||||
|
|
||||||
## Converts either [DeckType.DeckTypeNumeric], [DeckType.DeckTypeDictionary] or [DeckType.DeckTypeArray]
|
|
||||||
## to a boolean.[br]
|
|
||||||
## In the case of [DeckType.DeckTypeNumeric], the resulting value will be [code]false[/code]
|
|
||||||
## if the value is a zero value ([code]0.0[/code] and [code]-0.0[/code]), [code]true[/code] otherwise.[br]
|
|
||||||
## In the case of [DeckType.DeckTypeDictionary] or [DeckType.DeckTypeArray],
|
|
||||||
## the resulting value will be [code]true[/code] if the container is not empty.
|
|
||||||
static func from(other: DeckType) -> DeckType:
|
|
||||||
if other is DeckTypeBool:
|
|
||||||
return other
|
|
||||||
|
|
||||||
if other is DeckTypeNumeric:
|
|
||||||
var inst := DeckTypeBool.new()
|
|
||||||
inst._value = bool(other.get_value())
|
|
||||||
return inst
|
|
||||||
|
|
||||||
if other is DeckTypeDictionary or other is DeckTypeArray:
|
|
||||||
var inst := DeckTypeBool.new()
|
|
||||||
inst._value = !other.get_value().is_empty()
|
|
||||||
return inst
|
|
||||||
|
|
||||||
var err := DeckTypeError.from(other)
|
|
||||||
err.error_message = "Cannot create a DeckTypeBool from non-numeric or non-container type"
|
|
||||||
return err
|
|
||||||
|
|
||||||
|
|
||||||
## Array type. Corresponds to the JSON Array type.
|
|
||||||
class DeckTypeArray extends DeckType:
|
|
||||||
func _init(value: Array = []) -> void:
|
|
||||||
_value = value
|
|
||||||
|
|
||||||
|
|
||||||
## Arrays can only be converted from a string in the format
|
|
||||||
## [code]"["foo", 2, "bar"][/code].
|
|
||||||
static func from(other: DeckType) -> DeckType:
|
|
||||||
if other is DeckTypeString:
|
|
||||||
var inst := DeckTypeArray.new()
|
|
||||||
inst._value = str_to_var(other.get_value())
|
|
||||||
return inst
|
|
||||||
|
|
||||||
var err: DeckTypeError = DeckTypeError.from(other)
|
|
||||||
err.error_message = "Conversions to Array is only possible from String"
|
|
||||||
return err
|
|
||||||
|
|
||||||
|
|
||||||
## Dictionary class. Corresponds to the JSON Object type.
|
|
||||||
class DeckTypeDictionary extends DeckType:
|
|
||||||
func _init(value: Dictionary = {}) -> void:
|
|
||||||
_value = value
|
|
||||||
|
|
||||||
|
|
||||||
## Dictionaries can only be converted from a string in the format
|
|
||||||
## [code]{"key": "value"}[/code].
|
|
||||||
static func from(other: DeckType) -> DeckType:
|
|
||||||
if other is DeckTypeString:
|
|
||||||
var inst := DeckTypeDictionary.new()
|
|
||||||
inst._value = str_to_var(other.get_value())
|
|
||||||
return inst
|
|
||||||
|
|
||||||
var err: DeckTypeError = DeckTypeError.from(other)
|
|
||||||
err.error_message = "conversions to Dictionary is only possible from String"
|
|
||||||
return err
|
|
||||||
|
|
|
@ -25,12 +25,12 @@ func _ready() -> void:
|
||||||
if port.port_type == DeckNode.PortType.OUTPUT:
|
if port.port_type == DeckNode.PortType.OUTPUT:
|
||||||
button.pressed.connect(
|
button.pressed.connect(
|
||||||
func():
|
func():
|
||||||
node.send(port.index_of_type, DeckType.DeckTypeBool.new(true))
|
node.send(port.index_of_type, true)
|
||||||
)
|
)
|
||||||
elif port.port_type == DeckNode.PortType.INPUT:
|
elif port.port_type == DeckNode.PortType.INPUT:
|
||||||
button.pressed.connect(
|
button.pressed.connect(
|
||||||
func():
|
func():
|
||||||
node._receive(port.index_of_type, DeckType.DeckTypeBool.new(true))
|
node._receive(port.index_of_type, true)
|
||||||
)
|
)
|
||||||
"field":
|
"field":
|
||||||
var line_edit := LineEdit.new()
|
var line_edit := LineEdit.new()
|
||||||
|
@ -48,10 +48,10 @@ func _ready() -> void:
|
||||||
set_slot(
|
set_slot(
|
||||||
port.index,
|
port.index,
|
||||||
port.port_type == DeckNode.PortType.INPUT,
|
port.port_type == DeckNode.PortType.INPUT,
|
||||||
0,
|
port.type,
|
||||||
Color.WHITE,
|
Color.WHITE,
|
||||||
port.port_type == DeckNode.PortType.OUTPUT,
|
port.port_type == DeckNode.PortType.OUTPUT,
|
||||||
0,
|
port.type,
|
||||||
Color.WHITE,
|
Color.WHITE,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -82,12 +82,12 @@ func _on_node_port_added(port_idx: int) -> void:
|
||||||
if port.port_type == DeckNode.PortType.OUTPUT:
|
if port.port_type == DeckNode.PortType.OUTPUT:
|
||||||
button.pressed.connect(
|
button.pressed.connect(
|
||||||
func():
|
func():
|
||||||
node.send(port.index_of_type, DeckType.DeckTypeBool.new(true))
|
node.send(port.index_of_type, true)
|
||||||
)
|
)
|
||||||
elif port.port_type == DeckNode.PortType.INPUT:
|
elif port.port_type == DeckNode.PortType.INPUT:
|
||||||
button.pressed.connect(
|
button.pressed.connect(
|
||||||
func():
|
func():
|
||||||
node._receive(port.index_of_type, DeckType.DeckTypeBool.new(true))
|
node._receive(port.index_of_type, true)
|
||||||
)
|
)
|
||||||
"field":
|
"field":
|
||||||
var line_edit := LineEdit.new()
|
var line_edit := LineEdit.new()
|
||||||
|
@ -105,10 +105,10 @@ func _on_node_port_added(port_idx: int) -> void:
|
||||||
set_slot(
|
set_slot(
|
||||||
port.index,
|
port.index,
|
||||||
port.port_type == DeckNode.PortType.INPUT,
|
port.port_type == DeckNode.PortType.INPUT,
|
||||||
0,
|
port.type,
|
||||||
Color.WHITE,
|
Color.WHITE,
|
||||||
port.port_type == DeckNode.PortType.OUTPUT,
|
port.port_type == DeckNode.PortType.OUTPUT,
|
||||||
0,
|
port.type,
|
||||||
Color.WHITE,
|
Color.WHITE,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -144,12 +144,12 @@ func _on_node_ports_updated() -> void:
|
||||||
if port.port_type == DeckNode.PortType.OUTPUT:
|
if port.port_type == DeckNode.PortType.OUTPUT:
|
||||||
button.pressed.connect(
|
button.pressed.connect(
|
||||||
func():
|
func():
|
||||||
node.send(port.index_of_type, DeckType.DeckTypeBool.new(true))
|
node.send(port.index_of_type, true)
|
||||||
)
|
)
|
||||||
elif port.port_type == DeckNode.PortType.INPUT:
|
elif port.port_type == DeckNode.PortType.INPUT:
|
||||||
button.pressed.connect(
|
button.pressed.connect(
|
||||||
func():
|
func():
|
||||||
node._receive(port.index_of_type, DeckType.DeckTypeBool.new(true))
|
node._receive(port.index_of_type, true)
|
||||||
)
|
)
|
||||||
"field":
|
"field":
|
||||||
var line_edit := LineEdit.new()
|
var line_edit := LineEdit.new()
|
||||||
|
@ -167,9 +167,9 @@ func _on_node_ports_updated() -> void:
|
||||||
set_slot(
|
set_slot(
|
||||||
port.index,
|
port.index,
|
||||||
port.port_type == DeckNode.PortType.INPUT,
|
port.port_type == DeckNode.PortType.INPUT,
|
||||||
0,
|
port.type,
|
||||||
Color.WHITE,
|
Color.WHITE,
|
||||||
port.port_type == DeckNode.PortType.OUTPUT,
|
port.port_type == DeckNode.PortType.OUTPUT,
|
||||||
0,
|
port.type,
|
||||||
Color.WHITE,
|
Color.WHITE,
|
||||||
)
|
)
|
||||||
|
|
|
@ -37,6 +37,10 @@ func _ready() -> void:
|
||||||
search_popup_panel.size = search_popup_size
|
search_popup_panel.size = search_popup_size
|
||||||
add_child(search_popup_panel, false, Node.INTERNAL_MODE_BACK)
|
add_child(search_popup_panel, false, Node.INTERNAL_MODE_BACK)
|
||||||
|
|
||||||
|
for t: DeckType.Types in DeckType.CONVERSION_MAP:
|
||||||
|
for out_type: DeckType.Types in DeckType.CONVERSION_MAP[t]:
|
||||||
|
add_valid_connection_type(t, out_type)
|
||||||
|
|
||||||
add_node_menu.node_selected.connect(_on_add_node_menu_node_selected)
|
add_node_menu.node_selected.connect(_on_add_node_menu_node_selected)
|
||||||
|
|
||||||
connection_request.connect(attempt_connection)
|
connection_request.connect(attempt_connection)
|
||||||
|
|
|
@ -5,10 +5,11 @@ func _init() -> void:
|
||||||
name = ""
|
name = ""
|
||||||
node_type = name.to_snake_case()
|
node_type = name.to_snake_case()
|
||||||
description = ""
|
description = ""
|
||||||
|
category = ""
|
||||||
|
|
||||||
props_to_serialize = []
|
props_to_serialize = []
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func _receive(to_input_port: int, data: DeckType, extra_data: Array = []) -> void:
|
func _receive(to_input_port: int, data: Variant, extra_data: Array = []) -> void:
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Reference in a new issue