mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-30 10:03:56 +01:00
Preserve both branches’ engine tests. Validate the project name before applying other dialog settings, clear discarded text undo history, and keep an unsaved project’s autosave filename stable across renames. Expand export tokens once, preserve literal text in names, and sanitize flat stems directly. Reuse the dialog’s name buffer and update the command documentation and generated headless parity report.
141 lines
5.9 KiB
Rust
141 lines
5.9 KiB
Rust
//! Project-level commands: new project, rename, sample project, smart materials, inspection.
|
|
|
|
use serde_json::{Value, json};
|
|
use texturecraft_compose::BakedMaps;
|
|
use texturecraft_core::{BakeResolution, CurvatureMethod};
|
|
use texturecraft_geom::primitives;
|
|
|
|
use crate::commands::{CommandSpec, always, bad, boolp, strp, u64p};
|
|
use crate::materials::Place;
|
|
use crate::{Result, Session, cmd};
|
|
|
|
/// Longest project name, in characters; it goes into export file names.
|
|
pub const MAX_PROJECT_NAME_CHARS: usize = 128;
|
|
|
|
pub fn specs() -> Vec<CommandSpec> {
|
|
vec![
|
|
cmd!(
|
|
"project.new",
|
|
"New project from a built-in mesh",
|
|
[],
|
|
None,
|
|
"{mesh?: material_ball|sphere|cube|rounded_cube|cylinder|torus|plane|crate|udim_plane, resolution?: 1024 (or the template's), name?, uvTiles?: bool (UDIM; default: when the UVs leave tile 1001), template?: project template name (see project.templates)}",
|
|
always,
|
|
new
|
|
),
|
|
cmd!("project.sample", "Open sample project", ["File"], None, "{}", always, sample),
|
|
cmd!("project.inspect", "Inspect project", [], None, "{}", always, |s, _| Ok(crate::inspect::session(s))),
|
|
cmd!(
|
|
"project.rename",
|
|
"Rename project",
|
|
[],
|
|
None,
|
|
"{name: 1-128 characters without control characters} — the name `$project` stands for in export file names; the project file keeps its own name",
|
|
always,
|
|
rename
|
|
),
|
|
cmd!(
|
|
"material.apply",
|
|
"Add smart material",
|
|
[],
|
|
None,
|
|
"{material: id or label, above?: layer id, inside?: bool, below?: layer id, top?: true, index?: n} — above the selection by default; `above` (+ `inside` for a folder), `below`, `top` or `index` in the top-level stack (0 = bottom) place it",
|
|
always,
|
|
material
|
|
),
|
|
cmd!("material.list", "List materials", [], None, "{}", always, |_, _| Ok(json!(
|
|
crate::materials::MATERIALS.iter().map(|m| json!({"id": m.id, "label": m.label, "smart": m.smart})).collect::<Vec<_>>()
|
|
))),
|
|
]
|
|
}
|
|
|
|
fn new(s: &mut Session, p: &Value) -> Result<Value> {
|
|
let mesh_id = strp(p, "mesh").unwrap_or("material_ball");
|
|
let mesh = primitives::builtin(mesh_id).ok_or_else(|| bad("project.new", format!("unknown built-in mesh `{mesh_id}`")))?;
|
|
let template = crate::template_cmds::template_param(s, p, "project.new")?;
|
|
let res = u64p(p, "resolution").or(template.as_ref().map(|t| u64::from(t.resolution))).unwrap_or(1024);
|
|
let name = strp(p, "name").unwrap_or("Untitled");
|
|
let mut next = Session::new_project_tiled(name, mesh, &format!("builtin:{mesh_id}"), None, u32::try_from(res).unwrap_or(0), boolp(p, "uvTiles"))?;
|
|
if let Some(t) = &template {
|
|
next.apply_template(t);
|
|
}
|
|
s.switch_to(next);
|
|
Ok(crate::inspect::session(s))
|
|
}
|
|
|
|
/// Trim and validate a proposed name without changing the project.
|
|
pub fn validate_project_name(name: &str) -> Result<&str> {
|
|
let name = name.trim();
|
|
if name.is_empty() || name.chars().nth(MAX_PROJECT_NAME_CHARS).is_some() || name.chars().any(char::is_control) {
|
|
return Err(bad("project.rename", format!("`name` must be 1-{MAX_PROJECT_NAME_CHARS} characters without control characters")));
|
|
}
|
|
|
|
Ok(name)
|
|
}
|
|
|
|
fn rename(session: &mut Session, params: &Value) -> Result<Value> {
|
|
let name = validate_project_name(strp(params, "name").unwrap_or_default())?;
|
|
|
|
// The same name is not an undo step and leaves a saved project unmodified
|
|
if name != session.doc.project.name {
|
|
session.edit("Rename project", |session| {
|
|
session.doc.project.name = name.to_owned();
|
|
Ok(())
|
|
})?;
|
|
}
|
|
|
|
Ok(json!({ "name": name }))
|
|
}
|
|
|
|
fn material(s: &mut Session, p: &Value) -> Result<Value> {
|
|
let id = strp(p, "material").ok_or_else(|| bad("material.apply", "`material` is required"))?;
|
|
let l = crate::materials::apply_at(s, id, Place::from_params(p))?;
|
|
Ok(json!({ "layer": l.0 }))
|
|
}
|
|
|
|
/// A baked material ball with smart materials on both texture sets
|
|
pub fn sample_session(resolution: u32, bake_resolution: u32) -> Result<Session> {
|
|
let mut session = Session::new_project("Material Ball", primitives::material_ball(), "builtin:material_ball", None, resolution)?;
|
|
for set_index in 0..session.set_count() {
|
|
let set = session.doc.project.texture_set_mut(set_index)?;
|
|
set.bake.common.resolution = BakeResolution::from_pixels(bake_resolution).ok_or_else(|| bad("project.sample", "invalid bake resolution"))?;
|
|
|
|
// Mesh curvature gives the procedural sample visible edge wear
|
|
set.bake.high_poly.use_low_as_high = true;
|
|
set.bake.maps.curvature.parameters.method = CurvatureMethod::Mesh;
|
|
for ray_settings in
|
|
[&mut set.bake.maps.ao.parameters.rays, &mut set.bake.maps.thickness.parameters.rays, &mut set.bake.maps.bent_normal.parameters.rays]
|
|
{
|
|
ray_settings.rays = 24;
|
|
}
|
|
session.bake_now(set_index, false)?;
|
|
}
|
|
session.active_set = 1;
|
|
crate::materials::apply(&mut session, "iron_rusted")?;
|
|
session.active_set = 0;
|
|
crate::materials::apply(&mut session, "steel_painted_worn")?;
|
|
|
|
// Paint above the smart material so new strokes remain visible
|
|
for set_index in (0..session.set_count()).rev() {
|
|
session.active_set = set_index;
|
|
session.select_none(set_index);
|
|
crate::execute(&mut session, "layer.add.paint", &Value::Null)?;
|
|
}
|
|
session.undo.clear();
|
|
session.modified = false;
|
|
Ok(session)
|
|
}
|
|
|
|
fn sample(s: &mut Session, _: &Value) -> Result<Value> {
|
|
let next = sample_session(2048, 1024)?;
|
|
s.switch_to(next);
|
|
Ok(crate::inspect::session(s))
|
|
}
|
|
|
|
pub(crate) fn baked_json(b: Option<&BakedMaps>) -> Value {
|
|
match b {
|
|
Some(b) => json!({ "resolution": b.resolution, "maps": b.maps.keys().map(|m| m.id()).collect::<Vec<_>>() }),
|
|
None => Value::Null,
|
|
}
|
|
}
|