Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / src / sketch_profiles.luc

## A sketch's profiles in the editor, Fusion's way: every closed region
## shaded light, the one under the pointer lit, the picked ones in the
## accent. At the Shapes and Smart levels a click inside a region, off its
## curves, picks its profile (Shift adds or takes it away); the picked keys
## are the workspace's `sketch_profiles`, which Extrude takes
## (cad_commands.extrude_profiles). The curves, their keys and fills are
## made again only when the sketch or its plane changes, and with them the
## regions laid out for pointing (luce-cad's CadProfileMap: hovering is a
## containment test, not an arrangement made again per move); fills come
## from profile_fills; profiles are kept by key so one holds as dimensions
## change.
from luce_cad.sketch import CadSketch
from luce_cad.modeling import CadModeling, CadProfileMap
from luce_geocore.geocore import GeometrySet, Mesh
from luce_3d.graphics import Camera, Vector3
from luce_gpu.gpu import RenderTarget
from sketch_node import SketchFrame
from profile_shading import ProfileShading
import theme

pub class SketchProfiles:
    ## The curves as of `made_from` (the sketch's text and plane), every
    ## profile's key, and the fill of every profile.
    var curves: GeometrySet? = none
    var made_from: str = ""
    pub var keys: list[str]
    var every: Mesh? = none
    ## The regions laid out for key_at.
    var layout: CadProfileMap? = none
    ## The fills of the picked and the hovered profiles, as of their keys.
    var picked_fill: Mesh? = none
    var picked_keys: str = ""
    var hover_fill: Mesh? = none
    var hover_key: str = ""
    ## The profile under the pointer ("": none).
    pub var hover: str = ""

    pub func init(self):
        self.keys = []

    ## The curves of `sketch` on plane `f`, made again when either changed.
    pub func curves_of(self, sketch: CadSketch, f: SketchFrame) -> GeometrySet!:
        let key = f"{sketch.text()}|{f.key()}"
        if key != self.made_from or self.curves == none:
            let made = sketch.curves(f.origin, f.u, f.v)
            self.curves = made
            self.made_from = key
            # A sketch without curves has no profiles.
            let text = CadModeling.profile_keys(made) if sketch.entity_count() > 0 else ""
            self.keys = text.split(";") if text != "" else []
            self.every = CadModeling.profile_fills(made) if self.keys.length > 0 else none
            self.layout = CadProfileMap.of(made) if self.keys.length > 0 else none
            self.picked_keys = "?"
            self.hover_key = "?"
        return self.curves else trap("curves")

    ## The key of the profile at plane point (`x`, `y`) of `sketch` on `f`,
    ## or "".
    pub func key_at(self, sketch: CadSketch, f: SketchFrame, x: float, y: float) -> str!:
        if sketch.entity_count() == 0:
            return ""
        _ = self.curves_of(sketch, f)
        let laid = self.layout else return ""
        return laid.key_at(f.at(x, y))

    ## `picked` without the keys `sketch` no longer has.
    pub func still_there(self, sketch: CadSketch, f: SketchFrame, picked: list[str]) -> list[str]!:
        _ = self.curves_of(sketch, f)
        let kept: list[str] = []
        for key in picked:
            if key in self.keys:
                kept.append(key)
        return kept

    ## The shading, before the curves: every profile, then the picked ones,
    ## then the one under the pointer; lifted by `lift` toward the eye (off
    ## the bodies under the plane) as it is drawn, so the profiles made on
    ## plane `f`, which pointing uses too, are not made again for the lift.
    pub func draw(self, target: RenderTarget, camera: Camera, sketch: CadSketch, f: SketchFrame, picked: list[str], width: float, height: float, raised: Vector3? = none) -> unit!:
        let lift = raised else Vector3()
        let curves = self.curves_of(sketch, f)
        let all = self.every else return
        ProfileShading.draw(target, all, camera, width, height, theme.sketch_free(), 0.22, lift)
        let chosen = picked.join(";")
        if chosen != self.picked_keys:
            self.picked_keys = chosen
            self.picked_fill = CadModeling.profile_fills(curves, chosen) if chosen != "" else none
        if let fill = self.picked_fill:
            ProfileShading.draw(target, fill, camera, width, height, theme.accent(), 0.35, lift)
        if self.hover != self.hover_key:
            self.hover_key = self.hover
            self.hover_fill = CadModeling.profile_fills(curves, self.hover) if self.hover != "" else none
        if let fill = self.hover_fill:
            ProfileShading.draw(target, fill, camera, width, height, theme.grey(235.0), 0.18, lift)