tirbofish/dropbear
main / crates / eucalyptus-editor / src / editor / settings / editor.rs · 13137 bytes
crates/eucalyptus-editor/src/editor/settings/editor.rs
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
//! The scene for a window that opens up settings related to the eucalyptus-editor.
use crate::editor::EditorTabId;
use app_dirs2::AppDataType;
use dropbear_engine::input::{Controller, Keyboard, Mouse};
use dropbear_engine::multisampling::AntiAliasingMode;
use dropbear_engine::scene::{Scene, SceneCommand};
use egui::{CentralPanel, ComboBox, Id, Panel, Slider, SliderClamping, Ui};
use egui_dock::DockState;
use egui_ltreeview::{Action, NodeBuilder};
use eucalyptus_core::input::InputState;
use eucalyptus_core::utils::option::HistoricalOption;
use eucalyptus_core::{APP_INFO, warn};
use gilrs::{Button, GamepadId};
use hecs::spin::Lazy;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use winit::dpi::PhysicalPosition;
use winit::event::MouseButton;
use winit::event_loop::ActiveEventLoop;
use winit::keyboard::KeyCode;
use winit::window::WindowId;
pub static EDITOR_SETTINGS: Lazy<RwLock<EditorSettings>> =
Lazy::new(|| RwLock::new(EditorSettings::new()));
/// Settings related to the eucalyptus-editor.
///
/// This is not related to a project, and is for each user who uses the editor.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct EditorSettings {
/// The layout of the dock within the game editor page.
#[serde(default)]
pub game_editor_dock_state: Option<DockState<EditorTabId>>,
/// The layout of the dock within the UI editor
#[serde(default)]
pub ui_editor_dock_state: Option<DockState<EditorTabId>>,
#[serde(default)]
pub target_fps: HistoricalOption<u32>,
#[serde(default)]
pub anti_aliasing_mode: AntiAliasingMode,
/// Is the debug menu shown?
///
/// Primarily used internally for testing out features of the editor, however
/// could be useful for certain people.
///
/// This option will not be shown in the editor settings, and can only be edited by file.
#[serde(default)]
pub is_debug_menu_shown: bool,
}
impl EditorSettings {
/// Creates a new instance of [EditorSettings]
pub fn new() -> Self {
Self {
game_editor_dock_state: None,
ui_editor_dock_state: None,
is_debug_menu_shown: false,
target_fps: HistoricalOption::none(),
anti_aliasing_mode: AntiAliasingMode::MSAA4,
}
}
/// Saves the current EditorSettings configuration (as shown in [EDITOR_SETTINGS]) into `{app_dir}/editor.eucc`.
pub fn save(&self) -> anyhow::Result<()> {
log::debug!("Saving the current EditorSettings configuration");
let app_data = app_dirs2::app_root(AppDataType::UserData, &APP_INFO)?;
let serialized = ron::ser::to_string_pretty(&self, ron::ser::PrettyConfig::default())?;
std::fs::write(app_data.join("editor.eucc"), serialized)?;
log::debug!(
"Saved editor config to {}",
app_data.join("editor.eucc").display()
);
Ok(())
}
/// Reads the current EditorSettings configuration from `{app_dir}/editor.eucc` and saves into [EDITOR_SETTINGS]
/// as well as returns the value.
///
/// If the configuration file does not exist, it will create a new configuration and then attempt to read from that.
pub fn read() -> anyhow::Result<Self> {
let app_data = app_dirs2::app_root(AppDataType::UserData, &APP_INFO)?;
let real = match std::fs::read(app_data.join("editor.eucc")) {
Ok(v) => v,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
log::warn!("Unable to read the configuration, overwriting");
{
EDITOR_SETTINGS.read().save()?;
}
std::fs::read(app_data.join("editor.eucc"))?
}
Err(e) => return Err(e.into()),
};
let content: EditorSettings = ron::de::from_reader(real.as_slice())?;
{
let mut temp = EDITOR_SETTINGS.write();
*temp = content.clone();
}
Ok(content)
}
}
impl Default for EditorSettings {
fn default() -> Self {
Self::new()
}
}
pub struct EditorSettingsWindow {
scene_command: SceneCommand,
input_state: InputState,
window: Option<WindowId>,
current_leaf: EditorSettingsCurrentLeaf,
}
#[derive(Default)]
enum EditorSettingsCurrentLeaf {
#[default]
None,
Performance,
}
impl EditorSettingsWindow {
pub fn new() -> Self {
Self {
scene_command: SceneCommand::None,
input_state: Default::default(),
window: None,
current_leaf: Default::default(),
}
}
}
impl Scene for EditorSettingsWindow {
fn load(&mut self, graphics: std::sync::Arc<dropbear_engine::graphics::SharedGraphicsContext>, _ui: &mut Ui,) {
self.window = Some(graphics.window.id());
}
fn physics_update(
&mut self,
_dt: f32,
_graphics: std::sync::Arc<dropbear_engine::graphics::SharedGraphicsContext>,
_ui: &mut Ui,
) {
}
fn update(
&mut self,
_dt: f32,
graphics: std::sync::Arc<dropbear_engine::graphics::SharedGraphicsContext>,
ui: &mut Ui,
) {
CentralPanel::default().show_inside(ui, |ui| {
let mut editor = EDITOR_SETTINGS.write();
Panel::left("editor_settings_tree_panel")
.resizable(true)
.default_size(200.0)
.size_range(150.0..=400.0)
.show_inside(ui, |ui| {
egui::ScrollArea::vertical()
.auto_shrink([false; 2])
.show(ui, |ui| {
let (_resp, action) = egui_ltreeview::TreeView::new(Id::from(
"editor_settings",
))
.show(ui, |builder| {
builder.node(NodeBuilder::leaf("Performance").label("Performance"));
});
for a in action {
match a {
Action::SetSelected(selected) => {
let selected = selected.first().cloned();
if let Some(s) = selected {
match s {
"Performance" => {
self.current_leaf =
EditorSettingsCurrentLeaf::Performance;
}
_ => {
self.current_leaf =
EditorSettingsCurrentLeaf::None;
}
}
}
}
Action::Move(_) => {}
Action::Drag(_) => {}
Action::Activate(_) => {}
Action::DragExternal(_) => {}
Action::MoveExternal(_) => {}
}
}
});
});
egui::CentralPanel::default().show_inside(ui, |ui| {
egui::ScrollArea::vertical()
.auto_shrink([false; 2])
.show(ui, |ui| match self.current_leaf {
EditorSettingsCurrentLeaf::Performance => {
ui.heading("Performance Settings");
ui.separator();
ui.label("Target FPS:");
ui.horizontal(|ui| {
let mut local_set_max_fps = editor.target_fps.is_some();
if ui
.checkbox(
&mut local_set_max_fps,
"Set max frames-per-second (FPS)",
)
.changed()
{
if local_set_max_fps {
editor.target_fps.enable_or(120);
} else {
editor.target_fps.disable();
}
}
if let Some(v) = editor.target_fps.get_mut() {
ui.add(
Slider::new(v, 1..=1000).clamping(SliderClamping::Never),
);
}
});
{
ui.label("Anti-aliasing mode:");
ComboBox::from_id_salt("anti-aliasing-mode-combobox")
.selected_text(match editor.anti_aliasing_mode {
AntiAliasingMode::None => "None",
AntiAliasingMode::MSAA4 => "MSAA4",
})
.show_ui(ui, |ui| {
ui.selectable_value(
&mut editor.anti_aliasing_mode,
AntiAliasingMode::None,
"None",
);
ui.selectable_value(
&mut editor.anti_aliasing_mode,
AntiAliasingMode::MSAA4,
"MSAA4",
);
});
}
}
_ => {}
});
});
});
self.window = Some(graphics.window.id());
}
fn render<'a>(
&mut self,
_graphics: std::sync::Arc<dropbear_engine::graphics::SharedGraphicsContext>,
_ui: &mut Ui,
) {
}
fn exit(&mut self, _event_loop: &ActiveEventLoop) {
if let Err(e) = EDITOR_SETTINGS.read().save() {
warn!("Failed to save editor settings: {:?}", e);
}
}
fn run_command(&mut self) -> SceneCommand {
std::mem::replace(&mut self.scene_command, SceneCommand::None)
}
}
impl Keyboard for EditorSettingsWindow {
fn key_down(&mut self, key: KeyCode, _event_loop: &ActiveEventLoop) {
match key {
KeyCode::Escape => {
if let Some(id) = self.window {
self.scene_command = SceneCommand::CloseWindow(id);
}
}
_ => {
self.input_state.pressed_keys.insert(key);
}
}
self.input_state.pressed_keys.insert(key);
}
fn key_up(&mut self, key: KeyCode, _event_loop: &ActiveEventLoop) {
self.input_state.pressed_keys.remove(&key);
}
}
impl Mouse for EditorSettingsWindow {
fn mouse_move(&mut self, position: PhysicalPosition<f64>, delta: Option<(f64, f64)>) {
self.input_state.last_mouse_pos = Some(<(f64, f64)>::from(position));
self.input_state.mouse_delta = delta;
self.input_state.mouse_pos = (position.x, position.y);
}
fn mouse_down(&mut self, button: MouseButton) {
self.input_state.mouse_button.insert(button);
}
fn mouse_up(&mut self, button: MouseButton) {
self.input_state.mouse_button.remove(&button);
}
}
impl Controller for EditorSettingsWindow {
fn button_down(&mut self, button: Button, id: GamepadId) {
self.input_state
.pressed_buttons
.entry(id)
.or_default()
.insert(button);
}
fn button_up(&mut self, button: Button, id: GamepadId) {
if let Some(buttons) = self.input_state.pressed_buttons.get_mut(&id) {
buttons.remove(&button);
}
}
fn left_stick_changed(&mut self, x: f32, y: f32, id: GamepadId) {
self.input_state.left_stick_position.insert(id, (x, y));
}
fn right_stick_changed(&mut self, x: f32, y: f32, id: GamepadId) {
self.input_state.right_stick_position.insert(id, (x, y));
}
fn on_connect(&mut self, id: GamepadId) {
self.input_state.connected_gamepads.insert(id);
}
fn on_disconnect(&mut self, id: GamepadId) {
self.input_state.connected_gamepads.remove(&id);
self.input_state.pressed_buttons.remove(&id);
self.input_state.left_stick_position.remove(&id);
self.input_state.right_stick_position.remove(&id);
}
}