diff options
Diffstat (limited to 'crates/fparkan-assets/src')
| -rw-r--r-- | crates/fparkan-assets/src/strategic_factory.rs | 223 |
1 files changed, 158 insertions, 65 deletions
diff --git a/crates/fparkan-assets/src/strategic_factory.rs b/crates/fparkan-assets/src/strategic_factory.rs index aaa590c6..1cf6d8a4 100644 --- a/crates/fparkan-assets/src/strategic_factory.rs +++ b/crates/fparkan-assets/src/strategic_factory.rs @@ -20,11 +20,18 @@ pub const VISIBLE_PLANTS: usize = ((EXTENT[1] - ROW_TOP) / ROW_PITCH) as usize; const BUILD: [u32; 4] = [263, 150, 313, 169]; const BATCH: [u32; 4] = [211, 150, 261, 169]; const CONSTRUCTOR: [u32; 4] = [5, 150, 55, 169]; +const ACTIVE: [u32; 4] = [135, 125, 165, 142]; +const FREE_BUILD: [u32; 4] = [228, 127, 258, 142]; +const MINDS: [u32; 4] = [274, 125, 307, 144]; +pub const TOOLTIP_IDS: [u32; 10] = [ + 0x611, 0xbfe, 0x185e, 0x185f, 0x5e7, 0xbfc, 0xbfd, 0xbfb, 0x1868, 0x1869, +]; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum FactoryAction { Close, Project(usize), + ActiveProject, Build, Batch, Cancel, @@ -59,46 +66,80 @@ fn project_rect(index: usize) -> [u32; 4] { [left, 125, left + 19, 142] } -pub fn hit_test( - [x, y]: [f32; 2], - plants: usize, - projects: usize, - orders: FactoryOrders, - designer_available: bool, -) -> Option<FactoryAction> { - let [x, y] = [x.round_ties_even() - ORIGIN[0] as f32, y.round_ties_even()]; - let hit = |[left, top, right, bottom]: [u32; 4]| { - (left as f32..right as f32).contains(&x) && (top as f32..bottom as f32).contains(&y) - }; - if hit([283, 0, 318, 19]) { - return Some(FactoryAction::Close); - } - if hit(BUILD) { - return match orders { - FactoryOrders::Ready => Some(FactoryAction::Build), - FactoryOrders::Single => Some(FactoryAction::Cancel), - _ => None, - }; - } - if hit(BATCH) { - return match orders { - FactoryOrders::Ready => Some(FactoryAction::Batch), - FactoryOrders::Batch => Some(FactoryAction::Cancel), - _ => None, - }; - } - if designer_available && hit(CONSTRUCTOR) { - return Some(FactoryAction::Designer); +#[derive(Clone, Copy)] +pub struct FactoryControls { + pub plants: usize, + pub projects: usize, + pub orders: FactoryOrders, + pub designer_available: bool, + pub active_available: bool, + pub free_build: bool, +} + +impl FactoryControls { + pub fn hit_test(self, at: [f32; 2]) -> Option<FactoryAction> { + let hit = |rect| contains(at, rect); + if hit([283, 0, 318, 19]) { + return Some(FactoryAction::Close); + } + if hit(BUILD) { + return match self.orders { + FactoryOrders::Ready => Some(FactoryAction::Build), + FactoryOrders::Single => Some(FactoryAction::Cancel), + _ => None, + }; + } + if hit(BATCH) { + return match self.orders { + FactoryOrders::Ready => Some(FactoryAction::Batch), + FactoryOrders::Batch => Some(FactoryAction::Cancel), + _ => None, + }; + } + if self.designer_available && hit(CONSTRUCTOR) { + return Some(FactoryAction::Designer); + } + if self.active_available && hit(ACTIVE) { + return Some(FactoryAction::ActiveProject); + } + if let Some(index) = (0..self.projects).find(|&index| hit(project_rect(index))) { + return Some(FactoryAction::Project(index)); + } + (0..self.plants.min(VISIBLE_PLANTS)) + .find(|&index| { + let top = ROW_TOP + index as u32 * ROW_PITCH; + hit([0, top, 318, top + ROW_HEIGHT]) + }) + .map(FactoryAction::Plant) } - if let Some(index) = (0..projects).find(|&index| hit(project_rect(index))) { - return Some(FactoryAction::Project(index)); + + pub fn tooltip(self, at: [f32; 2]) -> Option<u32> { + let hit = |rect| contains(at, rect); + let busy = matches!(self.orders, FactoryOrders::Single | FactoryOrders::Batch); + if hit(BUILD) { + Some(if busy { 0xbfe } else { 0x611 }) + } else if hit(BATCH) { + Some(if busy { 0x185f } else { 0x185e }) + } else if hit(CONSTRUCTOR) { + Some(0x5e7) + } else if self.active_available && hit(ACTIVE) { + Some(0xbfd) + } else if (0..self.projects).any(|index| hit(project_rect(index))) { + Some(0xbfc) + } else if hit(FREE_BUILD) { + Some(if self.free_build { 0x1868 } else { 0x1869 }) + } else if hit(MINDS) { + Some(0xbfb) + } else { + None + } } - (0..plants.min(VISIBLE_PLANTS)) - .find(|&index| { - let top = ROW_TOP + index as u32 * ROW_PITCH; - hit([0, top, 318, top + ROW_HEIGHT]) - }) - .map(FactoryAction::Plant) +} + +fn contains([x, y]: [f32; 2], [left, top, right, bottom]: [u32; 4]) -> bool { + let x = x.round_ties_even() - ORIGIN[0] as f32; + let y = y.round_ties_even(); + (left as f32..right as f32).contains(&x) && (top as f32..bottom as f32).contains(&y) } pub struct FactoryRow<'a> { @@ -110,12 +151,11 @@ pub struct FactoryPicture<'a> { pub rows: &'a [FactoryRow<'a>], pub project_name: Option<&'a str>, pub stats: &'a [Option<CardLine<'a>>], - pub projects: usize, + pub controls: FactoryControls, pub selected_project: Option<usize>, + pub viewing_active: bool, pub progress: Option<u8>, pub free_minds: u32, - pub orders: FactoryOrders, - pub designer_available: bool, } pub struct FactoryImages { @@ -126,6 +166,8 @@ pub struct FactoryImages { ray: RgbaImage, icons: [RgbaImage; 7], batch_icons: [RgbaImage; 2], + active_project: RgbaImage, + free_bots_icons: [RgbaImage; 2], } impl FactoryImages { @@ -148,7 +190,7 @@ impl FactoryImages { "ccres_frame_edge_v", ], )?; - let [exit, project, project_frame, build, stop, constructor, free, batch, stop_batch] = + let [exit, project, project_frame, build, stop, constructor, free, batch, stop_batch, active_project, free_bots, no_free_bots] = load_ui_sprites( vfs, "ui/hq.cfg", @@ -162,6 +204,9 @@ impl FactoryImages { "brain_icon", "batch_build_icon", "batch_stop_build_icon", + "active_project", + "free_bots_icon", + "no_free_bots_icon", ], )?; let atlas = load_ui_page(vfs, "ui_menu")?; @@ -212,6 +257,8 @@ impl FactoryImages { free, ], batch_icons: [batch, stop_batch], + active_project, + free_bots_icons: [no_free_bots, free_bots], }) } @@ -229,7 +276,7 @@ impl FactoryImages { )?; warbot_card::paint_at(&self.font, &mut image, picture.stats, [10, 144, 147], 43)?; } - for index in 0..picture.projects { + for index in 0..picture.controls.projects { let rect = project_rect(index); stamp( &mut image, @@ -237,13 +284,28 @@ impl FactoryImages { [rect[0] - 2, rect[1] - 2, rect[2] + 2, rect[3] + 2], [255; 3], ); - let tint = if picture.selected_project == Some(index) { + let tint = if !picture.viewing_active && picture.selected_project == Some(index) { [255; 3] } else { [128, 255, 128] }; stamp(&mut image, project, rect, tint); } + if picture.controls.active_available { + stamp(&mut image, frame, [133, 123, 167, 144], [255; 3]); + let tint = if picture.viewing_active { + [255; 3] + } else { + [128, 255, 128] + }; + stamp(&mut image, &self.active_project, ACTIVE, tint); + } + stamp( + &mut image, + &self.free_bots_icons[usize::from(picture.controls.free_build)], + FREE_BUILD, + [255; 3], + ); stamp(&mut image, free, [274, 127, 289, 142], [128, 255, 128]); text( &self.font, @@ -263,18 +325,23 @@ impl FactoryImages { [255; 3], )?; } - let busy = matches!(picture.orders, FactoryOrders::Single | FactoryOrders::Batch); + let orders = picture.controls.orders; + let busy = matches!(orders, FactoryOrders::Single | FactoryOrders::Batch); for (rect, icon, enabled) in [ - (CONSTRUCTOR, constructor, picture.designer_available), + ( + CONSTRUCTOR, + constructor, + picture.controls.designer_available, + ), ( BATCH, &self.batch_icons[usize::from(busy)], - matches!(picture.orders, FactoryOrders::Ready | FactoryOrders::Batch), + matches!(orders, FactoryOrders::Ready | FactoryOrders::Batch), ), ( BUILD, if busy { stop } else { build }, - matches!(picture.orders, FactoryOrders::Ready | FactoryOrders::Single), + matches!(orders, FactoryOrders::Ready | FactoryOrders::Single), ), ] { let tint = if enabled { [255; 3] } else { [104; 3] }; @@ -330,41 +397,67 @@ mod tests { #[test] fn factory_actions_follow_project_and_row_extents() { use FactoryOrders::{Batch, Ready, Single, Unavailable}; - + let mut controls = FactoryControls { + plants: 1, + projects: 2, + orders: Ready, + designer_available: true, + active_available: false, + free_build: true, + }; assert_eq!( - hit_test([95.0, 132.0], 1, 2, Ready, true), + controls.hit_test([95.0, 132.0]), Some(FactoryAction::Project(1)) ); - assert_eq!(hit_test([95.0, 132.0], 1, 1, Ready, true), None); assert_eq!( - hit_test([340.0, 160.0], 1, 2, Ready, true), + controls.hit_test([340.0, 160.0]), Some(FactoryAction::Build) ); - assert_eq!(hit_test([340.0, 160.0], 1, 2, Unavailable, true), None); - assert_eq!(FactoryOrders::for_plant(false, true, false, 0), Single); + assert_eq!(controls.tooltip([340.0, 160.0]), Some(0x611)); assert_eq!( - hit_test([340.0, 160.0], 1, 0, Single, true), - Some(FactoryAction::Cancel) + controls.hit_test([289.0, 160.0]), + Some(FactoryAction::Batch) ); assert_eq!( - hit_test([200.0, 172.0], 1, 2, Ready, true), + controls.hit_test([200.0, 172.0]), Some(FactoryAction::Plant(0)) ); - assert_eq!(hit_test([80.0, 160.0], 1, 0, Unavailable, false), None); assert_eq!( - hit_test([80.0, 160.0], 1, 0, Unavailable, true), + controls.hit_test([80.0, 160.0]), Some(FactoryAction::Designer) ); - assert_eq!(FactoryOrders::for_plant(true, false, true, 0), Batch); - assert_eq!(hit_test([340.0, 160.0], 1, 2, Batch, true), None); - assert_eq!(hit_test([289.0, 160.0], 1, 2, Single, true), None); + assert_eq!(controls.hit_test([201.0, 132.0]), None); + assert_eq!(controls.tooltip([201.0, 132.0]), None); + assert_eq!(controls.tooltip([95.0, 132.0]), Some(0xbfc)); + assert_eq!(controls.tooltip([287.0, 132.0]), Some(0x1868)); + controls.orders = Single; + controls.active_available = true; + assert_eq!(controls.hit_test([289.0, 160.0]), None); assert_eq!( - hit_test([289.0, 160.0], 1, 2, Ready, true), - Some(FactoryAction::Batch) + controls.hit_test([340.0, 160.0]), + Some(FactoryAction::Cancel) ); assert_eq!( - hit_test([289.0, 160.0], 1, 2, Batch, true), + controls.hit_test([201.0, 132.0]), + Some(FactoryAction::ActiveProject) + ); + assert_eq!(controls.tooltip([201.0, 132.0]), Some(0xbfd)); + + controls.orders = Batch; + assert_eq!(controls.hit_test([340.0, 160.0]), None); + assert_eq!( + controls.hit_test([289.0, 160.0]), Some(FactoryAction::Cancel) ); + assert_eq!(controls.tooltip([289.0, 160.0]), Some(0x185f)); + + controls.orders = Unavailable; + controls.projects = 1; + controls.designer_available = false; + controls.free_build = false; + assert_eq!(controls.hit_test([95.0, 132.0]), None); + assert_eq!(controls.hit_test([340.0, 160.0]), None); + assert_eq!(controls.hit_test([80.0, 160.0]), None); + assert_eq!(controls.tooltip([287.0, 132.0]), Some(0x1869)); } } |
