|
1 | | -use crate::instances::Instances; |
| 1 | +use crate::Artboard; |
2 | 2 | use crate::math::bbox::AxisAlignedBbox; |
3 | | -use crate::raster_types::{CPU, GPU, RasterDataTable}; |
4 | | -use crate::vector::VectorDataTable; |
5 | | -use crate::{Artboard, CloneVarArgs, Context, Ctx, ExtractAll, GraphicGroupTable, OwnedContextImpl}; |
| 3 | +pub use crate::vector::ReferencePoint; |
6 | 4 | use core::f64; |
7 | 5 | use glam::{DAffine2, DMat2, DVec2}; |
8 | 6 |
|
@@ -152,186 +150,3 @@ impl<T: TransformMut> ApplyTransform for T { |
152 | 150 | impl ApplyTransform for () { |
153 | 151 | fn apply_transform(&mut self, &_modification: &DAffine2) {} |
154 | 152 | } |
155 | | - |
156 | | -#[node_macro::node(category(""))] |
157 | | -async fn transform<T: 'n + 'static>( |
158 | | - ctx: impl Ctx + CloneVarArgs + ExtractAll, |
159 | | - #[implementations( |
160 | | - Context -> VectorDataTable, |
161 | | - Context -> GraphicGroupTable, |
162 | | - Context -> RasterDataTable<CPU>, |
163 | | - Context -> RasterDataTable<GPU>, |
164 | | - )] |
165 | | - transform_target: impl Node<Context<'static>, Output = Instances<T>>, |
166 | | - translate: DVec2, |
167 | | - rotate: f64, |
168 | | - scale: DVec2, |
169 | | - shear: DVec2, |
170 | | - _pivot: DVec2, |
171 | | -) -> Instances<T> { |
172 | | - let matrix = DAffine2::from_scale_angle_translation(scale, rotate, translate) * DAffine2::from_cols_array(&[1., shear.y, shear.x, 1., 0., 0.]); |
173 | | - |
174 | | - let footprint = ctx.try_footprint().copied(); |
175 | | - |
176 | | - let mut ctx = OwnedContextImpl::from(ctx); |
177 | | - if let Some(mut footprint) = footprint { |
178 | | - footprint.apply_transform(&matrix); |
179 | | - ctx = ctx.with_footprint(footprint); |
180 | | - } |
181 | | - |
182 | | - let mut transform_target = transform_target.eval(ctx.into_context()).await; |
183 | | - |
184 | | - for data_transform in transform_target.instance_mut_iter() { |
185 | | - *data_transform.transform = matrix * *data_transform.transform; |
186 | | - } |
187 | | - |
188 | | - transform_target |
189 | | -} |
190 | | - |
191 | | -#[node_macro::node(category(""))] |
192 | | -fn replace_transform<Data, TransformInput: Transform>( |
193 | | - _: impl Ctx, |
194 | | - #[implementations(VectorDataTable, RasterDataTable<CPU>, GraphicGroupTable)] mut data: Instances<Data>, |
195 | | - #[implementations(DAffine2)] transform: TransformInput, |
196 | | -) -> Instances<Data> { |
197 | | - for data_transform in data.instance_mut_iter() { |
198 | | - *data_transform.transform = transform.transform(); |
199 | | - } |
200 | | - data |
201 | | -} |
202 | | - |
203 | | -#[node_macro::node(category("Debug"))] |
204 | | -async fn boundless_footprint<T: 'n + 'static>( |
205 | | - ctx: impl Ctx + CloneVarArgs + ExtractAll, |
206 | | - #[implementations( |
207 | | - Context -> VectorDataTable, |
208 | | - Context -> GraphicGroupTable, |
209 | | - Context -> RasterDataTable<CPU>, |
210 | | - Context -> RasterDataTable<GPU>, |
211 | | - Context -> String, |
212 | | - Context -> f64, |
213 | | - )] |
214 | | - transform_target: impl Node<Context<'static>, Output = T>, |
215 | | -) -> T { |
216 | | - let ctx = OwnedContextImpl::from(ctx).with_footprint(Footprint::BOUNDLESS); |
217 | | - |
218 | | - transform_target.eval(ctx.into_context()).await |
219 | | -} |
220 | | -#[node_macro::node(category("Debug"))] |
221 | | -async fn freeze_real_time<T: 'n + 'static>( |
222 | | - ctx: impl Ctx + CloneVarArgs + ExtractAll, |
223 | | - #[implementations( |
224 | | - Context -> VectorDataTable, |
225 | | - Context -> GraphicGroupTable, |
226 | | - Context -> RasterDataTable<CPU>, |
227 | | - Context -> RasterDataTable<GPU>, |
228 | | - Context -> String, |
229 | | - Context -> f64, |
230 | | - )] |
231 | | - transform_target: impl Node<Context<'static>, Output = T>, |
232 | | -) -> T { |
233 | | - let ctx = OwnedContextImpl::from(ctx).with_real_time(0.); |
234 | | - |
235 | | - transform_target.eval(ctx.into_context()).await |
236 | | -} |
237 | | - |
238 | | -#[derive(Clone, Copy, Debug, Default, Hash, Eq, PartialEq, dyn_any::DynAny, serde::Serialize, serde::Deserialize, specta::Type)] |
239 | | -pub enum ReferencePoint { |
240 | | - #[default] |
241 | | - None, |
242 | | - TopLeft, |
243 | | - TopCenter, |
244 | | - TopRight, |
245 | | - CenterLeft, |
246 | | - Center, |
247 | | - CenterRight, |
248 | | - BottomLeft, |
249 | | - BottomCenter, |
250 | | - BottomRight, |
251 | | -} |
252 | | - |
253 | | -impl ReferencePoint { |
254 | | - pub fn point_in_bounding_box(&self, bounding_box: AxisAlignedBbox) -> Option<DVec2> { |
255 | | - let size = bounding_box.size(); |
256 | | - let offset = match self { |
257 | | - ReferencePoint::None => return None, |
258 | | - ReferencePoint::TopLeft => DVec2::ZERO, |
259 | | - ReferencePoint::TopCenter => DVec2::new(size.x / 2., 0.), |
260 | | - ReferencePoint::TopRight => DVec2::new(size.x, 0.), |
261 | | - ReferencePoint::CenterLeft => DVec2::new(0., size.y / 2.), |
262 | | - ReferencePoint::Center => DVec2::new(size.x / 2., size.y / 2.), |
263 | | - ReferencePoint::CenterRight => DVec2::new(size.x, size.y / 2.), |
264 | | - ReferencePoint::BottomLeft => DVec2::new(0., size.y), |
265 | | - ReferencePoint::BottomCenter => DVec2::new(size.x / 2., size.y), |
266 | | - ReferencePoint::BottomRight => DVec2::new(size.x, size.y), |
267 | | - }; |
268 | | - Some(bounding_box.start + offset) |
269 | | - } |
270 | | -} |
271 | | - |
272 | | -impl From<&str> for ReferencePoint { |
273 | | - fn from(input: &str) -> Self { |
274 | | - match input { |
275 | | - "None" => ReferencePoint::None, |
276 | | - "TopLeft" => ReferencePoint::TopLeft, |
277 | | - "TopCenter" => ReferencePoint::TopCenter, |
278 | | - "TopRight" => ReferencePoint::TopRight, |
279 | | - "CenterLeft" => ReferencePoint::CenterLeft, |
280 | | - "Center" => ReferencePoint::Center, |
281 | | - "CenterRight" => ReferencePoint::CenterRight, |
282 | | - "BottomLeft" => ReferencePoint::BottomLeft, |
283 | | - "BottomCenter" => ReferencePoint::BottomCenter, |
284 | | - "BottomRight" => ReferencePoint::BottomRight, |
285 | | - _ => panic!("Failed parsing unrecognized ReferencePosition enum value '{input}'"), |
286 | | - } |
287 | | - } |
288 | | -} |
289 | | - |
290 | | -impl From<ReferencePoint> for Option<DVec2> { |
291 | | - fn from(input: ReferencePoint) -> Self { |
292 | | - match input { |
293 | | - ReferencePoint::None => None, |
294 | | - ReferencePoint::TopLeft => Some(DVec2::new(0., 0.)), |
295 | | - ReferencePoint::TopCenter => Some(DVec2::new(0.5, 0.)), |
296 | | - ReferencePoint::TopRight => Some(DVec2::new(1., 0.)), |
297 | | - ReferencePoint::CenterLeft => Some(DVec2::new(0., 0.5)), |
298 | | - ReferencePoint::Center => Some(DVec2::new(0.5, 0.5)), |
299 | | - ReferencePoint::CenterRight => Some(DVec2::new(1., 0.5)), |
300 | | - ReferencePoint::BottomLeft => Some(DVec2::new(0., 1.)), |
301 | | - ReferencePoint::BottomCenter => Some(DVec2::new(0.5, 1.)), |
302 | | - ReferencePoint::BottomRight => Some(DVec2::new(1., 1.)), |
303 | | - } |
304 | | - } |
305 | | -} |
306 | | - |
307 | | -impl From<DVec2> for ReferencePoint { |
308 | | - fn from(input: DVec2) -> Self { |
309 | | - const TOLERANCE: f64 = 1e-5_f64; |
310 | | - if input.y.abs() < TOLERANCE { |
311 | | - if input.x.abs() < TOLERANCE { |
312 | | - return ReferencePoint::TopLeft; |
313 | | - } else if (input.x - 0.5).abs() < TOLERANCE { |
314 | | - return ReferencePoint::TopCenter; |
315 | | - } else if (input.x - 1.).abs() < TOLERANCE { |
316 | | - return ReferencePoint::TopRight; |
317 | | - } |
318 | | - } else if (input.y - 0.5).abs() < TOLERANCE { |
319 | | - if input.x.abs() < TOLERANCE { |
320 | | - return ReferencePoint::CenterLeft; |
321 | | - } else if (input.x - 0.5).abs() < TOLERANCE { |
322 | | - return ReferencePoint::Center; |
323 | | - } else if (input.x - 1.).abs() < TOLERANCE { |
324 | | - return ReferencePoint::CenterRight; |
325 | | - } |
326 | | - } else if (input.y - 1.).abs() < TOLERANCE { |
327 | | - if input.x.abs() < TOLERANCE { |
328 | | - return ReferencePoint::BottomLeft; |
329 | | - } else if (input.x - 0.5).abs() < TOLERANCE { |
330 | | - return ReferencePoint::BottomCenter; |
331 | | - } else if (input.x - 1.).abs() < TOLERANCE { |
332 | | - return ReferencePoint::BottomRight; |
333 | | - } |
334 | | - } |
335 | | - ReferencePoint::None |
336 | | - } |
337 | | -} |
0 commit comments