|
1 | 1 | use crate::blending::AlphaBlending; |
| 2 | +use crate::bounds::BoundingBox; |
2 | 3 | use crate::instances::{Instance, Instances}; |
| 4 | +use crate::math::quad::Quad; |
3 | 5 | use crate::raster::image::Image; |
4 | 6 | use crate::raster_types::{CPU, GPU, Raster, RasterDataTable}; |
5 | 7 | use crate::transform::TransformMut; |
6 | 8 | use crate::uuid::NodeId; |
7 | 9 | use crate::vector::{VectorData, VectorDataTable}; |
8 | 10 | use crate::{CloneVarArgs, Color, Context, Ctx, ExtractAll, OwnedContextImpl}; |
9 | 11 | use dyn_any::DynAny; |
10 | | -use glam::{DAffine2, IVec2}; |
| 12 | +use glam::{DAffine2, DVec2, IVec2}; |
11 | 13 | use std::hash::Hash; |
12 | 14 |
|
13 | | -pub mod renderer; |
14 | | - |
15 | 15 | // TODO: Eventually remove this migration document upgrade code |
16 | 16 | pub fn migrate_graphic_group<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<GraphicGroupTable, D::Error> { |
17 | 17 | use serde::Deserialize; |
@@ -182,6 +182,25 @@ impl GraphicElement { |
182 | 182 | } |
183 | 183 | } |
184 | 184 |
|
| 185 | +impl BoundingBox for GraphicElement { |
| 186 | + fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]> { |
| 187 | + match self { |
| 188 | + GraphicElement::VectorData(vector_data) => vector_data.bounding_box(transform, include_stroke), |
| 189 | + GraphicElement::RasterDataCPU(raster) => raster.bounding_box(transform, include_stroke), |
| 190 | + GraphicElement::RasterDataGPU(raster) => raster.bounding_box(transform, include_stroke), |
| 191 | + GraphicElement::GraphicGroup(graphic_group) => graphic_group.bounding_box(transform, include_stroke), |
| 192 | + } |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +impl BoundingBox for GraphicGroupTable { |
| 197 | + fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]> { |
| 198 | + self.instance_ref_iter() |
| 199 | + .filter_map(|element| element.instance.bounding_box(transform * *element.transform, include_stroke)) |
| 200 | + .reduce(Quad::combine_bounds) |
| 201 | + } |
| 202 | +} |
| 203 | + |
185 | 204 | impl<'de> serde::Deserialize<'de> for Raster<CPU> { |
186 | 205 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
187 | 206 | where |
@@ -247,6 +266,20 @@ impl Artboard { |
247 | 266 | } |
248 | 267 | } |
249 | 268 |
|
| 269 | +impl BoundingBox for Artboard { |
| 270 | + fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]> { |
| 271 | + let artboard_bounds = (transform * Quad::from_box([self.location.as_dvec2(), self.location.as_dvec2() + self.dimensions.as_dvec2()])).bounding_box(); |
| 272 | + if self.clip { |
| 273 | + Some(artboard_bounds) |
| 274 | + } else { |
| 275 | + [self.graphic_group.bounding_box(transform, include_stroke), Some(artboard_bounds)] |
| 276 | + .into_iter() |
| 277 | + .flatten() |
| 278 | + .reduce(Quad::combine_bounds) |
| 279 | + } |
| 280 | + } |
| 281 | +} |
| 282 | + |
250 | 283 | // TODO: Eventually remove this migration document upgrade code |
251 | 284 | pub fn migrate_artboard_group<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<ArtboardGroupTable, D::Error> { |
252 | 285 | use serde::Deserialize; |
@@ -282,6 +315,14 @@ pub fn migrate_artboard_group<'de, D: serde::Deserializer<'de>>(deserializer: D) |
282 | 315 |
|
283 | 316 | pub type ArtboardGroupTable = Instances<Artboard>; |
284 | 317 |
|
| 318 | +impl BoundingBox for ArtboardGroupTable { |
| 319 | + fn bounding_box(&self, transform: DAffine2, include_stroke: bool) -> Option<[DVec2; 2]> { |
| 320 | + self.instance_ref_iter() |
| 321 | + .filter_map(|instance| instance.instance.bounding_box(transform, include_stroke)) |
| 322 | + .reduce(Quad::combine_bounds) |
| 323 | + } |
| 324 | +} |
| 325 | + |
285 | 326 | #[node_macro::node(category(""))] |
286 | 327 | async fn layer<I: 'n + Send + Clone>( |
287 | 328 | _: impl Ctx, |
@@ -506,3 +547,7 @@ impl From<GraphicGroupTable> for GraphicElement { |
506 | 547 | GraphicElement::GraphicGroup(graphic_group) |
507 | 548 | } |
508 | 549 | } |
| 550 | + |
| 551 | +pub trait ToGraphicElement { |
| 552 | + fn to_graphic_element(&self) -> GraphicElement; |
| 553 | +} |
0 commit comments