mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-28 17:19:24 +01:00
Make the project renamable
This commit is contained in:
1 parent
6b01f32eab
commit
ab78d69fd8
6 files changed
+106
No files matched your search
@@ -9,6 +9,9 @@ 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!(
|
||||
@@ -22,6 +25,15 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
),
|
||||
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",
|
||||
@@ -51,6 +63,23 @@ fn new(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(crate::inspect::session(s))
|
||||
}
|
||||
|
||||
fn rename(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let name = strp(p, "name")
|
||||
.map(str::trim)
|
||||
.filter(|name| !name.is_empty() && name.chars().count() <= MAX_PROJECT_NAME_CHARS && !name.chars().any(char::is_control))
|
||||
.ok_or_else(|| bad("project.rename", format!("`name` must be 1-{MAX_PROJECT_NAME_CHARS} characters without control characters")))?;
|
||||
|
||||
// The same name is not an undo step and leaves a saved project unmodified
|
||||
if name != s.doc.project.name {
|
||||
s.edit("Rename project", |s| {
|
||||
s.doc.project.name = name.to_string();
|
||||
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))?;
|
||||
|
||||
@@ -63,6 +63,7 @@ mod perf;
|
||||
mod polygon_fill;
|
||||
mod project_recovery;
|
||||
mod rebuilt_uv_layout;
|
||||
mod rename_project;
|
||||
mod resources_used;
|
||||
mod sample;
|
||||
mod solo;
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
//! `project.rename`: the name `$project` stands for in export file names.
|
||||
|
||||
use serde_json::json;
|
||||
use texturecraft_engine::{Session, execute};
|
||||
|
||||
use texturecraft_engine::geom::primitives;
|
||||
|
||||
#[test]
|
||||
fn a_renamed_project_exports_under_its_new_name_until_undone() {
|
||||
let mut session = Session::new_project("hero", primitives::cube(2.0), "builtin:cube", None, 128).unwrap();
|
||||
let mut preset = execute(&mut session, "export.preset.get", &json!({"name": "Document channels + Normal + AO (No Alpha)"})).unwrap();
|
||||
preset["maps"][0]["file"] = json!("$project_$textureSet");
|
||||
|
||||
execute(&mut session, "project.rename", &json!({"name": " Knight final "})).unwrap();
|
||||
|
||||
let plan = execute(&mut session, "export.plan", &json!({"preset": preset})).unwrap().to_string();
|
||||
assert!(plan.contains("Knight_final_") && !plan.contains("hero_"), "{plan}");
|
||||
|
||||
execute(&mut session, "edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(session.doc.project.name, "hero");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unusable_or_unchanged_project_name_edits_nothing() {
|
||||
let mut session = Session::new_project("hero", primitives::cube(2.0), "builtin:cube", None, 128).unwrap();
|
||||
for name in [json!(null), json!(7), json!(" "), json!("a\nb"), json!("x".repeat(129))] {
|
||||
assert!(execute(&mut session, "project.rename", &json!({"name": name.clone()})).is_err(), "{name}");
|
||||
}
|
||||
|
||||
execute(&mut session, "project.rename", &json!({"name": "hero"})).unwrap();
|
||||
|
||||
assert_eq!(session.doc.project.name, "hero");
|
||||
assert!(!session.modified, "nothing changed");
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use egui::{Key, Modifiers};
|
||||
use serde_json::{Value, json};
|
||||
use texturecraft_engine::core::NormalFormat;
|
||||
use texturecraft_engine::project_cmds::MAX_PROJECT_NAME_CHARS;
|
||||
|
||||
use crate::TextureCraftApp;
|
||||
use crate::dock::info_icon;
|
||||
@@ -44,6 +45,7 @@ pub(crate) fn open(app: &mut TextureCraftApp) {
|
||||
mesh: None,
|
||||
reimport: false,
|
||||
preserve: true,
|
||||
name: None,
|
||||
normal_format: None,
|
||||
color_management: crate::project_form::ColorManagement::Legacy,
|
||||
confirm: false,
|
||||
@@ -211,6 +213,18 @@ pub(crate) fn show(app: &mut TextureCraftApp, ctx: &egui::Context) {
|
||||
ui.add_space(12.0);
|
||||
ui.label(caption(tr("PROJECT SETTINGS")));
|
||||
ui.add_space(4.0);
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_space(ROW_INDENT_POINTS);
|
||||
let label = ui.label(tr("Project name")).id;
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
info_icon(ui, tr("The name $project stands for in export file names. The project file keeps its own name."));
|
||||
let mut name = config.name.clone().unwrap_or_else(|| project.name.clone());
|
||||
let field = egui::TextEdit::singleline(&mut name).char_limit(MAX_PROJECT_NAME_CHARS).desired_width(FIELD_WIDTH_POINTS);
|
||||
if ui.add(field).labelled_by(label).changed() {
|
||||
config.name = Some(name);
|
||||
}
|
||||
});
|
||||
});
|
||||
let shown_format = config.normal_format.unwrap_or(current_normal_format);
|
||||
row(
|
||||
ui,
|
||||
@@ -328,6 +342,11 @@ pub(crate) fn show(app: &mut TextureCraftApp, ctx: &egui::Context) {
|
||||
}
|
||||
None => true,
|
||||
}
|
||||
// An untouched name is left alone, even one longer than a typed name may be
|
||||
&& match config.name.as_deref() {
|
||||
Some(name) => app.run("project.rename", json!({"name": name})).is_some(),
|
||||
None => true,
|
||||
}
|
||||
};
|
||||
|
||||
if !done && !cancel && open {
|
||||
|
||||
@@ -7,6 +7,8 @@ pub struct ProjectConfig {
|
||||
pub reimport: bool,
|
||||
/// Whether painted strokes follow the surface of a newly imported mesh
|
||||
pub preserve: bool,
|
||||
/// The project name as typed, when the field was edited
|
||||
pub name: Option<String>,
|
||||
/// The selected normal map format when it differs from the project's
|
||||
pub normal_format: Option<texturecraft_engine::core::NormalFormat>,
|
||||
/// Color management as chosen in the window; nothing is applied (the project stays Legacy)
|
||||
|
||||
@@ -765,3 +765,24 @@ fn project_config_snapshots() {
|
||||
h.run_steps(6);
|
||||
h.render().unwrap().save(output_dir.join("project-config-ocio.png")).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_typed_project_name_applies_only_with_ok() {
|
||||
let dir = temp_dir("project-name");
|
||||
let mut h = app_on_a_quad(&dir);
|
||||
let before = h.state().session.doc.project.name.clone();
|
||||
|
||||
for button in ["Cancel", "OK"] {
|
||||
open_window(&mut h);
|
||||
in_window(&h, Role::TextInput, "Project name").focus();
|
||||
h.run_steps(2);
|
||||
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
|
||||
in_window(&h, Role::TextInput, "Project name").type_text("Knight");
|
||||
h.run_steps(2);
|
||||
click_in_window(&mut h, Role::Button, button);
|
||||
|
||||
let expected = if button == "OK" { "Knight" } else { before.as_str() };
|
||||
assert_eq!(h.state().session.doc.project.name, expected, "after {button}");
|
||||
assert!(!window_is_open(&h), "{button} closes the window");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user