use canvist_scene::NodeId;
use kurbo::Affine;
use kurbo::Point;
use kurbo::Rect;
use kurbo::Vec2;
use crate::SceneIndex;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Selection {
nodes: Vec<NodeId>,
}
impl Selection {
#[must_use]
pub fn nodes(&self) -> &[NodeId] {
&self.nodes
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.nodes.is_empty()
}
#[must_use]
pub fn contains(&self, id: NodeId) -> bool {
self.nodes.contains(&id)
}
pub fn set(&mut self, ids: impl IntoIterator<Item = NodeId>) {
self.nodes.clear();
for id in ids {
if !self.nodes.contains(&id) {
self.nodes.push(id);
}
}
}
pub fn toggle(&mut self, id: NodeId) {
match self.nodes.iter().position(|&node| node == id) {
Some(at) => {
self.nodes.remove(at);
}
None => self.nodes.push(id),
}
}
pub fn clear(&mut self) {
self.nodes.clear();
}
pub fn retain(&mut self, mut keep: impl FnMut(NodeId) -> bool) {
self.nodes.retain(|&id| keep(id));
}
#[must_use]
pub fn frame(&self, index: &SceneIndex) -> Option<Frame> {
match self.nodes.as_slice() {
[] => None,
[single] => {
let geometry = index.get(*single)?;
Some(Frame::from_box(geometry.world, geometry.size))
}
many => index.bounds_of(many).map(Frame::from_rect),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Frame {
pub transform: Affine,
}
impl Frame {
#[must_use]
pub fn from_box(world: Affine, size: Vec2) -> Self {
let size = Vec2::new(size.x.max(f64::EPSILON), size.y.max(f64::EPSILON));
Self {
transform: world * Affine::scale_non_uniform(size.x, size.y),
}
}
#[must_use]
pub fn from_rect(rect: Rect) -> Self {
Self::from_box(
Affine::translate(rect.origin().to_vec2()),
rect.size().to_vec2(),
)
}
#[must_use]
pub fn point(&self, u: f64, v: f64) -> Point {
self.transform * Point::new(u, v)
}
#[must_use]
pub fn corners(&self) -> [Point; 4] {
[
self.point(0.0, 0.0),
self.point(1.0, 0.0),
self.point(1.0, 1.0),
self.point(0.0, 1.0),
]
}
#[must_use]
pub fn center(&self) -> Point {
self.point(0.5, 0.5)
}
}