aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-assets/src/lib.rs1
-rw-r--r--crates/fparkan-assets/src/mission_pager.rs210
-rw-r--r--crates/fparkan-assets/src/ui_sprite.rs53
-rw-r--r--crates/fparkan-font/src/bitmap.rs37
4 files changed, 299 insertions, 2 deletions
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs
index d55c9f83..810a5300 100644
--- a/crates/fparkan-assets/src/lib.rs
+++ b/crates/fparkan-assets/src/lib.rs
@@ -37,6 +37,7 @@ mod control_weapon_sound;
mod explosion_profile;
pub mod game_font;
pub mod mission_loader;
+pub mod mission_pager;
pub mod object_descriptions;
pub mod satellite_map;
mod turret_elevation;
diff --git a/crates/fparkan-assets/src/mission_pager.rs b/crates/fparkan-assets/src/mission_pager.rs
new file mode 100644
index 00000000..97708880
--- /dev/null
+++ b/crates/fparkan-assets/src/mission_pager.rs
@@ -0,0 +1,210 @@
+//! Current-message panel drawn in the original cockpit coordinate space.
+
+use crate::{
+ game_font::{load_game_font, BitmapFont},
+ ui_sprite::{load_ui_sprites, source_over},
+ RgbaImage,
+};
+use fparkan_vfs::Vfs;
+
+const PANEL: [u32; 3] = [230, 0, 182];
+const WRAP_WIDTH: u32 = 172;
+const FONT_SCALE: f64 = 1.25;
+const MAX_BODY_LINES: usize = 6;
+
+/// Original frame parts and the gameplay bitmap font.
+pub struct MissionPager {
+ parts: [RgbaImage; 6],
+ font: BitmapFont,
+}
+
+impl MissionPager {
+ /// Loads the frame and 800×600 font from the installed game.
+ pub fn load(vfs: &dyn Vfs) -> Result<Self, String> {
+ let parts = load_ui_sprites(
+ vfs,
+ "ui/compaund.cfg",
+ [
+ "ccres_frame_corner_1",
+ "ccres_frame_corner_2",
+ "ccres_frame_corner_3",
+ "ccres_frame_corner_4",
+ "ccres_frame_edge_h",
+ "ccres_frame_edge_v",
+ ],
+ )?;
+ Ok(Self {
+ parts,
+ font: load_game_font(vfs)?,
+ })
+ }
+
+ /// Produces the panel texture and its logical left, top, width and height.
+ pub fn paint(
+ &self,
+ sender: &str,
+ body: &str,
+ overflow_hint: &str,
+ ) -> Result<(RgbaImage, [f32; 4]), String> {
+ let line_width = (f64::from(WRAP_WIDTH) * FONT_SCALE * 0.98).round_ties_even() as u16;
+ let mut lines = self.font.wrap_pager_lines(body, line_width)?;
+ let overflow = lines.len() > MAX_BODY_LINES;
+ if overflow {
+ lines.truncate(MAX_BODY_LINES);
+ let last = lines.last_mut().expect("pager retains six lines");
+ last.truncate(last.trim_end().len());
+ let limit = f64::from(WRAP_WIDTH) * FONT_SCALE * 0.95;
+ let mut end = last.len();
+ last.push_str("...");
+ while f64::from(self.font.line_metrics(last)?[0]) > limit {
+ let Some((index, _)) = last[..end].char_indices().next_back() else {
+ break;
+ };
+ last.replace_range(index..end, "");
+ end = index;
+ }
+ }
+ let [_, glyph_height] = self.font.line_metrics("")?;
+ // The glyph raster includes the last row beyond the TFNT vertical extent.
+ let advance = (f64::from(glyph_height + 1) / FONT_SCALE).round_ties_even() as u32;
+ let [x, y, width] = PANEL;
+ let footer = overflow && !overflow_hint.is_empty();
+ let [sender_y, first_line_y, height] = panel_rows(advance, lines.len() as u32, footer);
+ let pixels = [scale(width), scale(height)];
+ let mut panel = RgbaImage {
+ width: pixels[0],
+ height: pixels[1],
+ rgba8: vec![0; (pixels[0] * pixels[1] * 4) as usize],
+ };
+ self.paint_frame(&mut panel, width, height);
+ self.paint_text(&mut panel, sender, sender_y, 255)?;
+ for (index, line) in lines.iter().enumerate() {
+ self.paint_text(&mut panel, line, first_line_y + index as u32 * advance, 220)?;
+ }
+ if footer {
+ let y = first_line_y + lines.len() as u32 * advance + advance / 2;
+ self.paint_text(&mut panel, overflow_hint, y, 255)?;
+ }
+ Ok((panel, [x as f32, y as f32, width as f32, height as f32]))
+ }
+
+ fn paint_frame(&self, panel: &mut RgbaImage, width: u32, height: u32) {
+ let [tl, tr, br, bl, horizontal, vertical] = &self.parts;
+ let middle = [8, 8, width - 8, height - 8];
+ for (sprite, rect, flip) in [
+ (tl, [0, 0, 8, 8], [false, false]),
+ (tr, [width - 8, 0, width, 8], [false, false]),
+ (br, [width - 8, height - 8, width, height], [false, false]),
+ (bl, [0, height - 8, 8, height], [false, false]),
+ (
+ horizontal,
+ [middle[0], 0, middle[2], horizontal.height],
+ [false, false],
+ ),
+ (
+ horizontal,
+ [middle[0], height - horizontal.height, middle[2], height],
+ [false, true],
+ ),
+ (
+ vertical,
+ [0, middle[1], vertical.width, middle[3]],
+ [true, false],
+ ),
+ (
+ vertical,
+ [width - vertical.width, middle[1], width, middle[3]],
+ [false, false],
+ ),
+ ] {
+ copy_sprite(panel, sprite, rect.map(scale), flip, 255);
+ }
+ let inset = horizontal.height;
+ let [left, top, right, bottom] = [inset, inset, width - inset, height - inset].map(scale);
+ for y in top..bottom {
+ for x in left..right {
+ let dest = offset(panel.width, x, y);
+ source_over(&mut panel.rgba8[dest..dest + 4], &[0, 128, 0, 128]);
+ }
+ }
+ }
+
+ fn paint_text(
+ &self,
+ panel: &mut RgbaImage,
+ text: &str,
+ y: u32,
+ tint: u8,
+ ) -> Result<(), String> {
+ let [w, h] = self.font.line_metrics(text)?;
+ if w == 0 {
+ return Ok(());
+ }
+ let sprite = self.font.centered_line(text, w, h)?;
+ let [x, y] = [scale(8), scale(y)];
+ copy_sprite(
+ panel,
+ &sprite,
+ [x, y, x + sprite.width, y + sprite.height],
+ [false, false],
+ tint,
+ );
+ Ok(())
+ }
+}
+
+fn panel_rows(advance: u32, lines: u32, footer: bool) -> [u32; 3] {
+ let labels = 1 + u32::from(footer);
+ [
+ 9,
+ 9 + 3 * advance / 2,
+ 16 + labels * advance / 2 + (lines + labels) * advance,
+ ]
+}
+
+fn scale(coordinate: u32) -> u32 {
+ (f64::from(coordinate) * FONT_SCALE).round_ties_even() as u32
+}
+
+fn copy_sprite(
+ panel: &mut RgbaImage,
+ sprite: &RgbaImage,
+ [left, top, right, bottom]: [u32; 4],
+ [flip_x, flip_y]: [bool; 2],
+ tint: u8,
+) {
+ for y in top..bottom.min(panel.height) {
+ let mut v = (y - top) * sprite.height / (bottom - top);
+ if flip_y {
+ v = sprite.height - 1 - v;
+ }
+ for x in left..right.min(panel.width) {
+ let mut u = (x - left) * sprite.width / (right - left);
+ if flip_x {
+ u = sprite.width - 1 - u;
+ }
+ let src = offset(sprite.width, u, v);
+ let dest = offset(panel.width, x, y);
+ let mut texel = [0; 4];
+ texel.copy_from_slice(&sprite.rgba8[src..src + 4]);
+ for component in &mut texel[..3] {
+ *component = (u16::from(*component) * u16::from(tint) / 255) as u8;
+ }
+ source_over(&mut panel.rgba8[dest..dest + 4], &texel);
+ }
+ }
+}
+
+fn offset(width: u32, x: u32, y: u32) -> usize {
+ ((y * width + x) * 4) as usize
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn cockpit_panel_grows_by_one_native_line_per_wrapped_row() {
+ assert_eq!(super::PANEL, [230, 0, 182]);
+ assert_eq!(super::panel_rows(10, 2, false), [9, 24, 51]);
+ assert_eq!(super::panel_rows(10, 2, true), [9, 24, 66]);
+ }
+}
diff --git a/crates/fparkan-assets/src/ui_sprite.rs b/crates/fparkan-assets/src/ui_sprite.rs
index fe4c77e8..812f1e6c 100644
--- a/crates/fparkan-assets/src/ui_sprite.rs
+++ b/crates/fparkan-assets/src/ui_sprite.rs
@@ -58,17 +58,66 @@ pub fn load_ui_sprites<const N: usize>(
.copied()
})
.collect();
- sprites.push(RgbaImage {
+ let image = RgbaImage {
width,
height,
rgba8,
- });
+ };
+ let angle = if region.contains_key("ROTATE") {
+ number(&region, "ROTATE")?
+ } else {
+ 0
+ };
+ sprites.push(rotate(image, angle)?);
}
Ok(sprites
.try_into()
.expect("one rectangle per UI sprite name"))
}
+fn rotate(image: RgbaImage, angle: u32) -> Result<RgbaImage, String> {
+ let (w, h) = (image.width, image.height);
+ let (width, height) = match angle {
+ 0 => return Ok(image),
+ 90 | 270 => (h, w),
+ 180 => (w, h),
+ _ => return Err(format!("invalid UI sprite rotation {angle}")),
+ };
+ let mut rgba8 = Vec::with_capacity(image.rgba8.len());
+ for y in 0..height {
+ for x in 0..width {
+ let (u, v) = match angle {
+ 90 => (y, h - 1 - x),
+ 180 => (w - 1 - x, h - 1 - y),
+ _ => (w - 1 - y, x),
+ };
+ let start = ((v * w + u) * 4) as usize;
+ rgba8.extend_from_slice(&image.rgba8[start..start + 4]);
+ }
+ }
+ Ok(RgbaImage {
+ width,
+ height,
+ rgba8,
+ })
+}
+
+/// Composes a source UI pixel with its alpha mask over the destination pixel.
+pub fn source_over(pixel: &mut [u8], source: &[u8]) {
+ let alpha = u32::from(source[3]);
+ if alpha == 0 {
+ return;
+ }
+ let under = (u32::from(pixel[3]) * (255 - alpha) + 127) / 255;
+ let combined = alpha + under;
+ for index in 0..3 {
+ pixel[index] =
+ ((u32::from(source[index]) * alpha + u32::from(pixel[index]) * under + combined / 2)
+ / combined) as u8;
+ }
+ pixel[3] = combined as u8;
+}
+
fn open_atlas(vfs: &dyn Vfs, path: &str) -> Result<NresDocument, String> {
decode(read(vfs, path)?, ReadProfile::Strict).map_err(|error| error.to_string())
}
diff --git a/crates/fparkan-font/src/bitmap.rs b/crates/fparkan-font/src/bitmap.rs
index 6360b367..0110de40 100644
--- a/crates/fparkan-font/src/bitmap.rs
+++ b/crates/fparkan-font/src/bitmap.rs
@@ -143,6 +143,43 @@ impl BitmapFont {
Ok([width, height])
}
+ /// Wraps pager tokens by their atlas advances. A newline starts a row
+ /// when it opens a token, while spaces inside a row retain their width.
+ pub fn wrap_pager_lines(&self, mut text: &str, max_width: u16) -> Result<Vec<String>, String> {
+ let mut lines = Vec::new();
+ let mut line = String::new();
+ let mut width = 0;
+ while !text.is_empty() {
+ if let Some(after) = text.strip_prefix('\n') {
+ lines.push(std::mem::take(&mut line));
+ text = after;
+ width = 0;
+ continue;
+ }
+ let is_space = text.starts_with(' ');
+ let end = if is_space {
+ text.len() - text.trim_start_matches(' ').len()
+ } else {
+ text.find(' ').unwrap_or(text.len())
+ };
+ let token = &text[..end];
+ text = &text[end..];
+ let advance = usize::from(self.line_metrics(token)?[0]);
+ if !line.is_empty() && width + advance > usize::from(max_width) {
+ lines.push(std::mem::take(&mut line));
+ width = 0;
+ }
+ if !is_space || !line.is_empty() {
+ line.push_str(token);
+ width += advance;
+ }
+ }
+ if !line.is_empty() {
+ lines.push(line);
+ }
+ Ok(lines)
+ }
+
/// Composes a single line from a game resource's original CP1251 bytes.
pub fn centered_line_bytes(
&self,