2024-04-16 03:08:25 +00:00
|
|
|
extends Sampler
|
|
|
|
class_name SamplerPriority
|
|
|
|
|
|
|
|
|
2024-04-23 07:28:45 +00:00
|
|
|
## If true, the [SamplerPriority] will attempt to automatically setup the recommended signals for all the [field possibilities] on NOTIFICATION_READY.
|
|
|
|
@export var autosetup_signals_on_ready: bool = true
|
2024-04-16 03:08:25 +00:00
|
|
|
|
2024-04-23 07:16:09 +00:00
|
|
|
|
|
|
|
## Update [field possibilities] with the most likely subset of nodes.
|
2024-04-23 07:28:45 +00:00
|
|
|
func autodetect_possibilities() -> void:
|
2024-04-23 07:16:09 +00:00
|
|
|
possibilities = find_children("*", "Priority", true, false)
|
|
|
|
|
2024-04-23 07:28:45 +00:00
|
|
|
## Setup the recommended signals for each node in [field possibilities].
|
|
|
|
func autosetup_signals() -> void:
|
|
|
|
for possibility in possibilities:
|
|
|
|
possibility.priority_changed.connect(_autosetup_on_possibility_priority_changed.bind(possibility))
|
|
|
|
|
2024-04-16 22:55:17 +00:00
|
|
|
## Get a reference.
|
|
|
|
func sample() -> Priority:
|
|
|
|
if len(possibilities) == 0:
|
|
|
|
return null
|
|
|
|
|
|
|
|
# FIXME: Change this to something more efficient when needed
|
|
|
|
var highest_possibility: Priority = null
|
|
|
|
for possibility in possibilities:
|
2024-04-17 02:52:11 +00:00
|
|
|
if highest_possibility == null or possibility.priority > highest_possibility.priority:
|
2024-04-16 22:55:17 +00:00
|
|
|
highest_possibility = possibility
|
|
|
|
|
|
|
|
if highest_possibility == null:
|
|
|
|
return null
|
2024-04-22 22:13:54 +00:00
|
|
|
|
|
|
|
return highest_possibility
|
|
|
|
|
2024-04-18 23:26:02 +00:00
|
|
|
func get_all_refs() -> Array[Node]:
|
2024-04-17 02:52:11 +00:00
|
|
|
var refs: Array[Node] = []
|
|
|
|
for possibility in possibilities:
|
|
|
|
refs.append(possibility.get_ref())
|
|
|
|
return refs
|
2024-04-22 22:13:54 +00:00
|
|
|
|
|
|
|
func get_ref(node: Node) -> Node:
|
2024-04-23 07:16:09 +00:00
|
|
|
return node.get_ref()
|
2024-04-23 07:28:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
if autosetup_signals_on_ready:
|
|
|
|
autosetup_signals()
|
|
|
|
|
|
|
|
func _autosetup_on_possibility_priority_changed(node: Priority, new: int, old: int):
|
|
|
|
if node == selected:
|
|
|
|
if new < old:
|
|
|
|
sample_and_enable()
|
|
|
|
else:
|
|
|
|
# A nice optimization!
|
|
|
|
if selected == null or new > selected.priority:
|
|
|
|
set_enabled(node)
|