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, /// Find and engage hostile mobile units. SearchAndDestroy, /// Patrol a point or a live object. Patrol, /// Travel to a friendly facility for ammunition, charge, and repairs. Recharge, } const ORDERS: [RosterOrder; 5] = [ RosterOrder::Stand, RosterOrder::FollowRoute, RosterOrder::SearchAndDestroy, RosterOrder::Patrol, RosterOrder::Recharge, ]; impl RosterOrder { fn slot(self) -> u32 { match self { Self::Stand => 0, Self::FollowRoute => 1, Self::SearchAndDestroy => 2, Self::Patrol => 3, Self::Recharge => 4, } } } /// Returns the visible command at the pointer, excluding borders and row gaps. pub fn order_at([x, y]: [f32; 2], count: usize) -> Option { 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 .into_iter() .find(|order| order.slot() == y / ROW_PITCH) .filter(|_| y % ROW_PITCH < ROW_HEIGHT) } fn panel_top(count: usize) -> u32 { ROW_TOP + count.min(19) as u32 * ROW_PITCH } pub(super) fn covers_pointer([x, y]: [f32; 2], count: usize) -> bool { let y_top = panel_top(count); let y_bottom = (y_top + (RosterOrder::Recharge.slot() + 2) * ROW_PITCH).min(480); (LEFT as f32..RIGHT as f32).contains(&x) && (y_top as f32..y_bottom as f32).contains(&y) } pub(super) struct OrderImages { stub: RgbaImage, disabled: RgbaImage, arrows: [RgbaImage; 2], } impl OrderImages { pub(super) fn load(vfs: &dyn Vfs) -> Result { 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, search, patrol, recharge]: [&str; 6], hovered: Option, ) -> 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 (order, label) in ORDERS .into_iter() .zip([stand, follow, search, patrol, recharge]) { let y = y + (order.slot() + 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)); assert_eq!( order_at([250.0, 238.0], 3), Some(RosterOrder::SearchAndDestroy) ); assert_eq!(order_at([250.0, 258.0], 3), Some(RosterOrder::Patrol)); assert_eq!(order_at([250.0, 278.0], 3), Some(RosterOrder::Recharge)); } }