1use bevy::prelude::*;
2
3use crate::{
4 components::{Dart, Edge, FaceId, Vertex},
5 import_map::Face,
6 resources::{
7 DartHeadMul, DartRenderColor, DartShrink, EdgeRenderColor, FaceNormals, FaceRenderColor,
8 FaceShrink, MapVertices, VertexRenderColor, VertexWidth,
9 },
10};
11
12#[derive(Default, Reflect, GizmoConfigGroup)]
14pub struct DartGizmos;
15
16pub fn render_dart_enabled(drc: Res<DartRenderColor>) -> bool {
18 drc.0
19}
20
21pub fn render_darts(
23 mut gizmos: Gizmos<DartGizmos>,
24 vertices: Res<MapVertices>,
26 face_normals: Res<FaceNormals>,
27 dart_render_color: Res<DartRenderColor>,
29 dart_head_mul: Res<DartHeadMul>,
30 dart_shrink: Res<DartShrink>,
32 q: Query<(&Dart, &FaceId)>,
34) {
35 let vertices = &vertices.0;
39 let face_normals = &face_normals.0;
40 let [red, green, blue, alpha] = dart_render_color.1.to_srgba_unmultiplied();
41 for (d, face_id) in &q {
42 let (n1, n2) = (
43 &face_normals[&(face_id.0, d.start)],
44 &face_normals[&(face_id.0, d.end)],
45 );
46 let (ov1, ov2) = (&vertices[d.start], &vertices[d.end]);
47
48 let (mut v1, mut v2) = (*ov1 + (*n1 * dart_shrink.0), *ov2 + (*n2 * dart_shrink.0));
49 let (dir, len) = ((v2 - v1).normalize(), (v2 - v1).length());
50 v1 += dir * (len * dart_shrink.0.abs() / 2.0);
51 v2 -= dir * (len * dart_shrink.0.abs() / 2.0);
52
53 gizmos
54 .arrow(v1, v2, Color::srgba_u8(red, green, blue, alpha))
55 .with_tip_length(dart_head_mul.0);
56 }
57}
58
59#[derive(Default, Reflect, GizmoConfigGroup)]
61pub struct VertexGizmos;
62
63pub fn render_vertex_enabled(vrc: Res<VertexRenderColor>) -> bool {
65 vrc.0
66}
67
68pub fn render_vertices(
70 mut gizmos: Gizmos<VertexGizmos>,
71 vertices: Res<MapVertices>,
72 vertex_render_color: Res<VertexRenderColor>,
73 vertex_width: Res<VertexWidth>,
74 q: Query<&Vertex>,
75) {
76 let vertices = &vertices.0;
77 let [red, green, blue, alpha] = vertex_render_color.1.to_srgba_unmultiplied();
78
79 for v in &q {
80 gizmos.sphere(
81 vertices[v.0],
82 Quat::default(),
83 vertex_width.0 / 2.0,
84 Color::srgba_u8(red, green, blue, alpha),
85 );
86 }
87}
88
89#[derive(Default, Reflect, GizmoConfigGroup)]
91pub struct EdgeGizmos;
92
93pub fn render_edge_enabled(erc: Res<EdgeRenderColor>) -> bool {
95 erc.0
96}
97
98pub fn render_edges(
100 mut gizmos: Gizmos<EdgeGizmos>,
101 vertices: Res<MapVertices>,
102 edge_render_color: Res<EdgeRenderColor>,
103 q: Query<&Edge>,
105) {
106 let vertices = &vertices.0;
107 let [red, green, blue, alpha] = edge_render_color.1.to_srgba_unmultiplied();
108
109 for e in &q {
110 gizmos.line(
111 vertices[e.0],
112 vertices[e.1],
113 Color::srgba_u8(red, green, blue, alpha),
114 );
115 }
116}
117
118#[derive(Default, Reflect, GizmoConfigGroup)]
120pub struct FaceGizmos;
121
122pub fn render_face_enabled(frc: Res<FaceRenderColor>) -> bool {
124 frc.0
125}
126
127#[allow(clippy::missing_panics_doc)]
128pub fn render_faces(
132 mut gizmos: Gizmos<VertexGizmos>,
133 vertices: Res<MapVertices>,
134 face_normals: Res<FaceNormals>,
135 face_render_color: Res<FaceRenderColor>,
136 face_shrink: Res<FaceShrink>,
137 q: Query<(&Face, &FaceId)>,
138) {
139 let vertices = &vertices.0;
140 let normals = &face_normals.0;
141 let [red, green, blue, alpha] = face_render_color.1.to_srgba_unmultiplied();
142
143 for (Face(v), FaceId(fid)) in &q {
144 v.windows(2).for_each(|sl| {
145 let &[vid1, vid2] = sl else { unreachable!() };
146 let (n1, n2) = (&normals[&(*fid, vid1)], &normals[&(*fid, vid2)]);
147 let (ov1, ov2) = (&vertices[vid1], &vertices[vid2]);
148
149 let (v1, v2) = (*ov1 + (*n1 * face_shrink.0), *ov2 + (*n2 * face_shrink.0));
150
151 gizmos.line(v1, v2, Color::srgba_u8(red, green, blue, alpha));
152 });
153 {
154 let [vid1, vid2] = [*v.last().unwrap(), v[0]];
155 let (n1, n2) = (&normals[&(*fid, vid1)], &normals[&(*fid, vid2)]);
156 let (ov1, ov2) = (&vertices[vid1], &vertices[vid2]);
157
158 let (v1, v2) = (*ov1 + (*n1 * face_shrink.0), *ov2 + (*n2 * face_shrink.0));
159
160 gizmos.line(v1, v2, Color::srgba_u8(red, green, blue, alpha));
161 }
162 }
163}