dymokomi/luced-2d
Source of release 0.5.0
0.5.0 / tests / brush_tests.luc
## Brushes: the pen reaching the stroke through the view, the dynamics and
## images in Brush Settings, the library on disk and the Brushes panel.
from luce_ui.ui import Font, Event, Application, DockPosition
from luce_window.input import EventKind, PointerKind
from workspace import Workspace
from panels import Panels
from view import CanvasView
from actions import Actions
from brush import Brush, preset
from luce_painting.brush_mask import BrushMask
from brush_dynamics import pen_of, no_mappings
from brush_library import SavedBrush, library_path, load_library, load_brush, save_brush, free_folder, export_brush, import_brush, brush_of, brush_text
from brush_import import import_brushes
from brush_settings import define_tip
from brushes_panel import BrushesPanel
from kit import Kit
from luce_config.toml import Table
from luce_std import paths, files
pub func brush_tests(arguments: list[str]) -> unit!:
let directory = paths.join(arguments[0], "brush-library")
files.ensure_directory(directory)
pen_samples()
pad_painting()
pen_strokes()
sampled_tips()
library_round_trip(directory)
shared_files(directory)
grouped_list(directory)
panel_library(paths.join(directory, "panel"))
dock_keeps_brushes(directory)
stroke_after_closing()
## A mouse press, drag and release routed through the window paint on the
## tip pad, and the Erase button's drag erases.
func pad_painting() -> unit!:
let workspace = Workspace(60, 40)
let panels = Panels(workspace, Actions(), Font())
let settings = panels.brush
let app = Application(settings.panel)
settings.show()
app.layout(360.0, 900.0)
app.layout(360.0, 900.0)
# Opening Transfer, the last section, scrolls it into view.
settings.open_section(5, false)
app.layout(360.0, 200.0)
app.layout(360.0, 200.0)
assert(settings.scrolled() > 0.0)
settings.open_section(0, false)
app.layout(360.0, 900.0)
app.layout(360.0, 900.0)
let pad = settings.tip_editor.pad
let box = pad.layout().bounds()
assert(box.width > 100.0 and box.height > 100.0)
let tip = BrushMask.create(256, 256)
settings.tip_editor.use(tip)
assert(tip.is_empty())
let x = box.x + box.width * 0.5
let y = box.y + box.height * 0.5
app.dispatch(Event(kind = EventKind.pointer_moved, x = x, y = y))
app.dispatch(Event(kind = EventKind.pointer_down, x = x, y = y, button = 0))
app.dispatch(Event(kind = EventKind.pointer_moved, x = x + 40.0, y = y))
app.dispatch(Event(kind = EventKind.pointer_up, x = x + 40.0, y = y, button = 0))
assert(not tip.is_empty())
# Inverted, then painted white over with Erase: the pixels change back.
let before = tip.coverage()
settings.tip_editor.erase()
app.dispatch(Event(kind = EventKind.pointer_down, x = x, y = y, button = 0))
app.dispatch(Event(kind = EventKind.pointer_up, x = x, y = y, button = 0))
assert(tip.coverage() != before)
panels.close()
## A pen event becomes the pen sample a stroke point carries; a mouse's is a
## full press standing upright.
func pen_samples() -> unit!:
let pen = pen_of(Event(kind = EventKind.pointer_moved, pointer_kind = PointerKind.pen, pressure = 0.4, azimuth = 90.0, altitude = 50.0, rotation = 30.0, tangential = 0.5), 12.0)
assert(pen.has_pressure and pen.pressure == 0.4 and pen.azimuth == 90.0 and pen.altitude == 50.0 and pen.rotation == 30.0 and pen.tangential == 0.5 and pen.time == 12.0 and not pen.eraser)
let eraser = pen_of(Event(kind = EventKind.pointer_moved, pointer_kind = PointerKind.eraser, pressure = 1.0, altitude = 90.0), 0.0)
assert(eraser.eraser)
let mouse = pen_of(Event(kind = EventKind.pointer_moved, pointer_kind = PointerKind.mouse), 0.0)
assert(not mouse.has_pressure and mouse.altitude == 90.0 and not mouse.eraser)
## A light pen press with pressure driving the size paints a thinner line than
## a full one; the pen's eraser end erases with the Brush tool.
func pen_strokes() -> unit!:
let workspace = Workspace(120, 80)
workspace.canvas.bind_headless()
let panels = Panels(workspace, Actions(), Font())
let view = panels.view
workspace.viewport.fitted = true
workspace.viewport.zoom = 1.0
workspace.viewport.offset_x = 0.0
workspace.viewport.offset_y = 0.0
panels.choose_tool("brush")
workspace.colors.set_color(1.0, 0.0, 0.0)
workspace.tools.brush = preset("Hard Round", 20.0, 1.0)
workspace.tools.brush.dynamics[0].minimum = 0.0
# A light press across the top, a full one across the bottom.
stroke(view, 10.0, 20.0, 110.0, 20.0, PointerKind.pen, 0.2)
stroke(view, 10.0, 60.0, 110.0, 60.0, PointerKind.pen, 1.0)
assert(alpha_at(workspace, 60, 20) > 0.99 and alpha_at(workspace, 60, 60) > 0.99)
# 6 pixels off the line: inside the full dab (radius 10), outside the light one (2).
assert(alpha_at(workspace, 60, 26) < 0.01 and alpha_at(workspace, 60, 66) > 0.99)
# A pen touching at zero pressure paints its minimum, not a full dab.
stroke(view, 10.0, 40.0, 110.0, 40.0, PointerKind.pen, 0.0)
assert(alpha_at(workspace, 60, 44) < 0.01)
# The eraser end takes the full line away where it passes.
stroke(view, 60.0, 50.0, 60.0, 70.0, PointerKind.eraser, 1.0)
assert(alpha_at(workspace, 60, 60) < 0.01 and alpha_at(workspace, 30, 60) > 0.99)
panels.close()
## A drag with a pen of `kind` at `pressure` from one point to another.
func stroke(view: CanvasView, x0: float, y0: float, x1: float, y1: float, kind: PointerKind, pressure: float) -> unit!:
view.event(Event(kind = EventKind.pointer_down, x = x0, y = y0, button = 0, inside = true, pointer_kind = kind, pressure = pressure, altitude = 90.0))
view.event(Event(kind = EventKind.pointer_moved, x = x1, y = y1, inside = true, pointer_kind = kind, pressure = pressure, altitude = 90.0))
view.event(Event(kind = EventKind.pointer_up, x = x1, y = y1, button = 0, inside = true, pointer_kind = kind, pressure = pressure, altitude = 90.0))
func alpha_at(workspace: Workspace, x: int, y: int) -> float!:
workspace.canvas.pick(x, y)
return workspace.canvas.picked_alpha()
## A painted tip in the brush paints its shape; Define Brush takes the
## selection as a tip; the Brush Settings editors put their images on the
## brush in hand.
func sampled_tips() -> unit!:
let workspace = Workspace(100, 60)
workspace.canvas.bind_headless()
let panels = Panels(workspace, Actions(), Font())
panels.choose_tool("brush")
# A brush without a sampled tip shows the round tip it paints with; the
# first press on the pad makes that the brush's tip, then paints on it.
let pad = panels.brush.tip_editor.pad
panels.brush.show()
assert(workspace.tools.brush.tip == none and pad.mask != none and not pad.owned)
pad.event(Event(kind = EventKind.pointer_down, x = 10.0, y = 10.0, button = 0, inside = true))
pad.event(Event(kind = EventKind.pointer_up, x = 10.0, y = 10.0, button = 0, inside = true))
let started = workspace.tools.brush.tip else trap("a tip from the round one")
assert(pad.owned and not started.is_empty())
workspace.colors.set_color(0.0, 0.0, 1.0)
# A tip that paints only its left half.
let tip = BrushMask.create(32, 32)
tip.clear(1.0)
var y = 0.0
while y < 32.0:
tip.line(24.0, y, 32.0, y, 8.0, 1.0, 0.0)
y += 2.0
workspace.tools.brush = preset("Hard Round", 30.0, 1.0)
workspace.tools.brush.dynamics = no_mappings()
workspace.tools.brush.tip = tip
workspace.painting.begin_stroke(50.0, 30.0)
workspace.painting.end_stroke()
assert(alpha_at(workspace, 40, 30) > 0.99 and alpha_at(workspace, 60, 30) < 0.01)
# Define Brush: the selected pixels of the picture, dark painting.
workspace.canvas.select_rectangle(35, 15, 30, 30)
let defined = define_tip(workspace)
assert(defined.width() == 30 and defined.height() == 30 and not defined.is_empty())
# The tip editor's image goes on the brush at once; Clear takes it off.
let settings = panels.brush
settings.tip_editor.use(defined)
assert(workspace.tools.brush.tip != none)
settings.show()
panels.close()
## A brush with dynamics, a tip and a texture image saves to the library
## and reads back the same.
func library_round_trip(directory: str) -> unit!:
let brush = sample_brush()
let library = library_path(directory)
let folder = free_folder(library, "Test Ink / 2")
assert(folder == "test-ink-2")
save_brush(library, SavedBrush("Test Ink / 2", folder, brush, "Inks"))
assert(free_folder(library, "Test Ink / 2") == "test-ink-2-2")
var found = false
for saved in load_library(library):
if saved.folder == folder:
found = true
assert(saved.name == "Test Ink / 2" and saved.group == "Inks")
# The library reads settings only; the pictures wait until used.
assert(saved.brush.tip == none and saved.brush.texture_image == none)
saved.load_images()
same_brush(saved.brush, brush)
# Saving a brush whose pictures were never read keeps them.
let unread = load_library(library)
for other in unread:
if other.folder == folder:
save_brush(library, other)
let again = load_brush(library, folder)
same_brush(again.brush, brush)
assert(found)
# A value the file gets wrong is an error, not a silent default.
let wrong = brush_of(Table("[texture]\nkind = \"weave\"\n")) catch failure:
_ = failure
return
_ = wrong
assert(false)
## The list groups brushes under folding headers, rows kept by brush index;
## an unknown file is refused by the importer.
func grouped_list(directory: str) -> unit!:
let workspace = Workspace(60, 40)
let panels = Panels(workspace, Actions(), Font())
let panel = BrushesPanel(workspace, Kit(Font()), panels.brush, paths.join(directory, "grouped"))
let rows = panel.rows
rows.set_rows(["A", "B", "C", "D"], ["", "Oils", "", "Oils"])
# "", then Oils' header: A, C, [Oils], B, D.
assert(rows.row_top(2) == 58.0 and rows.row_top(1) == 2.0 * 58.0 + 24.0 and rows.row_top(3) == 3.0 * 58.0 + 24.0)
rows.toggle_group("Oils")
assert(rows.is_folded("Oils") and rows.row_top(3) == 0.0)
rows.fold_all_but(["Oils"])
assert(not rows.is_folded("Oils"))
rows.fold_all_but([])
assert(rows.is_folded("Oils"))
let refused = import_brushes(paths.join(directory, "notes.txt")) catch failure:
_ = failure
return
_ = refused
assert(false)
## A `.l2dbrush` file carries the brush whole, images included.
func shared_files(directory: str) -> unit!:
let brush = sample_brush()
let path = paths.join(directory, "shared.l2dbrush")
export_brush(brush, "Shared \"Ink\"", path)
let imported = import_brush(path)
assert(imported.name == "Shared \"Ink\"")
same_brush(imported.brush, brush)
## The Brushes panel: "+" saves the brush in hand, Duplicate copies it,
## Rename and Delete change the library, and a saved brush picked is a copy.
func panel_library(directory: str) -> unit!:
files.ensure_directory(directory)
let workspace = Workspace(60, 40)
let panels = Panels(workspace, Actions(), Font())
let kit = Kit(Font())
let panel = BrushesPanel(workspace, kit, panels.brush, directory)
let built_in = panel.saved.length
workspace.tools.brush = sample_brush()
let shown = workspace.live.revision
panel.save_new()
assert(panel.saved.length == built_in + 1 and load_library(panel.library).length == built_in + 1)
# The new brush is the one in hand, and the header shows it (the window refreshes).
assert(workspace.tools.brush.preset == f"Brush {built_in + 1}" and workspace.live.revision != shown)
let row = panel.rows.selected
panel.duplicate()
assert(panel.saved.length == built_in + 2 and panel.saved[panel.saved.length - 1].name.ends_with(" copy"))
panel.rename(row, " Renamed ")
var names: list[str] = []
for saved in load_library(panel.library):
names.append(saved.name)
assert("Renamed" in names)
# Picking the saved brush gives a copy: painting settings do not touch the library.
panel.pick(row)
assert(workspace.tools.brush.diameter == 48.0 and workspace.tools.brush.preset == "Renamed")
workspace.tools.brush.diameter = 5.0
assert(panel.saved[row - panel.rows.first_saved].brush.diameter == 48.0)
# Save writes the brush in hand over the picked one.
panel.rows.selected = row
panel.save_over()
assert(load_library(panel.library)[0].brush.diameter == 5.0 or load_library(panel.library)[1].brush.diameter == 5.0)
panel.rows.selected = row
panel.pick(row)
panel.delete_selected()
# The bin asks first; nothing goes until Delete.
assert(panel.confirm.is_open() and panel.saved.length == built_in + 2)
panel.remove(row)
assert(panel.saved.length == built_in + 1 and load_library(panel.library).length == built_in + 1)
assert(workspace.tools.brush.folder == "" and workspace.tools.brush.preset == "(unsaved)")
# A built-in picked keeps the size in hand.
panel.pick(1)
assert(workspace.tools.brush.preset == "Soft Round" and workspace.tools.brush.diameter == 5.0)
kit.close()
panels.close()
## The first press after Brush Settings closes paints: it reaches the canvas.
func stroke_after_closing() -> unit!:
let workspace = Workspace(200, 150)
workspace.canvas.bind_headless()
let panels = Panels(workspace, Actions(), Font())
let app = Application(panels.body)
app.layout(1200.0, 800.0)
panels.choose_tool("brush")
panels.open_brush()
app.layout(1200.0, 800.0)
assert(panels.dock.is_floating(panels.brush.panel))
# Its close button, as a person closes it: the title bar's right end.
let content = panels.brush.panel.layout().bounds()
let close_x = content.x + content.width - 6.0
let close_y = content.y - 12.0
app.dispatch(Event(kind = EventKind.pointer_moved, x = close_x, y = close_y))
app.dispatch(Event(kind = EventKind.pointer_down, x = close_x, y = close_y, button = 0))
app.dispatch(Event(kind = EventKind.pointer_up, x = close_x, y = close_y, button = 0))
app.layout(1200.0, 800.0)
assert(not panels.catalog.is_open("brush_settings"))
let area = panels.view.layout().bounds()
let x = area.x + area.width * 0.5
let y = area.y + area.height * 0.5
app.dispatch(Event(kind = EventKind.pointer_moved, x = x, y = y))
app.dispatch(Event(kind = EventKind.pointer_down, x = x, y = y, button = 0))
app.dispatch(Event(kind = EventKind.pointer_moved, x = x + 30.0, y = y))
app.dispatch(Event(kind = EventKind.pointer_up, x = x + 30.0, y = y, button = 0))
assert(workspace.canvas.can_undo() and workspace.canvas.undo_name() == "Brush Stroke")
app.close()
panels.close()
## The Brushes and Brush Settings panels come back where they were docked.
func dock_keeps_brushes(directory: str) -> unit!:
let path = paths.join(directory, "layout.txt")
let panels = Panels(Workspace(300, 200), Actions(), Font())
panels.catalog.open("brushes")
panels.open_brush()
panels.dock.move(panels.brush.panel, panels.properties.panel, DockPosition.tab)
panels.dock_layout.restore(path)
panels.dock_layout.save()
let kept = panels.dock.layout_text()
assert(kept.contains("brushes") and kept.contains("brush_settings"))
let again = Panels(Workspace(300, 200), Actions(), Font())
again.dock_layout.restore(path)
assert(again.dock.layout_text() == kept and again.catalog.is_open("brushes"))
panels.close()
again.close()
## A brush with a bit of everything.
func sample_brush() -> Brush!:
let brush = Brush()
brush.diameter = 48.0
brush.spacing = 0.1
brush.angle = 30.0
brush.size_jitter = 0.25
brush.texture = 4
brush.texture_mode = 1
brush.texture_invert = true
brush.texture_scale = 2.0
brush.dynamics[3].control = 3
brush.dynamics[1].control = 5
brush.dynamics[1].minimum = 0.25
brush.dynamics[1].curve = -0.5
brush.dynamics[2].control = 8
brush.dynamics[2].steps = 40
let tip = BrushMask.create(40, 20)
tip.dab(12.0, 10.0, 8.0, 0.5, 1.0)
brush.tip = tip
let texture = BrushMask.create(16, 16)
texture.clear(1.0)
texture.dab(8.0, 8.0, 4.0, 1.0, 0.0)
brush.texture_image = texture
return brush
## Whether two brushes have the same settings, dynamics and images.
func same_brush(a: Brush, b: Brush) -> unit!:
assert(brush_text(a, "x") == brush_text(b, "x"))
same_mask(a.tip, b.tip)
same_mask(a.texture_image, b.texture_image)
func same_mask(a: BrushMask?, b: BrushMask?) -> unit!:
if a == none or b == none:
assert(a == none and b == none)
return
let first = a else return
let second = b else return
assert(first.width() == second.width() and first.height() == second.height() and first.coverage() == second.coverage())