dymokomi/luced-3d
Source of release 0.11.8
0.11.8 / src / sketch_value.luc
## The value box Sketch Dimension opens where its dimension goes, as
## Fusion's does: the measured value, selected for typing, in the
## document's unit (any unit may be typed: "25 mm", "1 in"; an angle in
## degrees). Enter sets the dimension and the sketch follows; Esc or a click
## elsewhere leaves it as measured. A tool asks it for a length the same way
## (Offset's distance): Enter hands the number to the tool.
from luce_ui.ui import HStack, Text, NumberField, Font, Unit
from interop import Connection
from workspace import Workspace
import sketch_commands
import units
from angles import radians, degrees
pub class SketchValue:
let w: Workspace
pub let field: NumberField
pub let body: HStack
let label: Text
var node: int = -1
var constraint: int = -1
var angle: bool = false
## A tool's question being answered (none: a dimension's value).
var answer: func(float) -> unit!? = none
let connections: list[Connection]
pub func init(self, w: Workspace, font: Font) -> unit!:
self.w = w
self.label = Text("d1", font = font, muted = true)
self.field = NumberField(0.0, -1.0e9, 1.0e9, 1.0, 2, font = font, cells = 9.0)
self.body = HStack([self.label, self.field], spacing = 6.0, padding = 6.0)
# Floating, not modal: its field takes the typing, and a click
# elsewhere still reaches the viewport (which closes the box).
self.body.layout().set_popup(true)
self.body.layout().set_modal(false)
self.body.layout().set_visible(false)
self.connections = [self.field.on_submit(self.submit), self.field.on_cancel(self.close)]
## Show the box at (`x`, `y`) (the body's coordinates) for dimension
## `constraint` of Sketch node `node`, named `name`, holding `value`
## (mm, or radians for an angle).
## Without `named`, the name is left out, so the field sits on the
## dimension's label (x, y then its corner).
pub func open(self, node: int, constraint: int, name: str, value: float, angle: bool, x: float, y: float, named: bool = true) -> unit!:
self.answer = none
self.label.layout().set_visible(named)
self.node = node
self.constraint = constraint
self.angle = angle
self.label.set_text(name)
if angle:
self.field.set_units(Unit(suffix = "°"))
self.field.set_value(degrees(value))
else:
self.field.set_units(units.document_unit(self.w))
self.field.set_value(value)
self.body.layout().set_popup_point(x, y)
self.body.layout().set_visible(true)
self.field.edit()
## Ask for length `value` labeled `label` at (`x`, `y`) (the body's
## coordinates); Enter hands the typed length to `answer` (degrees with
## `angle`).
pub func ask(self, label: str, value: float, x: float, y: float, answer: func(float) -> unit!, angle: bool = false) -> unit!:
self.answer = answer
self.label.layout().set_visible(true)
self.angle = false
self.label.set_text(label)
self.field.set_units(Unit(suffix = "°") if angle else units.document_unit(self.w))
self.field.set_value(value)
self.body.layout().set_popup_point(x, y)
self.body.layout().set_visible(true)
self.field.edit()
pub func is_open(self) -> bool:
return self.body.layout().is_visible()
## Enter: the typed value is the dimension's.
func submit(self) -> unit!:
let value = self.field.value()
self.body.layout().set_visible(false)
if let reply = self.answer:
self.answer = none
reply(value)
return
sketch_commands.set_dimension(self.w, self.node, self.constraint, radians(value) if self.angle else value)
func close(self) -> unit!:
self.body.layout().set_visible(false)
## A click elsewhere: the dimension stays as measured.
pub func dismiss(self) -> unit!:
self.body.layout().set_visible(false)
pub func disconnect(self):
for connection in self.connections:
connection.disconnect()
self.connections.clear()