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
use std::{hash::Hasher, ops::DerefMut};
use crate::inspectable::Inspectable;
pub trait TreeNode: Inspectable {
fn inspect_child(&mut self, this_id: u64, search_id: u64, ui: &mut egui::Ui);
fn node_ui(&mut self, name: &str, id: u64, selected: &mut Option<u64>, ui: &mut egui::Ui) {
default_parent_node_ui(
std::any::type_name::<Self>(),
name,
id,
selected,
ui,
|id, selected, ui| self.contents_ui(id, selected, ui),
);
}
fn contents_ui(&mut self, _id: u64, _selected: &mut Option<u64>, _ui: &mut egui::Ui) {}
}
pub fn default_node_ui(
type_name: &str,
name: &str,
id: u64,
selected: &mut Option<u64>,
ui: &mut egui::Ui,
) {
ui.horizontal(|ui| {
if ui
.selectable_label(matches!(*selected, Some(i) if i == id), name)
.clicked()
{
*selected = Some(id);
}
ui.add_enabled_ui(false, |ui| ui.small(type_name));
});
}
pub fn default_parent_node_ui(
type_name: &str,
name: &str,
id: u64,
selected: &mut Option<u64>,
ui: &mut egui::Ui,
body: impl FnOnce(u64, &mut Option<u64>, &mut egui::Ui),
) {
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
ui.make_persistent_id(id),
false,
)
.show_header(ui, |ui| {
if ui
.selectable_label(matches!(*selected, Some(i) if i == id), name)
.clicked()
{
*selected = Some(id);
}
ui.add_enabled_ui(false, |ui| ui.small(type_name));
})
.body(|ui| body(id, selected, ui));
}
impl<T: TreeNode, const X: usize> TreeNode for [T; X] {
fn inspect_child(&mut self, this_id: u64, search_id: u64, ui: &mut egui::Ui) {
if this_id == search_id {
self.inspect_ui(ui);
} else {
for (i, element) in self.into_iter().enumerate() {
let mut hasher = std::collections::hash_map::DefaultHasher::default();
hasher.write_u64(this_id);
hasher.write_u64(i as u64);
element.inspect_child(this_id, search_id, ui);
}
}
}
fn contents_ui(&mut self, id: u64, selected: &mut Option<u64>, ui: &mut egui::Ui) {
for (i, element) in self.into_iter().enumerate() {
let mut hasher = std::collections::hash_map::DefaultHasher::default();
hasher.write_u64(id);
hasher.write_u64(i as u64);
element.node_ui(&i.to_string(), hasher.finish(), selected, ui);
}
}
}
impl<T: TreeNode> TreeNode for Vec<T> {
fn inspect_child(&mut self, this_id: u64, search_id: u64, ui: &mut egui::Ui) {
if this_id == search_id {
self.inspect_ui(ui);
} else {
for (i, element) in self.into_iter().enumerate() {
let mut hasher = std::collections::hash_map::DefaultHasher::default();
hasher.write_u64(this_id);
hasher.write_u64(i as u64);
element.inspect_child(hasher.finish(), search_id, ui);
}
}
}
fn contents_ui(&mut self, id: u64, selected: &mut Option<u64>, ui: &mut egui::Ui) {
for (i, element) in self.into_iter().enumerate() {
let mut hasher = std::collections::hash_map::DefaultHasher::default();
hasher.write_u64(id);
hasher.write_u64(i as u64);
element.node_ui(&i.to_string(), hasher.finish(), selected, ui);
}
}
}
impl<T: TreeNode + ?Sized> TreeNode for &mut T {
fn inspect_child(&mut self, this_id: u64, search_id: u64, ui: &mut egui::Ui) {
(*self).inspect_child(this_id, search_id, ui)
}
fn contents_ui(&mut self, id: u64, selected: &mut Option<u64>, ui: &mut egui::Ui) {
(*self).contents_ui(id, selected, ui)
}
fn node_ui(&mut self, name: &str, id: u64, selected: &mut Option<u64>, ui: &mut egui::Ui) {
(*self).node_ui(name, id, selected, ui)
}
}
impl<T: TreeNode + ?Sized> TreeNode for Box<T> {
fn inspect_child(&mut self, this_id: u64, search_id: u64, ui: &mut egui::Ui) {
self.deref_mut().inspect_child(this_id, search_id, ui)
}
fn contents_ui(&mut self, id: u64, selected: &mut Option<u64>, ui: &mut egui::Ui) {
self.deref_mut().contents_ui(id, selected, ui)
}
fn node_ui(&mut self, name: &str, id: u64, selected: &mut Option<u64>, ui: &mut egui::Ui) {
self.deref_mut().node_ui(name, id, selected, ui)
}
}