dymokomi/luced-2d
Source of release 0.5.0
## Tool options remembered between runs, as Photoshop keeps them: the tool in
## hand, the brush in hand whole (settings, dynamics, tip and texture, in a
## folder beside the file, with the library brush it came from), the selection, wand, type, gradient, clone, healing,
## smudge and transform
## options and the crop ratio, in `~/.luced-2d/tools.toml`, written when the
## window closes and read when it opens. What the file lacks or gets out of
## range keeps its default; an unreadable file is passed over.
from luce_config.toml import Table
from workspace import Workspace
from tools import tools
from luce_std import files, paths
from brush_library import SavedBrush, load_brush, save_brush, quoted
## Where the options live, beside the settings.
pub func tools_path(directory: str = "") -> str!:
let chosen = directory if directory != "" else paths.join(files.home_directory(), ".luced-2d")
files.ensure_directory(chosen)
return paths.join(chosen, "tools.toml")
## The workspace's options as `path` last kept them.
pub func restore_tools(workspace: Workspace, path: str) -> unit!:
let source = files.read_text(path, limit = 65536) catch failure:
_ = failure
return
let table = Table(source) catch failure:
_ = failure
return
let tool = words(table, "tool.current", workspace.tools.tool)
for known in tools():
if known.id == tool:
workspace.tools.tool = tool
# The brush in hand, whole, from its folder beside the options.
if boolean(table, "brush.kept", false):
restore_brush(workspace, table, paths.directory(path))
let selecting = workspace.tools.selecting
selecting.mode = int(number(table, "selection.mode", float(selecting.mode), 0.0, 3.0))
selecting.feather = int(number(table, "selection.feather", float(selecting.feather), 0.0, 250.0))
selecting.antialias = boolean(table, "selection.antialias", selecting.antialias)
selecting.sample = int(number(table, "selection.sample", float(selecting.sample), 1.0, 5.0))
selecting.all_layers = boolean(table, "selection.all_layers", selecting.all_layers)
workspace.tools.tolerance = number(table, "selection.tolerance", workspace.tools.tolerance, 0.0, 255.0)
workspace.tools.contiguous = boolean(table, "selection.contiguous", workspace.tools.contiguous)
let typing = workspace.tools.typing
typing.family = words(table, "type.family", typing.family)
typing.style = words(table, "type.style", typing.style)
typing.size = number(table, "type.size", typing.size, 1.0, 1296.0)
typing.alignment = int(number(table, "type.alignment", float(typing.alignment), 0.0, 2.0))
workspace.tools.gradient_radial = boolean(table, "gradient.radial", workspace.tools.gradient_radial)
workspace.tools.gradient_reverse = boolean(table, "gradient.reverse", workspace.tools.gradient_reverse)
workspace.tools.gradient_transparent = boolean(table, "gradient.transparent", workspace.tools.gradient_transparent)
workspace.tools.clone_aligned = boolean(table, "clone.aligned", workspace.tools.clone_aligned)
workspace.tools.clone_all = boolean(table, "clone.all_layers", workspace.tools.clone_all)
workspace.tools.smear_mode = int(number(table, "smear.mode", float(workspace.tools.smear_mode), 0.0, 2.0))
workspace.tools.tone_range = int(number(table, "tone.range", float(workspace.tools.tone_range), 0.0, 2.0))
workspace.tools.tone_exposure = number(table, "tone.exposure", workspace.tools.tone_exposure, 0.01, 1.0)
workspace.tools.auto_select = boolean(table, "move.auto_select", workspace.tools.auto_select)
workspace.transforms.transform_sampling = int(number(table, "transform.sampling", float(workspace.transforms.transform_sampling), 0.0, 2.0))
workspace.transforms.lock_transform_ratio = boolean(table, "transform.lock_ratio", workspace.transforms.lock_transform_ratio)
workspace.image.crop_ratio = number(table, "crop.ratio", workspace.image.crop_ratio, 0.0, 100.0)
workspace.tools.shape_weight = number(table, "shape.weight", workspace.tools.shape_weight, 1.0, 1000.0)
workspace.tools.shape_sides = int(number(table, "shape.sides", float(workspace.tools.shape_sides), 3.0, 64.0))
workspace.tools.shape_fill = boolean(table, "shape.fill", workspace.tools.shape_fill)
workspace.tools.shape_stroke = boolean(table, "shape.stroke", workspace.tools.shape_stroke)
workspace.tools.shape_stroke_width = number(table, "shape.stroke_width", workspace.tools.shape_stroke_width, 0.5, 500.0)
workspace.tools.shape_radius = number(table, "shape.radius", workspace.tools.shape_radius, 0.0, 1000.0)
## The brush kept in `directory`'s brush folder as the brush in hand; one
## that does not read keeps the brush there is.
func restore_brush(workspace: Workspace, table: Table, directory: str) -> unit!:
let saved = load_brush(directory, brush_folder) catch failure:
_ = failure
return
saved.brush.preset = words(table, "brush.preset", saved.name)
saved.brush.folder = words(table, "brush.folder", "")
workspace.tools.brush = saved.brush
## Keep the workspace's options at `path`.
pub func remember_tools(workspace: Workspace, path: str) -> unit!:
let brush = workspace.tools.brush
save_brush(paths.directory(path), SavedBrush(brush.preset, brush_folder, brush))
files.write_text(path, serialize_tools(workspace))
## The brush in hand's folder beside tools.toml: every setting, its dynamics,
## tip and texture (brush_library.luc's form), so it comes back as it was.
let brush_folder: str = "brush-in-hand"
## The options file's text.
pub func serialize_tools(workspace: Workspace) -> str:
let brush = workspace.tools.brush
let selecting = workspace.tools.selecting
let typing = workspace.tools.typing
var text = "# luced-2d's tool options, kept when the window closes.\n\n"
text = text + f"[tool]\ncurrent = \"{workspace.tools.tool}\"\n\n"
text = text + f"[brush]\n# The brush in hand is kept whole in {brush_folder}/; the library brush it came from, if any.\nkept = true\npreset = {quoted(brush.preset)}\nfolder = {quoted(brush.folder)}\n\n"
text = text + f"[selection]\nmode = {selecting.mode}\nfeather = {selecting.feather}\nantialias = {flag(selecting.antialias)}\nsample = {selecting.sample}\n"
text = text + f"all_layers = {flag(selecting.all_layers)}\ntolerance = {workspace.tools.tolerance}\ncontiguous = {flag(workspace.tools.contiguous)}\n\n"
text = text + f"[type]\nfamily = \"{typing.family}\"\nstyle = \"{typing.style}\"\nsize = {typing.size}\nalignment = {typing.alignment}\n\n"
text = text + f"[gradient]\nradial = {flag(workspace.tools.gradient_radial)}\nreverse = {flag(workspace.tools.gradient_reverse)}\ntransparent = {flag(workspace.tools.gradient_transparent)}\n\n"
text = text + f"[clone]\naligned = {flag(workspace.tools.clone_aligned)}\nall_layers = {flag(workspace.tools.clone_all)}\n\n"
text = text + f"[smear]\nmode = {workspace.tools.smear_mode}\n\n[tone]\nrange = {workspace.tools.tone_range}\nexposure = {workspace.tools.tone_exposure}\n\n"
text = text + f"[move]\nauto_select = {flag(workspace.tools.auto_select)}\n\n"
text = text + f"[transform]\nsampling = {workspace.transforms.transform_sampling}\nlock_ratio = {flag(workspace.transforms.lock_transform_ratio)}\n\n"
text = text + f"[crop]\nratio = {workspace.image.crop_ratio}\n\n"
text = text + f"[shape]\nweight = {workspace.tools.shape_weight}\nsides = {workspace.tools.shape_sides}\nfill = {flag(workspace.tools.shape_fill)}\nstroke = {flag(workspace.tools.shape_stroke)}\nstroke_width = {workspace.tools.shape_stroke_width}\nradius = {workspace.tools.shape_radius}\n"
return text
## A number the table holds within `minimum`..`maximum`, else `fallback`.
func number(table: Table, key: str, fallback: float, minimum: float, maximum: float) -> float:
return table.number(key, fallback, minimum, maximum) catch _:
recover fallback
func words(table: Table, key: str, fallback: str) -> str:
return table.string(key, fallback) catch _:
recover fallback
func boolean(table: Table, key: str, fallback: bool) -> bool:
return table.boolean(key, fallback) catch _:
recover fallback
func flag(value: bool) -> str:
return "true" if value else "false"