oxframe-2d 0.11.0

2D scene graph and nodes for oxframe
Documentation
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
use kurbo::{BezPath, Vec2};
use oxframe_core::{BlendMode, Color, Gradient, Signal};
use oxframe_text::TextStyle;

use crate::draw::{LineCap, LineJoin};
use crate::graph::{Direction, Justify, LayoutAlign, NodeId, NodeKind, SceneGraph, with_graph};
use crate::handles::{
    Camera, Circle, Code, Custom, Grid, Group, Img, Layout, Line, Particles, Path, Polygon, Rect,
    Shader, Star, Txt,
};
use crate::image::ImageSource;
use crate::place::{self, Axis, Edge, Place, Side, Span};
use crate::text::{CodeTheme, RichSpan};
use crate::theme::{self, Corner, IntoPaint, Len, Step, TypeRole};

/// Initial values shared by every node; set through [`NodeBuilder`] methods.
#[doc(hidden)]
#[derive(Debug)]
pub struct BaseProps {
    pub name: Option<String>,
    pub x: f64,
    pub y: f64,
    pub rotation: f64,
    pub scale: Vec2,
    pub opacity: f64,
    pub fill: Color,
    /// Set when `fill` was chosen explicitly. Text leaves this false and
    /// takes the theme's text color at build time.
    pub fill_explicit: bool,
    pub stroke: Color,
    pub line_width: f64,
    pub start: f64,
    pub end: f64,
    pub fill_gradient: Option<Gradient>,
    pub stroke_gradient: Option<Gradient>,
    pub dash: Vec<f64>,
    pub dash_offset: f64,
    pub line_cap: LineCap,
    pub line_join: LineJoin,
    pub blur: f64,
    pub shadow_color: Color,
    pub shadow_blur: f64,
    pub shadow_offset: Vec2,
    pub clip: bool,
    pub blend: BlendMode,
    pub z_index: f64,
    pub anchor: Vec2,
    pub grow: f64,
    pub shrink: f64,
    pub basis: Option<f64>,
    pub margin: f64,
    pub align_self: Option<LayoutAlign>,
    pub children: Vec<ChildBuilder>,
    /// Resolved every frame. Set by `next_to`, `align_to`, `to_edge`, `center_in`.
    pub place: Option<Place>,
}

impl Default for BaseProps {
    fn default() -> Self {
        Self {
            name: None,
            x: 0.0,
            y: 0.0,
            rotation: 0.0,
            scale: Vec2::new(1.0, 1.0),
            opacity: 1.0,
            fill: Color::TRANSPARENT,
            fill_explicit: false,
            stroke: Color::TRANSPARENT,
            line_width: 0.0,
            start: 0.0,
            end: 1.0,
            fill_gradient: None,
            stroke_gradient: None,
            dash: Vec::new(),
            dash_offset: 0.0,
            line_cap: LineCap::Round,
            line_join: LineJoin::Round,
            blur: 0.0,
            shadow_color: Color::TRANSPARENT,
            shadow_blur: 0.0,
            shadow_offset: Vec2::ZERO,
            clip: false,
            blend: BlendMode::Normal,
            z_index: 0.0,
            anchor: Vec2::ZERO,
            grow: 0.0,
            shrink: 0.0,
            basis: None,
            margin: 0.0,
            align_self: None,
            children: Vec::new(),
            place: None,
        }
    }
}

/// A type-erased child added with [`NodeBuilder::child`].
type BuildFn = Box<dyn FnOnce(&SceneGraph, NodeId)>;

/// A child declared with [`NodeBuilder::child`], built together with its parent.
pub struct ChildBuilder(BuildFn);

impl std::fmt::Debug for ChildBuilder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("ChildBuilder")
    }
}

/// Describes a node before it is added to the scene.
pub trait NodeBuilder: Sized + 'static {
    /// Handle returned by [`Node::add`](crate::Node::add).
    type Handle;

    #[doc(hidden)]
    fn base(&mut self) -> &mut BaseProps;

    #[doc(hidden)]
    fn build(self, graph: &SceneGraph, parent: NodeId) -> Self::Handle;

    /// Name shown in the editor and in `oxframe check` / `oxframe inspect`.
    /// Without it, the node is labeled by the `add` call site (`src/main.rs:42`).
    fn name(mut self, v: impl Into<String>) -> Self {
        self.base().name = Some(v.into());
        self
    }
    /// Horizontal position; 0 is the parent's center, positive is right.
    fn x(mut self, v: f64) -> Self {
        self.base().x = v;
        self
    }
    /// Vertical position; 0 is the parent's center, positive is down.
    fn y(mut self, v: f64) -> Self {
        self.base().y = v;
        self
    }
    /// Sets `x` and `y` together, e.g. `.position((100.0, -50.0))`.
    fn position(mut self, v: impl Into<Vec2>) -> Self {
        let v = v.into();
        let b = self.base();
        (b.x, b.y) = (v.x, v.y);
        self
    }
    /// Degrees, clockwise.
    fn rotation(mut self, v: f64) -> Self {
        self.base().rotation = v;
        self
    }
    /// Uniform scale; 1 is the natural size.
    fn scale(mut self, v: f64) -> Self {
        self.base().scale = Vec2::new(v, v);
        self
    }
    /// Separate horizontal and vertical scale.
    fn scale_xy(mut self, v: impl Into<Vec2>) -> Self {
        self.base().scale = v.into();
        self
    }
    /// Opacity of the node and its children, 0 (invisible) to 1.
    fn opacity(mut self, v: f64) -> Self {
        self.base().opacity = v;
        self
    }
    /// Fill color: a [`Color`](oxframe_core::Color) or a theme role such as `Accent`.
    /// Default: transparent, except text, which uses the theme's text color.
    fn fill(mut self, v: impl IntoPaint) -> Self {
        let b = self.base();
        b.fill = v.into_paint();
        b.fill_explicit = true;
        self
    }
    /// Outline color; also set `line_width`. A theme role works the same as a color.
    /// Default: transparent.
    fn stroke(mut self, v: impl IntoPaint) -> Self {
        self.base().stroke = v.into_paint();
        self
    }
    /// Outline width in pixels. Default: 0 (no outline).
    fn line_width(mut self, v: f64) -> Self {
        self.base().line_width = v;
        self
    }
    /// Start of the drawn part of the outline, fraction of its length.
    fn start(mut self, v: f64) -> Self {
        self.base().start = v;
        self
    }
    /// End of the drawn part of the outline; animate 0 → 1 to draw it on.
    fn end(mut self, v: f64) -> Self {
        self.base().end = v;
        self
    }
    /// Gradient fill (local coordinates, origin at the node center);
    /// overrides `fill`.
    fn fill_gradient(mut self, g: Gradient) -> Self {
        self.base().fill_gradient = Some(g);
        self
    }
    /// Gradient outline; overrides `stroke`.
    fn stroke_gradient(mut self, g: Gradient) -> Self {
        self.base().stroke_gradient = Some(g);
        self
    }
    /// Dash pattern: on, off, on, … lengths. Animate `dash_offset` for
    /// marching ants.
    fn dash(mut self, pattern: impl Into<Vec<f64>>) -> Self {
        self.base().dash = pattern.into();
        self
    }
    /// Shifts the dash pattern along the outline.
    fn dash_offset(mut self, v: f64) -> Self {
        self.base().dash_offset = v;
        self
    }
    /// Shape of outline ends. Default: round.
    fn line_cap(mut self, v: LineCap) -> Self {
        self.base().line_cap = v;
        self
    }
    /// Shape of outline corners. Default: round.
    fn line_join(mut self, v: LineJoin) -> Self {
        self.base().line_join = v;
        self
    }
    /// Gaussian blur of the node and its children (standard deviation, in
    /// the node's units).
    fn blur(mut self, v: f64) -> Self {
        self.base().blur = v;
        self
    }
    /// Drop shadow under the node and its children.
    fn shadow(mut self, color: impl IntoPaint, blur: f64, offset: impl Into<Vec2>) -> Self {
        let b = self.base();
        (b.shadow_color, b.shadow_blur, b.shadow_offset) =
            (color.into_paint(), blur, offset.into());
        self
    }
    /// The theme's shadow for this step (`Xs` and `Sm` are the small one, `Xl` the large).
    fn elevation(self, step: Step) -> Self {
        let s = theme::current().shadow(step);
        self.shadow(s.color, s.blur, s.offset)
    }
    /// A shadow without offset.
    fn glow(self, color: impl IntoPaint, blur: f64) -> Self {
        self.shadow(color, blur, Vec2::ZERO)
    }
    /// Clip children to this node's outline (or box).
    fn clip(mut self, v: bool) -> Self {
        self.base().clip = v;
        self
    }
    /// How the node is composited onto what is below it.
    fn blend(mut self, v: BlendMode) -> Self {
        self.base().blend = v;
        self
    }
    /// Paint order among siblings; higher is on top.
    fn z_index(mut self, v: f64) -> Self {
        self.base().z_index = v;
        self
    }
    /// Pivot and placement point: (-1, -1) top-left … (1, 1) bottom-right.
    fn anchor(mut self, v: impl Into<Vec2>) -> Self {
        self.base().anchor = v.into();
        self
    }
    /// Inside a `Layout`: share of the free space this node takes.
    fn grow(mut self, v: f64) -> Self {
        self.base().grow = v;
        self
    }
    /// Inside a `Layout`: how much this node gives up when space is short.
    fn shrink(mut self, v: f64) -> Self {
        self.base().shrink = v;
        self
    }
    /// Inside a `Layout`: main-axis size before growing/shrinking.
    fn basis(mut self, v: f64) -> Self {
        self.base().basis = Some(v);
        self
    }
    /// Inside a `Layout`: space around this node, in pixels or a theme step (`.margin(Md)`).
    fn margin(mut self, v: impl Len) -> Self {
        self.base().margin = v.px();
        self
    }
    /// Inside a `Layout`: overrides the layout's cross-axis alignment.
    fn align_self(mut self, v: LayoutAlign) -> Self {
        self.base().align_self = Some(v);
        self
    }
    /// Sits just outside `other`, on `side`. The gap is the theme's `Md`
    /// step. Recomputed every frame, so this node follows `other`.
    fn next_to(mut self, other: impl Into<crate::Node>, side: Side) -> Self {
        self.base().place = Some(Place::next_to(other.into().id(), side));
        self
    }
    /// Lines this node's `edge` up with the same edge of `other`.
    fn align_to(mut self, other: impl Into<crate::Node>, edge: Edge) -> Self {
        self.base().place = Some(Place::align_to(other.into().id(), edge));
        self
    }
    /// Pins one edge to the safe area (not the raw frame edge). The other
    /// axis stays at `x` or `y`.
    fn to_edge(mut self, edge: Edge) -> Self {
        self.base().place = Some(Place::to_edge(edge));
        self
    }
    /// Same center as `other`. Pass the view to center in the frame.
    fn center_in(mut self, other: impl Into<crate::Node>) -> Self {
        self.base().place = Some(Place::center_in(other.into().id()));
        self
    }
    /// Adds a child created together with this node. Use `Node::add` when
    /// you need the child's handle.
    fn child<C: NodeBuilder>(mut self, child: C) -> Self {
        self.base()
            .children
            .push(ChildBuilder(Box::new(move |g, parent| {
                child.build(g, parent);
            })));
        self
    }
}

pub(crate) fn add<B: NodeBuilder>(parent: NodeId, builder: B) -> B::Handle {
    with_graph(|g| builder.build(g, parent))
}

/// Inserts the node and then its declared children.
fn insert(graph: &SceneGraph, parent: NodeId, kind: NodeKind, mut base: BaseProps) -> NodeId {
    let children = std::mem::take(&mut base.children);
    let id = graph.insert(Some(parent), kind, base);
    for c in children {
        (c.0)(graph, id);
    }
    id
}

fn sig<T: Clone + 'static>(graph: &SceneGraph, kind: &str, field: &str, v: T) -> Signal<T> {
    Signal::new(v).with_name(format!("{kind}#{}.{field}", graph.next_id()))
}

macro_rules! builder {
    (
        $(#[$doc:meta])*
        $builder:ident => $handle:ident, kind $kind:literal, defaults { $($bf:ident: $bv:expr),* $(,)? },
        fields { $($field:ident: $ty:ty = $default:expr),* $(,)? }
    ) => {
        $(#[$doc])*
        #[doc = ""]
        #[doc = concat!("Builder for a [`", stringify!($handle), "`] node: set the initial values, then")]
        #[doc = "pass it to `add`."]
        #[derive(Debug)]
        #[must_use = "a builder does nothing until it is passed to `add`"]
        pub struct $builder {
            base: BaseProps,
            $(pub(crate) $field: $ty,)*
        }

        impl Default for $builder {
            fn default() -> Self {
                Self {
                    base: BaseProps { $($bf: $bv,)* ..Default::default() },
                    $($field: $default,)*
                }
            }
        }

        impl $builder {
            #[allow(dead_code)]
            const KIND: &'static str = $kind;
        }
    };
}

macro_rules! setters {
    ($builder:ident { $($(#[$doc:meta])* $field:ident: $ty:ty),* $(,)? }) => {
        impl $builder {
            $(
                $(#[$doc])*
                #[doc = ""]
                #[doc = concat!("Sets the initial `", stringify!($field), "`.")]
                pub fn $field(mut self, v: impl Into<$ty>) -> Self {
                    self.$field = v.into();
                    self
                }
            )*
        }
    };
}

macro_rules! base_accessor {
    () => {
        fn base(&mut self) -> &mut BaseProps {
            &mut self.base
        }
    };
}

// ------------------------------------------------------------------ Group

builder!(GroupBuilder => Group, kind "Group", defaults {}, fields {});

impl NodeBuilder for GroupBuilder {
    type Handle = Group;
    base_accessor!();
    fn build(self, graph: &SceneGraph, parent: NodeId) -> Group {
        Group::from_id(insert(graph, parent, NodeKind::Group, self.base))
    }
}

// ----------------------------------------------------------------- Camera

builder!(
    /// A camera: everything added to it is seen through it, so animating its
    /// `x`/`y` (the point it looks at), `zoom` and `rotation` pans, zooms
    /// and turns the view. Nodes added to the view itself stay on screen
    /// (titles, captions, a HUD).
    CameraBuilder => Camera, kind "Camera", defaults {}, fields { zoom: f64 = 1.0 });
setters!(CameraBuilder {
    /// 1 shows the world at its size, 2 twice as big.
    zoom: f64
});

impl NodeBuilder for CameraBuilder {
    type Handle = Camera;
    base_accessor!();
    fn build(self, g: &SceneGraph, parent: NodeId) -> Camera {
        let kind = NodeKind::Camera {
            zoom: sig(g, Self::KIND, "zoom", self.zoom),
        };
        Camera::from_id(insert(g, parent, kind, self.base))
    }
}

// ------------------------------------------------------------------- Rect

builder!(RectBuilder => Rect, kind "Rect", defaults { fill: Color::WHITE, fill_explicit: true },
    fields { width: Span = Span::Px(100.0), height: Span = Span::Px(100.0), radius: f64 = 0.0 });

impl RectBuilder {
    /// Width in pixels, or [`pct`](crate::pct)`(80)` for a fraction of the frame.
    pub fn width(mut self, v: impl Into<Span>) -> Self {
        self.width = v.into();
        self
    }
    /// Height in pixels, or a percentage of the frame.
    pub fn height(mut self, v: impl Into<Span>) -> Self {
        self.height = v.into();
        self
    }
    /// Corner radius, in pixels or a theme step (`.radius(Md)`).
    /// Clamped to half of the shorter side when drawn.
    pub fn radius(mut self, v: impl Corner) -> Self {
        self.radius = v.radius();
        self
    }
    /// Sets width and height to the same pixel size.
    pub fn size(mut self, v: f64) -> Self {
        (self.width, self.height) = (Span::Px(v), Span::Px(v));
        self
    }
}

impl NodeBuilder for RectBuilder {
    type Handle = Rect;
    base_accessor!();
    fn build(self, g: &SceneGraph, parent: NodeId) -> Rect {
        let k = Self::KIND;
        let n = g.next_id();
        let kind = NodeKind::Rect {
            width: place::span_signal(g, parent, Axis::X, self.width, format!("{k}#{n}.width")),
            height: place::span_signal(g, parent, Axis::Y, self.height, format!("{k}#{n}.height")),
            radius: sig(g, k, "radius", self.radius),
        };
        Rect::from_id(insert(g, parent, kind, self.base))
    }
}

// ----------------------------------------------------------------- Circle

builder!(CircleBuilder => Circle, kind "Circle", defaults { fill: Color::WHITE, fill_explicit: true },
    fields { size: f64 = 100.0, start_angle: f64 = 0.0, end_angle: f64 = 360.0, pie: bool = false });
setters!(CircleBuilder {
    /// Diameter.
    size: f64,
    /// Degrees clockwise from 12 o'clock.
    start_angle: f64,
    end_angle: f64,
    /// Close the arc through the center (pie slice).
    pie: bool
});

impl NodeBuilder for CircleBuilder {
    type Handle = Circle;
    base_accessor!();
    fn build(self, g: &SceneGraph, parent: NodeId) -> Circle {
        let k = Self::KIND;
        let kind = NodeKind::Circle {
            size: sig(g, k, "size", self.size),
            start_angle: sig(g, k, "start_angle", self.start_angle),
            end_angle: sig(g, k, "end_angle", self.end_angle),
            pie: self.pie,
        };
        Circle::from_id(insert(g, parent, kind, self.base))
    }
}

// ---------------------------------------------------------------- Polygon

builder!(PolygonBuilder => Polygon, kind "Polygon", defaults { fill: Color::WHITE, fill_explicit: true },
    fields { sides: f64 = 6.0, size: f64 = 100.0 });
setters!(PolygonBuilder {
    /// Number of sides (rounded; animatable as a number).
    sides: f64,
    /// Diameter of the circumscribed circle.
    size: f64
});

impl NodeBuilder for PolygonBuilder {
    type Handle = Polygon;
    base_accessor!();
    fn build(self, g: &SceneGraph, parent: NodeId) -> Polygon {
        let k = Self::KIND;
        let kind = NodeKind::Polygon {
            sides: sig(g, k, "sides", self.sides),
            size: sig(g, k, "size", self.size),
        };
        Polygon::from_id(insert(g, parent, kind, self.base))
    }
}

// ------------------------------------------------------------------- Star

builder!(StarBuilder => Star, kind "Star", defaults { fill: Color::WHITE, fill_explicit: true },
    fields { points: f64 = 5.0, size: f64 = 100.0, inner: f64 = 0.45 });
setters!(StarBuilder {
    points: f64,
    /// Outer diameter.
    size: f64,
    /// Inner radius as a fraction of the outer one.
    inner: f64
});

impl NodeBuilder for StarBuilder {
    type Handle = Star;
    base_accessor!();
    fn build(self, g: &SceneGraph, parent: NodeId) -> Star {
        let k = Self::KIND;
        let kind = NodeKind::Star {
            points: sig(g, k, "points", self.points),
            size: sig(g, k, "size", self.size),
            inner: sig(g, k, "inner", self.inner),
        };
        Star::from_id(insert(g, parent, kind, self.base))
    }
}

// ------------------------------------------------------------------- Path

builder!(PathBuilder => Path, kind "Path", defaults { fill: Color::WHITE, fill_explicit: true },
    fields { data: BezPath = BezPath::new() });
setters!(PathBuilder { data: BezPath });

impl PathBuilder {
    /// Path from SVG path syntax (`"M0 0 L10 0 Z"`).
    ///
    /// # Panics
    ///
    /// If `d` is not valid SVG path data.
    pub fn svg(mut self, d: &str) -> Self {
        self.data = BezPath::from_svg(d).unwrap_or_else(|e| panic!("invalid SVG path {d:?}: {e}"));
        self
    }
}

impl NodeBuilder for PathBuilder {
    type Handle = Path;
    base_accessor!();
    fn build(self, g: &SceneGraph, parent: NodeId) -> Path {
        let kind = NodeKind::Path {
            data: sig(g, Self::KIND, "data", self.data),
        };
        Path::from_id(insert(g, parent, kind, self.base))
    }
}

// ------------------------------------------------------------------- Line

builder!(LineBuilder => Line, kind "Line",
    defaults { stroke: Color::WHITE, line_width: 4.0 },
    fields { points: Vec<Vec2> = Vec::new(), closed: bool = false, smooth: bool = false,
             start_arrow: bool = false, end_arrow: bool = false, arrow_size: f64 = 24.0 });
setters!(LineBuilder {
    closed: bool,
    /// Smooth curve through the points instead of straight segments.
    smooth: bool,
    start_arrow: bool,
    end_arrow: bool,
    arrow_size: f64
});

impl LineBuilder {
    /// Points the line passes through, in local coordinates.
    pub fn points<P: Into<Vec2>>(mut self, pts: impl IntoIterator<Item = P>) -> Self {
        self.points = pts.into_iter().map(Into::into).collect();
        self
    }
}

impl NodeBuilder for LineBuilder {
    type Handle = Line;
    base_accessor!();
    fn build(self, g: &SceneGraph, parent: NodeId) -> Line {
        let kind = NodeKind::Line {
            points: sig(g, Self::KIND, "points", self.points),
            closed: self.closed,
            smooth: self.smooth,
            start_arrow: self.start_arrow,
            end_arrow: self.end_arrow,
            arrow_size: sig(g, Self::KIND, "arrow_size", self.arrow_size),
        };
        Line::from_id(insert(g, parent, kind, self.base))
    }
}

// -------------------------------------------------------------------- Txt

builder!(TxtBuilder => Txt, kind "Txt", defaults { fill: Color::WHITE },
    fields { text: String = String::new(), spans: Vec<RichSpan> = Vec::new(),
             style: TextStyle = TextStyle::default() });

impl TxtBuilder {
    /// The text to show.
    pub fn text(mut self, v: impl Into<String>) -> Self {
        self.text = v.into();
        self
    }
    /// Styled runs instead of plain text: `[span("Hello "), span("world").bold()]`.
    pub fn rich(mut self, spans: impl IntoIterator<Item = RichSpan>) -> Self {
        self.spans = spans.into_iter().collect();
        self
    }
    /// Font size in pixels. Default: 48.
    pub fn font_size(mut self, v: f64) -> Self {
        self.style.size = v;
        self
    }
    /// 100..=900.
    pub fn weight(mut self, v: f64) -> Self {
        self.style.weight = v;
        self
    }
    /// [`SANS`](crate::SANS), [`MONO`](crate::MONO) or a family registered
    /// with `oxframe_text::register_font`.
    pub fn family(mut self, v: impl Into<String>) -> Self {
        self.style.family = v.into();
        self
    }
    /// Italic style.
    pub fn italic(mut self, v: bool) -> Self {
        self.style.italic = v;
        self
    }
    /// Multiple of the font size.
    pub fn line_height(mut self, v: f64) -> Self {
        self.style.line_height = v;
        self
    }
    /// Extra space between letters, in pixels.
    pub fn letter_spacing(mut self, v: f64) -> Self {
        self.style.letter_spacing = v;
        self
    }
    /// Alignment of lines within the text block.
    pub fn align(mut self, v: crate::TextAlign) -> Self {
        self.style.align = v;
        self
    }
    /// Wrap width.
    pub fn max_width(mut self, v: f64) -> Self {
        self.style.max_width = Some(v);
        self
    }
    /// Largest title from the theme.
    pub fn display(self) -> Self {
        self.role(TypeRole::Display)
    }
    /// Page title from the theme.
    pub fn h1(self) -> Self {
        self.role(TypeRole::H1)
    }
    /// Section title from the theme.
    pub fn h2(self) -> Self {
        self.role(TypeRole::H2)
    }
    /// Running text from the theme.
    pub fn body(self) -> Self {
        self.role(TypeRole::Body)
    }
    /// Secondary text from the theme.
    pub fn caption(self) -> Self {
        self.role(TypeRole::Caption)
    }
    /// Monospace text from the theme.
    pub fn code(self) -> Self {
        self.family(crate::MONO).role(TypeRole::Code)
    }

    fn role(mut self, role: TypeRole) -> Self {
        let r = theme::current().role(role);
        self.style.size = r.size;
        self.style.weight = r.weight;
        self.style.line_height = r.line_height;
        self
    }
}

impl NodeBuilder for TxtBuilder {
    type Handle = Txt;
    base_accessor!();
    fn build(mut self, g: &SceneGraph, parent: NodeId) -> Txt {
        if !self.base.fill_explicit {
            self.base.fill = theme::current().text;
        }
        let k = Self::KIND;
        let kind = NodeKind::Txt {
            text: sig(g, k, "text", self.text),
            spans: sig(g, k, "spans", self.spans),
            font_size: sig(g, k, "font_size", self.style.size),
            weight: sig(g, k, "weight", self.style.weight),
            style: self.style,
            morph: sig(g, k, "morph", None),
            morph_t: sig(g, k, "morph_t", 0.0),
        };
        Txt::from_id(insert(g, parent, kind, self.base))
    }
}

// ------------------------------------------------------------------- Code

builder!(CodeBuilder => Code, kind "Code", defaults {},
    fields { code: String = String::new(), lang: oxframe_text::Lang = oxframe_text::Lang::Rust,
             theme: CodeTheme = CodeTheme::dark(), theme_set: bool = false, style: TextStyle = TextStyle {
                 family: oxframe_text::MONO.to_owned(), size: 32.0, line_height: 1.45, ..TextStyle::default()
             }, focus: Vec2 = Vec2::ZERO, dim: f64 = 0.3 });

impl CodeBuilder {
    /// The source code to show.
    pub fn code(mut self, v: impl Into<String>) -> Self {
        self.code = v.into();
        self
    }
    /// "rust", "js", "ts", "python", "json", "toml", "shell"; anything else
    /// is plain text.
    pub fn language(mut self, name: &str) -> Self {
        self.lang = oxframe_text::Lang::parse(name);
        self
    }
    /// Syntax highlighting colors. Without this, a light project theme uses
    /// [`CodeTheme::light`] and a dark one uses [`CodeTheme::dark`].
    pub fn theme(mut self, t: CodeTheme) -> Self {
        self.theme = t;
        self.theme_set = true;
        self
    }
    /// Font size in pixels.
    pub fn font_size(mut self, v: f64) -> Self {
        self.style.size = v;
        self
    }
    /// Line height as a multiple of the font size.
    pub fn line_height(mut self, v: f64) -> Self {
        self.style.line_height = v;
        self
    }
    /// Highlight lines `first..=last` (1-based); others are dimmed.
    pub fn focus(mut self, first: usize, last: usize) -> Self {
        self.focus = Vec2::new(first as f64, last as f64);
        self
    }
    /// Alpha of unfocused lines (default 0.3).
    pub fn dim(mut self, v: f64) -> Self {
        self.dim = v;
        self
    }
}

impl NodeBuilder for CodeBuilder {
    type Handle = Code;
    base_accessor!();
    fn build(self, g: &SceneGraph, parent: NodeId) -> Code {
        let k = Self::KIND;
        let theme = if self.theme_set {
            self.theme
        } else if theme::current().is_light() {
            CodeTheme::light()
        } else {
            CodeTheme::dark()
        };
        let kind = NodeKind::Code {
            code: sig(g, k, "code", self.code),
            edit_from: sig(g, k, "edit_from", String::new()),
            edit_to: sig(g, k, "edit_to", String::new()),
            editing: sig(g, k, "editing", false),
            edit_t: sig(g, k, "edit_t", 0.0),
            edit_flash: sig(g, k, "edit_flash", false),
            lang: self.lang,
            theme,
            font_size: sig(g, k, "font_size", self.style.size),
            style: self.style,
            focus: sig(g, k, "focus", self.focus),
            dim: sig(g, k, "dim", self.dim),
        };
        Code::from_id(insert(g, parent, kind, self.base))
    }
}

// -------------------------------------------------------------------- Img

builder!(ImgBuilder => Img, kind "Img", defaults {},
    fields { source: Option<ImageSource> = None, width: Option<f64> = None, height: Option<f64> = None });

impl ImgBuilder {
    /// PNG, JPEG or SVG bytes, typically `include_bytes!("../assets/x.png")`
    /// — the only option under wasm.
    ///
    /// # Panics
    ///
    /// If the bytes are not a PNG, JPEG or SVG image.
    pub fn src(mut self, bytes: &[u8]) -> Self {
        self.source =
            Some(ImageSource::decode(bytes).unwrap_or_else(|e| panic!("cannot decode image: {e}")));
        self
    }

    /// Reads an image file (native only).
    ///
    /// # Panics
    ///
    /// If the file cannot be read or decoded.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn file(self, path: impl AsRef<std::path::Path>) -> Self {
        let path = path.as_ref();
        let bytes =
            std::fs::read(path).unwrap_or_else(|e| panic!("cannot read {}: {e}", path.display()));
        self.src(&bytes)
    }

    /// Width; height follows the aspect ratio unless also set.
    pub fn width(mut self, v: f64) -> Self {
        self.width = Some(v);
        self
    }

    /// Height; width follows the aspect ratio unless also set.
    pub fn height(mut self, v: f64) -> Self {
        self.height = Some(v);
        self
    }
}

impl NodeBuilder for ImgBuilder {
    type Handle = Img;
    base_accessor!();
    fn build(self, g: &SceneGraph, parent: NodeId) -> Img {
        let source = self
            .source
            .expect("Img needs a source: call .src(bytes) or .file(path)");
        let (nw, nh) = source.natural_size();
        let aspect = if nh > 0.0 { nw / nh } else { 1.0 };
        let (w, h) = match (self.width, self.height) {
            (Some(w), Some(h)) => (w, h),
            (Some(w), None) => (w, w / aspect),
            (None, Some(h)) => (h * aspect, h),
            (None, None) => (nw, nh),
        };
        let k = Self::KIND;
        let kind = NodeKind::Img {
            source,
            width: sig(g, k, "width", w),
            height: sig(g, k, "height", h),
        };
        Img::from_id(insert(g, parent, kind, self.base))
    }
}

// -------------------------------------------------------------------- Svg

builder!(
    /// An SVG file, expanded into groups, paths and images.
    SvgBuilder => Svg, kind "Svg", defaults {},
    fields { bytes: Vec<u8> = Vec::new(), width: Option<f64> = None, height: Option<f64> = None });

impl SvgBuilder {
    /// SVG bytes, typically `include_bytes!("../assets/logo.svg")` — the only
    /// option under wasm.
    ///
    /// # Panics
    ///
    /// If the bytes are not an SVG `usvg` can parse.
    pub fn src(mut self, bytes: &[u8]) -> Self {
        self.bytes = bytes.to_vec();
        self
    }

    /// Reads an SVG file (native only).
    ///
    /// # Panics
    ///
    /// If the file cannot be read or parsed.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn file(self, path: impl AsRef<std::path::Path>) -> Self {
        let path = path.as_ref();
        let bytes =
            std::fs::read(path).unwrap_or_else(|e| panic!("cannot read {}: {e}", path.display()));
        self.src(&bytes)
    }

    /// Width in pixels. Height keeps the view box aspect unless also set.
    pub fn width(mut self, v: f64) -> Self {
        self.width = Some(v);
        self
    }

    /// Height in pixels. Width keeps the view box aspect unless also set.
    pub fn height(mut self, v: f64) -> Self {
        self.height = Some(v);
        self
    }
}

impl NodeBuilder for SvgBuilder {
    type Handle = crate::handles::Svg;
    base_accessor!();
    fn build(mut self, g: &SceneGraph, parent: NodeId) -> crate::handles::Svg {
        if self.bytes.is_empty() {
            panic!("Svg needs a source: call .src(bytes) or .file(path)");
        }
        let tree = usvg::Tree::from_data(&self.bytes, &usvg::Options::default())
            .unwrap_or_else(|e| panic!("cannot parse svg: {e}"));
        let size = tree.size();
        let (nw, nh) = (f64::from(size.width()), f64::from(size.height()));
        let aspect = if nh > 0.0 { nw / nh } else { 1.0 };
        let (w, h) = match (self.width, self.height) {
            (Some(w), Some(h)) => (w, h),
            (Some(w), None) => (w, if aspect > 0.0 { w / aspect } else { w }),
            (None, Some(h)) => (h * aspect, h),
            (None, None) => (nw, nh),
        };
        if self.base.name.is_none()
            && let Some(id) = (!tree.root().id().is_empty()).then(|| tree.root().id())
        {
            self.base.name = Some(id.to_string());
        }
        self.base.opacity *= f64::from(tree.root().opacity().get());
        self.base.blend = crate::svg::blend(tree.root().blend_mode());
        let k = Self::KIND;
        let kind = NodeKind::Svg {
            width: sig(g, k, "width", w),
            height: sig(g, k, "height", h),
        };
        let id = insert(g, parent, kind, self.base);
        let fit = crate::svg::Fit {
            nw,
            nh,
            sx: if nw > 0.0 { w / nw } else { 1.0 },
            sy: if nh > 0.0 { h / nh } else { 1.0 },
        };
        crate::svg::inflate(g, id, tree.root(), &fit);
        crate::handles::Svg::from_id(id)
    }
}

// ----------------------------------------------------------------- Layout

builder!(
    /// Places children in a row or column. Children's `x`/`y` become offsets
    /// from their layout position.
    LayoutBuilder => Layout, kind "Layout", defaults {},
    fields { direction: Direction = Direction::Row, align: LayoutAlign = LayoutAlign::Center,
             justify: Justify = Justify::Center, wrap: bool = false, columns: Option<u16> = None,
             gap: f64 = 0.0, padding: f64 = 0.0,
             width: Option<Span> = None, height: Option<Span> = None, radius: f64 = 0.0 });
setters!(LayoutBuilder {
    direction: Direction,
    align: LayoutAlign,
    justify: Justify
});

impl LayoutBuilder {
    /// Space between children, in pixels or a theme step (`.gap(Sm)`).
    pub fn gap(mut self, v: impl Len) -> Self {
        self.gap = v.px();
        self
    }
    /// Space inside the edges, in pixels or a theme step (`.padding(Md)`).
    pub fn padding(mut self, v: impl Len) -> Self {
        self.padding = v.px();
        self
    }
    /// Corner radius, in pixels or a theme step.
    pub fn radius(mut self, v: impl Corner) -> Self {
        self.radius = v.radius();
        self
    }
}

impl LayoutBuilder {
    /// Wrap children onto several lines when they do not fit.
    pub fn wrap(mut self, v: bool) -> Self {
        self.wrap = v;
        self
    }
    /// CSS grid with `n` equal columns instead of a row/column.
    pub fn grid(mut self, columns: u16) -> Self {
        self.columns = Some(columns.max(1));
        self
    }
    /// Children side by side (the default).
    pub fn row(self) -> Self {
        self.direction(Direction::Row)
    }
    /// Children stacked top to bottom.
    pub fn column(self) -> Self {
        self.direction(Direction::Column)
    }
    /// Fixed width in pixels or [`pct`](crate::pct)`(80)` (otherwise sized to content).
    pub fn width(mut self, v: impl Into<Span>) -> Self {
        self.width = Some(v.into());
        self
    }
    /// Fixed height in pixels or a percentage (otherwise sized to content).
    pub fn height(mut self, v: impl Into<Span>) -> Self {
        self.height = Some(v.into());
        self
    }
}

impl NodeBuilder for LayoutBuilder {
    type Handle = Layout;
    base_accessor!();
    fn build(self, g: &SceneGraph, parent: NodeId) -> Layout {
        let k = Self::KIND;
        let kind = NodeKind::Layout {
            direction: self.direction,
            align: self.align,
            justify: self.justify,
            wrap: self.wrap,
            columns: self.columns,
            gap: sig(g, k, "gap", self.gap),
            padding: sig(g, k, "padding", self.padding),
            width: place::span_option(
                g,
                parent,
                Axis::X,
                self.width,
                format!("{k}#{}.width", g.next_id()),
            ),
            height: place::span_option(
                g,
                parent,
                Axis::Y,
                self.height,
                format!("{k}#{}.height", g.next_id()),
            ),
            radius: sig(g, k, "radius", self.radius),
        };
        Layout::from_id(insert(g, parent, kind, self.base))
    }
}

// ------------------------------------------------------------------- Grid

builder!(GridBuilder => Grid, kind "Grid",
    defaults { stroke: Color::rgba(1.0, 1.0, 1.0, 0.15), line_width: 1.0 },
    fields { width: f64 = 1920.0, height: f64 = 1080.0, spacing: f64 = 80.0 });
setters!(GridBuilder {
    width: f64,
    height: f64,
    spacing: f64
});

impl NodeBuilder for GridBuilder {
    type Handle = Grid;
    base_accessor!();
    fn build(self, g: &SceneGraph, parent: NodeId) -> Grid {
        let k = Self::KIND;
        let kind = NodeKind::Grid {
            width: sig(g, k, "width", self.width),
            height: sig(g, k, "height", self.height),
            spacing: sig(g, k, "spacing", self.spacing),
        };
        Grid::from_id(insert(g, parent, kind, self.base))
    }
}

// ------------------------------------------------------------------ Custom

/// Builder for a [`Custom`] node.
#[must_use = "a builder does nothing until it is passed to `add`"]
pub struct CustomBuilder {
    base: BaseProps,
    draw: crate::draw::DrawFn,
    width: f64,
    height: f64,
    fullscreen: bool,
}

impl CustomBuilder {
    pub(crate) fn from_draw(draw: impl Fn(&mut crate::draw::DrawCtx, f64) + 'static) -> Self {
        Self {
            base: BaseProps::default(),
            draw: std::rc::Rc::new(draw),
            width: 100.0,
            height: 100.0,
            fullscreen: false,
        }
    }

    /// Layout box. The closure's shapes are not measured into this.
    pub fn size(mut self, width: f64, height: f64) -> Self {
        self.width = width;
        self.height = height;
        self.fullscreen = false;
        self
    }

    /// Uses the frame as the box, centered on the parent. Put the node on
    /// the view when it should ignore a camera.
    pub fn fullscreen(mut self) -> Self {
        self.fullscreen = true;
        self
    }
}

impl NodeBuilder for CustomBuilder {
    type Handle = Custom;
    base_accessor!();
    fn build(self, graph: &SceneGraph, parent: NodeId) -> Custom {
        let (width, height) = if self.fullscreen {
            graph.frame_size()
        } else {
            (self.width, self.height)
        };
        let kind = NodeKind::Custom {
            draw: self.draw,
            width,
            height,
        };
        Custom::from_id(insert(graph, parent, kind, self.base))
    }
}

// -------------------------------------------------------------- Particles

/// Builder for a [`Particles`] node.
#[must_use = "a builder does nothing until it is passed to `add`"]
pub struct ParticlesBuilder {
    base: BaseProps,
    seed: u64,
    gravity: f64,
    toward_camera: bool,
    life: f64,
    speed: f64,
    radius: f64,
}

impl Default for ParticlesBuilder {
    fn default() -> Self {
        Self {
            base: BaseProps::default(),
            seed: 1,
            gravity: 980.0,
            toward_camera: false,
            life: 1.4,
            speed: 360.0,
            radius: 5.0,
        }
    }
}

impl ParticlesBuilder {
    /// Seed of the burst generator. The same seed is the same spray.
    pub fn seed(mut self, seed: u64) -> Self {
        self.seed = seed;
        self
    }

    /// Downward acceleration, in pixels per second squared.
    pub fn gravity(mut self, gravity: f64) -> Self {
        self.gravity = gravity;
        self
    }

    /// Particles grow as they come toward the viewer. Their motion stays flat.
    pub fn toward_camera(mut self) -> Self {
        self.toward_camera = true;
        self
    }

    /// How long each particle lives, in seconds.
    pub fn life(mut self, seconds: f64) -> Self {
        self.life = seconds;
        self
    }

    /// Launch speed, in pixels per second.
    pub fn speed(mut self, speed: f64) -> Self {
        self.speed = speed;
        self
    }

    /// Dot radius, in pixels, before the depth scale.
    pub fn radius(mut self, radius: f64) -> Self {
        self.radius = radius;
        self
    }
}

impl NodeBuilder for ParticlesBuilder {
    type Handle = Particles;
    base_accessor!();
    fn build(self, graph: &SceneGraph, parent: NodeId) -> Particles {
        let kind = NodeKind::Particles {
            seed: self.seed,
            gravity: sig(graph, "Particles", "gravity", self.gravity),
            toward_camera: self.toward_camera,
            life: self.life,
            speed: self.speed,
            radius: self.radius,
            state: crate::sim::ParticlesState::new(),
        };
        Particles::from_id(insert(graph, parent, kind, self.base))
    }
}

// ------------------------------------------------------------------ Shader

enum Seed {
    Num(f64),
    Vec2(f64, f64),
    Color(Color),
    Bool(bool),
}

/// Builder for a [`Shader`] node.
#[must_use = "a builder does nothing until it is passed to `add`"]
pub struct ShaderBuilder {
    base: BaseProps,
    code: crate::shader::ShaderCode,
    waves: bool,
    width: Span,
    height: Span,
    fullscreen: bool,
    uniforms: Vec<(String, Seed)>,
}

impl ShaderBuilder {
    pub(crate) fn new(code: crate::shader::ShaderCode) -> Self {
        Self {
            base: BaseProps::default(),
            code,
            waves: false,
            width: Span::Px(100.0),
            height: Span::Px(100.0),
            fullscreen: false,
            uniforms: Vec::new(),
        }
    }

    pub(crate) fn preset() -> Self {
        let mut s = Self::new(crate::shader::ShaderCode::Wgsl(
            crate::shader::waves_wgsl().into(),
        ));
        s.waves = true;
        s.uniform("amp", 0.05)
            .uniform("speed", 1.0)
            .uniform_color("deep", Color::rgba(0.102_f32, 0.322_f32, 0.549_f32, 1.0))
            .uniform_color("foam", Color::rgba(0.910_f32, 0.953_f32, 0.980_f32, 1.0))
    }

    /// Width in pixels, or [`pct`](crate::pct)`(100)` for the whole frame.
    pub fn width(mut self, v: impl Into<Span>) -> Self {
        self.width = v.into();
        self.fullscreen = false;
        self
    }

    /// Height in pixels, or a percentage of the frame.
    pub fn height(mut self, v: impl Into<Span>) -> Self {
        self.height = v.into();
        self.fullscreen = false;
        self
    }

    /// Uses the frame as the box. Put the node on the view so a camera
    /// does not move it.
    pub fn fullscreen(mut self) -> Self {
        self.fullscreen = true;
        self
    }

    /// Sets a numeric `Params` field. A name that is not in the shader
    /// fails `check` (`param_unknown`).
    pub fn uniform(mut self, name: impl Into<String>, value: f64) -> Self {
        self.set_uniform(name.into(), Seed::Num(value));
        self
    }

    /// Sets a `vec2<f32>` field from a point.
    pub fn uniform_vec2(mut self, name: impl Into<String>, value: impl Into<Vec2>) -> Self {
        let v = value.into();
        self.set_uniform(name.into(), Seed::Vec2(v.x, v.y));
        self
    }

    /// Sets a `vec4<f32>` field from a color or a theme role such as `Accent`.
    pub fn uniform_color(mut self, name: impl Into<String>, value: impl IntoPaint) -> Self {
        self.set_uniform(name.into(), Seed::Color(value.into_paint()));
        self
    }

    /// Sets a `bool` field.
    pub fn uniform_bool(mut self, name: impl Into<String>, value: bool) -> Self {
        self.set_uniform(name.into(), Seed::Bool(value));
        self
    }

    /// Wave height, as a fraction of the box. The preset default is 0.05.
    pub fn amp(self, value: f64) -> Self {
        self.uniform("amp", value)
    }

    /// How fast the crest travels. The preset default is 1.
    pub fn speed(self, value: f64) -> Self {
        self.uniform("speed", value)
    }

    /// Color below the crest. A theme role such as `Accent` follows the theme.
    pub fn deep(self, value: impl IntoPaint) -> Self {
        self.uniform_color("deep", value)
    }

    /// Color of the crest.
    pub fn foam(self, value: impl IntoPaint) -> Self {
        self.uniform_color("foam", value)
    }

    fn set_uniform(&mut self, name: String, value: Seed) {
        if let Some(slot) = self.uniforms.iter_mut().find(|(n, _)| *n == name) {
            slot.1 = value;
        } else {
            self.uniforms.push((name, value));
        }
    }
}

impl NodeBuilder for ShaderBuilder {
    type Handle = Shader;
    base_accessor!();
    fn build(self, graph: &SceneGraph, parent: NodeId) -> Shader {
        let (width, height) = if self.fullscreen {
            let (w, h) = graph.frame_size();
            (Span::Px(w), Span::Px(h))
        } else {
            (self.width, self.height)
        };
        let n = graph.next_id();
        let uniforms = self
            .uniforms
            .into_iter()
            .map(|(name, seed)| {
                let value = match seed {
                    Seed::Num(v) => crate::shader::UniformValue::Num(
                        Signal::new(v).with_name(format!("Shader#{n}.{name}")),
                    ),
                    Seed::Vec2(x, y) => crate::shader::UniformValue::Vec2(
                        Signal::new(Vec2::new(x, y)).with_name(format!("Shader#{n}.{name}")),
                    ),
                    Seed::Color(c) => crate::shader::UniformValue::Color(
                        Signal::new(c).with_name(format!("Shader#{n}.{name}")),
                    ),
                    Seed::Bool(v) => crate::shader::UniformValue::Bool(
                        Signal::new(if v { 1.0 } else { 0.0 })
                            .with_name(format!("Shader#{n}.{name}")),
                    ),
                };
                crate::shader::Uniform { name, value }
            })
            .collect();
        let kind = NodeKind::Shader {
            code: self.code,
            waves: self.waves,
            width: place::span_signal(graph, parent, Axis::X, width, format!("Shader#{n}.width")),
            height: place::span_signal(
                graph,
                parent,
                Axis::Y,
                height,
                format!("Shader#{n}.height"),
            ),
            uniforms,
        };
        Shader::from_id(insert(graph, parent, kind, self.base))
    }
}

/// The waves preset: a crest of `foam` over `deep` water.
pub fn waves() -> ShaderBuilder {
    ShaderBuilder::preset()
}