Skip to content

Commit

Permalink
Redo anim frame importing to handle all 3 types at once if wanted
Browse files Browse the repository at this point in the history
And overwrites scale values to 4/2/1 depending on hd/hd2/sd
  • Loading branch information
neivv committed Apr 16, 2023
1 parent e300538 commit 625a4cd
Show file tree
Hide file tree
Showing 11 changed files with 879 additions and 495 deletions.
61 changes: 29 additions & 32 deletions src/files.rs
Expand Up @@ -847,7 +847,7 @@ impl Files {
Ok((Files {
sprites: mainsd_sprites(mainsd.sprites().len() as u16),
mainsd_anim: Some((one_filename.into(), mainsd)),
root_path: Some(one_filename.into()),
root_path: None,
open_files: OpenFiles::new(),
sd_grp_sizes: SdGrpSizes::new(),
edits: HashMap::new(),
Expand All @@ -864,7 +864,7 @@ impl Files {
Ok((Files {
sprites: vec![SpriteFiles::DdsGrp(one_filename.into())],
mainsd_anim: None,
root_path: Some(one_filename.into()),
root_path: None,
open_files: OpenFiles::new(),
sd_grp_sizes: SdGrpSizes::new(),
edits: HashMap::new(),
Expand Down Expand Up @@ -1042,6 +1042,13 @@ impl Files {
self.open_files.clear();
}

pub fn is_anim(&self) -> bool {
match self.sprites.get(0) {
Some(SpriteFiles::DdsGrp(..)) => false,
_ => true,
}
}

pub fn sprites(&self) -> &[SpriteFiles] {
&self.sprites[..]
}
Expand Down Expand Up @@ -1133,31 +1140,21 @@ impl Files {
}
}

pub fn set_tex_changes(&mut self, sprite: usize, ty: SpriteType, changes: anim::TexChanges) {
let file = file_location(
self.mainsd_anim.as_ref().map(|x| &x.1),
&mut self.open_files,
&self.sprites,
sprite,
ty,
&self.hd_layer_names,
&self.edits,
).ok().and_then(|x| x);
pub fn set_tex_changes(
&mut self,
sprite: usize,
ty: SpriteType,
changes: anim::TexChanges,
(width, height): (u16, u16),
) {
let entry = self.edits.entry((sprite, ty));
let orig = match file.as_ref().and_then(|x| x.values_or_ref()) {
Some(s) => s,
None => {
warn!("Tried to update nonexisting sprite {}/{:?}", sprite, ty);
return;
}
};
let values = entry.or_insert_with(|| match orig {
anim::ValuesOrRef::Values(orig) => Edit::Values(EditValues {
values: orig,
tex_changes: None,
}),
anim::ValuesOrRef::Ref(i) => Edit::Ref(i),
});
let values = entry.or_insert_with(|| Edit::Values(EditValues {
values: SpriteValues {
width,
height,
},
tex_changes: None,
}));
if let Edit::Values(ref mut vals) = values {
vals.tex_changes = Some(changes);
}
Expand Down Expand Up @@ -1274,6 +1271,11 @@ impl Files {
Edit::Values(ref v) => v,
Edit::Grp(..) => unreachable!(),
};
let scale = match ty {
SpriteType::Sd => 1,
SpriteType::Hd2 => 2,
SpriteType::Hd => 4,
};
match fs::File::open(path) {
Ok(file) => {
let anim = anim::Anim::read(file)
Expand All @@ -1289,19 +1291,14 @@ impl Files {
};
anim.write_patched(
&mut out,
anim.scale(),
scale,
1,
&layer_names,
&[(0, anim::ValuesOrRef::Values(edit.values))],
tex_edits
).with_context(|| format!("Writing {}", path.display()))?;
}
Err(e) if e.kind() == io::ErrorKind::NotFound => {
let scale = match ty {
SpriteType::Sd => 1,
SpriteType::Hd2 => 2,
SpriteType::Hd => 4,
};
let layer_names = if ty == SpriteType::Sd {
self.sd_layer_names.as_ref()
.ok_or_else(|| anyhow!("Need SD layer names"))?
Expand Down
3 changes: 3 additions & 0 deletions src/frame_export.rs
Expand Up @@ -16,6 +16,7 @@ pub struct ExportLayer {
pub id: u32,
pub sub_id: u32,
pub prefix: String,
pub name: String,
pub mode: LayerExportMode,
}

Expand Down Expand Up @@ -167,6 +168,7 @@ pub fn export_frames<F: Fn(f32)>(
id: layer.id,
sub_id: layer.sub_id,
filename_prefix: layer.prefix.clone(),
name: layer.name.clone(),
encoding: match layer.mode {
LayerExportMode::Rgba => frame_info::LayerEncoding::Raw,
LayerExportMode::Green | LayerExportMode::Alpha => {
Expand Down Expand Up @@ -436,6 +438,7 @@ pub fn export_grp<F: Fn(f32)>(
id: 0,
sub_id: 0,
filename_prefix: prefix.into(),
name: "grp".into(),
encoding: frame_info::LayerEncoding::Raw,
}],
frame_types: Vec::new(),
Expand Down
4 changes: 3 additions & 1 deletion src/frame_export_dialog.rs
Expand Up @@ -39,7 +39,7 @@ pub fn frame_export_dialog(this: &Arc<SpriteInfo>, parent: &gtk::ApplicationWind
Ok(Some(o)) => o,
_ => return,
};
let layer_names = file.layer_names();
let layer_names = file.layer_names().into_owned();

let window = gtk::Window::new(gtk::WindowType::Toplevel);

Expand Down Expand Up @@ -282,9 +282,11 @@ pub fn frame_export_dialog(this: &Arc<SpriteInfo>, parent: &gtk::ApplicationWind
if !layer.check.is_active() || !layer.check.is_visible() {
return None;
}
let name = layer_names.get(layer.layer as usize)?;
let prefix = layer.entry.text();
Some(frame_export::ExportLayer {
prefix,
name: name.into(),
id: layer.layer,
sub_id: layer.sublayer,
mode: layer.export_mode,
Expand Down
49 changes: 15 additions & 34 deletions src/frame_import.rs
Expand Up @@ -585,7 +585,6 @@ pub fn import_frames<F: Fn(f32) + Sync>(
frame_scale: f32,
hd2_frame_scale: Option<f32>,
formats: &[anim::TextureFormat],
layer_names: &[String],
sprite: usize,
ty: SpriteType,
grp_path: Option<&Path>,
Expand All @@ -598,7 +597,6 @@ pub fn import_frames<F: Fn(f32) + Sync>(
first_layer: usize,
frame_scale: f32,
scale: u32,
layer_names: &[String],
report_progress: F,
) -> Result<(u32, u32), Error> {
// Try to minimize amount of memory used by keeping PNGs loaded,
Expand All @@ -619,31 +617,22 @@ pub fn import_frames<F: Fn(f32) + Sync>(
report_progress: &report_progress,
};
fn is_merge_ao_depth(
layer_names: &[String],
layer: &frame_info::Layer,
) -> bool {
let name_ok = layer_names
.get(layer.id as usize)
.filter(|&x| x == "ao_depth")
.is_some();
name_ok && layer.encoding == frame_info::LayerEncoding::SingleChannel
layer.name == "ao_depth" && layer.encoding == frame_info::LayerEncoding::SingleChannel
}

for layer in &frame_info.layers {
let alpha_used = layer_names.get(layer.id as usize)
.map(|x| layer_has_alpha_bounding_box(&x))
.unwrap_or(false);
let alpha_used = layer_has_alpha_bounding_box(&layer.name);
if layer.sub_id == 0 && alpha_used {
let merge_ao_depth = is_merge_ao_depth(layer_names, layer);
let merge_ao_depth = is_merge_ao_depth(layer);
ctx.add_layer(layer.id, true, merge_ao_depth)?;
}
}
for layer in &frame_info.layers {
let alpha_used = layer_names.get(layer.id as usize)
.map(|x| layer_has_alpha_bounding_box(&x))
.unwrap_or(false);
let alpha_used = layer_has_alpha_bounding_box(&layer.name);
if layer.sub_id == 0 && !alpha_used {
let merge_ao_depth = is_merge_ao_depth(layer_names, layer);
let merge_ao_depth = is_merge_ao_depth(layer);
ctx.add_layer(layer.id, false, merge_ao_depth)?;
}
}
Expand All @@ -669,7 +658,6 @@ pub fn import_frames<F: Fn(f32) + Sync>(
0,
frame_scale,
1,
layer_names,
|step| report_progress(step * progress_mul),
)?;
if let Some((hd2, dir)) = hd2_frame_info {
Expand All @@ -680,7 +668,6 @@ pub fn import_frames<F: Fn(f32) + Sync>(
layer_count,
hd2_frame_scale.unwrap_or(1.0),
2,
layer_names,
|step| report_progress(0.5 + step * 0.5),
)?;
}
Expand Down Expand Up @@ -713,29 +700,18 @@ pub fn import_frames<F: Fn(f32) + Sync>(
ty
};

let scale_mul = match ty {
SpriteType::Hd2 => 2u16,
_ => 1,
};

let mut changes = layout_result.encode(0, &formats, 1);
let frame_count = changes.frames.len() as u32;
for f in &mut changes.frames {
f.tex_x *= scale_mul;
f.tex_y *= scale_mul;
f.x_off *= scale_mul as i16;
f.y_off *= scale_mul as i16;
f.width = f.width * scale_mul;
f.height = f.height * scale_mul;
}
for ty in &frame_info.frame_types {
for f in ty.first_frame..ty.last_frame + 1 {
if let Some(f) = changes.frames.get_mut(f as usize) {
f.unknown = ty.frame_type;
}
}
}
files.set_tex_changes(sprite, ty, changes);
// width and height are already scaled by frame_scale
let wh_scaled = (width as u16, height as u16);
files.set_tex_changes(sprite, ty, changes, wh_scaled);
if let Some((hd2, _dir)) = hd2_frame_info {
let mut changes = layout_result.encode(layer_count, &formats, 2);
for ty in &hd2.frame_types {
Expand All @@ -745,7 +721,7 @@ pub fn import_frames<F: Fn(f32) + Sync>(
}
}
}
files.set_tex_changes(sprite, SpriteType::Hd2, changes);
files.set_tex_changes(sprite, SpriteType::Hd2, changes, wh_scaled);
}

// Resize lit frames if lit exists
Expand Down Expand Up @@ -776,6 +752,7 @@ pub fn import_grp_to_anim<F: Fn(f32) + Sync>(
];

let frame_count = grp_decode::frame_count(grp)?;
let (width, height) = grp_decode::width_height(grp)?;
let mut layout = anim_encoder::Layout::new();
let formats = [
Some(format),
Expand Down Expand Up @@ -867,7 +844,11 @@ pub fn import_grp_to_anim<F: Fn(f32) + Sync>(
}).collect()
};
changes.textures = ordered_textures;
files.set_tex_changes(sprite, sprite_type, changes);
let wh_scaled = (
width.saturating_mul(scale as u16),
height.saturating_mul(scale as u16),
);
files.set_tex_changes(sprite, sprite_type, changes, wh_scaled);
}

// Resize lit frames if lit exists
Expand Down

0 comments on commit 625a4cd

Please sign in to comment.