use crate::ui::animation::AnimationCoordinator;
use crate::input::{InputState, InputCoordinator, WidgetResponse};
use super::layout::tree::LayoutTree;
use super::state::StateRegistry;
use crate::types::{Rect, WidgetId};
pub struct ContextManager {
pub input: InputCoordinator,
pub layout: LayoutTree,
pub registry: StateRegistry,
pub animations: AnimationCoordinator,
pub time: f64,
}
impl ContextManager {
pub fn new(root_node: super::layout::types::LayoutNode) -> Self {
Self {
input: InputCoordinator::new(),
layout: LayoutTree::new(root_node),
registry: StateRegistry::new(),
animations: AnimationCoordinator::new(),
time: 0.0,
}
}
pub fn begin_frame(&mut self, input: InputState, viewport: Rect) {
self.time = input.time;
self.input.begin_frame(input);
self.animations.update(self.time);
self.layout.compute(viewport);
}
pub fn begin_frame_widgets_only(&mut self, time_secs: f64, viewport: Rect) {
self.time = time_secs;
self.input.begin_frame_widgets_only();
self.animations.update(self.time);
self.layout.compute(viewport);
}
pub fn end_frame(&mut self) -> Vec<(WidgetId, WidgetResponse)> {
self.input.end_frame()
}
pub fn state<T: 'static + Send + Sync + Default>(&mut self, id: impl Into<WidgetId>) -> &mut T {
self.registry.get_or_insert_with(id.into(), T::default)
}
pub fn widget_rect(&self, id: &WidgetId) -> Rect {
self.layout.get_rect(id).unwrap_or_default()
}
}