dymokomi/luced-3d
Source of release 0.11.6
0.11.6 / src / viewport_chrome.luc
## The viewport's chrome, drawn over the scene and keeping its clicks
## from every tool: the view buttons (top right; the shading button left
## of them, view.luc's), the selection levels (top left, 1-5) and beside
## them whether boxes select through the depth (V), the transform tools
## (left center, Q, W, E, R, T; Select alone while nothing is selected)
## with the tool in use below them, a tool prompt's hint, the snapping
## button and its options (bottom left), the tool shelf's button beside it
## (where the context has a shelf: tool_shelf.luc), and where the pointer
## snapped.
##
## With four panes it is docked: drawn once over the whole viewport (as
## Maya's and Blender's one tool palette), in the viewport's coordinates
## (button_at's `dock`), its view buttons beside the levels acting on every
## pane, and the context display button with them; each pane keeps only
## its own view's controls (the shading button, the camera menu and the
## navigation gizmo, view.luc's).
from luce_ui.ui import Painter, Point
from luce_gpu.gpu import Rect, Color
from workspace import Workspace
from actions import Actions, context_icon
from luce_std import math
import view_projection as gm
import theme
from luce_ui import ui
from tool_icons import draw_icon
import sketch_tools
from edit_types import smart_level, level_names, level_icons, sketch_level_names
from tools import transform_tools, transform_key_names
from snapping import snap_names, snap_icons
from tool_prompt import hint
import component_moves
import edit_cad_nets
import node_commands
from shelf_state import ShelfState
from shelf_context import context_of
pub class ViewportChrome:
let workspace: Workspace
let actions: Actions
## The button under the pointer: 100 + a view button, 200 + a level,
## 300 + a transform tool (the slot below them: 300 + their count), 400
## the snapping button, 410 + one of its options, 500 through the
## depth, 600 + an Edit CAD CV move mode, 620 + a CV falloff, 700 the
## tool shelf's button (-1: none).
pub var hover_tool: int = -1
## The snapping options shown (the snapping button opened them).
pub var snaps_open: bool = false
## Pick a tool again from the column (the sketch's tools are the
## editor's): the viewport sets it.
pub var on_reuse: func(str) -> unit!? = none
## Whether the sketch edited has entities selected (the view says; in
## a sketch the transform tools show only then).
pub var sketch_selected: (func() -> bool)? = none
## The tool shelf: open or not, its tab per context (its button here
## opens and closes it; the shelf, the viewport panel's footer, reads it).
pub let shelf: ShelfState
## The shelf opened or closed: the editor shows or hides it.
pub var on_shelf: func() -> unit!? = none
pub func init(self, workspace: Workspace, actions: Actions):
self.workspace = workspace
self.actions = actions
self.shelf = ShelfState()
## Whether the viewport's context has a tool shelf (its button shows).
pub func shelved(self) -> bool:
return context_of(self.workspace) != ""
## Open the tool shelf, or close it.
pub func toggle_shelf(self) -> unit!:
self.shelf.open = not self.shelf.open
if let changed = self.on_shelf:
changed()
## Whether the chrome is docked over four panes (see the top).
pub func docked(self) -> bool:
return self.workspace.panes.four
## The view buttons, top right (docked, beside the levels): Frame, Grid
## and Control hulls; then the switchers, the prompt and snapping.
pub func draw(self, painter: Painter, width: float, height: float) -> unit!:
let w = self.workspace
let docked = self.docked()
let names = ["Frame", "Grid", "Hulls"]
let tips = ["Frame every pane (F: the pane under the pointer)", "Grid in every pane (G: the pane under the pointer)", "Toggle control hulls"] if docked else ["Frame geometry (F)", "Toggle grid (G)", "Toggle control hulls"]
var grids = false
for view in w.panes.views:
grids = grids or view.grid_visible
let shown = [false, grids if docked else w.view.grid_visible, w.display.hulls_visible]
for index in 0..<names.length:
let x = self.view_button_x(index, width)
painter.rounded_rectangle(Rect(x, 8.0, 32.0, 30.0), theme.surface(), 4.0)
draw_icon(painter, names[index], x + 6.0, 13.0, 20.0, theme.accent() if self.hover_tool == index + 100 or shown[index] else theme.text())
if self.hover_tool == index + 100:
if docked:
tip(painter, tips[index], x, 44.0)
else:
painter.rounded_rectangle(Rect(width - 196.0, 43.0, 180.0, 30.0), theme.surface(), 4.0)
painter.text(tips[index], width - 188.0, 49.0, theme.text())
self.draw_switchers(painter, width, height)
self.draw_cv_modes(painter)
## The left of view button `index` (Frame, Grid, Hulls) in a viewport
## `width` wide: top right, or docked after the context display button.
func view_button_x(self, index: int, width: float) -> float:
if self.docked():
return self.dock_eye().x + float(index + 1) * 38.0
return width - view_buttons_width() + float(index) * 38.0
## Where the levels' row ends: past the levels and the through-the-depth
## button.
func row_end(self) -> float:
let shown = self.workspace.shown_levels()
return level_x(shown.length) + (38.0 if self.actions.through_depth.is_enabled() else 0.0)
## Docked, the context display button: first of the view buttons, after
## the levels' row.
pub func dock_eye(self) -> Rect:
return Rect(self.row_end() + 10.0, 8.0, 32.0, 30.0)
## Under the levels at Edit CAD's CVs, Alias's Transform CV modes and
## the falloff kinds (the Edit node's CV move and CV falloff), the
## chosen ones lit.
func draw_cv_modes(self, painter: Painter) -> unit!:
for (button, label, x, on) in self.cv_buttons():
let r = Rect(x, cv_row_y, float(label.length) * 7.5 + 14.0, 24.0)
painter.rounded_rectangle(r, theme.accent() if on else theme.surface(), 4.0)
painter.text(label, x + 7.0, cv_row_y + 4.0, theme.grey(0.0) if on else (theme.accent() if self.hover_tool == button else theme.text()))
## The CV row's buttons (see hover_tool), their labels, where they
## start and whether they are chosen; none away from Edit CAD's CVs.
func cv_buttons(self) -> list[(int, str, float, bool)]!:
let w = self.workspace
let result: list[(int, str, float, bool)] = []
if not component_moves.at_cvs(w):
return result
let node = w.selected() else return result
if node.values.length <= edit_cad_nets.hull_pick_slot:
return result
var x = 16.0
let modes = ["World", "NUV", "Slide", "Project", "Parallel", "View"]
for index in 0..<modes.length:
result.append((600 + index, modes[index], x, int(node.values[edit_cad_nets.move_mode_slot]) == index))
x += float(modes[index].length) * 7.5 + 18.0
x += 12.0
let falloffs = ["Radius", "Rows", "Hull"]
for index in 0..<falloffs.length:
result.append((620 + index, falloffs[index], x, int(node.values[edit_cad_nets.falloff_kind_slot]) == index))
x += float(falloffs[index].length) * 7.5 + 18.0
return result
## Top left, the selection levels (1-5); left center, the transform
## tools (Q, W, E, R, T); a tool prompt's hint at the bottom.
func draw_switchers(self, painter: Painter, width: float, height: float) -> unit!:
let w = self.workspace
let levels = w.levels()
let sketching = w.sketching()
let shown = w.shown_levels()
for slot in 0..<shown.length:
let index = shown[slot]
let x = level_x(slot)
let on = index == (w.sketch_level if sketching else w.component_mode)
# A level the kind does not offer now draws pale; its tooltip
# still shows, and a click does nothing (its command is off).
let available = self.actions.modes[index].is_enabled()
painter.rounded_rectangle(Rect(x, 8.0, 32.0, 30.0), theme.accent() if on else theme.surface(), 4.0)
let icon = levels[index].icon if not sketching and index < levels.length and levels[index].available else level_icons()[index]
let ink = theme.grey(0.0) if on else (theme.accent() if self.hover_tool == 200 + index and available else (theme.text() if available else theme.disabled(theme.text())))
ui.draw_icon(painter.target(), icon, Rect(x + 5.0, 12.0, 22.0, 22.0), ink)
if self.hover_tool == 200 + index:
tip(painter, f"{self.level_label(index)} ({index + 1})", x, 44.0)
# Beside them, lit while boxes select through the depth (V).
var caption_x = (self.view_button_x(3, 0.0) if self.docked() else level_x(shown.length)) + 4.0
if self.actions.through_depth.is_enabled():
let x = level_x(shown.length)
let through = self.actions.through_depth.is_checked()
painter.rounded_rectangle(Rect(x, 8.0, 32.0, 30.0), theme.accent() if through else theme.surface(), 4.0)
ui.draw_icon(painter.target(), "x-ray", Rect(x + 5.0, 12.0, 22.0, 22.0), theme.grey(0.0) if through else (theme.accent() if self.hover_tool == 500 else theme.text()))
if self.hover_tool == 500:
tip(painter, ("Selecting through the depth" if through else "Selecting what shows") + " (V)", x, 44.0)
if not self.docked():
caption_x += 38.0
var caption = ""
if sketching:
caption = sketch_level_names()[w.sketch_level]
else:
# A prompt's filter shows what alone is picked while it waits.
let filtered = w.prompt_level() >= 0
caption = w.level().name + (" only" if filtered else (" (Smart)" if w.component_mode == smart_level else ""))
caption = caption + (f" / {w.selected_entities()} selected" if w.selected_entities() > 0 else "")
if self.docked():
# Docked, the network's path follows (no pane has it), as far
# as the top left pane has room left of its camera menu.
let room = (width - 2.0) * 0.5 - 200.0 - caption_x
let where = caption + " " + w.network.path()
caption = where if float(where.length) * 7.5 <= room else (caption if float(caption.length) * 7.5 <= room else "")
painter.text(caption, caption_x, 15.0, theme.text_muted())
let tools = transform_tools()
let keys = transform_key_names()
let current = self.current_tool()
let shown_tools = self.shown_tools()
for slot in 0..<shown_tools.length:
let index = shown_tools[slot]
let y = tools_top(height) + float(slot) * 36.0
let on = current == tools[index]
# In a sketch only Select is there (its own tools move things);
# a tool with nothing to act on draws pale and takes no click.
let usable = index == 0 if sketching else self.actions.tools[index].is_enabled()
painter.rounded_rectangle(Rect(16.0, y, 32.0, 30.0), theme.accent() if on else theme.surface(), 4.0)
draw_icon(painter, tools[index], 22.0, y + 5.0, 20.0, theme.grey(0.0) if on else (theme.disabled(theme.text()) if not usable else (theme.accent() if self.hover_tool == 300 + index else theme.text())))
if self.hover_tool == 300 + index:
tip(painter, f"{tools[index]} ({keys[index]})", 54.0, y)
# Below them, the tool in use (yellow), or the last one (to pick
# again).
let last = self.last_tool()
let y = tools_top(height) + float(shown_tools.length) * 36.0 + 6.0
let active = last != "" and current == last
# No slot until there is a tool to show in it.
if last != "":
painter.rounded_rectangle(Rect(16.0, y, 32.0, 30.0), theme.accent() if active else theme.surface(), 4.0)
ui.draw_icon(painter.target(), tool_asset(self.workspace, last), Rect(22.0, y + 5.0, 20.0, 20.0), theme.grey(0.0) if active else (theme.accent() if self.hover_tool == 300 + tools.length else theme.text()))
if self.hover_tool == 300 + tools.length:
tip(painter, last if active else f"{last} (again)", 54.0, y)
if let prompt = w.prompt:
let said = hint(prompt)
painter.rounded_rectangle(Rect(16.0, height - 78.0, float(said.length) * 7.5 + 20.0, 28.0), theme.surface(), 4.0)
painter.text(said, 24.0, height - 73.0, theme.accent())
# Bottom left, snapping (snapping.luc): one button (lit while on),
# its options above it when open.
let snapping_on = w.snaps.on()
painter.rounded_rectangle(Rect(16.0, height - 40.0, 32.0, 30.0), theme.accent() if snapping_on else theme.surface(), 4.0)
ui.draw_icon(painter.target(), "snap", Rect(22.0, height - 35.0, 20.0, 20.0), theme.grey(0.0) if snapping_on else (theme.accent() if self.hover_tool == 400 else theme.text()))
if self.hover_tool == 400 and not self.snaps_open:
tip(painter, "Snapping (Cmd held: free)", 54.0, height - 40.0)
# Beside it, the tool shelf's button, lit while the shelf is open.
if self.shelved():
let open = self.shelf.open
painter.rounded_rectangle(Rect(shelf_x, height - 40.0, 32.0, 30.0), theme.accent() if open else theme.surface(), 4.0)
ui.draw_icon(painter.target(), "tool-shelf", Rect(shelf_x + 6.0, height - 35.0, 20.0, 20.0), theme.grey(0.0) if open else (theme.accent() if self.hover_tool == 700 else theme.text()))
if self.hover_tool == 700 and not self.snaps_open:
tip(painter, "Close the tool shelf (Esc)" if open else "Tool shelf", shelf_x + 38.0, height - 40.0)
if self.snaps_open:
let names = snap_names()
let icons = snap_icons()
let top = snap_menu_top(height, names.length)
painter.rounded_rectangle(Rect(16.0, top - 4.0, snap_menu_width, float(names.length) * snap_row + 8.0), theme.surface(), 4.0)
for index in 0..<names.length:
let row = top + float(index) * snap_row
let on = w.snaps.flags[index]
if self.hover_tool == 410 + index:
painter.rectangle(Rect(20.0, row, snap_menu_width - 8.0, snap_row - 2.0), theme.grey(72.0))
painter.rounded_rectangle(Rect(26.0, row + 6.0, 16.0, 16.0), theme.accent() if on else theme.grey(60.0), 3.0)
ui.draw_icon(painter.target(), icons[index], Rect(50.0, row + 4.0, 20.0, 20.0), theme.text() if on and (index == 0 or snapping_on) else theme.text_muted())
painter.text(names[index] if index > 0 else "Snapping", 78.0, row + 6.0, theme.text() if on and (index == 0 or snapping_on) else theme.text_muted())
## Where the pointer snapped (the workspace's snap mark): a marker by
## its kind, and its name.
pub func draw_snap(self, painter: Painter, width: float, height: float) -> unit!:
let mark = self.workspace.snap_mark else return
let p = gm.project(self.workspace, mark.at, width, height)
if p[2] <= 0.0:
return
let ink = theme.accent()
let (x, y) = (p[0], p[1])
if mark.kind == "Point":
outline(painter, Rect(x - 5.0, y - 5.0, 10.0, 10.0), ink)
elif mark.kind == "Middle":
painter.line(Point(x - 6.0, y + 5.0), Point(x + 6.0, y + 5.0), 1.5, ink)
painter.line(Point(x + 6.0, y + 5.0), Point(x, y - 6.0), 1.5, ink)
painter.line(Point(x, y - 6.0), Point(x - 6.0, y + 5.0), 1.5, ink)
elif mark.kind == "Crossing":
painter.line(Point(x - 6.0, y - 6.0), Point(x + 6.0, y + 6.0), 1.5, ink)
painter.line(Point(x - 6.0, y + 6.0), Point(x + 6.0, y - 6.0), 1.5, ink)
elif mark.kind == "Curve" or mark.kind == "Surface":
painter.line(Point(x - 6.0, y), Point(x, y - 6.0), 1.5, ink)
painter.line(Point(x, y - 6.0), Point(x + 6.0, y), 1.5, ink)
painter.line(Point(x + 6.0, y), Point(x, y + 6.0), 1.5, ink)
painter.line(Point(x, y + 6.0), Point(x - 6.0, y), 1.5, ink)
else:
painter.line(Point(x - 5.0, y), Point(x + 5.0, y), 1.5, ink)
painter.line(Point(x, y - 5.0), Point(x, y + 5.0), 1.5, ink)
painter.text(mark.kind, x + 10.0, y + 6.0, theme.text_muted())
## Level `index`'s name in the current kind (Vertices, CVs), or its
## universal name when the kind lacks it.
func level_label(self, index: int) -> str:
if self.workspace.sketching():
return sketch_level_names()[index] + (" (not in a sketch)" if index == 0 else "")
let levels = self.workspace.levels()
if index < levels.length and levels[index].available:
if self.workspace.network.context() == "cad" and index == 2:
return "Edges · double-click for tangent chain"
return levels[index].name
return level_names()[index] + ("" if self.workspace.level_available(index) else " (not here)")
## The tool in use: the sketch editor's in a sketch, else the
## workspace's (Select, Move, …, the Knife).
func current_tool(self) -> str:
let w = self.workspace
return w.sketch_tool if w.sketching() else w.tool_in_use()
## The transform tools the column shows, as places in transform_tools():
## Select alone while nothing is selected (there is nothing to move),
## in a sketch too, else all (in a sketch only Select is usable).
func shown_tools(self) -> list[int]:
let w = self.workspace
var selected = w.transform_target()
if w.sketching():
selected = false
if let asked = self.sketch_selected:
selected = asked()
if not selected:
return [0]
let all: list[int] = []
for index in 0..<transform_tools().length:
all.append(index)
return all
## The bottom slot's tool: the one in use when it is not Q, W, E, R or
## T, else the last such ("" none yet).
func last_tool(self) -> str:
let w = self.workspace
if w.sketching():
return w.last_sketch_tool
if not (w.edit_tool in transform_tools()):
return w.edit_tool
return ""
## The button under (x, y): see hover_tool (400 the snapping button,
## 410 + one of its options); -1 in a pane while docked, and over the
## whole viewport (`dock`) while not.
pub func button_at(self, x: float, y: float, width: float, height: float, dock: bool = false) -> int:
if dock != self.docked():
return -1
if y >= cv_row_y and y <= cv_row_y + 24.0:
let buttons = self.cv_buttons() catch failure:
recover []
for (button, label, left, on) in buttons:
_ = on
if x >= left and x <= left + float(label.length) * 7.5 + 14.0:
return button
if y >= 8.0 and y <= 38.0:
for index in 0..<3:
let left = self.view_button_x(index, width)
if x >= left and x <= left + 32.0:
return 100 + index
let shown = self.workspace.shown_levels()
if y >= 8.0 and y <= 38.0 and x >= 16.0 and x < level_x(shown.length) - 6.0:
return 200 + shown[int((x - 16.0) / 38.0)]
if y >= 8.0 and y <= 38.0 and x >= level_x(shown.length) and x <= level_x(shown.length) + 32.0 and self.actions.through_depth.is_enabled():
return 500
let top = tools_top(height)
let count = transform_tools().length
let shown_tools = self.shown_tools()
if x >= 16.0 and x <= 48.0 and y >= top and y < top + float(shown_tools.length) * 36.0 - 6.0:
return 300 + shown_tools[int((y - top) / 36.0)]
if y >= height - 40.0 and y <= height - 10.0 and x >= 16.0 and x <= 48.0:
return 400
if y >= height - 40.0 and y <= height - 10.0 and x >= shelf_x and x <= shelf_x + 32.0 and self.shelved():
return 700
if self.snaps_open:
let options = snap_names().length
let rows = snap_menu_top(height, options)
if x >= 16.0 and x <= 16.0 + snap_menu_width and y >= rows and y < rows + float(options) * snap_row:
return 410 + int((y - rows) / snap_row)
let slot = top + float(shown_tools.length) * 36.0 + 6.0
if x >= 16.0 and x <= 48.0 and y >= slot and y <= slot + 30.0 and self.last_tool() != "":
return 300 + count
return -1
## A click on button `button` (see hover_tool).
pub func press(self, button: int) -> unit!:
if button == 700:
self.toggle_shelf()
elif button >= 600:
let slot = edit_cad_nets.move_mode_slot if button < 620 else edit_cad_nets.falloff_kind_slot
node_commands.set_parameter(self.workspace, slot, float(button - (600 if button < 620 else 620)))
elif button == 500:
self.actions.through_depth.trigger()
elif button >= 410:
self.workspace.snaps.toggle(button - 410)
self.workspace.revision += 1
elif button == 400:
self.snaps_open = not self.snaps_open
elif button >= 300:
let index = button - 300
let w = self.workspace
if index == transform_tools().length:
if let reuse = self.on_reuse:
reuse(self.last_tool())
elif w.sketching():
if index == 0:
if let reuse = self.on_reuse:
reuse("Select")
else:
self.actions.tools[index].trigger()
elif button >= 200:
self.actions.modes[button - 200].trigger()
elif self.docked() and button == 100:
self.workspace.frame_every_pane()
elif self.docked() and button == 101:
self.workspace.toggle_every_grid()
else:
[self.actions.frame, self.actions.grid, self.actions.hulls][button - 100].trigger()
## The top of the CV mode row, under the levels.
let cv_row_y = 46.0
## The x of level button `index` (5: just past the last).
func level_x(index: int) -> float:
return 16.0 + float(index) * 38.0
## The top of the transform tool column, centered down the viewport's left.
func tools_top(height: float) -> float:
return math.max(84.0, height * 0.5 - 90.0)
## A tooltip at (x, y).
func tip(painter: Painter, text: str, x: float, y: float) -> unit!:
painter.rounded_rectangle(Rect(x, y, float(text.length) * 7.5 + 16.0, 30.0), theme.surface(), 4.0)
painter.text(text, x + 8.0, y + 6.0, theme.text())
## Rectangle `r`'s outline, a point wide.
pub func outline(painter: Painter, r: Rect, color: Color) -> unit!:
painter.rectangle(Rect(r.x, r.y, r.width, 1.0), color)
painter.rectangle(Rect(r.x, r.y + r.height - 1.0, r.width, 1.0), color)
painter.rectangle(Rect(r.x, r.y, 1.0, r.height), color)
painter.rectangle(Rect(r.x + r.width - 1.0, r.y, 1.0, r.height), color)
## How far from the viewport's right edge its view buttons start.
pub func view_buttons_width() -> float:
return 126.0
## The shading button, left of the view buttons; among four panes
## (`compact`), alone in the top right corner, the camera menu left of it.
pub func shading_button(width: float, compact: bool = false) -> Rect:
if compact:
return Rect(width - 48.0, 8.0, 32.0, 30.0)
return Rect(width - view_buttons_width() - 38.0, 8.0, 32.0, 30.0)
## Whether (`x`, `y`) is on the shading button.
pub func on_shading_button(x: float, y: float, width: float, compact: bool = false) -> bool:
let r = shading_button(width, compact)
return x >= r.x and x <= r.x + r.width and y >= r.y and y <= r.y + r.height
## A tool's icon: a sketch tool's own, else the viewport context's tool's.
func tool_asset(w: Workspace, name: str) -> str:
let sketched = sketch_tools.icon_of(name)
return sketched if sketched != "" else context_icon(w, name)
## The tool shelf button's left, right of the snapping button.
let shelf_x = 54.0
## Where the snapping and tool shelf buttons end along the bottom.
pub func snap_row_end() -> float:
return shelf_x + 38.0
## The snapping options' rows: their height, the list's width, its top.
let snap_row = 28.0
let snap_menu_width = 170.0
func snap_menu_top(height: float, count: int) -> float:
return height - 48.0 - float(count) * snap_row