add scalar math nodes (#123)

Reviewed-on: https://codeberg.org/StreamGraph/StreamGraph/pulls/123
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-17 10:09:23 +00:00 committed by yagich
parent 877e29f580
commit edeb8e22dc
13 changed files with 467 additions and 0 deletions

View file

@ -0,0 +1,20 @@
# (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 DeckNode
func _init() -> void:
name = "Numeric Constant"
node_type = name.to_snake_case()
add_output_port(
DeckType.Types.NUMERIC,
"Value",
"spinbox:unbounded:0.0001",
Port.UsageType.VALUE_REQUEST,
)
func _value_request(_from_port: int) -> Variant:
return ports[0].value

View file

@ -0,0 +1,21 @@
# (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 DeckNode
func _init() -> void:
name = "PI Constant"
node_type = "pi_constant"
description = "The Pi constant."
add_output_port(
DeckType.Types.NUMERIC,
"π Value",
"",
Port.UsageType.VALUE_REQUEST
)
func _value_request(_on_output_port: int) -> float:
return PI

View file

@ -0,0 +1,53 @@
# (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 DeckNode
func _init() -> void:
name = "Add Numbers"
node_type = "scalar_add"
description = "Adds two numbers together."
add_input_port(
DeckType.Types.NUMERIC,
"Number A",
"spinbox:unbounded:0.0001",
)
add_input_port(
DeckType.Types.NUMERIC,
"Number B",
"spinbox:unbounded:0.0001",
)
add_output_port(
DeckType.Types.NUMERIC,
"Result"
)
func _value_request(_on_output_port: int) -> Variant:
var va = await resolve_input_port_value_async(0)
var vb = await resolve_input_port_value_async(1)
va = DeckType.convert_value(va, DeckType.Types.NUMERIC)
vb = DeckType.convert_value(vb, DeckType.Types.NUMERIC)
return va + vb
func _receive(on_input_port: int, data: Variant) -> void:
var va
var vb
if on_input_port == 0:
va = data
vb = await resolve_input_port_value_async(1)
else:
va = await resolve_input_port_value_async(0)
vb = data
va = DeckType.convert_value(va, DeckType.Types.NUMERIC)
vb = DeckType.convert_value(vb, DeckType.Types.NUMERIC)
send(0, va + vb)

View file

@ -0,0 +1,53 @@
# (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 DeckNode
func _init() -> void:
name = "Divide Numbers"
node_type = "scalar_divide"
description = "Divide one number by another."
add_input_port(
DeckType.Types.NUMERIC,
"Number A",
"spinbox:unbounded:0.0001",
)
add_input_port(
DeckType.Types.NUMERIC,
"Number B",
"spinbox:unbounded:0.0001",
)
add_output_port(
DeckType.Types.NUMERIC,
"Result"
)
func _value_request(_on_output_port: int) -> Variant:
var va = await resolve_input_port_value_async(0)
var vb = await resolve_input_port_value_async(1)
va = DeckType.convert_value(va, DeckType.Types.NUMERIC)
vb = DeckType.convert_value(vb, DeckType.Types.NUMERIC)
return va / vb
func _receive(on_input_port: int, data: Variant) -> void:
var va
var vb
if on_input_port == 0:
va = data
vb = await resolve_input_port_value_async(1)
else:
va = await resolve_input_port_value_async(0)
vb = data
va = DeckType.convert_value(va, DeckType.Types.NUMERIC)
vb = DeckType.convert_value(vb, DeckType.Types.NUMERIC)
send(0, va / vb)

View file

@ -0,0 +1,54 @@
# (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 DeckNode
func _init() -> void:
name = "Raise Number to Power"
node_type = "scalar_exponentiate"
description = "Raise one number to the power of another (exponentiation)."
aliases = ["exponentiate", "square", "cube", "quad"]
add_input_port(
DeckType.Types.NUMERIC,
"Base",
"spinbox:unbounded:0.0001",
)
add_input_port(
DeckType.Types.NUMERIC,
"Exponent",
"spinbox:unbounded:0.0001",
).set_value(2)
add_output_port(
DeckType.Types.NUMERIC,
"Result"
)
func _value_request(_on_output_port: int) -> Variant:
var va = await resolve_input_port_value_async(0)
var vb = await resolve_input_port_value_async(1)
va = DeckType.convert_value(va, DeckType.Types.NUMERIC)
vb = DeckType.convert_value(vb, DeckType.Types.NUMERIC)
return va ** vb
func _receive(on_input_port: int, data: Variant) -> void:
var va
var vb
if on_input_port == 0:
va = data
vb = await resolve_input_port_value_async(1)
else:
va = await resolve_input_port_value_async(0)
vb = data
va = DeckType.convert_value(va, DeckType.Types.NUMERIC)
vb = DeckType.convert_value(vb, DeckType.Types.NUMERIC)
send(0, va ** vb)

View file

@ -0,0 +1,34 @@
# (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 DeckNode
func _init() -> void:
name = "Natural Logarithm"
node_type = "scalar_log"
description = "Returns the natural logarithm of the number (base e)."
add_input_port(
DeckType.Types.NUMERIC,
"Number",
"spinbox:unbounded:0.0001",
)
add_output_port(
DeckType.Types.NUMERIC,
"Result",
)
func _value_request(_on_output_port: int) -> float:
var v = await resolve_input_port_value_async(0)
v = DeckType.convert_value(v, DeckType.Types.NUMERIC)
return log(v)
func _receive(_to_input_port: int, data: Variant) -> void:
var v = DeckType.convert_value(data, DeckType.Types.NUMERIC)
send(0, log(v))

View file

@ -0,0 +1,53 @@
# (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 DeckNode
func _init() -> void:
name = "Multiply Numbers"
node_type = "scalar_multiply"
description = "Multiply two numbers."
add_input_port(
DeckType.Types.NUMERIC,
"Number A",
"spinbox:unbounded:0.0001",
)
add_input_port(
DeckType.Types.NUMERIC,
"Number B",
"spinbox:unbounded:0.0001",
)
add_output_port(
DeckType.Types.NUMERIC,
"Result"
)
func _value_request(_on_output_port: int) -> Variant:
var va = await resolve_input_port_value_async(0)
var vb = await resolve_input_port_value_async(1)
va = DeckType.convert_value(va, DeckType.Types.NUMERIC)
vb = DeckType.convert_value(vb, DeckType.Types.NUMERIC)
return va * vb
func _receive(on_input_port: int, data: Variant) -> void:
var va
var vb
if on_input_port == 0:
va = data
vb = await resolve_input_port_value_async(1)
else:
va = await resolve_input_port_value_async(0)
vb = data
va = DeckType.convert_value(va, DeckType.Types.NUMERIC)
vb = DeckType.convert_value(vb, DeckType.Types.NUMERIC)
send(0, va * vb)

View file

@ -0,0 +1,49 @@
# (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 DeckNode
func _init() -> void:
name = "Round Number"
node_type = "scalar_round"
description = "Rounds a number to the nearest whole number."
add_virtual_port(
DeckType.Types.STRING,
"Function",
"singlechoice:round:floor:ceil"
)
add_input_port(
DeckType.Types.NUMERIC,
"Number",
"spinbox:unbounded:0.0001"
)
add_output_port(
DeckType.Types.NUMERIC,
"Result"
)
func _round(v: float) -> float:
match get_virtual_ports()[0].value:
"floor":
return floorf(v)
"ceil":
return ceilf(v)
_:
return roundf(v)
func _value_request(on_output_port: int) -> Variant:
var v = await resolve_input_port_value_async(0)
v = DeckType.convert_value(v, DeckType.Types.NUMERIC)
return _round(v)
func _receive(_to_input_port: int, data: Variant) -> void:
var v = DeckType.convert_value(data, DeckType.Types.NUMERIC)
send(0, _round(v))

View file

@ -0,0 +1,53 @@
# (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 DeckNode
func _init() -> void:
name = "Subtract Numbers"
node_type = "scalar_subtract"
description = "Subtracts one number from another."
add_input_port(
DeckType.Types.NUMERIC,
"Number A",
"spinbox:unbounded:0.0001",
)
add_input_port(
DeckType.Types.NUMERIC,
"Number B",
"spinbox:unbounded:0.0001",
)
add_output_port(
DeckType.Types.NUMERIC,
"Result"
)
func _value_request(_on_output_port: int) -> Variant:
var va = await resolve_input_port_value_async(0)
var vb = await resolve_input_port_value_async(1)
va = DeckType.convert_value(va, DeckType.Types.NUMERIC)
vb = DeckType.convert_value(vb, DeckType.Types.NUMERIC)
return va - vb
func _receive(on_input_port: int, data: Variant) -> void:
var va
var vb
if on_input_port == 0:
va = data
vb = await resolve_input_port_value_async(1)
else:
va = await resolve_input_port_value_async(0)
vb = data
va = DeckType.convert_value(va, DeckType.Types.NUMERIC)
vb = DeckType.convert_value(vb, DeckType.Types.NUMERIC)
send(0, va - vb)

View file

@ -0,0 +1,69 @@
# (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 DeckNode
func _init() -> void:
name = "Trigonometry"
node_type = "scalar_trig"
description = "A selection of trigonometric functions on numbers."
aliases = [
"sine", "cosine", "tangent",
"arcsin", "acos", "arccos", "arctan", "atan",
"sinh", "cosh", "tanh",
"asinh", "acosh", "atanh",
]
add_virtual_port(
DeckType.Types.STRING,
"Function",
"singlechoice:sin:cos:tan:asin:acos:atan:asinh:acosh:atanh",
)
add_input_port(
DeckType.Types.NUMERIC,
"Angle",
"spinbox:unbounded:0.0001",
)
add_output_port(
DeckType.Types.NUMERIC,
"Result",
)
func _do(v: float) -> float:
match get_virtual_ports()[0].value:
"cos":
return cos(v)
"tan":
return tan(v)
"asin":
return asin(v)
"acos":
return acos(v)
"atan":
return atan(v)
"asinh":
return asinh(v)
"acosh":
return acosh(v)
"atanh":
return atanh(v)
_:
return sin(v)
func _value_request(_on_output_port: int) -> float:
var v = await resolve_input_port_value_async(0)
v = DeckType.convert_value(v, DeckType.Types.NUMERIC)
return _do(v)
func _receive(on_input_port: int, data: Variant) -> void:
var v = DeckType.convert_value(data, DeckType.Types.NUMERIC)
send(0, _do(v))

View file

@ -21,3 +21,9 @@ func _setup(port: Port, _node: DeckNode) -> void:
func(_id: int): func(_id: int):
port.set_value.call(box.get_item_text(box.get_selected_id())) port.set_value.call(box.get_item_text(box.get_selected_id()))
) )
if port.value != null:
for i in box.get_item_count():
if box.get_item_text(i) == port.value:
box.select(i)
break

View file

@ -7,6 +7,7 @@ extends DescriptorContainer
func _setup(port: Port, _node: DeckNode) -> void: func _setup(port: Port, _node: DeckNode) -> void:
spin_box.tooltip_text = port.label
if port.value != null: if port.value != null:
spin_box.value = float(port.value) spin_box.value = float(port.value)
if "unbounded" in descriptor: if "unbounded" in descriptor:

View file

@ -10,3 +10,4 @@ script = ExtResource("2_cxb6h")
unique_name_in_owner = true unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
size_flags_horizontal = 3 size_flags_horizontal = 3
step = 0.0