use cranpose_core::NodeId;
use cranpose_ui::{Point, Rect, Size};
use super::{
CachePolicy, DrawRunNode, HitTestNode, InlineNode, IsolationReasons, LayerProperties,
PrimitiveNode, PrimitivePhase, ProjectiveTransform,
};
#[derive(Clone, Debug, PartialEq)]
pub struct PrimitiveEntry {
pub phase: PrimitivePhase,
pub node: PrimitiveNode,
}
#[derive(Clone)]
pub struct LayerNode {
pub node_id: Option<NodeId>,
pub wraps: Option<NodeId>,
pub size: Size,
pub layer_bounds: Option<Rect>,
pub transform_to_parent: ProjectiveTransform,
pub content_offset: Point,
pub motion_context_animated: bool,
pub translated_content_context: bool,
pub translated_content_offset: Point,
pub origin_in_parent: Point,
pub graphics_layer: LayerProperties,
pub clip_to_bounds: bool,
pub hit_test: Option<HitTestNode>,
pub has_origin_sinks: bool,
pub isolation: IsolationReasons,
pub draw_phase: Option<PrimitivePhase>,
pub cache_policy: CachePolicy,
pub children: Vec<RenderNode>,
}
impl Default for LayerNode {
fn default() -> Self {
Self {
node_id: None,
wraps: None,
size: Size::default(),
layer_bounds: None,
transform_to_parent: ProjectiveTransform::identity(),
content_offset: Point::default(),
motion_context_animated: false,
translated_content_context: false,
translated_content_offset: Point::default(),
origin_in_parent: Point::default(),
graphics_layer: LayerProperties::default(),
clip_to_bounds: false,
hit_test: None,
has_origin_sinks: false,
isolation: IsolationReasons::default(),
draw_phase: None,
cache_policy: CachePolicy::None,
children: Vec::new(),
}
}
}
#[derive(Clone)]
pub enum RenderNode {
Primitive(PrimitiveEntry),
DrawRun(DrawRunNode),
Layer(Box<LayerNode>),
Inline(Box<InlineNode>),
}
impl RenderNode {
pub fn draw_phase(&self) -> Option<PrimitivePhase> {
match self {
Self::Primitive(entry) => Some(entry.phase),
Self::DrawRun(run) => Some(run.phase),
Self::Layer(layer) => layer.draw_phase,
Self::Inline(_) => None,
}
}
fn draws_after_children(&self) -> bool {
self.draw_phase() == Some(PrimitivePhase::AfterChildren)
}
}
pub fn for_each_in_draw_order(children: &[RenderNode], mut visit: impl FnMut(&RenderNode)) {
let mut first_deferred = children.len();
for (index, child) in children.iter().enumerate() {
if child.draws_after_children() {
first_deferred = first_deferred.min(index);
} else {
visit(child);
}
}
children[first_deferred..]
.iter()
.filter(|child| child.draws_after_children())
.for_each(visit);
}