diff options
Diffstat (limited to 'crates/fparkan-assets/src')
| -rw-r--r-- | crates/fparkan-assets/src/strategic_roster.rs | 11 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/strategic_roster/orders.rs | 144 |
2 files changed, 155 insertions, 0 deletions
diff --git a/crates/fparkan-assets/src/strategic_roster.rs b/crates/fparkan-assets/src/strategic_roster.rs index 0faeb7e4..440ead26 100644 --- a/crates/fparkan-assets/src/strategic_roster.rs +++ b/crates/fparkan-assets/src/strategic_roster.rs @@ -9,6 +9,10 @@ use crate::{ }; use fparkan_vfs::Vfs; +mod orders; +use orders::OrderImages; +pub use orders::{order_at, RosterOrder}; + /// List origin and size in the original logical screen coordinates. pub const ORIGIN: [u32; 2] = [51, 0]; pub const EXTENT: [u32; 2] = [319, 480]; @@ -63,6 +67,7 @@ pub struct RosterImages { end: RgbaImage, class_icon: RgbaImage, chassis_icons: [RgbaImage; 4], + orders: OrderImages, } impl RosterImages { @@ -149,6 +154,7 @@ impl RosterImages { end, class_icon: icon(49, 110)?, chassis_icons: [icon(97, 94)?, icon(49, 94)?, icon(65, 94)?, icon(81, 94)?], + orders: OrderImages::load(vfs)?, }) } @@ -157,6 +163,7 @@ impl RosterImages { &self, rows: &[Row<'_>], card: &[Option<CardLine<'_>>], + orders: Option<([&str; 3], Option<RosterOrder>)>, ) -> Result<RgbaImage, String> { let mut panel = self.background.clone(); if let Some(row) = rows.iter().find(|row| row.selected) { @@ -210,6 +217,10 @@ impl RosterImages { tint, ); } + if let Some((labels, hovered)) = orders { + self.orders + .paint(self, &mut panel, rows.len(), labels, hovered)?; + } Ok(panel) } diff --git a/crates/fparkan-assets/src/strategic_roster/orders.rs b/crates/fparkan-assets/src/strategic_roster/orders.rs new file mode 100644 index 00000000..743b4801 --- /dev/null +++ b/crates/fparkan-assets/src/strategic_roster/orders.rs @@ -0,0 +1,144 @@ +use super::{stamp, RosterImages, ORIGIN, ROW_HEIGHT, ROW_PITCH, ROW_TOP}; +use crate::{ui_sprite::load_ui_sprites, RgbaImage}; +use fparkan_vfs::Vfs; + +const LEFT: u32 = 100; +const RIGHT: u32 = 367; + +/// Commands supported by the strategic warbot panel. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum RosterOrder { + /// Stop in place. + Stand, + /// Draw a route and send it to the unit. + FollowRoute, +} + +const ORDERS: [RosterOrder; 2] = [RosterOrder::Stand, RosterOrder::FollowRoute]; + +/// Returns the visible command at the pointer, excluding borders and row gaps. +pub fn order_at([x, y]: [f32; 2], count: usize) -> Option<RosterOrder> { + let [x, y] = [x.round_ties_even(), y.round_ties_even()]; + if !(LEFT as f32..RIGHT as f32).contains(&x) || !(0.0..480.0).contains(&y) { + return None; + } + let y = (y as u32).checked_sub(panel_top(count) + ROW_PITCH)?; + ORDERS + .get((y / ROW_PITCH) as usize) + .copied() + .filter(|_| y % ROW_PITCH < ROW_HEIGHT) +} + +fn panel_top(count: usize) -> u32 { + ROW_TOP + count.min(19) as u32 * ROW_PITCH +} + +pub(super) struct OrderImages { + stub: RgbaImage, + disabled: RgbaImage, + arrows: [RgbaImage; 2], +} + +impl OrderImages { + pub(super) fn load(vfs: &dyn Vfs) -> Result<Self, String> { + let [stub, disabled] = load_ui_sprites( + vfs, + "ui/compaund.cfg", + ["ccres_ending_stub", "ccres_long_button_off"], + )?; + let arrows = load_ui_sprites(vfs, "ui/hq.cfg", ["scroll_up_icon", "scroll_down_icon"])?; + Ok(Self { + stub, + disabled, + arrows, + }) + } + + pub(super) fn paint( + &self, + roster: &RosterImages, + panel: &mut RgbaImage, + count: usize, + [title, stand, follow]: [&str; 3], + hovered: Option<RosterOrder>, + ) -> Result<(), String> { + let x = LEFT - ORIGIN[0]; + let y = panel_top(count); + let title = roster.font.centered_line(title, 158, ROW_HEIGHT as u16)?; + for (sprite, left, right) in [ + (&self.stub, 0, 9), + (&roster.icon_frame, 8, 167), + (&title, 8, 166), + ] { + stamp( + panel, + sprite, + [x + left, y, x + right, y + ROW_HEIGHT], + [255; 3], + ); + } + for (index, icon) in self.arrows.iter().enumerate() { + let left = 166 + 50 * index as u32; + stamp( + panel, + &self.disabled, + [x + left, y, x + left + 51, y + ROW_HEIGHT], + [255; 3], + ); + let icon_left = x + left + 50_u32.saturating_sub(icon.width) / 2; + let icon_top = y + ROW_HEIGHT.saturating_sub(icon.height) / 2; + stamp( + panel, + icon, + [ + icon_left, + icon_top, + icon_left + icon.width, + icon_top + icon.height, + ], + [128; 3], + ); + } + for (index, (order, label)) in ORDERS.into_iter().zip([stand, follow]).enumerate() { + let y = y + (index as u32 + 1) * ROW_PITCH; + let bottom = y + ROW_HEIGHT; + let label = roster.font.centered_line(label, 247, ROW_HEIGHT as u16)?; + for (sprite, left, right) in [ + (&self.stub, 0, 9), + (&roster.emitter[1], 8, 15), + (&roster.ray, 14, 262), + (&roster.end, 261, 267), + ] { + stamp(panel, sprite, [x + left, y, x + right, bottom], [255; 3]); + } + if hovered == Some(order) { + stamp( + panel, + &RgbaImage { + width: 1, + height: 1, + rgba8: vec![0, 128, 0, 128], + }, + [x + 16, y + 3, x + 260, bottom - 3], + [255; 3], + ); + } + stamp(panel, &label, [x + 14, y, x + 261, bottom], [255; 3]); + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn hit_test_follows_the_last_warbot_row() { + assert_eq!(order_at([250.0, 197.0], 3), None); + assert_eq!(order_at([250.0, 198.0], 3), Some(RosterOrder::Stand)); + assert_eq!(order_at([250.0, 217.0], 3), None); + assert_eq!(order_at([250.0, 218.0], 3), Some(RosterOrder::FollowRoute)); + assert_eq!(order_at([250.0, 218.0], 4), Some(RosterOrder::Stand)); + } +} |
