dymokomi/luced-3d
Source of release 0.11.8
## The tool bar: the Select tool run last while its selection stands
## (select_run.luc), else the Edit node's last tool step's parameters, in a small
## card at the bottom of the viewport (Fillet: its radius, continuity and tangent
## chain), the step running again live as they change. What the tool works
## on was picked before it ran, so its selection rows are not here: these
## are the node's own rows less those (node_catalog.tool_rows), or the
## kind's for its own verbs (Edit SDF's Blend); a step a command made (Edit
## CAD's Extrude and Revolve of a sketch) shows its node's rows. A step that
## could not be built says why under its rows. A tool run outside an Edit
## node makes a node, and its parameters are the inspector's.
from luce_ui.ui import VStack, Text, Font, ParameterPanel, ParameterSpec, Pane, Size
from luce_gpu.gpu import Rect
from interop import Connection
from workspace import Workspace
from parameter_rows import Parameter, ramp_numbers, padded
import tool_picks
import tool_registry
import pick_kinds
import edit_types
import edit_cad_nets
import node_catalog
import modeling_commands
import selection_commands
import units
from luce_std import math
## The bar's width.
let bar_width = 380.0
pub class ToolBar:
let w: Workspace
pub let body: Pane
let title: Text
pub let panel: ParameterPanel
## Why the step could not be built ("" when it was).
pub let problem: Text
var rows: list[Parameter]
## The step shown (node, its place in the recipe, its verb): rows are
## made again only when it changes.
var shown: str = ""
## Where it was last put.
var placed: list[float]
## The step's numbers as the row change began (picks_after).
var before: list[float]
## The tool of the step shown (its rows and picks); the title may name
## the gesture tool that made the step instead.
var step_tool: str = ""
let connections: list[Connection]
pub func init(self, w: Workspace, font: Font) -> unit!:
self.w = w
self.rows = []
self.placed = []
self.before = []
self.title = Text("", font = font, muted = true)
self.panel = ParameterPanel([], font = font)
self.problem = Text("", font = font)
self.problem.layout().set_visible(false)
self.body = Pane(VStack([self.title, self.panel, self.problem], spacing = 2.0, padding = 6.0), minimum = Size(bar_width, 52.0))
# Floating, not modal: the viewport keeps its clicks around it.
self.body.layout().set_popup(true)
self.body.layout().set_modal(false)
self.body.layout().set_visible(false)
self.connections = [self.panel.on_begin(self.began),
self.panel.on_change(self.changed),
self.panel.on_commit(self.committed)]
## The title shown: the tool's label as its menus name it.
pub func heading(self) -> str:
return self.title.text()
pub func is_open(self) -> bool:
return self.body.layout().is_visible()
## Show the last tool step's rows; hide without one, or while a menu
## covers the viewport (`covered`).
pub func refresh(self, covered: bool = false) -> unit!:
if not covered and self.refresh_select():
return
let found = self.last_rows()
if found == none or covered:
self.shown = ""
self.body.layout().set_visible(false)
return
let (name, rows) = found else return
let node = self.w.selected() else return
let step = node.edits[-1]
# A step a gesture tool makes for each action (Poly Pen's faces are
# PolyDraw steps, its deletions Delete steps) is titled by the tool
# chosen, as its menu names it.
var title = name
if tool_registry.gesture_of(self.w.edit_tool) == "pen":
if let chosen = tool_registry.find(self.w.edit_tool):
title = chosen.label
let key = f"{node.id}/{node.edits.length}/{step.verb}/{title}/{units.document_unit(self.w).suffix}"
if key != self.shown:
self.shown = key
self.rows = rows
let specs: list[ParameterSpec] = []
let shown_unit = units.document_unit(self.w)
for row in self.rows:
specs.append(row.spec(shown_unit))
self.panel.set_specs(specs)
self.title.set_text(title)
self.step_tool = name
# The step's own failure, under its rows (the node says it too).
let failed = node.edits.length > 0 and node.warning.starts_with(node.edits[-1].verb + " ") and edit_types.step_failure(node.warning)
# A structure step that was built says what it did (how far a
# Rebuild moved the surface) where a failure would.
let told = not failed and step.verb in edit_cad_nets.structure_verbs() and not node.computing
self.problem.set_text(edit_types.step_reason(node.warning) if failed else (node.summary if told else ""))
self.problem.layout().set_visible(failed or (told and node.summary != ""))
for index in 0..<self.rows.length:
let row = self.rows[index]
if row.value_ramp():
if row.slot + row.components <= step.numbers.length:
let data = ramp_numbers(step.numbers[row.slot..<row.slot + row.components])
if data.length > 0 and self.panel.ramp(index) != data:
self.panel.set_ramp(index, data)
continue
if not row.valued():
continue
for component in 0..<row.components:
let slot = row.slot + component
if slot < step.numbers.length:
self.panel.set_component(index, component, step.numbers[slot])
self.body.layout().set_visible(true)
## The Select tool run last, while the selection is still the one it
## made: its rows, a change running it again (Blender's Adjust Last
## Operation). Whether it shows.
func refresh_select(self) -> bool!:
let run = self.w.last_select else return false
if not run.current(self.w.selection) or run.tool.rows.length == 0 or self.w.sketching():
return false
let key = f"select/{run.tool.name}/{units.document_unit(self.w).suffix}"
if key != self.shown:
self.shown = key
self.rows = run.tool.rows
let specs: list[ParameterSpec] = []
let shown_unit = units.document_unit(self.w)
for row in self.rows:
specs.append(row.spec(shown_unit))
self.panel.set_specs(specs)
self.title.set_text(run.tool.label)
self.step_tool = run.tool.name
self.problem.layout().set_visible(false)
for index in 0..<self.rows.length:
let row = self.rows[index]
if not row.valued():
continue
for component in 0..<row.components:
let slot = row.slot + component
if slot < run.numbers.length:
self.panel.set_component(index, component, run.numbers[slot])
self.body.layout().set_visible(true)
return true
## Keep it centered along the bottom of the viewport at `area` (the
## body's coordinates), its bottom 14 points above the viewport's; a
## tall one (an option window as long as Align's, over a third of the
## viewport) at the left instead, past the tool strip, so the model in
## the middle stays in sight, as Fusion's dialogs stand at the side. Its
## size as last laid out (its least before then). Called every frame:
## it moves only when the viewport or its size changed.
## Where the bar was placed last (tests).
pub func placed_at(self) -> list[float]:
return self.placed
pub func place(self, area: Rect) -> unit!:
if not self.is_open():
return
let size = self.body.layout().bounds()
let width = size.width if size.width > 0.0 else bar_width
let height = size.height if size.height > 0.0 else 52.0
let tall = height > area.height * 0.35
# (Tall, it stands clear of the prompt and the snap button below it.)
let point = [area.x + 64.0 if tall else area.x + (area.width - width) * 0.5, area.y + area.height - height - (74.0 if tall else 14.0)]
if point != self.placed:
self.placed = point
self.body.layout().set_popup_point(point[0], point[1])
## The name and rows of the displayed Edit node's last step, when it has
## rows: its tool's, or a command's step's node rows.
func last_rows(self) -> (str, list[Parameter])?!:
if not self.w.editing() or self.w.sketching():
return none
let node = self.w.selected() else return none
let kind = self.w.active_kind() else return none
if node.edits.length == 0:
return none
let verb = node.edits[-1].verb
for tool in kind.tools:
if tool.verb != verb:
continue
let rows = tool.rows else node_catalog.tool_rows(verb)
return (tool.name, rows) if rows.length > 0 else none
# A command's feature step (a sketch's step has no rows here: its
# plane is set when it is made).
if verb in kind.steps and verb == "Sketch Plane":
let rows = node_catalog.tool_rows(verb)
return (verb, rows) if rows.length > 0 else none
return none
## Row `index`'s change is one undo step; then what it may start.
func committed(self, index: int) -> unit!:
self.w.commit()
self.picks_after()
## The step's numbers as a change began.
func began(self, index: int) -> unit!:
# A scrub previews as a viewport drag does; let go, it cooks in full.
self.w.begin_preview("Change " + self.title.text())
self.before = []
if let node = self.w.selected():
if node.edits.length > 0:
self.before = node.edits[-1].numbers.copy()
## After a row was set: a pick of the step's tool the change made
## wanted waits for it (Fusion's selection fields: an Extrude's extent
## turned To object waits for its face, a Move turned Point to point for
## its From point).
func picks_after(self) -> unit!:
let node = self.w.selected() else return
if node.edits.length == 0 or self.before.length == 0:
return
let step = node.edits[-1]
let kind = self.w.active_kind() else return
let edit = kind.tool(self.step_tool) else return
let tool = edit.tool else return
let texts: list[str] = []
for k in 0..<5:
texts.append(step.text(k))
for at in tool.picks_of(true):
let pick = tool.picks[at]
if pick.step < 0:
continue
let empty = pick.kind == pick_kinds.point or step.text(pick.step).trim() == ""
if empty and pick.wanted(step.numbers, texts, false) and not pick.wanted(self.before, texts, false):
tool_picks.ask(self.w, node, tool, at)
return
func changed(self, index: int) -> unit!:
let row = self.rows[index]
if row.value_ramp() and not self.shown.starts_with("select/"):
modeling_commands.set_last_step_values(self.w, row.slot, padded(self.panel.ramp(index), row.components))
return
if not row.valued():
return
if self.shown.starts_with("select/"):
for component in 0..<row.components:
selection_commands.set_last_select(self.w, row.slot + component, self.panel.component(index, component), row.minimum, row.maximum)
return
# The row's components together: one run of the step.
let values: list[float] = []
for component in 0..<row.components:
values.append(math.max(row.minimum, math.min(row.maximum, self.panel.component(index, component))))
if row.components == 1:
modeling_commands.set_last_step(self.w, row.slot, values[0], row.minimum, row.maximum)
else:
modeling_commands.set_last_step_values(self.w, row.slot, values)
pub func close(self):
for connection in self.connections:
connection.disconnect()
self.connections.clear()