scal-runtime 1.2.0

Runtime for SCAL- easy animation system focused on code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
use anyhow::{Result, bail};
use scal_core::{CodeAnimationStyle, Sfx, Theme};

use crate::{
    anim_object::{
        code_window,
        compose::{
            Alignment as RuntimeAlignment, LayoutBackground, LayoutDir as RuntimeLayoutDir,
            LayoutItem, PinPoint,
        },
        scroll::ScrollLayout,
    },
    anim_op::AnimOperation,
};
fn convert_anim_op(
    op: scal_core::AnimOP,
    default_theme: &scal_core::Theme,
) -> Result<AnimOperation> {
    Ok(match op {
        scal_core::AnimOP::Wait(dur, loc) => AnimOperation::Wait(dur, loc),
        scal_core::AnimOP::All(children, loc) => {
            AnimOperation::All(convert_anim_ops(children, default_theme)?, loc)
        }
        scal_core::AnimOP::Sequence(children, loc) => {
            AnimOperation::Sequence(convert_anim_ops(children, default_theme)?, loc)
        }
        scal_core::AnimOP::PlaySound(sfx, delay, loc) => AnimOperation::PlaySound(
            Sfx {
                path: sfx.path,
                volume: sfx.volume,
                pitch: sfx.pitch,
                time_offset: sfx.time_offset,
                duration: sfx.duration,
                pitch_variation: sfx.pitch_variation,
            },
            delay,
            loc,
        ),
        scal_core::AnimOP::Instantiate(core_obj, loc) => {
            if let scal_core::anim_obj::AnimObjKind::CodeWindow { .. } = &core_obj.kind {
                let mut op = build_code_window_op(*core_obj, default_theme)?;
                if let AnimOperation::Instantiate(_, ref mut l) = op {
                    *l = loc;
                }
                op
            } else if let scal_core::anim_obj::AnimObjKind::Terminal { .. } = &core_obj.kind {
                let mut op = build_terminal_op(*core_obj, default_theme)?;
                if let AnimOperation::Instantiate(_, ref mut l) = op {
                    *l = loc;
                }
                op
            } else if let scal_core::anim_obj::AnimObjKind::ScrollLayout { .. } = &core_obj.kind {
                let mut op = build_scroll_layout_op(*core_obj, default_theme)?;
                if let AnimOperation::Instantiate(_, ref mut l) = op {
                    *l = loc;
                }
                op
            } else if let scal_core::anim_obj::AnimObjKind::Layout { .. } = &core_obj.kind {
                let mut op = build_layout_op(*core_obj, default_theme)?;
                if let AnimOperation::Instantiate(_, ref mut l) = op {
                    *l = loc;
                }
                op
            } else {
                let render_obj = convert_core_anim_obj(*core_obj, default_theme)?;
                AnimOperation::Instantiate(render_obj, loc)
            }
        }
        scal_core::AnimOP::TransformMovePos(u, v, d, e, loc) => {
            AnimOperation::TransformMovePos(u, v, d, e, loc)
        }
        scal_core::AnimOP::TransformMoveToObj(u, t, o, d, e, loc) => {
            AnimOperation::TransformMoveToObj(u, t, o, d, e, loc)
        }
        scal_core::AnimOP::TransformRotate(u, r, d, e, loc) => {
            AnimOperation::TransformRotate(u, r, d, e, loc)
        }
        scal_core::AnimOP::TransformScale(u, v, d, e, loc) => {
            AnimOperation::TransformScale(u, v, d, e, loc)
        }
        scal_core::AnimOP::CodeAddLines(u, t, f, d, e, s, loc) => {
            AnimOperation::CodeAddLines(u, t, f, d, e, s, loc)
        }
        scal_core::AnimOP::CodeModifyLine(u, l, t, d, e, s, loc) => {
            AnimOperation::CodeModifyLine(u, l, t, d, e, s, loc)
        }
        scal_core::AnimOP::CodeRemoveLines(u, r, d, e, s, loc) => {
            AnimOperation::CodeRemoveLines(u, r, d, e, s, loc)
        }
        scal_core::AnimOP::CodeHighlight(u, action, loc) => {
            AnimOperation::CodeHighlight(u, action, loc)
        }
        scal_core::AnimOP::TerminalTypeInput(u, cmd, ovr, captured, prompt, d, e, style, loc) => {
            AnimOperation::TerminalTypeInput(u, cmd, ovr, captured, prompt, d, e, style, loc)
        }
        scal_core::AnimOP::TerminalOutput(u, action, d, e, style, loc) => {
            AnimOperation::TerminalOutput(u, action, d, e, style, loc)
        }
        scal_core::AnimOP::ObjectColor(u, c, d, e, loc) => {
            AnimOperation::ObjectColor(u, c, d, e, loc)
        }
        scal_core::AnimOP::MathPartColor(u, part, c, d, e, loc) => {
            AnimOperation::MathPartColor(u, part, c, d, e, loc)
        }
        scal_core::AnimOP::MathHighlight(u, parts, c, dim, d, e, loc) => {
            AnimOperation::MathHighlight(u, parts, c, dim, d, e, loc)
        }
        scal_core::AnimOP::ScrollOffset(u, target, d, e, loc) => {
            AnimOperation::ScrollOffset(u, target, d, e, loc)
        }
    })
}

pub fn convert_anim_ops(
    ops: Vec<scal_core::AnimOP>,
    default_theme: &scal_core::Theme,
) -> Result<Vec<AnimOperation>> {
    let mut result = Vec::with_capacity(ops.len());
    for op in ops {
        result.push(convert_anim_op(op, default_theme)?);
    }
    Ok(result)
}

fn build_code_window_op(
    obj: scal_core::AnimObj,
    default_theme: &scal_core::Theme,
) -> Result<AnimOperation> {
    use scal_core::anim_obj::{AnimObjKind, Syntax};
    if let AnimObjKind::CodeWindow {
        source_code,
        font_family,
        font_size,
        syntax,
        theme,
        title,
        title_font_size,
        width,
        height,
        background_color,
        code_id,
        close_btn_id,
        minimize_btn_id,
        maximize_btn_id,
        title_id,
        container_id,
        title_bar_bg_id,
        show_line_numbers,
        line_number_color,
        ..
    } = obj.kind
    {
        let t = theme.as_ref().unwrap_or(default_theme);
        let render_base16 = t.base;

        let th = Theme::from_base16(render_base16);
        let cw = code_window(
            obj.transform.position,
            source_code,
            th,
            font_family,
            RuntimeAlignment::Start,
            font_size,
            syntax,
            title,
            width,
            height,
            title_font_size,
            background_color,
            code_id,
            close_btn_id,
            minimize_btn_id,
            maximize_btn_id,
            title_id,
            obj.id,
            container_id,
            title_bar_bg_id,
            show_line_numbers,
            line_number_color,
        );
        Ok(cw.instantiate())
    } else {
        bail!("build_code_window_op called on non-CodeWindow kind")
    }
}

fn build_terminal_op(
    obj: scal_core::AnimObj,
    default_theme: &scal_core::Theme,
) -> Result<AnimOperation> {
    use scal_core::anim_obj::AnimObjKind;
    if let AnimObjKind::Terminal {
        shell,
        prompt,
        font_family,
        font_size,
        theme,
        width,
        height,
        background_color,
        text_color,
        term_id,
        text_buffer_id,
        bg_id,
        container_id,
        close_btn_id,
        minimize_btn_id,
        maximize_btn_id,
        title_id,
        title_bar_bg_id,
        title,
        title_font_size,
        ..
    } = obj.kind
    {
        let t = theme.as_ref().unwrap_or(default_theme);
        let term = crate::anim_object::terminal::terminal(
            obj.transform.position,
            shell,
            prompt,
            font_family,
            font_size,
            width,
            height,
            background_color,
            text_color,
            term_id,
            text_buffer_id,
            bg_id,
            container_id,
            close_btn_id,
            minimize_btn_id,
            maximize_btn_id,
            title_id,
            title_bar_bg_id,
            title,
            title_font_size,
            t,
        );
        Ok(term.instantiate())
    } else {
        bail!("build_terminal_op called on non-Terminal kind")
    }
}

fn convert_core_anim_obj(
    obj: scal_core::AnimObj,
    default_theme: &scal_core::Theme,
) -> Result<crate::anim_object::object_trait::DynAnimObj> {
    use crate::anim_object::object_trait::DynAnimObj as RenderObj;
    let transform = make_transform(&obj);
    match obj.kind {
        scal_core::anim_obj::AnimObjKind::Rectangle {
            size,
            corner_radius,
            color,
        } => Ok(RenderObj(Box::new(
            crate::anim_object::primitive_shapes::Rectangle {
                size,
                corner_radius,
                color,
                transform,
            },
        ))),
        scal_core::anim_obj::AnimObjKind::Circle { radius, color } => Ok(RenderObj(Box::new(
            crate::anim_object::primitive_shapes::Circle {
                radius,
                color,
                transform,
            },
        ))),
        scal_core::anim_obj::AnimObjKind::Polygon {
            radius,
            sides,
            color,
        } => Ok(RenderObj(Box::new(
            crate::anim_object::primitive_shapes::Polygon {
                radius,
                sides,
                color,
                transform,
            },
        ))),
        scal_core::anim_obj::AnimObjKind::Text {
            value,
            font_family,
            align,
            color,
            font_size,
            modifications,
            spans,
        } => {
            let align_val = match align {
                scal_core::anim_obj::Alignment::Center => RuntimeAlignment::Center,
                scal_core::anim_obj::Alignment::Start => RuntimeAlignment::Start,
                scal_core::anim_obj::Alignment::End => RuntimeAlignment::End,
            };
            Ok(RenderObj(Box::new(crate::anim_object::text::Text {
                id: obj.id,
                value,
                font_family,
                align: align_val,
                color,
                font_size,
                transform,
                cached_size: None,
                modifications,
                spans,
            })))
        }
        scal_core::anim_obj::AnimObjKind::Math {
            latex,
            color,
            font_size,
            display,
            palette,
        } => Ok(RenderObj(Box::new(crate::anim_object::math::Math::new(
            latex, color, font_size, display, &palette, transform,
        )?))),
        scal_core::anim_obj::AnimObjKind::Svg {
            path,
            size,
            tint,
            fill,
            stroke,
            stroke_width,
            stretch,
        } => Ok(RenderObj(Box::new(crate::anim_object::svg::Svg {
            path,
            size,
            tint,
            fill,
            stroke,
            stroke_width,
            stretch,
            transform,
        }))),
        scal_core::anim_obj::AnimObjKind::Image {
            path,
            size,
            color,
            stretch,
        } => Ok(RenderObj(Box::new(crate::anim_object::image::Image {
            path,
            size,
            color,
            stretch,
            transform,
        }))),
        scal_core::anim_obj::AnimObjKind::Code {
            source_code,
            font_family,
            font_size,
            syntax,
            theme,
            padding,
            show_line_numbers,
            line_number_color,
        } => Ok(RenderObj(Box::new(crate::anim_object::text::code::Code {
            id: obj.id,
            source_code,
            theme: theme.unwrap_or(default_theme.to_owned()),
            font_family,
            font_size,
            syntax,
            padding,
            show_line_numbers,
            line_number_color,
            transform,
            align: RuntimeAlignment::Start,
            lines: vec![],
            dirty: true,
            anim_reveal: 1.0,
            anim_spacing: 0.0,
            anim_line_start: 0,
            anim_line_end: 0,
            anim_style: CodeAnimationStyle::TypeWriter,
            anim_spacing_accum: 0.0,
            cached_size: None,
            highlights: vec![],
        }))),
        scal_core::anim_obj::AnimObjKind::CodeWindow { .. } => {
            bail!("CodeWindow should be handled by build_code_window_op")
        }
        scal_core::anim_obj::AnimObjKind::Terminal { .. } => {
            bail!("Terminal should be handled by build_terminal_op")
        }
        scal_core::anim_obj::AnimObjKind::Padding { size } => {
            Ok(RenderObj(Box::new(crate::anim_object::padding::Padding {
                size,
                transform,
            })))
        }
        scal_core::anim_obj::AnimObjKind::Group { .. } => {
            bail!("Group object conversion not yet implemented")
        }
        scal_core::anim_obj::AnimObjKind::Layout { .. } => {
            bail!("Layout should be handled by build_layout_op")
        }
        scal_core::anim_obj::AnimObjKind::ScrollLayout { .. } => {
            bail!("ScrollLayout should be handled by build_scroll_layout_op")
        }
    }
}

fn build_scroll_layout_op(
    obj: scal_core::AnimObj,
    default_theme: &scal_core::Theme,
) -> Result<AnimOperation> {
    use crate::anim_object::object_trait::AnimObjectTrait;
    use scal_core::anim_obj::{
        Alignment as CoreAlignment, AnimObjKind, LayoutDir as CoreLayoutDir,
    };
    use uuid::Uuid;
    let id = obj.id;
    let transform = make_transform(&obj);
    if let AnimObjKind::ScrollLayout {
        children,
        direction,
        align,
        justify,
        gap,
        padding_top,
        padding_bottom,
        padding_left,
        padding_right,
        viewport_width,
        viewport_height,
        show_scrollbar,
        scroll_offset,
        background_color,
        corner_radius,
        mask_children,
    } = obj.kind
    {
        let layout_dir = match direction {
            CoreLayoutDir::Column => RuntimeLayoutDir::Column,
            CoreLayoutDir::Row => RuntimeLayoutDir::Row,
        };
        let align_val = match align {
            CoreAlignment::Start => RuntimeAlignment::Start,
            CoreAlignment::Center => RuntimeAlignment::Center,
            CoreAlignment::End => RuntimeAlignment::End,
        };
        let justify_val = match justify {
            CoreAlignment::Start => RuntimeAlignment::Start,
            CoreAlignment::Center => RuntimeAlignment::Center,
            CoreAlignment::End => RuntimeAlignment::End,
        };

        struct ScrollChild {
            obj: crate::anim_object::object_trait::DynAnimObj,
            size: glam::Vec2,
        }

        // World-space viewport bounds used for masking. The background is
        // positioned at the scroll layout's world position with no parent.
        let world_clip_rect = if mask_children {
            Some([
                transform.position.x - viewport_width / 2.0,
                transform.position.y - viewport_height / 2.0,
                transform.position.x + viewport_width / 2.0,
                transform.position.y + viewport_height / 2.0,
            ])
        } else {
            None
        };

        // Convert children first so we can measure their actual runtime sizes.
        // Nested Layouts are flattened: their background becomes a scroll child and
        // their container/items/nested layouts are instantiated separately.
        let mut scroll_children: Vec<ScrollChild> = Vec::with_capacity(children.len());
        let mut nested_ops: Vec<AnimOperation> = Vec::new();
        for child in children.into_iter() {
            match convert_to_layout_item(child, default_theme)? {
                LayoutItem::Object(obj) => {
                    scroll_children.push(ScrollChild {
                        size: obj.size(),
                        obj,
                    });
                }
                LayoutItem::Layout(mut lr) => {
                    if let Some(clip) = world_clip_rect {
                        lr.apply_clip_rect(clip);
                    }
                    let size = lr.background.size();
                    nested_ops.push(lr.instantiate_children());
                    scroll_children.push(ScrollChild {
                        size,
                        obj: lr.background,
                    });
                }
            }
        }

        let sizes: Vec<glam::Vec2> = scroll_children.iter().map(|c| c.size).collect();
        let gaps = gap * (sizes.len() as f32 - 1.0).max(0.0);
        let sb_thickness = if show_scrollbar { 8.0 } else { 0.0 };

        let content_w = match layout_dir {
            RuntimeLayoutDir::Column => {
                let max_w = sizes.iter().map(|s| s.x).fold(0.0f32, f32::max);
                max_w + padding_left + padding_right + sb_thickness
            }
            RuntimeLayoutDir::Row => {
                sizes.iter().map(|s| s.x).sum::<f32>() + gaps + padding_left + padding_right
            }
        };
        let content_h = match layout_dir {
            RuntimeLayoutDir::Column => {
                sizes.iter().map(|s| s.y).sum::<f32>() + gaps + padding_top + padding_bottom
            }
            RuntimeLayoutDir::Row => {
                let max_h = sizes.iter().map(|s| s.y).fold(0.0f32, f32::max);
                max_h + padding_top + padding_bottom
            }
        };
        let content_total = match layout_dir {
            RuntimeLayoutDir::Column => content_h,
            RuntimeLayoutDir::Row => content_w,
        };
        let viewport_size = match layout_dir {
            RuntimeLayoutDir::Column => viewport_height,
            RuntimeLayoutDir::Row => viewport_width,
        };
        let clamped_offset = scroll_offset.clamp(0.0, (content_total - viewport_size).max(0.0));

        let total_content_w: f32 = sizes.iter().map(|s| s.x).sum::<f32>() + gaps;
        let total_content_h: f32 = sizes.iter().map(|s| s.y).sum::<f32>() + gaps;

        // Position children relative to the viewport edges so that the justified
        // starting edge sits at the viewport edge when the scroll offset is zero.
        let content_left = -viewport_width / 2.0 + padding_left;
        let content_right = viewport_width / 2.0 - padding_right - sb_thickness;
        let content_bottom = viewport_height / 2.0 - padding_bottom;
        let content_top = -viewport_height / 2.0 + padding_top;
        let content_area_w = content_right - content_left;
        let content_area_h = content_bottom - content_top;

        // Create background rectangle
        let bg = crate::anim_object::primitive_shapes::Rectangle {
            size: glam::vec2(viewport_width, viewport_height),
            corner_radius,
            color: background_color,
            transform: crate::anim_object::Transform {
                uuid: Uuid::new_v4(),
                parent: None,
                position: transform.position,
                rotation: 0.0,
                scale: glam::Vec2::ONE,
                layout_container: None,
                world_uniform: None,
                clip_rect: None,
            },
        };
        let bg_uuid = bg.transform.uuid;

        // Position children inside the scroll viewport and build instantiate ops
        let mut child_uuids = Vec::with_capacity(scroll_children.len());
        let mut child_base_positions: Vec<glam::Vec2> = Vec::with_capacity(scroll_children.len());
        let mut ops = Vec::with_capacity(scroll_children.len() + nested_ops.len() + 2);
        ops.extend(nested_ops);

        let mut main_pos = match (layout_dir, justify_val) {
            (RuntimeLayoutDir::Column, RuntimeAlignment::Start) => content_top,
            (RuntimeLayoutDir::Column, RuntimeAlignment::Center) => {
                content_top + (content_area_h - total_content_h) / 2.0
            }
            (RuntimeLayoutDir::Column, RuntimeAlignment::End) => content_bottom - total_content_h,
            (RuntimeLayoutDir::Row, RuntimeAlignment::Start) => content_left,
            (RuntimeLayoutDir::Row, RuntimeAlignment::Center) => {
                content_left + (content_area_w - total_content_w) / 2.0
            }
            (RuntimeLayoutDir::Row, RuntimeAlignment::End) => content_right - total_content_w,
        };

        for ScrollChild {
            obj: mut child_obj,
            size: s,
        } in scroll_children.into_iter()
        {
            let (base_cx, base_cy) = match layout_dir {
                RuntimeLayoutDir::Column => {
                    let cx_val = match align_val {
                        RuntimeAlignment::Start => content_left + s.x / 2.0,
                        RuntimeAlignment::Center => 0.0,
                        RuntimeAlignment::End => content_right - s.x / 2.0,
                    };
                    let cy_val = main_pos + s.y / 2.0;
                    main_pos += s.y + gap;
                    (cx_val, cy_val)
                }
                RuntimeLayoutDir::Row => {
                    let cx_val = main_pos + s.x / 2.0;
                    let cy_val = match align_val {
                        RuntimeAlignment::Start => content_bottom - s.y / 2.0,
                        RuntimeAlignment::Center => 0.0,
                        RuntimeAlignment::End => content_top + s.y / 2.0,
                    };
                    main_pos += s.x + gap;
                    (cx_val, cy_val)
                }
            };

            child_base_positions.push(glam::vec2(base_cx, base_cy));

            // Apply the initial scroll offset to the child position.
            let (cx, cy) = match layout_dir {
                RuntimeLayoutDir::Column => (base_cx, base_cy - clamped_offset),
                RuntimeLayoutDir::Row => (base_cx - clamped_offset, base_cy),
            };

            child_obj.transform_mut().parent = Some(bg_uuid);
            child_obj.transform_mut().position = glam::vec3(cx, cy, 0.0);
            if let Some(clip) = world_clip_rect {
                child_obj.transform_mut().clip_rect = Some(clip);
            }
            child_uuids.push(child_obj.uuid());
            ops.push(child_obj.instantiate());
        }

        // Create the scroll layout container. The scrollbar is rendered by this
        // object, so bump its z slightly above the scroll children so the scrollbar
        // sits on top.
        let sl = ScrollLayout {
            id,
            transform: crate::anim_object::Transform {
                uuid: id,
                parent: None,
                position: glam::vec3(
                    transform.position.x,
                    transform.position.y,
                    transform.position.z + 0.3,
                ),
                rotation: 0.0,
                scale: glam::Vec2::ONE,
                layout_container: None,
                world_uniform: None,
                clip_rect: None,
            },
            child_uuids,
            child_base_positions,
            viewport_width,
            viewport_height,
            show_scrollbar,
            scroll_offset,
            direction: layout_dir,
            gap,
            padding_top,
            padding_bottom,
            padding_left,
            padding_right,
            content_total,
            mask_children,
        };
        ops.push(crate::anim_object::object_trait::DynAnimObj(Box::new(bg)).instantiate());
        ops.push(crate::anim_object::object_trait::DynAnimObj(Box::new(sl)).instantiate());

        Ok(crate::anim_op::AnimOperation::All(ops, None))
    } else {
        bail!("build_scroll_layout_op called on non-ScrollLayout kind")
    }
}

fn build_layout_op(
    obj: scal_core::AnimObj,
    default_theme: &scal_core::Theme,
) -> Result<AnimOperation> {
    let layout_result = build_layout_result_from_core(obj, default_theme)?;
    Ok(layout_result.instantiate())
}

fn build_layout_result_from_core(
    obj: scal_core::AnimObj,
    default_theme: &scal_core::Theme,
) -> Result<crate::anim_object::compose::LayoutResult> {
    use scal_core::anim_obj::{
        Alignment as CoreAlignment, AnimObjKind, LayoutDir as CoreLayoutDir,
    };
    if let AnimObjKind::Layout {
        children,
        direction,
        align,
        justify,
        gap,
        padding_top,
        padding_bottom,
        padding_left,
        padding_right,
        min_width,
        min_height,
        background_color,
        corner_radius,
    } = obj.kind
    {
        let mut items = Vec::with_capacity(children.len());
        for child in children {
            items.push(convert_to_layout_item(child, default_theme)?);
        }

        let layout_dir = match direction {
            CoreLayoutDir::Column => RuntimeLayoutDir::Column,
            CoreLayoutDir::Row => RuntimeLayoutDir::Row,
        };
        let align_val = match align {
            CoreAlignment::Start => RuntimeAlignment::Start,
            CoreAlignment::Center => RuntimeAlignment::Center,
            CoreAlignment::End => RuntimeAlignment::End,
        };
        let justify_val = match justify {
            CoreAlignment::Start => RuntimeAlignment::Start,
            CoreAlignment::Center => RuntimeAlignment::Center,
            CoreAlignment::End => RuntimeAlignment::End,
        };

        Ok(crate::anim_object::compose::layout_with_ids(
            obj.transform.position,
            PinPoint::C,
            items,
            LayoutBackground {
                color: background_color,
                corner_radius,
            },
            layout_dir,
            align_val,
            justify_val,
            gap,
            padding_top,
            padding_bottom,
            padding_left,
            padding_right,
            min_width,
            min_height,
            Some(obj.id),
            None,
        ))
    } else {
        bail!("build_layout_result_from_core called on non-Layout kind")
    }
}

fn convert_to_layout_item(
    child: scal_core::AnimObj,
    default_theme: &scal_core::Theme,
) -> Result<LayoutItem> {
    let is_layout = matches!(child.kind, scal_core::anim_obj::AnimObjKind::Layout { .. });
    let is_scroll = matches!(
        child.kind,
        scal_core::anim_obj::AnimObjKind::ScrollLayout { .. }
    );
    if is_layout {
        let layout_result = build_layout_result_from_core(child, default_theme)?;
        Ok(LayoutItem::Layout(layout_result))
    } else if is_scroll {
        let op = build_scroll_layout_op(child, default_theme)?;
        let scroll_obj = match op {
            crate::anim_op::AnimOperation::Instantiate(obj, _) => obj,
            _ => bail!("build_scroll_layout_op did not return Instantiate"),
        };
        Ok(LayoutItem::Object(scroll_obj))
    } else {
        let dyn_obj = convert_core_anim_obj(child, default_theme)?;
        Ok(LayoutItem::Object(dyn_obj))
    }
}

const fn make_transform(obj: &scal_core::AnimObj) -> crate::anim_object::Transform {
    crate::anim_object::Transform {
        scale: obj.transform.scale,
        uuid: obj.id,
        parent: obj.transform.parent,
        position: obj.transform.position,
        rotation: obj.transform.rotation,
        layout_container: None,
        world_uniform: None,
        clip_rect: None,
    }
}