dymokomi/luced-3d
Source of release 0.11.3
0.11.3 / src / sketch_view.luc
## What the Sketch editor draws (sketch_editor.luc): its curves as line
## batches on the plane (made again only when the sketch changes), what is
## hovered, selected or picked and what is being drawn on top; then, flat
## on the screen, its points, constraint glyphs and dimension labels. Its
## tools are on the marking menus (A, S, D: marking_items.luc), its
## Points / Lines / Shapes levels on the viewport's level buttons.
from luce_gpu.gpu import RenderTarget, Rect, Color
from luce_ui.ui import Painter
from overlay_lines import OverlayLines
from view_grid import draw_grid
from workspace import Workspace
from sketch_editor import SketchEditor
import sketch_modify
import sketch_create
from view_projection import project
import sketch_kinds as names
import sketch_tools
from luce_cad.sketch import CadSketch
import units
from snapping import grid_step
import theme
from luce_std import math
pub class SketchView:
let w: Workspace
pub let editor: SketchEditor
## The curve batches as of editor revision `drawn`.
var drawn: int = -1
var batches: list[OverlayLines]
pub func init(self, w: Workspace):
self.w = w
self.editor = SketchEditor(w)
self.editor.on_modify = self.modify
self.editor.on_finish = self.finish
self.batches = []
## A drawing tool's clicks are in.
func finish(self) -> unit!:
sketch_create.finish(self.editor)
## A MODIFY tool's click on the editor (Shift held: `shift`).
func modify(self, shift: bool) -> unit!:
sketch_modify.modify_click(self.editor, shift)
## The curves, in the 3D pass: free, fully constrained and construction
## ones, then the highlighted and what is being drawn.
pub func draw_lines(self, target: RenderTarget) -> unit!:
let e = self.editor
if not e.active():
return
e.sync()
let sketch = e.sketch else return
let f = e.frame
if self.drawn != e.revision:
self.batches = []
for which in 0..<3:
let lines = OverlayLines.make()
lines.add_sketch(sketch, f.origin, f.u, f.v, which)
self.batches.append(lines)
self.drawn = e.revision
let camera = self.w.view.camera
self.grid(target)
# Project: the input body's edges cast on the plane, the one under
# the cursor brighter.
if e.tool == "Project":
if let cast = e.projection():
let all = OverlayLines.make()
all.add_projection(cast, f.origin, f.u, f.v)
if all.count() > 0:
all.batch().draw(camera, target, theme.grey(110.0), 1.0)
let under = e.projected_edge()
if under >= 0:
let one = OverlayLines.make()
one.add_projection(cast, f.origin, f.u, f.v, under)
one.batch().draw(camera, target, theme.accent(), 2.0)
let colors = [theme.sketch_free(), theme.sketch_fixed(), theme.sketch_construction()]
for which in 0..<3:
if self.batches[which].count() > 0:
self.batches[which].batch().draw(camera, target, colors[which], 1.5)
let chosen = OverlayLines.make()
for at in e.selection + e.picks:
chosen.add_sketch(sketch, f.origin, f.u, f.v, 3, at)
if chosen.count() > 0:
chosen.batch().draw(camera, target, theme.accent(), 2.5)
self.modify_preview(target)
let hovered = OverlayLines.make()
if e.hover >= 0 and e.tool != "Trim":
hovered.add_sketch(sketch, f.origin, f.u, f.v, 3, e.hover)
self.rubber_band(hovered)
if hovered.count() > 0:
hovered.batch().draw(camera, target, theme.grey(245.0), 2.0)
## The sketch plane's grid around where the camera looks: a line every
## 1, 2 or 5 × 10ⁿ mm, about a tenth of the view's distance apart, a
## brighter one every fifth.
func grid(self, target: RenderTarget) -> unit!:
let f = self.editor.frame
let view = self.w.view
# The snapping grid's step (snapping.luc).
let step = grid_step(view.distance)
let at = f.coordinates(view.target)
let major = step * 5.0
let center = f.at(math.round(at[0] / major) * major, math.round(at[1] / major) * major)
draw_grid(view.camera, target, center, f.u, f.v, step, theme.grey(48.0), theme.grey(70.0))
## What a drawing tool would make with the cursor where it is.
func rubber_band(self, lines: OverlayLines) -> unit!:
if sketch_tools.draws(self.editor.tool):
sketch_create.preview(self.editor, lines)
## What Trim would take, or Offset make, with the cursor where it is.
func modify_preview(self, target: RenderTarget) -> unit!:
let e = self.editor
let sketch = e.sketch else return
let f = e.frame
let lines = OverlayLines.make()
if e.tool == "Trim" and e.hover >= 0 and sketch.kind(e.hover) in [names.line_kind, names.circle_kind, names.arc_kind]:
lines.add_trim_piece(sketch, e.hover, e.cursor[0], e.cursor[1], f.origin, f.u, f.v) catch failure:
return
if lines.count() > 0:
lines.batch().draw(self.w.view.camera, target, theme.warning(), 3.0)
elif e.tool == "Offset" and e.picks.length > 0:
let distance = sketch_modify.offset_distance(e)
if distance > 0.0:
lines.add_offset_preview(sketch, e.picks, distance, e.cursor[0], e.cursor[1], f.origin, f.u, f.v) catch failure:
return
if lines.count() > 0:
lines.batch().draw(self.w.view.camera, target, theme.grey(200.0), 1.5)
## The flat layer: points, constraint glyphs and dimensions.
pub func draw(self, p: Painter, width: float, height: float) -> unit!:
let e = self.editor
if not e.active():
return
e.sync()
let sketch = e.sketch else return
let f = e.frame
for at in 0..<sketch.entity_count():
if sketch.kind(at) != names.point_kind:
continue
let s = project(self.w, f.at(sketch.x(at), sketch.y(at)), width, height)
if s[2] < 0.0:
continue
let chosen = at in e.selection or at in e.picks
let color = theme.accent() if chosen else (theme.sketch_fixed() if sketch.fixed(at) else theme.sketch_free())
let size = 7.0 if at == e.hover or chosen else 5.0
p.rectangle(Rect(s[0] - size * 0.5, s[1] - size * 0.5, size, size), color)
if e.snap >= 0 and sketch.kind(e.snap) == names.point_kind:
let s = project(self.w, f.at(e.cursor[0], e.cursor[1]), width, height)
outline(p, Rect(s[0] - 7.0, s[1] - 7.0, 14.0, 14.0), theme.accent())
self.glyphs(p, width, height)
## Each constraint's glyph beside what it holds, each dimension's value
## (driven ones in parentheses).
func glyphs(self, p: Painter, width: float, height: float) -> unit!:
let e = self.editor
let sketch = e.sketch else return
let f = e.frame
let shown_unit = units.document_unit(self.w)
for at in 0..<sketch.constraint_count():
let kind = sketch.constraint_kind(at)
let first = sketch.constraint_entity(at, 0)
let second = sketch.constraint_entity(at, 1)
var label = ""
var where = [0.0, 0.0]
if kind in [names.distance, names.line_distance, names.horizontal_distance, names.vertical_distance]:
where = middle(sketch, first, second)
label = units.length_text(sketch.constraint_value(at), shown_unit)
elif kind == names.radius or kind == names.diameter:
let c = sketch.part(first, 0)
let r = sketch.radius(first)
where = [sketch.x(c) + r * 0.7071, sketch.y(c) + r * 0.7071]
label = ("R " if kind == names.radius else "Ø ") + units.length_text(sketch.constraint_value(at), shown_unit)
elif kind == names.angle:
where = middle(sketch, first, -1)
label = units.angle_text(math.abs(sketch.constraint_value(at)))
else:
label = glyph(kind)
if label == "":
continue
where = middle(sketch, first, -1)
if sketch.driven(at):
label = f"({label})"
let s = project(self.w, f.at(where[0], where[1]), width, height)
if s[2] < 0.0:
continue
let dimension = names.is_dimension(kind)
if dimension:
p.rounded_rectangle(Rect(s[0] - 4.0, s[1] - 22.0, float(label.length) * 8.0 + 8.0, 20.0), theme.surface(), 3.0)
p.text(label, s[0], s[1] - 20.0, theme.text())
else:
p.rounded_rectangle(Rect(s[0] + 6.0, s[1] + 4.0, float(label.length) * 8.0 + 6.0, 18.0), theme.grey(60.0), 3.0)
p.text(label, s[0] + 9.0, s[1] + 5.0, theme.text_muted())
## Where a constraint on entities `first` (and `second`) shows: a line's
## middle, a curve's center offset, a point, or between two points.
func middle(sketch: CadSketch, first: int, second: int) -> list[float]:
let a = place_of(sketch, first)
if second < 0:
return a
let b = place_of(sketch, second)
return [(a[0] + b[0]) * 0.5, (a[1] + b[1]) * 0.5]
func place_of(sketch: CadSketch, at: int) -> list[float]:
let kind = sketch.kind(at)
if kind == names.point_kind:
return [sketch.x(at), sketch.y(at)]
if kind == names.line_kind:
let a = sketch.part(at, 0)
let b = sketch.part(at, 1)
return [(sketch.x(a) + sketch.x(b)) * 0.5, (sketch.y(a) + sketch.y(b)) * 0.5]
if kind == names.spline_kind or kind == names.conic_kind or kind == names.text_kind:
# Its first point (a text's anchor).
let first = sketch.part(at, 0)
return [sketch.x(first), sketch.y(first)]
let c = sketch.part(at, 0)
let r = sketch.radius(at)
return [sketch.x(c), sketch.y(c) + r]
## A constraint's glyph (none for those drawn by the points themselves).
func glyph(kind: int) -> str:
if kind == names.horizontal:
return "H"
if kind == names.vertical:
return "V"
if kind == names.parallel:
return "//"
if kind == names.perpendicular:
return "⊥"
if kind == names.tangent:
return "T"
if kind == names.equal:
return "="
if kind == names.fix:
return "Fix"
if kind == names.midpoint:
return "Mid"
if kind == names.concentric:
return "◎"
if kind == names.collinear:
return "Col"
if kind == names.symmetric:
return "Sym"
return ""
## A rectangle's outline.
func outline(p: Painter, r: Rect, color: Color) -> unit!:
p.rectangle(Rect(r.x, r.y, r.width, 1.5), color)
p.rectangle(Rect(r.x, r.y + r.height - 1.5, r.width, 1.5), color)
p.rectangle(Rect(r.x, r.y, 1.5, r.height), color)
p.rectangle(Rect(r.x + r.width - 1.5, r.y, 1.5, r.height), color)