use std::cell::Cell;
use cranpose_core::NodeId;
use cranpose_ui::{Point, Rect, Size};
use super::{
CONTAINED_DRAW_SLACK, LayerChange, ProjectiveTransform, RenderNode, inflate_rect,
nodes_draw_within, rect_within,
};
#[derive(Clone, Default)]
pub struct InlineNode {
pub node_id: NodeId,
pub origin: Point,
pub size: Size,
pub motion_context_animated: bool,
pub change: LayerChange,
pub content: Vec<RenderNode>,
contained: Cell<Option<bool>>,
content_hash: Cell<Option<u64>>,
}
impl InlineNode {
pub fn node_rect(&self) -> Rect {
Rect::from_size(self.size)
}
pub fn transform_to_parent(&self) -> ProjectiveTransform {
ProjectiveTransform::translation(self.origin.x, self.origin.y)
}
pub fn draws_within_bounds(&self) -> bool {
if let Some(contained) = self.contained.get() {
return contained;
}
let contained = nodes_draw_within(
&self.content,
inflate_rect(self.node_rect(), CONTAINED_DRAW_SLACK),
);
self.contained.set(Some(contained));
contained
}
pub(crate) fn draws_within_parent(&self, bounds: Rect) -> bool {
self.draws_within_bounds()
&& rect_within(
inflate_rect(self.node_rect(), CONTAINED_DRAW_SLACK)
.translate(self.origin.x, self.origin.y),
bounds,
)
}
pub(crate) fn content_hash(&self) -> u64 {
if let Some(hash) = self.content_hash.get() {
return hash;
}
let hash = crate::graph_hash::inline_content_hash(self);
self.content_hash.set(Some(hash));
hash
}
pub(crate) fn note_change(&mut self) {
self.change = LayerChange::next();
self.contained.set(None);
self.content_hash.set(None);
}
}