dymokomi/luced-2d
Source of release 0.5.0
0.5.0 / src / navigator_panel.luc
## The Navigator panel, as Photoshop's: the whole document small, with a box
## where the view is. Click or drag in it to move the view there; the slider
## zooms about the view's middle. The thumbnail is the canvas drawn at a small
## zoom, which composites from the layers' coarse pyramid levels, so even a
## 360-megapixel document costs a thumbnail's worth of work.
from luce_ui.ui import Panel, Widget, Layout, Size, Constraints, Event, Painter, Slider, Text, VStack, HStack
from luce_gpu.gpu import RenderTarget, Rect
from luce_window.input import EventKind, Cursor
from workspace import Workspace
from view import CanvasView
from kit import Kit, outline
import theme
from luce_std import math
pub class NavigatorPanel:
pub let panel: Panel
let thumbnail: Thumbnail
let zoom_slider: Slider
let percent: Text
let workspace: Workspace
let view: CanvasView
pub var refreshed: func() -> unit!? = none
pub func init(self, workspace: Workspace, view: CanvasView, kit: Kit) -> unit!:
self.workspace = workspace
self.view = view
self.thumbnail = Thumbnail(workspace, view)
self.thumbnail.moved = self.moved
# The zoom in powers of two, 1/64 to 64, as the workspace allows.
self.zoom_slider = Slider("", -6.0, 6.0, 0.0, 0.0, font = kit.font, show_value = false)
self.zoom_slider.layout().set_flex(horizontal = 1.0, vertical = 0.0)
kit.keep(self.zoom_slider.on_change(self.zoom_to))
self.percent = Text("100%", font = kit.font, muted = true)
self.percent.layout().set_limits(Size(52.0, 0.0), Size(52.0, 16384.0))
let content = VStack([self.thumbnail, HStack([self.percent, self.zoom_slider], spacing = 6.0)], spacing = 6.0, padding = 6.0)
self.panel = Panel("Navigator", content, minimum = Size(200.0, 160.0), id = "navigator")
## The slider at `power`: zoom 2^power about the view's middle.
func zoom_to(self, power: float) -> unit!:
let bounds = self.view.layout().bounds()
let wanted = math.pow(2.0, power)
self.workspace.viewport.zoom_about(wanted / self.workspace.viewport.zoom, bounds.width * 0.5, bounds.height * 0.5)
self.percent.set_text(f"{int(self.workspace.viewport.zoom * 100.0 + 0.5)}%")
self.moved()
func moved(self) -> unit!:
self.thumbnail.layout().invalidate()
if let refresh = self.refreshed:
refresh()
## Show the zoom and the view's box as they are now.
pub func show(self) -> unit!:
self.zoom_slider.set_value(math.max(-6.0, math.min(6.0, math.log2(self.workspace.viewport.zoom))))
self.percent.set_text(f"{int(self.workspace.viewport.zoom * 100.0 + 0.5)}%")
self.thumbnail.layout().invalidate()
## The document fitted into the panel, and the view's box over it.
class Thumbnail: Widget:
let box: Layout
let workspace: Workspace
let view: CanvasView
var dragging: bool = false
pub var moved: func() -> unit!? = none
pub func init(self, workspace: Workspace, view: CanvasView) -> unit!:
self.workspace = workspace
self.view = view
self.box = Layout()
self.box.set_interactive(true)
self.box.set_cursor(Cursor.pointer)
self.box.set_flex()
pub func layout(self) -> Layout:
return self.box
pub func measure(self, available: Constraints) -> Size!:
return Size(180.0, 120.0)
## The zoom that fits the document in a `width` by `height` box, and where
## its top-left corner goes then.
func fit(self, width: float, height: float) -> list[float]:
let w = float(self.workspace.canvas.width())
let h = float(self.workspace.canvas.height())
let zoom = math.min((width - 8.0) / w, (height - 8.0) / h)
return [zoom, (width - w * zoom) * 0.5, (height - h * zoom) * 0.5]
pub func draw(self, target: RenderTarget) -> unit!:
let size = target.size()
let painter = Painter(target)
painter.rectangle(Rect(0.0, 0.0, size.width, size.height), theme.backdrop())
let placed = self.fit(size.width, size.height)
let zoom = placed[0]
if zoom <= 0.0:
painter.close()
return
let w = float(self.workspace.canvas.width()) * zoom
let h = float(self.workspace.canvas.height()) * zoom
painter.rectangle(Rect(placed[1], placed[2], w, h), theme.checker_light())
painter.close()
self.workspace.canvas.draw(target, Rect(0.0, 0.0, size.width, size.height), zoom, placed[1], placed[2])
# The view's box: what the canvas view shows of the document, scaled.
let view = self.view.layout().bounds()
let over = Painter(target)
if let box = view_box(placed, float(self.workspace.canvas.width()), float(self.workspace.canvas.height()), view.width, view.height,
self.workspace.viewport.zoom, self.workspace.viewport.offset_x, self.workspace.viewport.offset_y):
outline(over, box, self.workspace.look.accent)
over.close()
pub func event(self, event: Event) -> unit!:
if event.kind == EventKind.pointer_down and event.button == 0 and event.inside:
self.dragging = true
self.center_on(event.x, event.y)
elif event.kind == EventKind.pointer_moved and self.dragging:
self.center_on(event.x, event.y)
elif event.kind == EventKind.pointer_up:
self.dragging = false
## Put the view's middle on thumbnail point (`x`, `y`).
func center_on(self, x: float, y: float) -> unit!:
let bounds = self.box.bounds()
let placed = self.fit(bounds.width, bounds.height)
if placed[0] <= 0.0:
return
let document_x = (x - placed[1]) / placed[0]
let document_y = (y - placed[2]) / placed[0]
let view = self.view.layout().bounds()
self.workspace.viewport.offset_x = view.width * 0.5 - document_x * self.workspace.viewport.zoom
self.workspace.viewport.offset_y = view.height * 0.5 - document_y * self.workspace.viewport.zoom
self.workspace.viewport.fitted = true
self.workspace.touch()
if let moved = self.moved:
moved()
## The Navigator's box for a view `view_width` by `view_height` showing the
## document (`width` by `height`) at `zoom` from (`offset_x`, `offset_y`), in a
## thumbnail placed as `placed` (its zoom and the document's corner there). As
## Photoshop's, the box is what shows of the document: cut to it, so a view
## wider than the document boxes the whole thumbnail rather than running past
## the panel's edges. None when the view shows none of the document.
pub func view_box(placed: list[float], width: float, height: float, view_width: float, view_height: float,
zoom: float, offset_x: float, offset_y: float) -> Rect?:
if placed[0] <= 0.0 or zoom <= 0.0:
return none
# The document pixels the view shows, cut to the document.
let left = math.max(0.0, -offset_x / zoom)
let top = math.max(0.0, -offset_y / zoom)
let right = math.min(width, (view_width - offset_x) / zoom)
let bottom = math.min(height, (view_height - offset_y) / zoom)
if right <= left or bottom <= top:
return none
let scale = placed[0]
return Rect(placed[1] + left * scale, placed[2] + top * scale, (right - left) * scale, (bottom - top) * scale)