Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / src / marquee.luc

## The marquee every selecting view shares (the viewport's objects and
## components, the network view): what a press's modifiers make of a box
## or a click, when a press became a drag, which way it was dragged (CAD's
## window or crossing), and the box drawn.
from luce_ui.ui import Painter, Event
from luce_gpu.gpu import Rect
from luce_std import math
from viewport_chrome import outline
import theme

## What a press's modifiers make of a box or click: Ctrl or Cmd takes away
## (2), Shift adds (1), else it replaces (0).
pub func operation_of(event: Event) -> int:
    return 2 if event.control or event.meta else (1 if event.shift else 0)

## Whether a press from (box[0], box[1]) to (box[2], box[3]) moved past a
## click's few points.
pub func dragged(box: list[float]) -> bool:
    return math.abs(box[2] - box[0]) > 4.0 or math.abs(box[3] - box[1]) > 4.0

## Whether a box was dragged right to left: in CAD, Fusion's crossing
## (what it touches), else a window (what lies wholly inside it).
pub func crosses(box: list[float]) -> bool:
    return box[2] < box[0]

## A marquee from (box[0], box[1]) to (box[2], box[3]); with `crossing`
## dashed, as Fusion draws a crossing box apart from a window.
pub func draw_box(painter: Painter, box: list[float], crossing: bool = false) -> unit!:
    let area = Rect(math.min(box[0], box[2]), math.min(box[1], box[3]), math.abs(box[2] - box[0]), math.abs(box[3] - box[1]))
    if not crossing:
        outline(painter, area, theme.text())
        return
    # Dashes of 6 points with gaps of 4 along each side.
    let ink = theme.text()
    for side in 0..<4:
        let horizontal = side % 2 == 0
        let length = area.width if horizontal else area.height
        var at = 0.0
        while at < length:
            let end = math.min(at + 6.0, length)
            if horizontal:
                let y = area.y if side == 0 else area.y + area.height - 1.0
                painter.rectangle(Rect(area.x + at, y, end - at, 1.0), ink)
            else:
                let x = area.x if side == 1 else area.x + area.width - 1.0
                painter.rectangle(Rect(x, area.y + at, 1.0, end - at), ink)
            at += 10.0