Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-2d

Source of release 0.5.1

0.5.1 / src / workspace.luc

## The editing session, a coordinator of focused parts: the open documents
## (one on screen at a time), how it is viewed, which layers are selected and
## the edits on them, the tool in hand and its options, the colors, the
## eyedroppers, painting, previews, transforms, the selection and the whole
## image. The parts share `live` — the shown document's canvas and the
## revision every change bumps so views know to redraw — and never import
## this module; the workspace wires them together and does what spans several.
from luce_image.canvas import Canvas
from theme import Look
from documents import blank_canvas
from live import Live
from open_documents import OpenDocuments
from viewport import Viewport
from layer_picking import LayerPicking
from layer_commands import LayerCommands
from tool_state import ToolState
from colors import Colors
from eyedroppers import Eyedroppers
from painting import Painting
from previews import Previews
from transforms import Transforms
from selection_commands import SelectionCommands
from image_commands import ImageCommands

pub class Workspace:
    ## The shown document's canvas: always `live.canvas`, kept here for the
    ## many readers; only a document switch changes it (`arrived`).
    pub var canvas: Canvas
    pub let live: Live
    ## The configurable look (accent, radius), set from Settings.
    pub var look: Look
    pub let documents: OpenDocuments
    pub let viewport: Viewport
    pub let layers: LayerPicking
    pub let edits: LayerCommands
    pub let tools: ToolState
    pub let colors: Colors
    pub let eyedroppers: Eyedroppers
    pub let painting: Painting
    pub let previews: Previews
    pub let transforms: Transforms
    pub let selection: SelectionCommands
    pub let image: ImageCommands

    pub func init(self, width: int, height: int) -> unit!:
        let live = Live(blank_canvas(width, height))
        self.live = live
        self.canvas = live.canvas
        self.look = Look()
        self.viewport = Viewport(live)
        self.layers = LayerPicking(live)
        self.edits = LayerCommands(live, self.layers)
        self.tools = ToolState(live)
        self.colors = Colors(live, self.tools)
        self.eyedroppers = Eyedroppers(live, self.colors)
        self.painting = Painting(live, self.tools, self.colors, self.layers)
        self.previews = Previews(live, self.layers, self.edits)
        self.image = ImageCommands(live, self.viewport)
        self.transforms = Transforms(live, self.layers, self.edits, self.tools, self.image)
        self.selection = SelectionCommands(live, self.layers, self.tools)
        self.documents = OpenDocuments(live, self.viewport, self.layers, self.edits)
        self.documents.leaving = self.settle_edits
        self.documents.arrived = self.show_document
        self.touch()

    ## Mark a change: whatever shows the document draws again.
    pub func touch(self):
        self.live.touch()

    ## Let tiles use `fraction` of the GPU's and the machine's memory, one
    ## setting for every document: past it, the least wanted move to RAM.
    pub func set_memory_usage(self, fraction: float) -> unit!:
        self.canvas.set_memory_usage(fraction)

    ## Where tiles past the RAM budget spill ("" for the default cache folder);
    ## it applies before the first spill.
    pub func set_scratch_folder(self, folder: str) -> unit!:
        Canvas.set_scratch_folder(folder)

    ## Make `tool` (a `tools()` id) active, with what goes with it: the Crop
    ## tool's box comes and goes with that tool, and a live gradient is kept
    ## when another tool is chosen.
    pub func choose_tool(self, tool: str) -> unit!:
        self.tools.choose(tool)
        self.image.follow_tool(tool)
        if tool != "gradient":
            self.painting.finish_gradient(true)

    # -- History.

    pub func undo(self) -> unit!:
        if self.canvas.can_undo() and not self.canvas.previewing():
            let width = self.canvas.width()
            let height = self.canvas.height()
            self.canvas.undo()
            self.after_history(width, height)

    pub func redo(self) -> unit!:
        if self.canvas.can_redo() and not self.canvas.previewing():
            let width = self.canvas.width()
            let height = self.canvas.height()
            self.canvas.redo()
            self.after_history(width, height)

    ## Undo or redo may have changed the layers under the selection — keep the
    ## picked layers that are still there, and it in range — or the canvas size
    ## from `width` × `height`: fit it again.
    func after_history(self, width: int, height: int) -> unit!:
        self.layers.keep_present()
        if self.canvas.width() != width or self.canvas.height() != height:
            self.viewport.fitted = false
        self.touch()

    # -- Document switches.

    ## Before the shown document is left: a free transform under way is
    ## dropped and a live gradient kept.
    func settle_edits(self) -> unit!:
        self.transforms.finish_transform(false)
        self.painting.finish_gradient(true)

    ## Another document is shown: its canvas here too, and the Crop tool's
    ## box around it.
    func show_document(self):
        self.canvas = self.live.canvas
        self.image.refresh_crop_box()