use crate::control::control_behavior::ControlBehavior;
use crate::{control::control_context::ControlContext, Point};
use std::any::Any;
use std::rc::Rc;
use std::rc::Weak;
pub trait ControlObject: ControlBehavior {
fn as_any(&self) -> &dyn Any;
fn get_context(&self) -> &ControlContext;
fn get_controls_at_point(&self, point: Point) -> Vec<Weak<dyn ControlObject>> {
let rect = self.get_rect();
let mut res = Vec::new();
if point.is_inside(&rect) {
let children = self.get_context().get_children();
for child in children {
res.append(&mut child.get_controls_at_point(point));
}
res.push(self.get_context().get_self_weak())
}
res
}
fn get_hit_path(&self, point: Point) -> Vec<Weak<dyn ControlObject>> {
let mut res = Vec::new();
let mut hit_control = self.hit_test(point);
while let Some(control) = &hit_control {
res.push(Rc::downgrade(control));
hit_control = if Rc::ptr_eq(&control, &self.get_context().get_self_rc()) {
None
} else {
control.get_context().get_parent()
}
}
res
}
}
impl PartialEq for dyn ControlObject {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(&self, &other)
}
}