mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-28 17:19:24 +01:00
Assets: renaming an image also moves a generator's texture, grunge, micro details and Grayscale Conversion image
This commit is contained in:
1 parent
e53ad146c2
commit
145a4db4ec
3 files changed
+37
-1
No files matched your search
@@ -1294,6 +1294,17 @@ impl Generator {
|
||||
self.inputs.values().chain(self.texture.as_ref().and_then(|texture| texture.source.as_ref())).chain(self.grunge.as_ref()).chain(micro)
|
||||
}
|
||||
|
||||
/// [`Generator::sources`], to rename the images they read
|
||||
pub fn sources_mut(&mut self) -> impl Iterator<Item = &mut Source> {
|
||||
let grayscale = match self.controls.as_deref_mut() {
|
||||
Some(KindControls::Grayscale(grayscale)) if self.kind == GeneratorKind::GrayscaleConversion => grayscale.source.as_mut(),
|
||||
_ => None,
|
||||
};
|
||||
let micro = self.micro.iter_mut().flat_map(|micro| micro.sources_mut()).chain(grayscale);
|
||||
let texture = self.texture.as_mut().and_then(|texture| texture.source.as_mut());
|
||||
self.inputs.values_mut().chain(texture).chain(self.grunge.as_mut()).chain(micro)
|
||||
}
|
||||
|
||||
/// The micro details as saved, when the generator computes with them: with a slot switched on, except a micro detail saved before Micro Details on a generator still on the earlier model, which [`Generator::earlier_micro`] reads (bring them into range before use: saved files are not trusted)
|
||||
pub fn micro_in_use(&self) -> Option<&MicroDetails> {
|
||||
self.micro.as_deref().filter(|micro| micro.in_use(self.kind) && !(micro.earlier && self.on_earlier_model()))
|
||||
@@ -2360,7 +2371,7 @@ impl EffectKind {
|
||||
match self {
|
||||
EffectKind::Fill { source, .. } => source.images_mut(),
|
||||
EffectKind::Material(fill) => fill.images_mut().collect(),
|
||||
EffectKind::Generator(generator) => generator.inputs.values_mut().flat_map(Source::images_mut).collect(),
|
||||
EffectKind::Generator(generator) => generator.sources_mut().flat_map(Source::images_mut).collect(),
|
||||
EffectKind::ColorSelection(selection) => selection.id_map.iter_mut().collect(),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
|
||||
@@ -119,6 +119,11 @@ impl MicroDetails {
|
||||
self.height_source.iter().chain(self.normal_source.as_ref())
|
||||
}
|
||||
|
||||
/// [`MicroDetails::sources`], to rename the images they read
|
||||
pub fn sources_mut(&mut self) -> impl Iterator<Item = &mut Source> {
|
||||
self.height_source.iter_mut().chain(self.normal_source.as_mut())
|
||||
}
|
||||
|
||||
/// Every value brought into its range, non-finite ones to the defaults of `kind`
|
||||
pub fn clamped(self, kind: GeneratorKind) -> MicroDetails {
|
||||
let Some(defaults) = MicroDetails::defaults(kind) else { return self };
|
||||
|
||||
@@ -654,6 +654,26 @@ mod tests {
|
||||
assert_eq!(link.outputs.values().collect::<Vec<_>>(), [&renamed], "the link's cooked outputs follow the rename");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renaming_an_image_a_generator_reads_as_its_grunge_moves_the_grunge_with_it() {
|
||||
use texturecraft_core::{Effect, EffectKind, Generator, GeneratorKind};
|
||||
|
||||
let mut session = crate::sample_session(128, 32).unwrap();
|
||||
let png = texturecraft_formats::gray16_png(1, 1, [0.5].into_iter()).unwrap();
|
||||
let grunge = session.import_resource_bytes("grunge.png", png).unwrap();
|
||||
crate::execute(&mut session, "layer.add.image", &json!({"name": grunge})).unwrap();
|
||||
let layer_id = session.selected_layer().unwrap();
|
||||
let generator = Generator { grunge: Some(Source::image(grunge.clone())), ..Generator::new(GeneratorKind::EdgeWear) };
|
||||
let effect_id = session.doc.project.alloc_id();
|
||||
session.doc.project.layers.get_mut(&layer_id).unwrap().content_effects.push(Effect::new(effect_id, EffectKind::Generator(generator)));
|
||||
|
||||
let renamed = crate::execute(&mut session, "resource.rename", &json!({"name": grunge, "to": "worn"})).unwrap()["name"].as_str().unwrap().to_string();
|
||||
|
||||
let EffectKind::Generator(generator) = &session.doc.project.layers[&layer_id].content_effects[0].kind else { panic!("the effect stays a generator") };
|
||||
assert_ne!(renamed, grunge);
|
||||
assert_eq!(generator.grunge, Some(Source::image(renamed)), "the generator's grunge follows the rename");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reloading_reads_the_file_again_and_undo_restores_the_pixels() {
|
||||
let mut session = crate::sample_session(128, 32).unwrap();
|
||||
|
||||
Reference in new issue
Block a user