GPU-accelerated UI toolkit (Vulkan)
git clone https://git.lucas.co/cce-ui.git
tests/lattice_probe.rs (4.4K)
1 use cce_ui::scene::layout::Rect;
2 use cce_ui::scene::paint::{PaintCtx, Prim};
3
4 #[test]
5 fn lattice_survives_tessellation_inside_a_clip() {
6 let mut pc = PaintCtx::new();
7 let clip = Rect { x: 40.0, y: 40.0, width: 600.0, height: 300.0 };
8 pc.clip(clip, |pc| {
9 pc.lattice(clip, (85.0, 43.0), (60.0, 60.0), (71.0, 31.0), 6.0, 6.0);
10 pc.groove((40.0, 100.0), (640.0, 100.0), 2.0, 2.0, clip);
11 });
12 let dl = pc.finish();
13 let kinds: Vec<String> = dl.items.iter().map(|i| format!("{:?}", std::mem::discriminant(&i.prim))).collect();
14 let n_lattice = dl.items.iter().filter(|i| matches!(i.prim, Prim::Lattice { .. })).count();
15 assert_eq!(n_lattice, 1, "display list: {kinds:?}");
16 let (verts, batches, _, _) = cce_ui::backend::tessellate::tessellate_display_list(&dl, 1280.0, 720.0, 2.0);
17 let modes: Vec<f32> = batches.iter().filter_map(|b| b.plate.as_ref().map(|p| p.mode)).collect();
18 eprintln!("verts={} batches={} plate modes={modes:?}", verts.len(), batches.len());
19 assert!(modes.contains(&13.0), "no lattice batch; modes={modes:?}");
20 }
21
22 /// The graph grid is a LATTICE of lines (since 2026-09-22): one quad per
23 /// line centred on its pitch coordinate, clipped to the rect, plus the two
24 /// heavier axes through the (0, 0) crossing. It used to be cells with grout
25 /// between them, one `Grout` draw, which is why this test once asserted that.
26 #[test]
27 fn graph_paints_its_lattice_as_lines_and_axes() {
28 use cce_ui::widget::GraphController;
29 // The one style value this path reads. Pinned, or the user's config.kdl
30 // decides the answer (a width of 0 turns the lines off entirely). This
31 // binary builds cce-ui without cfg(test), so the write is to the shared
32 // registry, which no other test in this file reads.
33 cce_ui::layout::set_graph_line_width(1.0);
34 let mut g = cce_ui::widget::Graph::new();
35 g.set_grid_pitch(60.0, 50.0);
36 g.set_grid_origin(50.0, 50.0);
37 g.set_network_opacity(1.0);
38 g.set_show_network_grid(true);
39 let mut pc = PaintCtx::new();
40 let rect = Rect { x: 40.0, y: 40.0, width: 600.0, height: 300.0 };
41 g.paint_grid(rect, &mut pc);
42 let dl = pc.finish();
43
44 assert!(!dl.items.iter().any(|i| matches!(i.prim, Prim::Grout { .. })), "the lattice draws no grout");
45 let quads: Vec<Rect> = dl
46 .items
47 .iter()
48 .filter_map(|i| match i.prim {
49 Prim::Quad { rect, .. } => Some(rect),
50 _ => None,
51 })
52 .collect();
53
54 // Verticals at x = 50 + 60c inside 40..640: 50, 110 … 590, ten of them;
55 // horizontals at y = 50 + 50r inside 40..340: 50, 100 … 300, six. No line
56 // sits within half a width of an edge, so none is clipped to a sliver.
57 let mut verticals: Vec<f32> = quads
58 .iter()
59 .filter(|q| q.width == 1.0 && q.height == rect.height)
60 .map(|q| q.x + 0.5)
61 .collect();
62 let mut horizontals: Vec<f32> = quads
63 .iter()
64 .filter(|q| q.height == 1.0 && q.width == rect.width)
65 .map(|q| q.y + 0.5)
66 .collect();
67 verticals.sort_by(f32::total_cmp);
68 horizontals.sort_by(f32::total_cmp);
69 assert_eq!(verticals, (0..10).map(|c| 50.0 + 60.0 * c as f32).collect::<Vec<_>>());
70 assert_eq!(horizontals, (0..6).map(|r| 50.0 + 50.0 * r as f32).collect::<Vec<_>>());
71
72 // The axes: 2 px, across the whole rect, centred on the origin.
73 let axes: Vec<&Rect> = quads.iter().filter(|q| q.width == 2.0 || q.height == 2.0).collect();
74 assert_eq!(axes.len(), 2, "two origin axes; quads: {quads:?}");
75 assert!(axes.iter().any(|q| q.height == 2.0 && q.y == 49.0 && q.width == rect.width));
76 assert!(axes.iter().any(|q| q.width == 2.0 && q.x == 49.0 && q.height == rect.height));
77 assert_eq!(quads.len(), 10 + 6 + 2, "nothing else is painted; quads: {quads:?}");
78 }
79
80 #[test]
81 fn grout_tessellates_to_a_mode_15_batch_in_its_colour() {
82 let mut pc = PaintCtx::new();
83 let clip = Rect { x: 40.0, y: 40.0, width: 600.0, height: 300.0 };
84 pc.clip(clip, |pc| pc.grout(clip, (85.0, 43.0), (60.0, 60.0), (71.0, 31.0), 15.5, [0.5, 0.5, 0.5, 0.1]));
85 let dl = pc.finish();
86 let (verts, batches, _, _) = cce_ui::backend::tessellate::tessellate_display_list(&dl, 1280.0, 720.0, 2.0);
87 let modes: Vec<f32> = batches.iter().filter_map(|b| b.plate.as_ref().map(|p| p.mode)).collect();
88 assert_eq!(modes, vec![15.0]);
89 assert_eq!(verts.len(), 6);
90 assert!(verts.iter().all(|v| (v.color[3] - 0.1).abs() < 1e-6), "cover quad carries the grout colour");
91 }