use ::bracket_lib::prelude::*; use ::specs::prelude::*; use crate::colors; #[derive(PartialEq, Copy, Clone)] pub enum ItemMenuResult { Cancel, NoResponse, Selected, } pub fn menu_box(draw_batch: &mut DrawBatch, x: i32, y: i32, width: i32, title: T) { draw_batch.draw_box( Rect::with_size(x, y - 2, 51, width), ColorPair::new(colors::WHITE, colors::BLACK), ); draw_batch.print_color( Point::new(18, y - 2), &title.to_string(), ColorPair::new(colors::MAGENTA, colors::BLACK), ); } pub fn menu_option( draw_batch: &mut DrawBatch, x: i32, y: i32, hotkey: FontCharType, text: T, ) { draw_batch.set( Point::new(x, y), ColorPair::new(colors::WHITE, colors::BLACK), to_cp437('('), ); draw_batch.set( Point::new(x + 1, y), ColorPair::new(colors::YELLOW, colors::BLACK), hotkey, ); draw_batch.set( Point::new(x + 2, y), ColorPair::new(colors::WHITE, colors::BLACK), to_cp437(')'), ); draw_batch.print_color( Point::new(x + 5, y), &text.to_string(), ColorPair::new(colors::YELLOW, colors::BLACK), ); } pub fn item_result_menu( draw_batch: &mut DrawBatch, title: S, count: usize, items: &[(Entity, String)], key: Option, ) -> (ItemMenuResult, Option) { let mut y = (25 - (count / 2)) as i32; draw_batch.draw_box( Rect::with_size(15, y - 2, 31, (count + 3) as i32), ColorPair::new(colors::WHITE, colors::BLACK), ); draw_batch.print_color( Point::new(18, y - 2), &title.to_string(), ColorPair::new(colors::YELLOW, colors::BLACK), ); draw_batch.print_color( Point::new(18, y + count as i32 + 1), "ESCAPE to cancel", ColorPair::new(colors::YELLOW, colors::BLACK), ); let mut item_list: Vec = Vec::new(); for (j, item) in items.iter().enumerate() { menu_option(draw_batch, 17, y, 97 + j as FontCharType, &item.1); item_list.push(item.0); y += 1; } match key { None => (ItemMenuResult::NoResponse, None), Some(key) => match key { VirtualKeyCode::Escape => (ItemMenuResult::Cancel, None), _ => { let selection = letter_to_option(key); if selection > -1 && selection < count as i32 { return ( ItemMenuResult::Selected, Some(item_list[selection as usize]), ); } (ItemMenuResult::NoResponse, None) } }, } }