Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.8

0.11.8 / src / grouping.luc

## Grouping nodes: picked nodes, and the nodes feeding them, move into a new
## Group where they sat. Wires cannot cross a Group's edge, so a feeding node
## must feed nothing left outside; what read a grouped node reads the Group.
## On an object level the members keep their display flags (the Group yields
## its flagged children); on a data level the Group yields one node, so the
## member read outside (else the one displayed, else the last) takes the flag.
## Node paths (node_paths.luc) keep naming the nodes they named.
from network import Network
from node import Node
from shader_nodes import is_shader
from luce_ui import ui
import levels

pub func group_nodes(graph: Network, ids: list[int]) -> Node!:
    # Node paths that name a moved node, or start at a moved holder (an
    # Import, a Render), follow it into the Group.
    let made: list[Node] = []
    graph.keeping_paths(func () -> unit!:
        made.append(make_group(graph, ids)))
    return made[0]

func make_group(graph: Network, ids: list[int]) -> Node!:
    if ids.length == 0:
        error(ui.invalid, "Select nodes to group")
    let outside = graph.require(ids[0]).parent
    var x = 0.0
    var y = 0.0
    var any_visible = false
    for id in ids:
        let node = graph.require(id)
        if node.kind == "Input":
            error(ui.invalid, "Group the nodes using an Input; the Input stays at the network boundary")
        if is_shader(node.kind):
            error(ui.invalid, "Shader nodes stay in their Material")
        if node.parent != outside:
            error(ui.invalid, "Group nodes of one network")
        x += node.x / float(ids.length)
        y += node.y / float(ids.length)
        any_visible = any_visible or node.visible
    # The picked nodes and everything feeding them.
    let members = ids.copy()
    var at = 0
    while at < members.length:
        for input in graph.require(members[at]).inputs:
            if input >= 0 and not (input in members) and graph.require(input).kind != "Input":
                members.append(input)
        at += 1
    let readers: list[Node] = []
    for other in graph.children(outside):
        if other.id in members:
            continue
        var reads = false
        for input in other.inputs:
            if input in members:
                if not (input in ids):
                    error(ui.invalid, f"{graph.require(input).name} also feeds {other.name}; pick it too, or group from where they meet")
                reads = true
        if reads:
            readers.append(other)
    let data = graph.level_of(outside) == levels.data_level
    let output = group_output(graph, members, readers) if data else -1
    let shown = graph.display_in(outside)
    # Boundary Inputs stay outside; the Group imports them through its ports.
    let external: list[int] = []
    for member in members:
        for input in graph.require(member).inputs:
            if input >= 0 and not (input in members) and not (input in external): external.append(input)
    if external.length > 2:
        error(ui.invalid, "A Group supports up to two external inputs")
    let saved = graph.scope
    graph.scope = outside
    let group = graph.add("Group", x, y) catch failure:
        graph.scope = saved
        error(failure.code, failure.message)
    let imported: list[int] = []
    graph.scope = group.id
    for port in 0..<external.length:
        group.inputs[port] = external[port]
        let input = graph.add("Input", x - 150.0 + float(port) * 120.0, y - 100.0)
        input.values[0] = float(port)
        imported.append(input.id)
    graph.scope = saved
    if ids.length == 1:
        let wanted = graph.require(ids[0]).name + " group"
        group.name = graph.fresh_name(outside, wanted, 2) if graph.name_taken(outside, wanted) else wanted
    for member in members:
        let node = graph.require(member)
        node.parent = group.id
        for port in 0..<node.inputs.length:
            if let external_at = external.index_of(node.inputs[port]): node.inputs[port] = imported[external_at]
        # Hidden together on an object level, the members show inside the
        # hidden Group.
        if data:
            node.visible = member == output
        elif not any_visible:
            node.visible = true
    for input in imported:
        graph.require(input).visible = false
    if data:
        # The Group shows in its level where a member did; else the flag
        # stays where it was.
        group.visible = shown in members
        if not (shown in members):
            if let other = graph.find(shown):
                other.visible = true
    else:
        group.visible = any_visible
    for reader in readers:
        for port in 0..<reader.inputs.length:
            if reader.inputs[port] in members:
                reader.inputs[port] = group.id
                reader.sources[port] = 0
        graph.invalidate(reader.id)
    if graph.selected in members:
        graph.selected = group.id
    if graph.display in members:
        graph.display = group.id
    graph.picked = [group.id]
    graph.invalidate(group.id)
    return group

## On a data level, which member the new Group yields: the one nodes left
## outside read (only one may be), else the displayed one, else the last.
func group_output(graph: Network, members: list[int], readers: list[Node]) -> int!:
    let read: list[int] = []
    for reader in readers:
        for input in reader.inputs:
            if input in members and not (input in read):
                read.append(input)
    if read.length > 1:
        error(ui.invalid, "Only one grouped node may feed nodes outside the Group")
    if read.length == 1:
        return read[0]
    for member in members:
        if graph.require(member).visible:
            return member
    for member in members:
        var last = true
        for other in members:
            last = last and not (member in graph.require(other).inputs)
        if last:
            return member
    return members[0]