Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108use crate::bar::{ bar_renderer::BarRenderer, component::{Component, Value}, module::Data, text_renderer::TextRenderer,};
pub struct MatchComponent { pub value: String, pub lookup: Vec<(Pattern, Vec<Component>)>, pub cached: Option<usize>, tick: u64,}
impl MatchComponent { pub fn new(value: String, lookup: Vec<(Pattern, Vec<Component>)>) -> Self { Self { value, lookup, cached: None, tick: 0, } }
pub fn get_component(&self, data: &Data) -> Option<&[Component]> { if let Some(idx) = self.cached { return Some(&self.lookup[idx].1); }
let val = data.get_value(&self.value)?;
self.lookup .iter() .filter_map(|(pat, comp)| pat.matches(val).then_some(comp)) .next() .map(|v| &**v) }
pub fn get_component_mut(&mut self, data: &Data) -> Option<&mut [Component]> { let val = data.get_value(&self.value)?;
if let Some(idx) = self.cached { return Some(&mut self.lookup[idx].1); }
let (idx, val) = self .lookup .iter_mut() .enumerate() .filter_map(|(idx, (pat, comp))| pat.matches(val).then_some((idx, comp))) .next()?;
self.cached = Some(idx);
Some(val) }
pub fn draw( &mut self, text_renderer: &mut TextRenderer, bar_renderer: &mut BarRenderer, width: f32, height: f32, offset: f32, ) { let Some(idx) = self.cached.take() else { return; }; let (_, components) = &mut self.lookup[idx];
let mut offset = offset; for component in components { component.draw(text_renderer, bar_renderer, width, height, offset); offset += component.width(); } }
pub fn width(&self) -> f32 { let Some(idx) = self.cached else { return 0.0 }; let (_, components) = &self.lookup[idx];
components.iter().map(|x| x.width()).sum() }
pub fn needs_update(&self, data: &Data) -> bool { data.get_tick(&self.value).unwrap() > self.tick }}
pub enum Pattern { Value(Value), NumRange(f32, f32), Otherwise,}
impl Pattern { pub fn matches(&self, target: &Value) -> bool { match self { Pattern::Value(value) => target == value, Pattern::NumRange(min, max) => match target { Value::Num(num) => (min..max).contains(&num), _ => false, }, Pattern::Otherwise => true, } }}