use nativeview_engine::LayoutRect;
use nativeview_store::{NodeId, NodeStore};
use nativeview_tables::transform::{Transform2D, affine_map_aabb};
use crate::{MAX_TREE_DEPTH, StoreLayoutEngine};
use super::super::LaidOutScene;
use super::resolved::ResolvedOverlay;
impl<L: StoreLayoutEngine> LaidOutScene<L> {
pub fn overlay_contains_point(
&self,
store: &NodeStore,
ro: ResolvedOverlay,
x: f32,
y: f32,
) -> bool {
self.overlay_contains_point_with_transform(store, ro, x, y, |_, base| base)
}
pub fn overlay_contains_point_with_transform(
&self,
store: &NodeStore,
ro: ResolvedOverlay,
x: f32,
y: f32,
resolve_transform: impl Fn(NodeId, Option<Transform2D>) -> Option<Transform2D>,
) -> bool {
let (px, py) = match resolve_transform(ro.node, self.effective_transform(store, ro.node)) {
Some(tf) if !tf.is_identity() => {
let Some(p) = tf.inverse_map_surface_point(
x,
y,
ro.origin_x,
ro.origin_y,
ro.width,
ro.height,
) else {
return false;
};
p
}
_ => (x, y),
};
ro.box_contains(px, py)
}
pub fn visual_rect_of(&self, store: &NodeStore, node_id: NodeId) -> Option<LayoutRect> {
self.visual_rect_of_with_transform(store, node_id, |_, base| base)
}
pub fn visual_rect_of_with_transform(
&self,
store: &NodeStore,
node_id: NodeId,
resolve_transform: impl Fn(NodeId, Option<Transform2D>) -> Option<Transform2D>,
) -> Option<LayoutRect> {
if let Some(ro) = self.overlays.get(node_id) {
let (mut x0, mut y0) = (ro.origin_x, ro.origin_y);
let (mut x1, mut y1) = (x0 + ro.width, y0 + ro.height);
if let Some(tf) = resolve_transform(node_id, self.effective_transform(store, node_id))
&& !tf.is_identity()
{
let m = tf.to_surface_matrix_2d(ro.origin_x, ro.origin_y, ro.width, ro.height);
(x0, y0, x1, y1) = affine_map_aabb(&m, x0, y0, x1, y1);
}
return Some(LayoutRect {
x: x0,
y: y0,
width: (x1 - x0).max(0.0),
height: (y1 - y0).max(0.0),
});
}
let base_rect = self.layout.layout_of(node_id)?;
let (own_sx, own_sy) = self.compute_sticky_delta(store, node_id);
let mut x0 = base_rect.x + own_sx;
let mut y0 = base_rect.y + own_sy;
let mut x1 = x0 + base_rect.width;
let mut y1 = y0 + base_rect.height;
if let Some(tf) = resolve_transform(node_id, self.effective_transform(store, node_id))
&& !tf.is_identity()
{
let m = tf.to_surface_matrix_2d(x0, y0, base_rect.width, base_rect.height);
(x0, y0, x1, y1) = affine_map_aabb(&m, x0, y0, x1, y1);
}
let mut ancestor = store.parent(node_id);
for _ in 0..MAX_TREE_DEPTH {
let Some(pid) = ancestor else {
break;
};
let (sx, sy) = self.compositor_scroll_shift(store, pid);
x0 -= sx;
x1 -= sx;
y0 -= sy;
y1 -= sy;
if let Some(ro) = self.overlays.get(pid) {
if let Some(base) = self.layout.layout_of(pid) {
let dx = ro.origin_x - base.x;
let dy = ro.origin_y - base.y;
x0 += dx;
x1 += dx;
y0 += dy;
y1 += dy;
}
if let Some(tf) = resolve_transform(pid, self.effective_transform(store, pid))
&& !tf.is_identity()
{
let m = tf.to_surface_matrix_2d(ro.origin_x, ro.origin_y, ro.width, ro.height);
(x0, y0, x1, y1) = affine_map_aabb(&m, x0, y0, x1, y1);
}
break;
}
let (stx, sty) = self.compute_sticky_delta(store, pid);
x0 += stx;
x1 += stx;
y0 += sty;
y1 += sty;
if let Some(tf) = resolve_transform(pid, self.effective_transform(store, pid))
&& !tf.is_identity()
&& let Some(p_rect) = self.layout.layout_of(pid)
{
let m = tf.to_surface_matrix_2d(
p_rect.x + stx,
p_rect.y + sty,
p_rect.width,
p_rect.height,
);
(x0, y0, x1, y1) = affine_map_aabb(&m, x0, y0, x1, y1);
}
ancestor = store.parent(pid).filter(|&p| p != pid);
}
Some(LayoutRect {
x: x0,
y: y0,
width: (x1 - x0).max(0.0),
height: (y1 - y0).max(0.0),
})
}
}