use crate::context;
use crate::dom::Dom;
use crate::event::{Event, EventResponse};
use crate::geometry::{Rect, Vec2};
use crate::id::ManagedTextureId;
use crate::input::InputState;
use crate::layout::LayoutDom;
use crate::paint::{PaintDom, Texture};
#[derive(Debug)]
pub struct Yakui {
dom: Dom,
layout: LayoutDom,
paint: PaintDom,
input: InputState,
}
impl Yakui {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
dom: Dom::new(),
layout: LayoutDom::new(),
paint: PaintDom::new(),
input: InputState::new(),
}
}
pub fn handle_event(&mut self, event: Event) -> bool {
log::debug!("State::handle_event({event:?})");
context::bind_dom(&self.dom);
let response = self.input.handle_event(&self.dom, &self.layout, &event);
if let Event::ViewportChanged(viewport) = event {
self.layout.set_unscaled_viewport(viewport);
}
context::unbind_dom();
response == EventResponse::Sink
}
pub fn add_texture(&mut self, texture: Texture) -> ManagedTextureId {
self.paint.add_texture(texture)
}
pub fn textures(&self) -> impl Iterator<Item = (ManagedTextureId, &Texture)> {
self.paint.textures()
}
pub fn set_surface_size(&mut self, size: Vec2) {
self.paint.set_surface_size(size);
}
pub fn set_unscaled_viewport(&mut self, view: Rect) {
self.layout.set_unscaled_viewport(view);
self.paint.set_unscaled_viewport(view);
}
pub fn set_scale_factor(&mut self, factor: f32) {
self.layout.set_scale_factor(factor);
self.paint.set_scale_factor(factor);
}
pub fn start(&mut self) {
self.dom.start();
self.input.start(&self.dom, &self.layout);
self.paint.start();
context::bind_dom(&self.dom);
}
pub fn finish(&mut self) {
context::unbind_dom();
self.dom.finish();
self.layout.calculate_all(&self.dom, &self.input);
self.input.finish();
}
pub fn paint(&mut self) -> &PaintDom {
self.paint.paint_all(&self.dom, &self.layout);
&self.paint
}
pub fn dom(&self) -> &Dom {
&self.dom
}
pub fn layout_dom(&self) -> &LayoutDom {
&self.layout
}
}