dymokomi/luced-3d
Source of release 0.11.8
## The application: the window and its panels, settings kept between
## runs, and the error handler. Nothing polls and an idle window does no
## work: a change to the workspace notifies the loop, whose next turn
## refreshes the panels once (Panels.catch_up); the compute worker, Script
## checks and renders wake it through notifiers when they have something; a
## timed wait (a long press, a pause in typing) asks for one turn when it is
## due; frames follow one another only while something plays or renders
## (Panels.animate); and the settings are written a moment after they change.
from luce_ui.ui import Application, Font, Notifier
from luce_cad.cad_geometry import CadGeometry
import theme
from workspace import Workspace
from panels import Panels
from ui_state import UIState, settings_path
import node_commands
import node_paths
pub class Luce3D:
pub let app: Application
pub let workspace: Workspace
pub let panels: Panels
let settings: UIState
## Notified by each change to the workspace: the panels catch up on the
## next turn, once however many changes came.
let changes: Notifier
## The compute worker's: its result or progress is taken then.
var computed: Notifier? = none
## Woken when a Script check's sync or job ends.
var checks: Notifier? = none
## Woken when a render has compiled its scene (the viewport's and the
## Render panel's), so no frames are drawn while it compiles.
var renders: Notifier? = none
## `persist`: the user's settings and the desktop's clipboard; tests
## keep their settings in `settings_file` (none when empty).
pub func init(self, persist: bool = false, width: int = 1400, height: int = 900, settings_file: str = "") -> unit!:
# Geometry files carry CAD models too (the Cache node).
_ = CadGeometry.register_codec()
self.settings = UIState(settings_path() if persist else settings_file)
self.workspace = Workspace()
self.panels = Panels(self.workspace, Font())
# The editor's window copies nodes to the desktop's clipboard; tests'
# editors keep them to themselves.
self.panels.actions.clipboard.system = persist
self.settings.restore(self.panels)
self.app = Application(self.panels.body, title = "Luced 3D", width = self.settings.width if persist else width, height = self.settings.height if persist else height, theme = theme.editor_theme())
self.panels.projects.app = self.app
self.app.set_close_handler(self.panels.projects.can_quit)
self.app.focus(self.panels.view.layout())
self.app.set_commands(self.panels.actions.all)
self.panels.app = self.app
self.changes = self.app.notifier(self.panels.catch_up)
let changes = self.changes
self.workspace.on_change = func ():
# Only a closed notifier fails, as the editor closes: nothing to show then.
changes.notify() catch failure:
_ = failure
self.panels.on_settings = func () -> unit!: self.settings.changed(self.app, self.panels)
self.app.set_error_handler(self.panels.report_error)
self.panels.inspector.on_browse = self.browse_geometry
self.panels.inspector.on_render = self.panels.render
if self.settings.warning != "": self.panels.report_error(self.settings.warning)
## A path row's Browse: a geometry file for a File node, any file for
## another node's path row (a Script node's k.file), into text slot `slot`;
## a node path row (node_paths.luc) opens the node path picker instead.
func browse_geometry(self, slot: int) -> unit!:
if let node = self.workspace.selected():
if node_paths.row(node.kind, slot) != none:
let row = self.panels.inspector.text_bounds(slot)
self.panels.node_path_picker.open(row.x + row.width - 30.0, row.y + row.height, slot)
return
let geometry = slot == 0
let path = self.app.open_file("Open geometry (.obj, .fbx, .ply, .spz, .splat, .usd, .step, .stp)" if geometry else "Open a file")
if path != "":
node_commands.set_text(self.workspace, slot, path)
self.panels.refresh()
## The compute worker wakes the loop when it has a result or progress:
## the panels take it then (and only then, between frames).
pub func wake_on_compute(self) -> unit!:
let session = self.workspace.compute else return
if self.computed != none:
return
let notifier = self.app.notifier(self.panels.computed)
session.service.wake_with(notifier)
self.computed = notifier
pub func run(self, arguments: list[str]) -> unit!:
self.workspace.enable_compute()
self.wake_on_compute()
let checks = self.app.notifier(self.panels.checked)
self.panels.inspector.wake_checks_with(checks.waker())
self.checks = checks
let renders = self.app.notifier(self.panels.animate)
self.panels.view.wake_renders_with(renders.waker())
self.panels.render_view.wake_with(renders.waker())
self.renders = renders
# A project named on the command line that cannot be opened is
# reported, and the editor opens without it.
for argument in arguments:
if argument == "--smoke" or argument == "--empty": continue
self.panels.projects.load_from(argument) catch failure:
print(f"luced-3d: could not open {argument}: {failure.message}")
# The open dialog, made ahead once the window shows: Open project
# and Browse then show it at once (macOS's first panel takes ~260 ms).
if not ("--smoke" in arguments):
_ = self.app.after(0.5, func () -> unit!: self.app.prepare_dialogs())
self.app.run(frame_limit = 3 if "--smoke" in arguments else 0)
pub func close(self):
self.settings.save(self.panels) catch failure:
_ = failure
self.workspace.on_change = none
self.changes.close()
if let notifier = self.computed:
notifier.close()
self.computed = none
if let notifier = self.checks:
notifier.close()
self.checks = none
if let notifier = self.renders:
notifier.close()
self.renders = none
if let compute = self.workspace.compute:
compute.close()
self.panels.close()
self.app.close()