dymokomi/luced-2d
Source of release 0.5.1
0.5.1 / src / adjust_dialog.luc
## The adjust dialog, after Compositor's floating panels: a card centred over
## the canvas with the open adjustment's or filter's controls
## (adjustment_controls.luc), previewing live on the selected layer's pixels,
## with Cancel and Apply. This is the destructive path — Photoshop's Image ›
## Adjustments and the Filter menu: Apply keeps the preview as one undo step.
## Adjustment layers are the live path; their controls are in the Properties
## panel.
from luce_ui.ui import Text, HStack, Spacer, Widget, Dialog, Checkbox
from workspace import Workspace
from kit import Kit
from adjustment_controls import AdjustmentControls
from adjustments import blur_kind, noise_kind, first_filter_kind
from preview_session import PreviewDialogSession
import time
pub class AdjustDialog:
pub let dialog: Dialog
## The preview's lifecycle (preview_session.luc).
pub let session: PreviewDialogSession
pub let title: Text
pub let controls: AdjustmentControls
## The open kind, or -1.
pub var kind: int = -1
## Add Noise's grain, the same while the dialog is open.
var seed: int = 1
## What the window does when the document changed.
pub var changed: func() -> unit!? = none
let preview_box: Checkbox
## Whether the edit shows on the layer (Preview checked).
var showing: bool = true
let workspace: Workspace
pub func init(self, workspace: Workspace, kit: Kit) -> unit!:
self.workspace = workspace
self.title = Text("", font = kit.font)
self.controls = AdjustmentControls(workspace, kit)
var items: list[Widget] = [self.title, self.controls.view]
# Preview unchecked shows the layer as it was, the settings kept (Alt-P toggles it).
self.preview_box = kit.checkbox("Preview", self.toggle_preview)
self.preview_box.set_checked(true)
items.append(HStack([self.preview_box, kit.button("Reset", self.reset), Spacer(), kit.button("Cancel", self.cancel), kit.button("Apply", self.apply)], spacing = 8.0))
self.dialog = Dialog(items, width = 440.0)
self.session = PreviewDialogSession(self.dialog, kit, "Cannot adjust")
self.session.begin = workspace.previews.begin_preview
self.session.preview = self.preview
self.session.finish = self.finish
# A control moved: preview at the next frame. Moves coming faster than a
# large picture renders are folded into one, so a drag never queues up
# work behind it.
self.controls.changed = self.session.request
self.controls.failed = self.session.tell
kit.dialog_keys(self.dialog, self.cancel, self.apply)
## Adjust menu: `kind` on the selected layer's pixels.
pub func open(self, kind: int) -> unit!:
self.start(kind)
## Filter › Gaussian Blur…
pub func blur(self) -> unit!:
self.start(blur_kind)
## Filter › Noise › Add Noise…: a fresh grain each time it opens.
pub func add_noise(self) -> unit!:
self.seed = time.now() % 1000000007
self.start(noise_kind)
## Filter menu: luce-image's filter `filter` (Motion Blur, Lens
## Correction, Bloom, Tonal Contrast).
pub func filter(self, filter: int) -> unit!:
self.start(first_filter_kind + filter)
## Another kind chosen while the dialog is up drops the one before.
func start(self, kind: int) -> unit!:
if not self.session.open(restart = true):
return
self.kind = kind
self.showing = true
self.preview_box.set_checked(true)
self.controls.below = -1
self.controls.show(kind, [])
self.title.set_text(self.controls.spec(kind).name)
self.dialog.open()
self.session.refresh()
self.session.preview_now()
## Show a page of the open kind (Levels' channel, a hue range, a tone).
pub func show_page(self, kind: int, page: int) -> unit!:
self.controls.show_page(kind, page)
## Set one slider from code, as if moved.
pub func set_control(self, kind: int, slot: int, value: float) -> unit!:
self.controls.set_control(kind, slot, value)
# From code, at once: nothing waits for a frame.
self.session.flush()
## The session's end: the on-image adjustment disarmed, the preview kept or dropped.
func finish(self, keep: bool) -> unit!:
self.controls.end_on_image()
if self.workspace.previews.previewing():
self.workspace.previews.end_preview(keep)
self.kind = -1
pub func apply(self) -> unit!:
# What the controls say is what is kept, even if its frame has not come yet.
self.session.flush()
self.session.close(true)
pub func cancel(self) -> unit!:
self.session.close(false)
## Preview on or off (the checkbox, or Alt-P while the dialog is open).
pub func toggle_preview(self) -> unit!:
if not self.dialog.is_open():
return
self.showing = not self.showing
self.preview_box.set_checked(self.showing)
self.session.preview_now()
## Put every setting of the open kind back to its default.
func reset(self) -> unit!:
if self.kind < 0:
return
self.controls.show(self.kind, [])
self.session.preview_now()
## Show the open kind with the controls as they are, on the pixels' preview.
func preview(self) -> unit!:
let kind = self.kind
if kind < 0 or not self.workspace.previews.previewing():
return
if not self.showing:
self.workspace.canvas.show_preview_original()
self.workspace.touch()
if let changed = self.changed:
changed()
return
let values = self.controls.values(kind)
if kind == blur_kind:
self.workspace.previews.preview_blur(values[0])
elif kind == noise_kind:
self.workspace.previews.preview_noise(self.controls.numbers(kind)[0], values[1] != 0.0, self.seed, values[2] != 0.0)
elif kind >= first_filter_kind:
let numbers = self.controls.numbers(kind) + [0.0, 0.0]
self.workspace.previews.preview_filter(kind - first_filter_kind, numbers[0], numbers[1], numbers[2])
else:
self.workspace.previews.preview_adjust(kind, self.controls.numbers(kind))
if let changed = self.changed:
changed()