aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/strategic_buildings.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-assets/src/strategic_buildings.rs')
-rw-r--r--crates/fparkan-assets/src/strategic_buildings.rs176
1 files changed, 176 insertions, 0 deletions
diff --git a/crates/fparkan-assets/src/strategic_buildings.rs b/crates/fparkan-assets/src/strategic_buildings.rs
new file mode 100644
index 00000000..53deabdf
--- /dev/null
+++ b/crates/fparkan-assets/src/strategic_buildings.rs
@@ -0,0 +1,176 @@
+//! Compound controls of the strategic «Другие здания» section.
+
+use crate::{
+ game_font::{load_layout_font, BitmapFont},
+ satellite_map_chrome::{building_glyph, building_glyph_tint},
+ strategic_factory::{stamp, text},
+ strategic_roster::durability_fill,
+ ui_sprite::{atlas_region, load_ui_page, load_ui_sprites},
+ RgbaImage,
+};
+use fparkan_vfs::Vfs;
+
+pub use crate::strategic_factory::{EXTENT, ORIGIN};
+const ROW_PITCH: u32 = 20;
+const ROW_HEIGHT: u32 = 19;
+const TEXT_LEFT: u32 = 44;
+const TEXT_RIGHT: u32 = 313;
+pub const TITLE: u32 = 0x64a;
+pub const VISIBLE_ROWS: usize = (EXTENT[1] / ROW_PITCH - 1) as usize;
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum Hit {
+ Close,
+ Row(usize),
+}
+
+pub fn hit_test([x, y]: [f32; 2], rows: usize) -> Option<Hit> {
+ let x = x.round_ties_even() - ORIGIN[0] as f32;
+ let y = y.round_ties_even();
+ if (283.0..318.0).contains(&x) && (0.0..19.0).contains(&y) {
+ return Some(Hit::Close);
+ }
+ if !(0.0..(EXTENT[0] - 1) as f32).contains(&x) || !(20.0..480.0).contains(&y) {
+ return None;
+ }
+ let y = y as u32 - ROW_PITCH;
+ let row = (y / ROW_PITCH) as usize;
+ (row < rows.min(VISIBLE_ROWS) && y % ROW_PITCH < ROW_HEIGHT).then_some(Hit::Row(row))
+}
+
+pub struct Row<'a> {
+ pub label: &'a str,
+ pub class: u32,
+ pub size: u32,
+ pub selected: bool,
+ pub durability_percent: Option<i32>,
+}
+
+pub struct Images {
+ header: RgbaImage,
+ font: BitmapFont,
+ cap: [RgbaImage; 2],
+ frame: RgbaImage,
+ separator: RgbaImage,
+ emitter: [RgbaImage; 2],
+ ray: RgbaImage,
+ end: RgbaImage,
+ atlas: RgbaImage,
+}
+
+impl Images {
+ pub fn load(vfs: &dyn Vfs, title: &str) -> Result<Self, String> {
+ let [title_cap, frame, close, cap_off, cap_on, separator, emitter_off, emitter_on, ray, end] =
+ load_ui_sprites(
+ vfs,
+ "ui/compaund.cfg",
+ [
+ "ccres_ending_text",
+ "ccres_body_text",
+ "ccres_exit_button_pressed",
+ "ccres_lamp_stub_ending_off",
+ "ccres_lamp_stub_ending_normal",
+ "ccres_separator_left_text",
+ "ccres_ray_emitter_off",
+ "ccres_ray_emitter_normal",
+ "ccres_ray_body",
+ "ccres_ray_ending",
+ ],
+ )?;
+ let [exit] = load_ui_sprites(vfs, "ui/hq.cfg", ["exit_icon"])?;
+ let font = load_layout_font(vfs)?;
+ let [width, height] = EXTENT;
+ let mut header = RgbaImage {
+ width,
+ height,
+ rgba8: vec![0; (width * height * 4) as usize],
+ };
+ for (sprite, rect) in [
+ (&title_cap, [0, 0, 6, 19]),
+ (&frame, [5, 0, 284, 19]),
+ (&close, [283, 0, 318, 19]),
+ (&exit, [298, 3, 311, 16]),
+ ] {
+ stamp(&mut header, sprite, rect, [255; 3]);
+ }
+ text(&font, &mut header, title, [5, 0, 283, 19], [255; 3])?;
+ Ok(Self {
+ header,
+ font,
+ cap: [cap_off, cap_on],
+ frame,
+ separator,
+ emitter: [emitter_off, emitter_on],
+ ray,
+ end,
+ atlas: load_ui_page(vfs, "ui_menu")?,
+ })
+ }
+
+ pub fn paint<'a>(&self, rows: impl Iterator<Item = Row<'a>>) -> Result<RgbaImage, String> {
+ let mut image = self.header.clone();
+ for (index, row) in rows.take(VISIBLE_ROWS).enumerate() {
+ let y = (index as u32 + 1) * ROW_PITCH;
+ let bottom = y + ROW_HEIGHT;
+ let selected = usize::from(row.selected);
+ for (sprite, left, right) in [
+ (&self.cap[selected], 0, 11),
+ (&self.frame, 10, 30),
+ (&self.separator, 29, 35),
+ (&self.emitter[selected], 34, 45),
+ (&self.ray, TEXT_LEFT, TEXT_RIGHT + 1),
+ (&self.end, TEXT_RIGHT, EXTENT[0]),
+ ] {
+ stamp(&mut image, sprite, [left, y, right, bottom], [255; 3]);
+ }
+ if let Some([x, atlas_y]) = building_glyph(row.class) {
+ let glyph = atlas_region(&self.atlas, [x, atlas_y, 15, 15])
+ .ok_or("building glyph outside ui_menu")?;
+ stamp(
+ &mut image,
+ &glyph,
+ [12, y + 2, 27, y + 17],
+ building_glyph_tint(row.size),
+ );
+ }
+ if let Some((width, rgba)) = row
+ .durability_percent
+ .and_then(|percent| durability_fill(percent, TEXT_RIGHT - TEXT_LEFT))
+ {
+ let fill = RgbaImage {
+ width: 1,
+ height: 1,
+ rgba8: rgba.to_vec(),
+ };
+ stamp(
+ &mut image,
+ &fill,
+ [TEXT_LEFT + 2, y + 3, TEXT_LEFT + width, bottom - 3],
+ [255; 3],
+ );
+ }
+ text(
+ &self.font,
+ &mut image,
+ row.label,
+ [TEXT_LEFT, y, TEXT_RIGHT, bottom],
+ if row.selected { [255; 3] } else { [128; 3] },
+ )?;
+ }
+ Ok(image)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn building_list_hits_labels_icons_and_close_with_native_row_gaps() {
+ assert_eq!(hit_test([350.0, 9.0], 4), Some(Hit::Close));
+ assert_eq!(hit_test([68.0, 39.0], 4), None);
+ assert_eq!(hit_test([68.0, 40.0], 4), Some(Hit::Row(1)));
+ assert_eq!(hit_test([340.0, 49.0], 4), Some(Hit::Row(1)));
+ assert_eq!(hit_test([340.0, 49.0], 1), None);
+ }
+}