Skip to content

Commit

Permalink
improve ui
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-tennert committed Jan 8, 2024
1 parent cfeafce commit 7a9a6bc
Show file tree
Hide file tree
Showing 6 changed files with 320 additions and 256 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ edition = "2021"
opt-level = 1

[profile.dev.package."*"]
opt-level = 3
opt-level = 3

[profile.release]
opt-level = 's'
lto = "thin"

[build-dependencies]
embed-resource = "1.6.3"
Expand Down
2 changes: 1 addition & 1 deletion assets/bodies.sim

Large diffs are not rendered by default.

66 changes: 54 additions & 12 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy::{prelude::{App, in_state, Input, IntoSystemConfigs, KeyCode, Plugin, Query, Res, ResMut, Update, Vec3}, window::{Window, WindowMode}};
use bevy_egui::{egui::{self}, EguiContexts, EguiSettings};

use crate::{camera::PanOrbitCamera, SimState, ui::{UiState, StepType}, physics::{Pause, SubSteps}, speed::Speed};

Expand All @@ -8,19 +9,64 @@ impl Plugin for InputPlugin {

fn build(&self, app: &mut App) {
app
.add_systems(Update, input_system.run_if(in_state(SimState::Simulation)));
.add_systems(Update, global_input_system)
.add_systems(Update, key_window.run_if(in_state(SimState::Simulation)))
.add_systems(Update, sim_input_system.run_if(in_state(SimState::Simulation)));
}

}

fn input_system(
fn key_window(
mut egui_ctx: EguiContexts,
mut ui_state: ResMut<UiState>,
) {
if !ui_state.visible {
return;
}
egui::Window::new("Keybind Information")
.open(&mut ui_state.show_keys)
.collapsible(true)
.constrain(true)
.scroll2([true, true])
.default_width(250.0)
.show(egui_ctx.ctx_mut(), |ui| {
ui.label("F11 - Toggle Fullscreen");
ui.label("F10 - Hide Ui");
ui.label("Space - Pause");
ui.label("Left Arrow - 2x Speed");
ui.label("Right Arrow - 1/2 Speed");
ui.label("Left Alt - Change Step Type");
ui.label("C - Reset Camera");
ui.label("Left Mouse - Rotate Camera");
ui.label("Right Mouse - Move Camera");
ui.label("Ctrl + , - Increase Ui Scale");
ui.label("Ctrl + . - Decrease Ui Scale");
});
}

fn global_input_system(
keys: Res<Input<KeyCode>>,
mut windows: Query<&mut Window>,
) {
if keys.just_pressed(KeyCode::F11) {
let mut window = windows.single_mut();
let current = window.mode;
if current == WindowMode::Windowed {
window.mode = WindowMode::BorderlessFullscreen;
} else {
window.mode = WindowMode::Windowed;
}
}
}

fn sim_input_system(
keys: Res<Input<KeyCode>>,
mut ui_state: ResMut<UiState>,
mut camera: Query<&mut PanOrbitCamera>,
mut windows: Query<&mut Window>,
mut pause: ResMut<Pause>,
mut speed: ResMut<Speed>,
mut sub_steps: ResMut<SubSteps>
mut sub_steps: ResMut<SubSteps>,
mut egui_settings: ResMut<EguiSettings>,
) {
let timestep_selected = match ui_state.step_type {
StepType::SUBSTEPS => false,
Expand All @@ -30,14 +76,6 @@ fn input_system(
ui_state.visible = !ui_state.visible
} else if keys.just_pressed(KeyCode::C) {
camera.single_mut().focus = Vec3::ZERO;
} else if keys.just_pressed(KeyCode::F11) {
let mut window = windows.single_mut();
let current = window.mode;
if current == WindowMode::Windowed {
window.mode = WindowMode::BorderlessFullscreen;
} else {
window.mode = WindowMode::Windowed;
}
} else if keys.just_pressed(KeyCode::Space) {
pause.0 = !pause.0;
} else if keys.just_pressed(KeyCode::Left) {
Expand All @@ -58,5 +96,9 @@ fn input_system(
} else {
ui_state.step_type = StepType::TIMESTEPS;
}
} else if keys.pressed(KeyCode::ControlLeft) && keys.just_pressed(KeyCode::Comma) {
egui_settings.scale_factor *= 1.1;
} else if keys.pressed(KeyCode::ControlLeft) && keys.just_pressed(KeyCode::Period) {
egui_settings.scale_factor *= 0.9;
}
}
Loading

0 comments on commit 7a9a6bc

Please sign in to comment.