Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console Log in UI #90

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bevy_editor_pls_default_windows = { version = "0.6.0", path = "crates/bevy_edito
bevy-inspector-egui = "0.21.0"
egui = "0.23"
egui-gizmo = "0.12"
egui_extras = "0.23"

[profile.dev.package."*"]
opt-level = 2
7 changes: 6 additions & 1 deletion crates/bevy_editor_pls/examples/2d_3d_mixed.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use bevy::log::LogPlugin;
use bevy::prelude::*;

fn main() {
console_log::set_module_filter("2d_3d_mixed=trace");

App::new()
.add_plugins(DefaultPlugins)
.add_plugins(DefaultPlugins.build().disable::<LogPlugin>())
.add_plugins(bevy_editor_pls::EditorPlugin::new())
.add_systems(Startup, (setup_2d, setup_3d))
.run();
}

fn setup_2d(mut commands: Commands) {
log::info!("Start Setup Example 2D");
commands.spawn(Camera2dBundle::default());
commands.spawn(SpriteBundle {
sprite: Sprite {
Expand All @@ -25,6 +29,7 @@ fn setup_3d(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
log::info!("Start Setup Example 3D");
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane::from_size(0.5))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_editor_pls/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy::log::LogPlugin;
use bevy::{
diagnostic::{EntityCountDiagnosticsPlugin, FrameTimeDiagnosticsPlugin},
prelude::*,
Expand All @@ -8,16 +9,18 @@ use bevy::{
},
};
use bevy_editor_pls::prelude::*;
use bevy_editor_pls_default_windows::console_log;

fn main() {
console_log::set_module_filter("wgpu_core=off;basic=trace");
// enable wireframe rendering
let mut wgpu_settings = WgpuSettings::default();
wgpu_settings.features |= WgpuFeatures::POLYGON_MODE_LINE;

App::new()
.add_plugins(DefaultPlugins.set(RenderPlugin {
render_creation: RenderCreation::Automatic(wgpu_settings),
}))
}).disable::<LogPlugin>())
.add_plugins((
EditorPlugin::new(),
FrameTimeDiagnosticsPlugin,
Expand All @@ -33,6 +36,7 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
log::info!("Start Setup Example");
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane::from_size(5.0))),
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_editor_pls/examples/breakout.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! A simplified implementation of the classic game "Breakout".

use bevy::log::LogPlugin;
use bevy::{
prelude::*,
sprite::collide_aabb::{collide, Collision},
Expand Down Expand Up @@ -49,8 +50,10 @@ const TEXT_COLOR: Color = Color::rgb(0.5, 0.5, 1.0);
const SCORE_COLOR: Color = Color::rgb(1.0, 0.5, 0.5);

fn main() {
console_log::set_module_filter("breakout=trace");

App::new()
.add_plugins(DefaultPlugins)
.add_plugins(DefaultPlugins.build().disable::<LogPlugin>())
.add_plugins(EditorPlugin::new())
.insert_resource(Scoreboard { score: 0 })
.insert_resource(ClearColor(BACKGROUND_COLOR))
Expand Down Expand Up @@ -175,6 +178,7 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
log::info!("Start Setup Example");
// Camera
commands.spawn(Camera2dBundle::default());

Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_editor_pls/examples/controls.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use bevy::prelude::*;
use bevy::log::LogPlugin;
use bevy_editor_pls::{controls, EditorPlugin};

fn main() {
console_log::set_module_filter("controls=trace");

App::new()
.add_plugins(DefaultPlugins)
.add_plugins(DefaultPlugins.build().disable::<LogPlugin>())
.add_plugins(EditorPlugin::new())
.insert_resource(editor_controls())
.add_systems(Startup, (set_cam3d_controls, setup))
Expand Down Expand Up @@ -41,6 +44,7 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
log::info!("Start Setup Example");
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane::from_size(5.0))),
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_editor_pls/examples/empty.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use bevy::prelude::*;
use bevy::log::LogPlugin;
use bevy_editor_pls::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(DefaultPlugins.build().disable::<LogPlugin>())
.add_plugins(EditorPlugin::new())
.run();
}
6 changes: 5 additions & 1 deletion crates/bevy_editor_pls/examples/load_gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@

use std::f32::consts::*;

use bevy::log::LogPlugin;
use bevy::{
pbr::{CascadeShadowConfigBuilder, DirectionalLightShadowMap},
prelude::*,
};

fn main() {
console_log::set_module_filter("load_gltf=trace");

App::new()
.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 1.0 / 5.0f32,
})
.insert_resource(DirectionalLightShadowMap { size: 4096 })
.add_plugins(DefaultPlugins)
.add_plugins(DefaultPlugins.build().disable::<LogPlugin>())
.add_plugins(bevy_editor_pls::EditorPlugin::new())
.add_systems(Startup, setup)
.add_systems(Update, animate_light_direction)
.run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
log::info!("Start Setup Example");
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(0.7, 0.7, 1.0)
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_editor_pls/examples/separate_window.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use bevy::log::LogPlugin;
use bevy::prelude::*;
use bevy_editor_pls::EditorPlugin;

fn main() {
console_log::set_module_filter("separate_window=trace");

App::new()
.add_plugins(DefaultPlugins)
.add_plugins(DefaultPlugins.build().disable::<LogPlugin>())
// .add_plugin(EditorPlugin::new().in_separate_window())
.add_plugins(EditorPlugin::new().in_new_window(Window::default()))
.add_systems(Startup, setup)
Expand All @@ -16,6 +19,7 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
log::info!("Start Setup Example");
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane::from_size(5.0))),
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_editor_pls/examples/ui.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! This example illustrates the various features of Bevy UI.

use bevy::log::LogPlugin;
use bevy::{
input::mouse::{MouseScrollUnit, MouseWheel},
prelude::*,
Expand All @@ -8,8 +9,10 @@ use bevy::{
use bevy_editor_pls::EditorPlugin;

fn main() {
console_log::set_module_filter("ui=trace");

App::new()
.add_plugins(DefaultPlugins)
.add_plugins(DefaultPlugins.build().disable::<LogPlugin>())
.add_plugins(EditorPlugin::new())
// Only run the app when there is user input. This will significantly reduce CPU/GPU use.
.insert_resource(WinitSettings::desktop_app())
Expand All @@ -19,6 +22,7 @@ fn main() {
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
log::info!("Start Setup Example");
// Camera
commands.spawn(Camera2dBundle::default());

Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_editor_pls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ impl Plugin for EditorPlugin {
use bevy_editor_pls_default_windows::renderer::RendererWindow;
use bevy_editor_pls_default_windows::resources::ResourcesWindow;
use bevy_editor_pls_default_windows::scenes::SceneWindow;
use bevy_editor_pls_default_windows::console_log;

console_log::setup(app);

app.add_editor_window::<HierarchyWindow>();
app.add_editor_window::<AssetsWindow>();
Expand All @@ -125,6 +128,7 @@ impl Plugin for EditorPlugin {
app.add_editor_window::<SceneWindow>();
app.add_editor_window::<GizmoWindow>();
app.add_editor_window::<controls::ControlsWindow>();
app.add_editor_window::<console_log::ConsoleLogWindow>();

app.add_plugins(bevy::pbr::wireframe::WireframePlugin);

Expand All @@ -145,6 +149,7 @@ impl Plugin for EditorPlugin {
std::any::TypeId::of::<AssetsWindow>(),
std::any::TypeId::of::<DebugSettingsWindow>(),
std::any::TypeId::of::<DiagnosticsWindow>(),
std::any::TypeId::of::<console_log::ConsoleLogWindow>(),
],
);
}
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_editor_pls_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub mod editor;
/// Trait definition for new editor windows
pub mod editor_window;

pub mod widgets;

use std::marker::PhantomData;

use bevy::prelude::*;
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_editor_pls_core/src/widgets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod selectable;
73 changes: 73 additions & 0 deletions crates/bevy_editor_pls_core/src/widgets/selectable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#![allow(dead_code)]
use bevy_inspector_egui::egui::{self, Button, Color32, KeyboardShortcut, Layout, Rounding, Vec2};

#[derive(Clone, PartialEq, Eq)]
pub struct Selectable<T: Clone + PartialEq> {
tabs: Vec<(String, T, Option<KeyboardShortcut>)>,
changed: bool,
font_size: u32,
space: u32,
selected_color: Color32,
selected: Option<T>,
}

impl<T> Selectable<T>
where
T: Clone + PartialEq,
{
pub fn new<N: ToString>(
tabs: &[(N, T, Option<KeyboardShortcut>)],
default_selected: T,
font_size: u32,
space: u32,
selected_color: Color32,
) -> Self {
Self {
space,
font_size,
selected_color,
changed: false,
selected: Some(default_selected),
tabs: tabs
.iter()
.map(|t| (t.0.to_string(), t.1.clone(), t.2.clone()))
.collect(),
}
}

pub fn add_tab(&mut self, pos: usize, name: &str, tab: T, shortcut: Option<KeyboardShortcut>) {
self.tabs.insert(pos, (name.to_string(), tab, shortcut));
}

pub fn remove_tab(&mut self, pos: usize) {
self.tabs.remove(pos);
}

pub fn show(&mut self, ui: &mut egui::Ui) -> Option<T> {
ui.horizontal(|ui| {
ui.with_layout(Layout::left_to_right(egui::Align::Center), |ui| {
ui.style_mut().visuals.button_frame = false;
for (tab, d, shortcut) in self.tabs.iter() {
let item = if self.selected.as_ref().is_some_and(|s| s == d) {
Button::new(tab)
.fill(self.selected_color)
.min_size(Vec2::splat(self.font_size as f32))
.rounding(Rounding::ZERO)
} else {
Button::new(tab)
.min_size(Vec2::splat(self.font_size as f32))
.rounding(Rounding::ZERO)
};
let item = ui.add(item);
ui.add_space(self.space as f32);
if item.clicked()
|| shortcut.is_some_and(|s| ui.input_mut(|i| i.consume_shortcut(&s)))
{
self.selected = Some(d.clone());
}
}
});
});
self.selected.clone()
}
}
1 change: 1 addition & 0 deletions crates/bevy_editor_pls_default_windows/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ pretty-type-name = "1.0"
bevy_mod_debugdump = "0.9"
opener = "0.6.0"
egui-gizmo.workspace = true
egui_extras.workspace = true
48 changes: 48 additions & 0 deletions crates/bevy_editor_pls_default_windows/src/console_log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
mod console;
mod logs;

use bevy::prelude::{App, World};
use bevy_editor_pls_core::editor_window::{EditorWindow, EditorWindowContext};
use bevy_inspector_egui::egui::Ui;
use console::*;

pub use logs::*;

pub fn setup(app: &mut App) {
let logger = Logs::default();

app.insert_resource(logger.clone());

log::set_max_level(log::LevelFilter::Trace);

if let Err(e) = log::set_boxed_logger(Box::from(logger)) {
println!("Error setting boxed logger: {e:?}");
}
}

pub struct ConsoleLogState {
pub(crate) filter: log::Level,
}

impl Default for ConsoleLogState {
fn default() -> Self {
Self {
filter: log::Level::Trace,
}
}
}

pub struct ConsoleLogWindow;
impl EditorWindow for ConsoleLogWindow {
type State = ConsoleLogState;
const NAME: &'static str = "Console";

fn ui(world: &mut World, mut ctx: EditorWindowContext, ui: &mut Ui) {
let state_mut = ctx.state_mut::<ConsoleLogWindow>();
let state = state_mut.unwrap();

let logs_res = world.resource::<Logs>();

draw_console_logs(ui, &mut state.filter, logs_res);
}
}
Loading