Skip to content

Commit

Permalink
UI Revamp
Browse files Browse the repository at this point in the history
  • Loading branch information
doonv committed Dec 28, 2023
1 parent 1ef7f66 commit e237858
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 185 deletions.
92 changes: 83 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
//! Configuration structs for the developer console.
use bevy::prelude::*;
use bevy_egui::egui::FontId;
use bevy::{log::Level, prelude::*};
use bevy_egui::egui::{self, Color32, FontId, TextFormat};

/// The configuration of the developer console.
#[derive(Resource)]
pub struct ConsoleConfig {
/// The colors used in the developer console.
pub colors: ConsoleColors,
pub theme: ConsoleTheme,
/// The key used to open the developer console.
pub open_key: KeyCode,
/// The font used in the developer console.
pub font: FontId,
}

impl Default for ConsoleConfig {
fn default() -> Self {
Self {
colors: ConsoleColors::ONE_DARK,
theme: ConsoleTheme::ONE_DARK,
open_key: KeyCode::Grave,
font: FontId::monospace(14.0),
}
}
}

/// The colors used by the text in the developer console.
pub struct ConsoleColors {
pub struct ConsoleTheme {
/// The font used in the developer console.
pub font: FontId,
pub dark: Color,
pub text_color: Color,
/// The color of the "error" level.
///
/// Designates very serious errors.
Expand All @@ -48,13 +49,86 @@ pub struct ConsoleColors {
pub trace: Color,
}

impl ConsoleColors {
/// Helper trait that allows conversion between [`bevy::Color`](Color) and [`egui::Color32`].
pub trait ToColor32 {
/// Convert this [`bevy::Color`](Color) to a [`egui::Color32`].
fn to_color32(&self) -> Color32;
}
impl ToColor32 for Color {
fn to_color32(&self) -> Color32 {
let [r, g, b, a] = self.as_rgba_u8();

Color32::from_rgba_unmultiplied(r, g, b, a)
}
}

macro_rules! define_text_format_method {
($name:ident, $color:ident) => {
#[doc = concat!("Returns a [`TextFormat`] colored with [`Self::", stringify!($color), "`]")]
pub fn $name(&self) -> TextFormat {
TextFormat {
color: self.$color.to_color32(),
..self.format_text()
}
}
};
}

impl ConsoleTheme {
/// Atom's iconic One Dark theme.
pub const ONE_DARK: Self = Self {
font: FontId::monospace(14.0),
dark: Color::rgb(0.42, 0.44, 0.48),
text_color: Color::rgb(0.67, 0.7, 0.75),
error: Color::rgb(0.91, 0.46, 0.5),
warning: Color::rgb(0.82, 0.56, 0.32),
info: Color::rgb(0.55, 0.76, 0.4),
debug: Color::rgb(0.29, 0.65, 0.94),
trace: Color::rgb(0.78, 0.45, 0.89),
};
/// High constrast theme, might help some people.
pub const HIGH_CONTRAST: Self = Self {
font: FontId::monospace(14.0),
dark: Color::rgb(0.5, 0.5, 0.5),
text_color: Color::rgb(1.0, 1.0, 1.0),
error: Color::rgb(1.0, 0.0, 0.0),
warning: Color::rgb(1.0, 1.0, 0.0),
info: Color::rgb(0.0, 1.0, 0.0),
debug: Color::rgb(0.25, 0.25, 1.0),
trace: Color::rgb(1.0, 0.0, 1.0),
};

/// Returns a [`Color32`] based on the `level`
pub fn color_level(&self, level: Level) -> Color32 {
match level {
Level::ERROR => self.error.to_color32(),
Level::WARN => self.warning.to_color32(),
Level::INFO => self.info.to_color32(),
Level::DEBUG => self.debug.to_color32(),
Level::TRACE => self.trace.to_color32(),
}
}

pub fn format_level(&self, level: Level) -> TextFormat {
TextFormat {
color: self.color_level(level),
..self.format_text()
}
}

pub fn format_text(&self) -> TextFormat {
TextFormat {
font_id: self.font.clone(),
color: self.text_color.to_color32(),

..default()
}
}

define_text_format_method!(format_dark, dark);
define_text_format_method!(format_error, error);
define_text_format_method!(format_warning, warning);
define_text_format_method!(format_info, info);
define_text_format_method!(format_debug, debug);
define_text_format_method!(format_trace, trace);
}
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ impl Plugin for DevConsolePlugin {
app.init_resource::<ConsoleUiState>()
.init_resource::<CommandHints>()
.init_resource::<ConsoleConfig>()
.add_systems(Update, ui::ui);
.add_systems(
Update,
(
ui::read_logs,
ui::open_close_ui,
ui::render_ui.run_if(|s: Res<ConsoleUiState>| s.open),
),
);
}
}
Loading

0 comments on commit e237858

Please sign in to comment.