use super::*;
#[must_use]
pub fn hit_test(tree: &SceneTree, point: Point) -> Option<NodeId> {
let mut visited: usize = 0;
hit_test_with_cache(tree, point, None, &mut visited)
}
#[must_use]
pub(crate) fn hit_test_with_cache(
tree: &SceneTree,
point: Point,
cache: Option<&SplitLayoutCache>,
visited: &mut usize,
) -> Option<NodeId> {
let active_cache: Option<&SplitLayoutCache> =
cache.filter(|c: &&SplitLayoutCache| c.is_synced_with(tree));
let registered: &[NodeId] = tree.portal_nodes();
if !registered.is_empty() {
let root: NodeId = tree.get_root();
let portal_query: HitTestQuery<'_> = HitTestQuery {
tree,
point,
in_portal_pass: true,
active_cache,
};
let mut stack_portals: [PortalZItem; HIT_TEST_STACK_PORTAL_CAP] =
[PortalZItem::default(); HIT_TEST_STACK_PORTAL_CAP];
let mut stack_len: usize = 0;
let mut overflowed: bool = false;
for portal_id in registered.iter().copied() {
if is_active_top_layer_portal(tree, root, portal_id)
&& let Some(node) = tree.get_node(portal_id)
{
let item: PortalZItem = PortalZItem {
z_index: node.style_ref().get_layout().get_z_index(),
node_id: portal_id,
};
if stack_len < HIT_TEST_STACK_PORTAL_CAP {
stack_portals[stack_len] = item;
stack_len += 1;
} else {
overflowed = true;
break;
}
}
}
if overflowed {
if let Some(hit) = hit_test_overflow_portals(portal_query, root, registered, visited) {
return Some(hit);
}
} else if stack_len == 1 {
if let Some(hit) = hit_test_subtree(
portal_query,
stack_portals[0].node_id,
Point::new(0.0, 0.0),
None,
visited,
) {
return Some(hit);
}
} else if stack_len > 1 {
stack_portals[..stack_len].sort_unstable_by(|a: &PortalZItem, b: &PortalZItem| {
b.z_index.cmp(&a.z_index).then_with(|| {
let (rb, ra): (usize, usize) = tree_order_pair(tree, b.node_id, a.node_id);
rb.cmp(&ra)
})
});
for item in stack_portals[..stack_len].iter().copied() {
if let Some(hit) = hit_test_subtree(
portal_query,
item.node_id,
Point::new(0.0, 0.0),
None,
visited,
) {
return Some(hit);
}
}
}
}
let root_query: HitTestQuery<'_> = HitTestQuery {
tree,
point,
in_portal_pass: false,
active_cache,
};
hit_test_subtree(
root_query,
tree.get_root(),
Point::new(0.0, 0.0),
None,
visited,
)
}
fn hit_test_overflow_portals(
query: HitTestQuery<'_>,
root: NodeId,
registered: &[NodeId],
visited: &mut usize,
) -> Option<NodeId> {
let range_opt: Option<(usize, usize)> = HIT_TEST_SCRATCH
.try_with(|cell: &RefCell<HitTestScratch>| {
let Ok(mut scratch) = cell.try_borrow_mut() else {
return None;
};
let base: usize = scratch.portals.len();
for portal_id in registered.iter().copied() {
if is_active_top_layer_portal(query.tree, root, portal_id)
&& let Some(node) = query.tree.get_node(portal_id)
{
scratch.portals.push(PortalZItem {
z_index: node.style_ref().get_layout().get_z_index(),
node_id: portal_id,
});
}
}
let end: usize = scratch.portals.len();
scratch.portals[base..end].sort_unstable_by(|a: &PortalZItem, b: &PortalZItem| {
b.z_index.cmp(&a.z_index).then_with(|| {
let (rb, ra): (usize, usize) =
tree_order_pair_stack_only(query.tree, b.node_id, a.node_id);
rb.cmp(&ra)
})
});
Some((base, end))
})
.ok()
.flatten();
let (base, end): (usize, usize) = range_opt?;
let mut found: Option<NodeId> = None;
for idx in base..end {
let portal_id_opt: Option<NodeId> = HIT_TEST_SCRATCH
.try_with(|cell: &RefCell<HitTestScratch>| {
cell.try_borrow()
.ok()
.and_then(|s: Ref<'_, HitTestScratch>| {
s.portals.get(idx).map(|p: &PortalZItem| p.node_id)
})
})
.ok()
.flatten();
if let Some(portal_id) = portal_id_opt
&& let Some(hit) =
hit_test_subtree(query, portal_id, Point::new(0.0, 0.0), None, visited)
{
found = Some(hit);
break;
}
}
let _truncated: bool = HIT_TEST_SCRATCH
.try_with(|cell: &RefCell<HitTestScratch>| {
if let Ok(mut scratch) = cell.try_borrow_mut() {
scratch.portals.truncate(base);
}
})
.is_ok();
found
}
fn is_active_top_layer_portal(tree: &SceneTree, root: NodeId, portal_id: NodeId) -> bool {
let Some(node) = tree.get_node(portal_id) else {
return false;
};
let Style { layout, paint, .. } = node.style_ref();
if layout.get_display() == Display::None
|| paint.get_pointer_events() == PointerEvents::None
|| paint.get_opacity() <= 0.001
{
return false;
}
let is_portal: bool =
matches!(node.get_kind(), NodeKind::Portal) || layout.get_position() == Position::Fixed;
if !is_portal {
return false;
}
if portal_id == root {
return true;
}
let mut cursor: Option<NodeId> = node.parent_id();
while let Some(pid) = cursor {
let Some(pnode) = tree.get_node(pid) else {
return false;
};
let Style {
layout: playout,
paint: ppaint,
..
} = pnode.style_ref();
if playout.get_display() == Display::None
|| ppaint.get_pointer_events() == PointerEvents::None
|| ppaint.get_opacity() <= 0.001
{
return false;
}
let parent_is_portal: bool = matches!(pnode.get_kind(), NodeKind::Portal)
|| playout.get_position() == Position::Fixed;
if parent_is_portal {
return false;
}
if pid == root {
return true;
}
cursor = pnode.parent_id();
}
false
}
fn tree_order_pair(tree: &SceneTree, a: NodeId, b: NodeId) -> (usize, usize) {
if a == b {
return (0, 0);
}
let mut path_a: [NodeId; HIT_TEST_STACK_PATH_CAP] =
[NodeId::default(); HIT_TEST_STACK_PATH_CAP];
let mut len_a: usize = 0;
let mut cur_a: Option<NodeId> = Some(a);
let mut overflow_a: bool = false;
while let Some(id) = cur_a {
if len_a < HIT_TEST_STACK_PATH_CAP {
path_a[len_a] = id;
len_a += 1;
} else {
overflow_a = true;
break;
}
cur_a = tree.get_node(id).and_then(SceneNode::parent_id);
}
let mut path_b: [NodeId; HIT_TEST_STACK_PATH_CAP] =
[NodeId::default(); HIT_TEST_STACK_PATH_CAP];
let mut len_b: usize = 0;
let mut cur_b: Option<NodeId> = Some(b);
let mut overflow_b: bool = false;
while let Some(id) = cur_b {
if len_b < HIT_TEST_STACK_PATH_CAP {
path_b[len_b] = id;
len_b += 1;
} else {
overflow_b = true;
break;
}
cur_b = tree.get_node(id).and_then(SceneNode::parent_id);
}
if !overflow_a && !overflow_b {
path_a[..len_a].reverse();
path_b[..len_b].reverse();
return compare_root_paths(tree, &path_a[..len_a], &path_b[..len_b], a, b);
}
HIT_TEST_SCRATCH
.try_with(|cell: &RefCell<HitTestScratch>| {
let Ok(mut scratch) = cell.try_borrow_mut() else {
return (a.as_usize(), b.as_usize());
};
scratch.path_a.clear();
let mut ca: Option<NodeId> = Some(a);
while let Some(id) = ca {
scratch.path_a.push(id);
ca = tree.get_node(id).and_then(SceneNode::parent_id);
}
scratch.path_a.reverse();
scratch.path_b.clear();
let mut cb: Option<NodeId> = Some(b);
while let Some(id) = cb {
scratch.path_b.push(id);
cb = tree.get_node(id).and_then(SceneNode::parent_id);
}
scratch.path_b.reverse();
let res: (usize, usize) =
compare_root_paths(tree, &scratch.path_a, &scratch.path_b, a, b);
scratch.path_a.clear();
scratch.path_b.clear();
res
})
.unwrap_or((a.as_usize(), b.as_usize()))
}
fn tree_order_pair_stack_only(tree: &SceneTree, a: NodeId, b: NodeId) -> (usize, usize) {
if a == b {
return (0, 0);
}
let mut path_a: [NodeId; HIT_TEST_STACK_PATH_CAP] =
[NodeId::default(); HIT_TEST_STACK_PATH_CAP];
let mut len_a: usize = 0;
let mut cur_a: Option<NodeId> = Some(a);
while let Some(id) = cur_a {
if len_a == HIT_TEST_STACK_PATH_CAP {
return (a.as_usize(), b.as_usize());
}
path_a[len_a] = id;
len_a += 1;
cur_a = tree.get_node(id).and_then(SceneNode::parent_id);
}
let mut path_b: [NodeId; HIT_TEST_STACK_PATH_CAP] =
[NodeId::default(); HIT_TEST_STACK_PATH_CAP];
let mut len_b: usize = 0;
let mut cur_b: Option<NodeId> = Some(b);
while let Some(id) = cur_b {
if len_b == HIT_TEST_STACK_PATH_CAP {
return (a.as_usize(), b.as_usize());
}
path_b[len_b] = id;
len_b += 1;
cur_b = tree.get_node(id).and_then(SceneNode::parent_id);
}
path_a[..len_a].reverse();
path_b[..len_b].reverse();
compare_root_paths(tree, &path_a[..len_a], &path_b[..len_b], a, b)
}
fn compare_root_paths(
tree: &SceneTree,
path_a: &[NodeId],
path_b: &[NodeId],
a: NodeId,
b: NodeId,
) -> (usize, usize) {
let min_len: usize = path_a.len().min(path_b.len());
let mut diff_idx: usize = 0;
while diff_idx < min_len && path_a[diff_idx] == path_b[diff_idx] {
diff_idx += 1;
}
if diff_idx == min_len {
return (path_a.len(), path_b.len());
}
if diff_idx == 0 {
return (a.as_usize(), b.as_usize());
}
let lca: NodeId = path_a[diff_idx - 1];
let branch_a: NodeId = path_a[diff_idx];
let branch_b: NodeId = path_b[diff_idx];
if tree.get_node(lca).is_some() {
for child_id in tree.children_iter(lca) {
if child_id == branch_a {
return (0, 1);
}
if child_id == branch_b {
return (1, 0);
}
}
}
(a.as_usize(), b.as_usize())
}
#[inline(always)]
pub(crate) fn shifted_node_bounds(
raw_bounds: Rect,
scroll_offset: Point,
transform: Transform2D,
) -> Rect {
let tx: f32 = transform.get_translate_x();
let ty: f32 = transform.get_translate_y();
let sx: f32 = transform.get_scale_x();
let sy: f32 = transform.get_scale_y();
if sx == 1.0 && sy == 1.0 {
Rect::new(
raw_bounds.get_x() - scroll_offset.get_x() + tx,
raw_bounds.get_y() - scroll_offset.get_y() + ty,
raw_bounds.get_width(),
raw_bounds.get_height(),
)
} else {
let scaled_w: f32 = raw_bounds.get_width() * sx;
let scaled_h: f32 = raw_bounds.get_height() * sy;
let scale_dx: f32 = (raw_bounds.get_width() - scaled_w) * 0.5;
let scale_dy: f32 = (raw_bounds.get_height() - scaled_h) * 0.5;
Rect::new(
raw_bounds.get_x() - scroll_offset.get_x() + tx + scale_dx,
raw_bounds.get_y() - scroll_offset.get_y() + ty + scale_dy,
scaled_w,
scaled_h,
)
}
}
fn hit_test_subtree(
query: HitTestQuery<'_>,
node_id: NodeId,
scroll_offset: Point,
clip_rect: Option<Rect>,
visited: &mut usize,
) -> Option<NodeId> {
let node: &SceneNode = query.tree.get_node(node_id)?;
hit_test_resolved(query, node_id, node, scroll_offset, clip_rect, visited)
}
#[inline(always)]
fn hit_test_resolved(
query: HitTestQuery<'_>,
node_id: NodeId,
node: &SceneNode,
scroll_offset: Point,
clip_rect: Option<Rect>,
visited: &mut usize,
) -> Option<NodeId> {
*visited = visited.saturating_add(1);
let Style { layout, paint, .. } = node.style_ref();
if layout.get_display() == Display::None
|| paint.get_pointer_events() == PointerEvents::None
|| paint.get_opacity() <= 0.001
{
return None;
}
let is_portal: bool =
matches!(node.get_kind(), NodeKind::Portal) || layout.get_position() == Position::Fixed;
if !query.in_portal_pass && is_portal {
return None;
}
let transform: Transform2D = paint.get_transform();
let shifted_bounds: Rect =
shifted_node_bounds(node.get_layout_box().get_bounds(), scroll_offset, transform);
let is_clipped: bool = matches!(layout.get_overflow(), Overflow::Hidden | Overflow::Scroll);
let child_count: usize = query.tree.child_count(node_id);
if child_count == 0 {
return if shifted_bounds.contains(query.point) {
Some(node_id)
} else {
None
};
}
if is_clipped && !shifted_bounds.contains(query.point) {
return None;
}
if !is_clipped
&& let Some(sc) = query.active_cache
&& let Some(sb) = sc.subtree_bounds_for(node_id)
{
let shifted_sb: Rect = Rect::new(
sb.get_x() - scroll_offset.get_x(),
sb.get_y() - scroll_offset.get_y(),
sb.get_width(),
sb.get_height(),
);
if !shifted_sb.contains(query.point) {
return None;
}
}
let effective_clip: Option<Rect> = if is_clipped {
match clip_rect {
Some(parent_clip) => parent_clip.intersect(shifted_bounds),
None => Some(shifted_bounds),
}
} else {
clip_rect
};
if let Some(clip) = effective_clip
&& !clip.contains(query.point)
{
return None;
}
let node_scroll: Point = node.get_layout_box().get_scroll_offset();
let child_scroll: Point = Point::new(
scroll_offset.get_x() + node_scroll.get_x() - transform.get_translate_x(),
scroll_offset.get_y() + node_scroll.get_y() - transform.get_translate_y(),
);
if child_count == 1 {
if let Some(single_id) = query.tree.first_child(node_id)
&& let Some(single_node) = query.tree.get_node(single_id)
{
if !query.tree.has_children(single_id) {
*visited = visited.saturating_add(1);
let Style {
layout: c_layout,
paint: c_paint,
..
} = single_node.style_ref();
let c_hidden: bool = c_layout.get_display() == Display::None
|| c_paint.get_pointer_events() == PointerEvents::None
|| c_paint.get_opacity() <= 0.001;
let c_portal: bool = matches!(single_node.get_kind(), NodeKind::Portal)
|| c_layout.get_position() == Position::Fixed;
if !c_hidden && (query.in_portal_pass || !c_portal) {
let c_bounds: Rect = shifted_node_bounds(
single_node.get_layout_box().get_bounds(),
child_scroll,
c_paint.get_transform(),
);
if c_bounds.contains(query.point) {
return Some(single_id);
}
}
} else if let Some(hit) = hit_test_resolved(
query,
single_id,
single_node,
child_scroll,
effective_clip,
visited,
) {
return Some(hit);
}
}
return if shifted_bounds.contains(query.point) {
Some(node_id)
} else {
None
};
}
hit_test_multi_children(
query,
node_id,
child_count,
shifted_bounds,
child_scroll,
effective_clip,
visited,
)
}
fn hit_test_multi_children(
query: HitTestQuery<'_>,
node_id: NodeId,
child_count: usize,
shifted_bounds: Rect,
child_scroll: Point,
effective_clip: Option<Rect>,
visited: &mut usize,
) -> Option<NodeId> {
let all_zero_z: bool = !query.tree.has_nonzero_z_child(node_id)
|| query
.active_cache
.and_then(|c: &SplitLayoutCache| c.has_nonzero_z_child(node_id))
== Some(false);
if all_zero_z {
for child_id in query.tree.children_iter(node_id).rev() {
if let Some(child_node) = query.tree.get_node(child_id)
&& let Some(hit) = hit_test_resolved(
query,
child_id,
child_node,
child_scroll,
effective_clip,
visited,
)
{
return Some(hit);
}
}
return if shifted_bounds.contains(query.point) {
Some(node_id)
} else {
None
};
}
if let Some(hit) = hit_test_z_sorted_children(
query,
node_id,
child_count,
child_scroll,
effective_clip,
visited,
) {
return Some(hit);
}
if shifted_bounds.contains(query.point) {
Some(node_id)
} else {
None
}
}
fn hit_test_z_sorted_children(
query: HitTestQuery<'_>,
node_id: NodeId,
child_count: usize,
child_scroll: Point,
effective_clip: Option<Rect>,
visited: &mut usize,
) -> Option<NodeId> {
if child_count <= HIT_TEST_STACK_Z_CAP {
let mut stack_items: [ZItem; HIT_TEST_STACK_Z_CAP] =
[ZItem::default(); HIT_TEST_STACK_Z_CAP];
let mut stack_len: usize = 0;
for (order, cid) in query.tree.children_iter(node_id).enumerate() {
if let Some(child_node) = query.tree.get_node(cid) {
stack_items[stack_len] = ZItem {
z_index: child_node.style_ref().get_layout().get_z_index(),
order,
node_id: cid,
};
stack_len += 1;
}
}
stack_items[..stack_len].sort_unstable_by(|a: &ZItem, b: &ZItem| {
b.z_index
.cmp(&a.z_index)
.then_with(|| b.order.cmp(&a.order))
});
for item in stack_items[..stack_len].iter().copied() {
if let Some(hit) =
hit_test_subtree(query, item.node_id, child_scroll, effective_clip, visited)
{
return Some(hit);
}
}
return None;
}
hit_test_z_sorted_scratch(query, node_id, child_scroll, effective_clip, visited)
}
fn hit_test_z_sorted_scratch(
query: HitTestQuery<'_>,
node_id: NodeId,
child_scroll: Point,
effective_clip: Option<Rect>,
visited: &mut usize,
) -> Option<NodeId> {
let range_opt: Option<(usize, usize)> = HIT_TEST_SCRATCH
.try_with(|cell: &RefCell<HitTestScratch>| {
let Ok(mut scratch) = cell.try_borrow_mut() else {
return None;
};
let base: usize = scratch.z_items.len();
for (order, cid) in query.tree.children_iter(node_id).enumerate() {
if let Some(child_node) = query.tree.get_node(cid) {
let z: i32 = child_node.style_ref().get_layout().get_z_index();
scratch.z_items.push(ZItem {
z_index: z,
order,
node_id: cid,
});
}
}
let end: usize = scratch.z_items.len();
scratch.z_items[base..end].sort_unstable_by(|a: &ZItem, b: &ZItem| {
b.z_index
.cmp(&a.z_index)
.then_with(|| b.order.cmp(&a.order))
});
Some((base, end))
})
.ok()
.flatten();
let (base, end): (usize, usize) = range_opt?;
let mut found: Option<NodeId> = None;
for idx in base..end {
let child_id_opt: Option<NodeId> = HIT_TEST_SCRATCH
.try_with(|cell: &RefCell<HitTestScratch>| {
cell.try_borrow()
.ok()
.and_then(|s: Ref<'_, HitTestScratch>| {
s.z_items.get(idx).map(|z: &ZItem| z.node_id)
})
})
.ok()
.flatten();
if let Some(child_id) = child_id_opt
&& let Some(hit) =
hit_test_subtree(query, child_id, child_scroll, effective_clip, visited)
{
found = Some(hit);
break;
}
}
let _truncated: bool = HIT_TEST_SCRATCH
.try_with(|cell: &RefCell<HitTestScratch>| {
if let Ok(mut scratch) = cell.try_borrow_mut() {
scratch.z_items.truncate(base);
}
})
.is_ok();
found
}