Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.1

0.11.1 / src / shading_menu.luc

## Modal viewport display flyout. The grouped square keeps its selected mode.
from luce_ui.ui import Widget, Layout, Constraints, Size, Painter, Event
from luce_gpu.gpu import RenderTarget, Rect
from luce_window.input import EventKind, Key
from workspace import Workspace
import theme
from luce_ui import ui

pub func mode_names() -> list[str]:
    return ["Wireframe", "Flat + Wireframe", "Flat", "Shaded", "Shaded + Wireframe", "Rendered (not available)", "Show normals", "Zebra", "Isophotes", "Curvature"]

pub func mode_asset(mode: int) -> str:
    return ["wireframe", "solid-wireframe", "solid-view", "material", "shaded-wireframe", "render", "surface-normal", "reflection", "light-sun", "gradient-tool"][mode]

func curvature_names() -> list[str]:
    return ["Mean", "Gaussian", "Max (k1)", "Min (k2)", "Largest |k|"]

## Fixed ranges a click steps through, in 1/unit; 0 is automatic.
func curvature_ranges() -> list[float]:
    return [0.0, 0.1, 1.0, 10.0, 100.0]

## The options shown under the modes: stripes while Zebra or Isophotes is on,
## the value and range while Curvature is.
func option_rows(workspace: Workspace) -> list[str]:
    if workspace.view.display_mode == 9:
        let range = "Auto" if workspace.view.curvature_range <= 0.0 else f"{workspace.view.curvature_range} / unit"
        return [f"Value: {curvature_names()[workspace.view.curvature_kind]}", f"Range: {range}"]
    if not (workspace.view.display_mode in [7, 8]):
        return []
    let axis = ["X", "Y", "Z", "View up"][workspace.view.zebra_axis]
    return [f"Stripes: {workspace.view.zebra_stripes}", f"Dark share: {int(workspace.view.zebra_duty * 100.0 + 0.5)}%", f"Axis: {axis}",
            "Model: Planar" if workspace.view.zebra_planar else "Model: Cylinder", "Shading: Off" if workspace.view.zebra_stripes_only else "Shading: On"]

let row: float = 35.0

pub func mode_icon(p: Painter, mode: int, x: float, y: float) -> unit!:
    ui.draw_icon(p.target(), mode_asset(mode), Rect(x, y, 24.0, 24.0), theme.text_muted() if mode == 5 else theme.text())

pub class ShadingMenu: Widget:
    let box: Layout
    let workspace: Workspace
    var hovered: int = -1
    var held: bool = false
    pub func init(self, workspace: Workspace) -> unit!:
        self.workspace = workspace
        self.box = Layout()
        self.box.set_popup(true)
        self.box.set_visible(false)
        self.box.set_focusable(true)
        self.box.set_interactive(true)
    pub func layout(self) -> Layout:
        return self.box
    pub func measure(self, available: Constraints) -> Size!:
        return Size(268.0, 12.0 + row * float(mode_names().length + option_rows(self.workspace).length))
    pub func open(self, x: float, y: float, held: bool) -> unit!:
        self.held = held
        self.hovered = -1
        self.box.set_popup_point(x, y)
        self.box.set_visible(true)
    pub func choose(self, index: int) -> unit!:
        let modes = mode_names().length
        if index >= modes:
            self.option(index - modes)
            return
        if index == 5 or index < 0:
            return
        if index == 6:
            self.workspace.toggle_normals()
        else:
            self.workspace.set_display_mode(index)
            if index in [7, 8, 9]:
                # Stay open: the options appear below.
                self.box.invalidate()
                return
        self.box.set_visible(false)

    ## Step an option; the draw state changes, nothing recooks.
    func option(self, index: int) -> unit!:
        let w = self.workspace
        if w.view.display_mode == 9:
            if index == 0:
                w.view.curvature_kind = (w.view.curvature_kind + 1) % curvature_names().length
            elif index == 1:
                let ranges = curvature_ranges()
                let at = ranges.index_of(w.view.curvature_range) else 0
                w.view.curvature_range = ranges[(at + 1) % ranges.length]
            else:
                return
            w.revision += 1
            self.box.invalidate()
            return
        if index == 0:
            w.view.zebra_stripes = w.view.zebra_stripes * 2 if w.view.zebra_stripes < 64 else 8
        elif index == 1:
            w.view.zebra_duty = w.view.zebra_duty + 0.1 if w.view.zebra_duty < 0.85 else 0.1
        elif index == 2:
            w.view.zebra_axis = (w.view.zebra_axis + 1) % 4
        elif index == 3:
            w.view.zebra_planar = not w.view.zebra_planar
        elif index == 4:
            w.view.zebra_stripes_only = not w.view.zebra_stripes_only
        else:
            return
        w.revision += 1
        self.box.invalidate()
    pub func draw(self, target: RenderTarget) -> unit!:
        let p = Painter(target)
        p.rectangle(Rect(0.0, 0.0, target.size().width, target.size().height), theme.surface())
        let names = mode_names()
        for index in 0..<names.length:
            let y = 6.0 + float(index) * row
            let selected = index == self.workspace.view.display_mode or (index == 6 and self.workspace.view.normals_visible)
            if self.hovered == index and index != 5:
                p.rectangle(Rect(4.0, y, 260.0, 34.0), theme.grey(72.0))
            if selected:
                p.rectangle(Rect(4.0, y, 3.0, 34.0), theme.accent())
            mode_icon(p, index, 12.0, y + 4.0)
            p.text(names[index], 44.0, y + 8.0, theme.text_muted() if index == 5 else theme.text())
        let options = option_rows(self.workspace)
        for index in 0..<options.length:
            let y = 6.0 + float(names.length + index) * row
            if self.hovered == names.length + index:
                p.rectangle(Rect(4.0, y, 260.0, 34.0), theme.grey(72.0))
            p.text(options[index], 44.0, y + 8.0, theme.text_muted())
        p.close()
    pub func event(self, event: Event) -> unit!:
        if event.cancelled or event.kind == EventKind.focus_lost or (event.kind == EventKind.key_down and event.key == Key.escape):
            self.box.set_visible(false)
            self.held = false
            return
        if event.kind == EventKind.pointer_moved:
            let rows = float(mode_names().length + option_rows(self.workspace).length)
            self.hovered = int((event.y - 6.0) / row) if event.inside and event.y >= 6.0 and event.y < 6.0 + row * rows else -1
            self.box.invalidate()
        if (event.kind == EventKind.pointer_down and not self.held) or (event.kind == EventKind.pointer_up and self.held):
            self.held = false
            let rows = float(mode_names().length + option_rows(self.workspace).length)
            if event.inside and event.y >= 6.0 and event.y < 6.0 + row * rows:
                self.choose(int((event.y - 6.0) / row))