2023-12-15 22:44:25 +01:00
|
|
|
# (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)
|
2023-11-27 10:21:43 +01:00
|
|
|
extends DeckNode
|
|
|
|
|
|
|
|
var expr = Expression.new()
|
|
|
|
|
|
|
|
func _init():
|
|
|
|
name = "Expression"
|
|
|
|
node_type = name.to_snake_case()
|
2023-12-15 22:44:25 +01:00
|
|
|
description = "A node returning the result of a mathematical expression."
|
2023-11-27 10:21:43 +01:00
|
|
|
|
|
|
|
props_to_serialize = []
|
|
|
|
|
2023-12-15 22:44:25 +01:00
|
|
|
# TODO: order of ports is changed
|
|
|
|
# due to https://github.com/godotengine/godot/issues/85558
|
|
|
|
# when it's fixed, switch it back
|
|
|
|
add_input_port(
|
|
|
|
DeckType.Types.DICTIONARY,
|
2024-02-26 06:34:00 +01:00
|
|
|
"Expression Input",
|
2023-12-15 22:44:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
add_output_port(
|
|
|
|
DeckType.Types.ANY,
|
|
|
|
"Expression Text",
|
2024-02-26 06:34:00 +01:00
|
|
|
"codeblock",
|
2023-12-15 22:44:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-02-26 06:34:00 +01:00
|
|
|
func parse(input: Dictionary) -> Variant:
|
2023-11-27 10:21:43 +01:00
|
|
|
var text = get_output_ports()[0].value_callback.call()
|
2023-12-15 22:44:25 +01:00
|
|
|
|
|
|
|
var err = expr.parse(text, ["deck_var", "input"])
|
2023-11-27 10:21:43 +01:00
|
|
|
if err != OK:
|
2023-12-15 22:44:25 +01:00
|
|
|
|
|
|
|
DeckHolder.logger.log_node("Expression parse failed: %s" % err, Logger.LogType.ERROR)
|
2023-11-27 10:21:43 +01:00
|
|
|
printerr(err)
|
|
|
|
return null
|
2023-12-15 22:44:25 +01:00
|
|
|
|
2024-02-26 06:34:00 +01:00
|
|
|
var res = expr.execute([_belonging_to.variable_stack, input])
|
2023-11-27 10:21:43 +01:00
|
|
|
if expr.has_execute_failed():
|
2023-12-15 22:44:25 +01:00
|
|
|
|
|
|
|
DeckHolder.logger.log_node("Expression Execution Failed: %s" % text, Logger.LogType.ERROR)
|
2023-11-27 10:21:43 +01:00
|
|
|
return null
|
2023-12-15 22:44:25 +01:00
|
|
|
|
2023-11-27 10:21:43 +01:00
|
|
|
return res
|
2023-12-15 22:44:25 +01:00
|
|
|
|
2024-02-26 06:34:00 +01:00
|
|
|
func _value_request(_from_port : int) -> Variant:
|
|
|
|
var input = await request_value_async(0)
|
|
|
|
return parse(input)
|
|
|
|
|
|
|
|
|
|
|
|
func _receive(_on_input_port: int, data: Variant) -> void:
|
|
|
|
send(0, parse(data))
|