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
//! Persistent types used by the Blender-style editor area layout.
use serde::{Deserialize, Serialize};
/// Content that can be placed in any editor area.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum EditorPanel {
/// 3D view that uses the editor camera.
Scene,
/// 3D view that uses the active game camera.
Game,
/// Text editor for Rust and shader files.
Code,
/// Tree of all objects in the scene.
Hierarchy,
/// Settings for the selected object.
Inspector,
/// Project paths, render settings, and asset counts.
Project,
/// Messages created by the editor and engine.
Console,
/// List of assets loaded by the engine.
Assets,
}
impl EditorPanel {
pub(super) const ALL: [Self; 8] = [
Self::Scene,
Self::Game,
Self::Code,
Self::Hierarchy,
Self::Inspector,
Self::Project,
Self::Console,
Self::Assets,
];
pub(super) fn title(self) -> &'static str {
match self {
Self::Scene => "Scene View",
Self::Game => "Game View",
Self::Code => "Code Editor",
Self::Hierarchy => "Hierarchy",
Self::Inspector => "Inspector",
Self::Project => "Project Settings",
Self::Console => "Console",
Self::Assets => "Assets",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum EditorSplitAxis {
/// Places two areas next to each other.
Columns,
/// Places one area above the other.
Rows,
}
/// Persistent Blender-style area tree. A leaf selects its editor type; a split
/// owns two more areas and a draggable divider.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum EditorDockNode {
/// One visible editor area.
Area {
/// Stable number used by buttons and egui controls.
id: u64,
/// Content currently shown inside this area.
panel: EditorPanel,
},
/// Divider that owns two smaller layout nodes.
Split {
/// Controls whether children are side by side or top and bottom.
axis: EditorSplitAxis,
/// Part of the available size given to the first child.
ratio: f32,
/// Area or split shown before the divider.
first: Box<Self>,
/// Area or split shown after the divider.
second: Box<Self>,
},
}
#[derive(Serialize, Deserialize)]
pub(super) struct EditorLayoutFile {
/// Complete area tree saved to disk.
pub(super) layout: EditorDockNode,
/// Area that had the blue selection border.
pub(super) active_area: u64,
/// Number that will be given to the next new area.
pub(super) next_area_id: u64,
}
impl EditorDockNode {
/// Creates the layout shown when the editor starts for the first time.
pub(super) fn default_layout() -> Self {
// First build the top part: hierarchy, scene, and inspector.
let main = Self::Split {
axis: EditorSplitAxis::Columns,
ratio: 0.18,
first: Box::new(Self::Area {
id: 1,
panel: EditorPanel::Hierarchy,
}),
second: Box::new(Self::Split {
axis: EditorSplitAxis::Columns,
ratio: 0.72,
first: Box::new(Self::Area {
id: 2,
panel: EditorPanel::Scene,
}),
second: Box::new(Self::Area {
id: 3,
panel: EditorPanel::Inspector,
}),
}),
};
// Put project settings below the main part.
Self::Split {
axis: EditorSplitAxis::Rows,
ratio: 0.82,
first: Box::new(main),
second: Box::new(Self::Area {
id: 4,
panel: EditorPanel::Project,
}),
}
}
/// Changes the content of the area with the given ID.
///
/// Returns true when the area was found.
pub(super) fn set_panel(&mut self, id: u64, panel: EditorPanel) -> bool {
match self {
Self::Area {
id: area_id,
panel: current,
} if *area_id == id => {
*current = panel;
true
}
Self::Area { .. } => false,
Self::Split { first, second, .. } => {
first.set_panel(id, panel) || second.set_panel(id, panel)
}
}
}
/// Returns an area that still exists after another area is closed.
pub(super) fn first_area_id(&self) -> u64 {
match self {
Self::Area { id, .. } => *id,
Self::Split { first, .. } => first.first_area_id(),
}
}
/// Replaces one area with a divider and two copies of that area.
pub(super) fn split(
&mut self,
id: u64,
axis: EditorSplitAxis,
new_id: u64,
) -> bool {
match self {
Self::Area { id: area_id, panel } if *area_id == id => {
let old_panel = *panel;
*self = Self::Split {
axis,
ratio: 0.5,
first: Box::new(Self::Area {
id,
panel: old_panel,
}),
second: Box::new(Self::Area {
id: new_id,
panel: old_panel,
}),
};
true
}
Self::Area { .. } => false,
Self::Split { first, second, .. } => {
first.split(id, axis, new_id) || second.split(id, axis, new_id)
}
}
}
/// Removes one area and lets its neighbour use the free space.
pub(super) fn close(&mut self, id: u64) -> bool {
let Self::Split { first, second, .. } = self else {
return false;
};
if matches!(first.as_ref(), Self::Area { id: area_id, .. } if *area_id == id)
{
*self = *second.clone();
return true;
}
if matches!(second.as_ref(), Self::Area { id: area_id, .. } if *area_id == id)
{
*self = *first.clone();
return true;
}
first.close(id) || second.close(id)
}
}