Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.7

0.11.7 / src / component_moves.luc

## Moving the selected components: a Transform Components step on the
## active Edit node (consecutive moves of one group merge), else a node
## after the displayed one; gizmo previews redo the whole gesture from the
## state before it, one undo per drag.
from luce_geocore.geocore import Mesh
from luce_3d.graphics import Vector3
from workspace import Workspace
from tools import ToolRun
from verb_rows import initial_numbers
from edit_steps import edit_step, insert_after_display
from edit_operations import EditStep
from edit_types import point_level
import edit_cad_nets
from snapping import grid_step

pub func move_selection(w: Workspace, delta: Vector3) -> unit!:
    if w.selected_count() == 0:
        return
    let mesh = w.geometry else return
    w.perform("Move components", func () -> unit!:
        append_transform(w, mesh, transform_numbers(delta, Vector3(), Vector3(1.0, 1.0, 1.0)))
        w.rebuild())

## A move of the selection: an Edit step on the active Edit node, else a
## Transform Components node after the displayed one.
func append_transform(w: Workspace, mesh: Mesh, numbers: list[float]) -> unit!:
    let chosen = w.current_selection() else return
    let group = chosen.expression(mesh)
    if w.editing():
        let node = w.selected() else return
        let step = edit_step(w, node, "Transform Components", group, numbers, mesh)
        # Consecutive moves of one group merge into one step (D19); each
        # drag is still its own undo entry.
        if node.edits.length > 0 and node.edits[-1].merges(step):
            node.edits[-1] = node.edits[-1].merged(step)
        else:
            node.edits.append(step)
        w.network.invalidate(node.id)
        return
    let node = insert_after_display(w, ToolRun("Transform Components", numbers))
    node.texts[0] = group
    node.guard = mesh.topology_hash()

## Whether components picked are Edit CAD's CVs.
pub func at_cvs(w: Workspace) -> bool:
    let kind = w.active_kind() else return false
    return kind.name == "Edit CAD" and w.editing() and w.level_index() == point_level

## Whether the gizmo moves CVs of Edit CAD by its CV move mode or falloff
## (edit_cad_nets.luc) rather than as Transform Components.
pub func moves_cvs_by_mode(w: Workspace) -> bool:
    let node = w.selected() else return false
    return at_cvs(w) and edit_cad_nets.moves_by_mode(node.values)

## The gizmo's axes in the CV move mode (NUV's frame at the first picked
## CV, Slide's hull directions, View's screen axes); none for World.
pub func cv_axes(w: Workspace) -> list[Vector3]?!:
    if not moves_cvs_by_mode(w):
        return none
    let node = w.selected() else return none
    let mesh = w.geometry else return none
    let ids = w.selected_ids()
    if ids.length == 0:
        return none
    let lens = w.view.camera.projection(1.0)
    return edit_cad_nets.gizmo_axes(int(node.values[edit_cad_nets.move_mode_slot]), mesh, ids[0], [lens.right, lens.up, lens.backward])

## The gizmo's move `delta` as a Move CVs step on the Edit CAD node (the
## whole gesture: previews start from the state before it).
func append_cv_move(w: Workspace, mesh: Mesh, delta: Vector3) -> unit!:
    let chosen = w.current_selection() else return
    let node = w.selected() else return
    let mode = int(node.values[edit_cad_nets.move_mode_slot])
    let amount = edit_cad_nets.adjusted(node.values, edit_cad_nets.amount_of(mode, delta, w.cv_frame if w.cv_frame != none else cv_axes(w)))
    let numbers = edit_cad_nets.move_numbers(node.values, amount)
    node.edits.append(EditStep(edit_cad_nets.move_verb, chosen.expression(mesh), 1, numbers, mesh.topology_hash()))
    w.network.invalidate(node.id)

## Whether the arrow keys nudge CVs now (Alias's Lock step size): CVs
## picked at Edit CAD's CVs level with the Move tool on.
pub func nudges(w: Workspace) -> bool:
    return at_cvs(w) and w.selected_count() > 0 and w.edit_tool == "Move" and w.selecting()

## One arrow-key nudge of the picked CVs along gizmo axis `axis` (0 to 2,
## the CV move mode's frame or World's) by `sign` steps: the node's Step
## size, else the grid's. One undo each.
pub func nudge(w: Workspace, axis: int, sign: float) -> unit!:
    if not nudges(w) or w.in_transaction():
        return
    let node = w.selected() else return
    let mesh = w.geometry else return
    var step = node.values[edit_cad_nets.step_slot]
    if not (step > 0.0):
        step = grid_step(w.view.distance)
    let axes = cv_axes(w) else [Vector3(1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0), Vector3(0.0, 0.0, 1.0)]
    let locked = node.values[[edit_cad_nets.lock_x_slot, edit_cad_nets.lock_y_slot, edit_cad_nets.lock_z_slot][axis]] != 0.0
    if locked:
        w.notice = f"That axis is locked (Lock {["X", "Y", "Z"][axis]})"
        w.touch()
        return
    let delta = axes[axis].multiply_scalar(step * sign)
    w.perform("Nudge CVs", func () -> unit!:
        if moves_cvs_by_mode(w):
            let mode = int(node.values[edit_cad_nets.move_mode_slot])
            let numbers = edit_cad_nets.move_numbers(node.values, edit_cad_nets.amount_of(mode, delta, axes))
            let chosen = w.current_selection() else return
            node.edits.append(EditStep(edit_cad_nets.move_verb, chosen.expression(mesh), 1, numbers, mesh.topology_hash()))
            w.network.invalidate(node.id)
        else:
            append_transform(w, mesh, transform_numbers(delta, Vector3(), Vector3(1.0, 1.0, 1.0)))
        w.rebuild())

## Preview each gizmo delta against the original gesture snapshot, not
## against the last frame. A whole drag becomes one undo entry.
## One request per pointer move: the worker applies the preview recipe to
## the stored state before the gesture, never replaying the node.
pub func preview_move(w: Workspace, delta: Vector3) -> unit!:
    preview_transform(w, delta, Vector3(), Vector3(1.0, 1.0, 1.0))

## Transform Components' numbers: a translation, a rotation in degrees and
## scale factors, about the selection's centroid.
func transform_numbers(translate: Vector3, rotate: Vector3, scale: Vector3) -> list[float]:
    let numbers = initial_numbers("Transform Components")
    for (slot, value) in [(0, translate.x), (1, translate.y), (2, translate.z), (3, rotate.x), (4, rotate.y), (5, rotate.z), (6, scale.x), (7, scale.y), (8, scale.z)]:
        numbers[slot] = value
    return numbers

## A gizmo frame on the selected components (or a transforming node's
## translation): the whole gesture so far, from the state before it.
pub func preview_transform(w: Workspace, delta: Vector3, rotate: Vector3, scale: Vector3) -> unit!:
    let before = w.pending else return
    w.dragging = true
    w.network = before.graph.copy()
    w.selection = before.selection
    if w.transforming():
        let node = w.selected() else return
        node.values[1] += delta.x
        node.values[2] += delta.y
        node.values[3] += delta.z
        w.network.invalidate(node.id)
        w.pending_changed = true
    elif w.selecting() and w.selected_count() > 0 and moves_cvs_by_mode(w) and rotate.length() == 0.0 and scale.subtract(Vector3(1.0, 1.0, 1.0)).length() == 0.0:
        let mesh = w.geometry else return
        append_cv_move(w, mesh, delta) catch failure:
            w.cancel()
            w.notice = failure.message
            w.touch()
            return
        w.pending_changed = true
    elif w.selecting() and w.selected_count() > 0:
        let mesh = w.geometry else return
        # CVs take Edit CAD's Drag strength, Step size and locks (World axes).
        var moved = delta
        if at_cvs(w):
            let node = w.selected() else return
            moved = edit_cad_nets.adjusted(node.values, delta)
        append_transform(w, mesh, transform_numbers(moved, rotate, scale)) catch failure:
            w.cancel()
            w.notice = failure.message
            w.touch()
            return
        w.pending_changed = true
    w.rebuild()