Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions changelog/entries/2026-11-24_shared_battle_hud_atlas.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
issues: []
prs: []
no-links: true
---

# Changed
Fresh imports pack battle HUD icons and gauges into one texture, saving 11 MiB while preserving their colors.
120 changes: 65 additions & 55 deletions crates/content/src/battle_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::font::{BitmapFont, UiTexture};
use anyhow::{Result, ensure};
use serde::{Deserialize, Serialize};
pub mod document;
mod regions;
pub use regions::HudRegions;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
Expand All @@ -24,8 +26,8 @@ pub struct Art<Font = BitmapFont> {
pub result_colors: [[[u8; 4]; 2]; 3],
/// Shared target frame followed by one pointer palette per controller.
#[serde(with = "crate::ui_art::targets")]
pub targets: [UiTexture; 5],
pub stun: UiTexture,
pub targets: [HudImage; 5],
pub stun: HudImage,
pub notices: NoticeArt,
pub combo: ComboArt,
#[serde(with = "crate::ui_art::vertical")]
Expand All @@ -36,14 +38,25 @@ pub struct Art<Font = BitmapFont> {
/// Attack, defense, up arrow, magic attack, physical/magical barriers,
/// accuracy/curse, reduced item effect, Holy Song, down arrow.
#[serde(with = "crate::ui_art::status")]
pub stat_icons: [UiTexture; 10],
pub stat_icons: [HudImage; 10],
/// Source bank roles retained in the art document; runtime prompts use the shared input atlas.
#[serde(with = "crate::ui_art::unison")]
pub unison_buttons: [UiTexture; 2],
pub commands: CommandArt,
pub feedback: FeedbackArt,
}

/// A palette-colored region placed on a shared HUD sheet. Older artwork uses
/// a whole image in the original coordinate space, with no translation.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HudImage {
#[serde(flatten)]
pub texture: UiTexture,
#[serde(default)]
pub offset: [i16; 2],
}

/// Timers, enemy inspection, and battle gauges use the shared battle atlas.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
Expand All @@ -55,13 +68,13 @@ pub struct FeedbackArt {
pub scan_vitals: [[[u8; 4]; 2]; 2],
/// Neutral and eight elements, followed by weakness/resistance headings.
#[serde(with = "crate::ui_art::scan")]
pub scan_icons: [UiTexture; 10],
pub unison_frame: UiTexture,
pub scan_icons: [HudImage; 10],
pub unison_frame: HudImage,
pub unison_ready: String,
pub unison_text: [[u8; 4]; 2],
/// Frame, needle, and fill.
#[serde(with = "crate::ui_art::escape")]
pub escape: [UiTexture; 3],
pub escape: [HudImage; 3],
#[serde(with = "crate::ui_art::sides")]
pub escape_positions: [[u16; 2]; 2],
}
Expand Down Expand Up @@ -167,19 +180,19 @@ impl CommandArt {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NoticeArt {
pub frame: UiTexture,
pub fill: UiTexture,
pub frame: HudImage,
pub fill: HudImage,
/// Martial arte, spell, item, and outcome emblems.
#[serde(with = "crate::ui_art::notices")]
pub icons: [UiTexture; 4],
pub icons: [HudImage; 4],
#[serde(with = "crate::ui_art::notices")]
pub icon_origins: [[u16; 2]; 4],
pub defeated: String,
/// Older cooked libraries predate the escape outcome notice.
#[serde(default = "escaped_notice")]
pub escaped: String,
#[serde(with = "crate::ui_art::characters")]
pub faces: [UiTexture; 9],
pub faces: [HudImage; 9],
}

fn escaped_notice() -> String {
Expand Down Expand Up @@ -242,30 +255,16 @@ impl Art {
}

pub fn files(&self) -> impl Iterator<Item = &str> {
[
self.font.texture.as_str(),
self.commands.texture.path.as_str(),
]
.into_iter()
.chain(
self.portraits
.iter()
.flatten()
.map(|image| image.path.as_str()),
)
.chain(
self.targets
.iter()
.chain([&self.stun])
.chain([&self.notices.frame, &self.notices.fill])
.chain(&self.notices.icons)
.chain(&self.notices.faces)
.chain(&self.stat_icons)
.chain(&self.feedback.scan_icons)
.chain([&self.feedback.unison_frame])
.chain(&self.feedback.escape)
.map(|image| image.path.as_str()),
)
let mut seen = std::collections::BTreeSet::new();
std::iter::once(self.font.texture.as_str())
.chain(self.required_images().map(|image| image.path.as_str()))
.chain(
self.portraits
.iter()
.flatten()
.map(|image| image.path.as_str()),
)
.filter(move |path| seen.insert(*path))
}

pub fn validate(&self) -> Result<()> {
Expand All @@ -278,14 +277,25 @@ impl Art {

fn required_images(&self) -> impl Iterator<Item = &UiTexture> {
std::iter::once(&self.commands.texture)
.chain(&self.targets)
.chain([&self.stun, &self.notices.frame, &self.notices.fill])
.chain(&self.notices.icons)
.chain(&self.notices.faces)
.chain(&self.stat_icons)
.chain(&self.feedback.scan_icons)
.chain([&self.feedback.unison_frame])
.chain(&self.feedback.escape)
.chain(self.hud_images().map(|(image, _)| &image.texture))
}

fn hud_images(&self) -> impl Iterator<Item = (&HudImage, [u16; 4])> {
let regions = HudRegions::new(self.notices.icon_origins);
self.targets
.iter()
.zip(regions.targets)
.chain([
(&self.stun, regions.stun),
(&self.notices.frame, regions.notice_frame),
(&self.notices.fill, regions.notice_fill),
])
.chain(self.notices.icons.iter().zip(regions.notice_icons))
.chain(self.notices.faces.iter().zip(regions.notice_faces))
.chain(self.stat_icons.iter().zip(regions.stat_icons))
.chain(self.feedback.scan_icons.iter().zip(regions.scan_icons))
.chain([(&self.feedback.unison_frame, regions.unison_frame)])
.chain(self.feedback.escape.iter().zip(regions.escape))
}

/// Required controls and shared geometry; optional feedback validates separately.
Expand All @@ -302,22 +312,22 @@ impl Art {
.all(|&size| (1..=128).contains(&size)),
"invalid damage numeral dimensions"
);
for image in self
.targets
.iter()
.chain([&self.stun, &self.notices.frame, &self.notices.fill])
.chain(&self.notices.icons)
.chain(&self.notices.faces)
.chain(&self.stat_icons)
.chain(&self.feedback.scan_icons)
.chain([&self.feedback.unison_frame])
.chain(&self.feedback.escape)
{
image.validate()?;
for (image, rect) in self.hud_images() {
image.texture.validate()?;
let [u, v, width, height] = rect.map(i32::from);
let [dx, dy] = image.offset.map(i32::from);
let (x, y) = (u + dx, v + dy);
ensure!(
[image.width, image.height] == [512, 512],
[image.texture.width, image.texture.height] == [512, 512]
&& image.offset.iter().all(|v| v.unsigned_abs() < 512),
"invalid battle marker atlas"
);
ensure!(
x >= 0 && y >= 0 && x + width <= 512 && y + height <= 512,
"battle HUD rectangle {rect:?} with offset {:?} exceeds {}",
image.offset,
image.texture.path
);
}
Ok(())
}
Expand Down
5 changes: 4 additions & 1 deletion crates/content/src/battle_ui/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ pub fn read(
let font: BitmapFont =
serde_json::from_slice(&bytes).with_context(|| definition.font.clone())?;
let mut art = definition.with_font(font);
let mut seen = std::collections::BTreeSet::new();
for image in art.required_images() {
crate::ui_art::prepare_image(image, &mut *read)?;
if seen.insert(&image.path) {
crate::ui_art::prepare_image(image, &mut *read)?;
}
}
for portrait in &mut art.portraits {
if let Some(image) = portrait
Expand Down
56 changes: 56 additions & 0 deletions crates/content/src/battle_ui/regions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! Source rectangles sampled by battle HUD roles, in [x, y, width, height] order.

type Rect = [u16; 4];

pub struct HudRegions {
pub targets: [Rect; 5],
pub stun: Rect,
pub notice_frame: Rect,
pub notice_fill: Rect,
pub notice_icons: [Rect; 4],
pub notice_faces: [Rect; 9],
pub stat_icons: [Rect; 10],
pub scan_icons: [Rect; 10],
pub unison_frame: Rect,
pub escape: [Rect; 3],
}

impl HudRegions {
pub fn new(notice_icon_origins: [[u16; 2]; 4]) -> Self {
let mut targets = [[48, 256, 144, 64]; 5];
targets[0] = [0, 256, 48, 64];
Self {
targets,
stun: [256, 25, 48, 30],
notice_frame: [384, 376, 128, 80],
notice_fill: [408, 456, 104, 56],
notice_icons: notice_icon_origins.map(|[u, v]| [u, v, 24, 24]),
// Notices and Unison use slightly different cuts of the last three faces.
notice_faces: std::array::from_fn(|i| {
[64 + i as u16 * 32, 104, if i >= 6 { 40 } else { 32 }, 32]
}),
stat_icons: [
[288, 136, 24, 24],
[352, 56, 24, 24],
[304, 56, 24, 24],
[328, 80, 24, 24],
[256, 56, 24, 24],
[280, 56, 24, 24],
// Accuracy and the blinking curse overlay share this role.
[0, 72, 280, 32],
[32, 72, 24, 24],
[352, 80, 24, 24],
[328, 56, 24, 24],
],
scan_icons: std::array::from_fn(|i| {
if i < 9 {
[i as u16 * 24, 136, 24, 24]
} else {
[214, 136, 48, 24]
}
}),
unison_frame: [0, 368, 352, 144],
escape: [[304, 208, 144, 80], [448, 208, 16, 32], [312, 208, 128, 80]],
}
}
}
10 changes: 9 additions & 1 deletion crates/content/src/ui_art/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn named_battle_roles_mod_independently_and_bad_portraits_stay_optional() -> Res
};
assert!(battle_ui::document::read(&mut read, &Diagnostics::new(true)).is_err());
let diagnostics = Diagnostics::new(false);
let art = battle_ui::document::read(&mut read, &diagnostics)?;
let mut art = battle_ui::document::read(&mut read, &diagnostics)?;
assert!(art.portraits[0].is_none());
assert!(art.portraits[1..].iter().all(Option::is_some));
assert_eq!(art.commands.names[4], "Inventory");
Expand All @@ -46,6 +46,14 @@ fn named_battle_roles_mod_independently_and_bad_portraits_stay_optional() -> Res
original["commands"]["names"]["techniques"]
);
assert!(diagnostics.has_errors());
// A mod can move its 24×24 emblem right up to either sheet edge.
art.notices.icon_origins[0] = [20, 30];
art.notices.icons[0].offset = [-20, 458];
art.validate_layout()?;
for offset in [[-21, 458], [-20, 459]] {
art.notices.icons[0].offset = offset;
assert!(art.validate_layout().is_err(), "accepted offset {offset:?}");
}
let mut invalid = original.clone();
invalid["version"] = 44.into();
assert!(serde_json::from_value::<battle_ui::Art<String>>(invalid).is_err());
Expand Down
Loading
Loading