Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.7

0.11.7 / src / tool_prompts.luc

## Tool prompts: a node waiting for its components while its input shows
## for picking (Tool → Select, Reselect, Pick profiles); the node follows
## the selection, Shift or Cmd peeks at the input, Enter keeps it, Esc
## removes it. A CAD tool's picks wait the same way (tool_picks.luc): the
## selection and Enter go to it.
from workspace import Workspace
from tools import ToolRun
from tool_prompt import ToolPrompt
from edit_steps import insert_after_display, show
import node_catalog
import profile_pick
import tool_picks
import tool_registry

## Tool → Select: the new node waits, empty, while the viewport shows its
## input for picking; the node follows the selection (follow_prompt), Enter
## keeps it (accept_prompt), Esc removes it.
pub func start_prompt(w: Workspace, label: str, run: ToolRun) -> unit!:
    if w.in_transaction():
        return
    w.begin(label)
    let source = w.network.display
    let node = insert_after_display(w, run) catch failure:
        w.cancel()
        w.notice = failure.message
        w.touch()
        return
    node.texts[0] = ""
    # The input is selected while picking, so its geometry is picked on.
    w.network.selected = source
    show(w, source)
    w.prompt = ToolPrompt(node.id, source, label, false)
    w.pending_changed = true
    w.notice = f"Select components for {label} (Shift adds), then Enter; Esc cancels"
    w.touch()
    w.rebuild()

## Reselect: show node `id`'s input with its group selected, to rewrite
## the group (and its guard) as the selection changes.
pub func reselect(w: Workspace, id: int) -> unit!:
    let node = w.network.require(id)
    if not node_catalog.is_mesh_verb(node.kind) or node.inputs[0] < 0 or w.in_transaction():
        return
    w.begin("Reselect")
    w.prompt = ToolPrompt(node.id, node.inputs[0], "Reselect", true)
    w.pending_group = node.texts[0] if node.texts[0].trim() != "" else "*"
    w.pending_type = int(node.values[node_catalog.group_type_slot])
    w.network.selected = node.inputs[0]
    show_prompt(w)
    w.pending_changed = true
    w.notice = "Change the selection, then Enter; Esc cancels"
    w.touch()
    w.rebuild()

## What a prompt shows: its node's result, or its input while peeking or
## while the node has no group yet (an empty group means everything).
func show_prompt(w: Workspace) -> unit!:
    let prompt = w.prompt else return
    let node = w.network.find(prompt.node) else return
    show(w, prompt.input if w.peeking or node.texts[0].trim() == "" else node.id)

## Shift or Cmd pressed or released (`held`) during a tool prompt: peek at
## its input, or show the tool's result again.
pub func set_peek(w: Workspace, held: bool) -> unit!:
    let peeking = held and w.prompt != none
    if peeking == w.peeking:
        return
    w.peeking = peeking
    show_prompt(w)
    w.touch()
    w.rebuild()

## Pick profiles: click the regions of Extrude's or Revolve's input sketch
## to take (profile_pick.luc); the result shows as they change.
pub func pick_profiles(w: Workspace, id: int) -> unit!:
    let node = w.network.require(id)
    let tool = tool_registry.cad_node(node.kind) else return
    if tool.gesture != "profiles" or node.inputs[0] < 0 or w.in_transaction():
        return
    w.begin(profile_pick.label)
    w.prompt = ToolPrompt(node.id, node.inputs[0], profile_pick.label, true)
    w.network.selected = node.id
    show(w, node.id)
    w.selection = none
    w.pending_changed = true
    w.notice = "Click the profiles to take (again to leave one out), then Enter; Esc cancels"
    w.touch()
    w.rebuild()

## The selection changed during a prompt: the prompted node takes it (its
## group) and its result shows (show_prompt), its input still picked on; a
## CAD tool's pick takes it its own way (tool_picks.luc).
pub func follow_prompt(w: Workspace) -> unit!:
    let prompt = w.prompt else return
    let node = w.network.find(prompt.node) else return
    let mesh = w.geometry else return
    let chosen = w.current_selection() else return
    if prompt.picking():
        tool_picks.follow(w, mesh, chosen)
        return
    if chosen.count() == 0:
        node.texts[0] = ""
        w.network.invalidate(node.id)
        show_prompt(w)
        w.rebuild()
        return
    node.texts[0] = chosen.expression(mesh)
    if node_catalog.takes_group_type(node.kind):
        node.values[node_catalog.group_type_slot] = float(w.group_type())
    node.guard = mesh.topology_hash()
    w.network.invalidate(node.id)
    show_prompt(w)
    w.notice = ""
    w.pending_changed = true
    w.rebuild()

## Enter: keep the prompted node as the selection made it.
pub func accept_prompt(w: Workspace) -> unit!:
    let prompt = w.prompt else return
    let node = w.network.find(prompt.node) else return
    if prompt.picking():
        tool_picks.accept(w)
        return
    # Profiles were written as they were clicked.
    if prompt.label == profile_pick.label:
        w.prompt = none
        w.peeking = false
        w.notice = ""
        w.commit()
        w.rebuild()
        return
    let chosen = w.current_selection() else return
    if chosen.count() == 0:
        w.notice = "Select components first, or Esc to cancel"
        w.touch()
        return
    w.network.selected = node.id
    show(w, node.id)
    w.prompt = none
    w.peeking = false
    w.notice = ""
    w.adopt_output = true
    w.pending_changed = true
    w.commit()
    w.rebuild()