use parley::Alignment; use crate::bar::{ bar_renderer::BarRenderer, component::{ bar::BarComponent, matcher::MatchComponent, stack::StackComponent, text::TextComponent, }, module::Data, text_renderer::TextRenderer, }; pub mod bar; pub mod matcher; pub mod stack; pub mod text; pub enum Component { Text(TextComponent), Bar(BarComponent), Stack(StackComponent), Match(MatchComponent), } impl Component { pub fn needs_update(&self, data: &Data) -> bool { match self { Component::Text(text_component) => text_component.display.needs_update(data), Component::Bar(bar_component) => bar_component.needs_update(data), Component::Stack(stack_component) => stack_component .contents .iter() .any(|x| x.needs_update(data)), Component::Match(match_component) => { match_component .get_component(data) .map(|v| v.iter().any(|x| x.needs_update(data))) .unwrap_or(true) || match_component.needs_update(data) } } } pub fn update( &mut self, text_renderer: &mut TextRenderer, data: &Data, width: f32, height: f32, align: Alignment, ) { match self { Component::Text(text_component) => { text_component.update(text_renderer, data, width, height, align); } Component::Bar(bar_component) => bar_component.update(data), Component::Stack(stack_component) => stack_component .contents .iter_mut() .for_each(|x| x.update(text_renderer, data, width, height, align)), Component::Match(match_component) => { if let Some(comp) = match_component.get_component_mut(data) { comp.iter_mut() .for_each(|x| x.update(text_renderer, data, width, height, align)); } } } } pub fn draw( &mut self, text_renderer: &mut TextRenderer, bar_renderer: &mut BarRenderer, width: f32, height: f32, offset: f32, ) { match self { Component::Text(text_component) => { text_renderer.push_layout(&text_component.layout, offset) } Component::Bar(bar_component) => { bar_renderer.push_bar(&bar_component, width, height, offset); } Component::Stack(stack_component) => stack_component .contents .iter_mut() .for_each(|x| x.draw(text_renderer, bar_renderer, width, height, offset)), Component::Match(match_component) => { match_component.draw(text_renderer, bar_renderer, width, height, offset) } } } pub fn width(&self) -> f32 { match self { Component::Text(text_component) => text_component.layout.full_width(), Component::Bar(bar_component) => bar_component.width + bar_component.padding * 2.0, Component::Stack(stack_component) => stack_component .contents .iter() .map(Component::width) .max_by(f32::total_cmp) .unwrap_or(0.0), Component::Match(match_component) => match_component.width(), } } pub fn register(&self, data: &mut Data) { match self { Component::Text(text_component) => { for val in text_component.display.values() { data.register_value(val.to_owned()); } } Component::Bar(bar_component) => { data.register_value(bar_component.measure.clone()); } Component::Stack(stack_component) => { stack_component .contents .iter() .for_each(|x| x.register(data)); } Component::Match(match_component) => { match_component .lookup .iter() .flat_map(|(_, x)| x) .for_each(|x| x.register(data)); data.register_value(match_component.value.clone()) } } dbg!(data); } } #[derive(Debug, PartialEq)] pub enum Value { String(String), Num(f32), Placeholder, }