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

Move colors and sizing modules out of space_editor_tabs #274

Merged
merged 4 commits into from
Mar 27, 2024
Merged
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
71 changes: 33 additions & 38 deletions crates/editor_tabs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,31 @@
pub mod colors;
/// This library contains dock-tab implementation for Space Editor
pub mod editor_tab;
pub mod schedule_editor_tab;
pub mod sizing;
pub mod start_layout;
pub mod tab_name;
pub mod tab_style;
pub mod tab_viewer;

use std::fmt::Display;

use bevy::{ecs::system::CommandQueue, prelude::*, utils::HashMap, window::PrimaryWindow};

use bevy_egui::egui::FontFamily::{Monospace, Proportional};
use bevy_egui::{
egui::{self, FontId, Rounding},
EguiContext,
};
use bevy_egui::{egui, EguiContext};

use colors::*;
use editor_tab::*;
use egui_dock::DockArea;
use schedule_editor_tab::*;
use sizing::{to_label, Sizing};
use start_layout::StartLayout;
use tab_name::{TabName, TabNameHolder};
use tab_style::*;
use tab_viewer::*;

use bevy_egui::egui::TextStyle as ETextStyle;

pub mod prelude {
pub use super::colors::*;
pub use super::editor_tab::*;
pub use super::schedule_editor_tab::*;
pub use super::sizing::*;
pub use super::start_layout::*;
pub use super::tab_name::*;
pub use super::tab_style::*;
pub use super::tab_viewer::*;

pub use super::{
Expand All @@ -55,29 +46,15 @@
let mut egui_context = egui_context.clone();
let ctx = egui_context.get_mut();
egui_extras::install_image_loaders(ctx);
ctx.style_mut(|stl| {
stl.spacing.button_padding = bevy_egui::egui::Vec2::new(8., 2.);
stl.spacing.icon_spacing = 4.;
stl.spacing.icon_width = 16.;
stl.spacing.menu_margin = bevy_egui::egui::Margin {
left: 8.,
right: 8.,
top: 4.,
bottom: 8.,
};
stl.visuals.error_fg_color = ERROR_COLOR;
stl.visuals.hyperlink_color = HYPERLINK_COLOR;
stl.visuals.warn_fg_color = WARM_COLOR;
stl.visuals.menu_rounding = Rounding::same(0.5);
stl.text_styles = [
(ETextStyle::Small, FontId::new(10.0, Proportional)),
(ETextStyle::Body, FontId::new(12., Proportional)),
(ETextStyle::Button, FontId::new(14., Proportional)),
(ETextStyle::Heading, FontId::new(20.0, Proportional)),
(ETextStyle::Monospace, FontId::new(12.0, Monospace)),
]
.into()
});

{
// set style for editor
let editor_ui = world.get_resource::<EditorUi>().unwrap();
let tab_style = (editor_ui.style_getter)(world);
ctx.style_mut(|stl| {
tab_style.set_egui_style(world, stl);
});
}

Check warning on line 57 in crates/editor_tabs/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L49-L57

Added lines #L49 - L57 were not covered by tests

world.resource_scope::<EditorUi, _>(|world, mut editor_ui| {
editor_ui.ui(world, ctx);
Expand All @@ -89,18 +66,24 @@
pub struct EditorUi {
pub registry: HashMap<TabNameHolder, EditorUiReg>,
pub tree: egui_dock::DockState<TabNameHolder>,
pub style_getter: fn(&World) -> &dyn TabStyle,
}

impl Default for EditorUi {
fn default() -> Self {
Self {
registry: HashMap::default(),
tree: egui_dock::DockState::new(vec![]),
style_getter: |_| &DEFAULT_STYLE,

Check warning on line 77 in crates/editor_tabs/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L77

Added line #L77 was not covered by tests
}
}
}

impl EditorUi {
pub fn set_style<T: TabStyle>(&mut self) {
self.style_getter = |world: &World| world.resource::<T>();
}

Check warning on line 85 in crates/editor_tabs/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L83-L85

Added lines #L83 - L85 were not covered by tests

pub fn set_layout<T: StartLayout>(&mut self, layout: &T) {
self.tree = layout.build();
}
Expand Down Expand Up @@ -129,6 +112,12 @@
}
}

let collected_style = {
let editor = world.resource::<Self>();
let editor_style = (editor.style_getter)(world);
editor_style.collect_style(world)
};

Check warning on line 120 in crates/editor_tabs/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L115-L120

Added lines #L115 - L120 were not covered by tests
let cell = world.as_unsafe_world_cell();

let mut command_queue = CommandQueue::default();
Expand All @@ -141,6 +130,7 @@
registry: &mut self.registry,
visible,
tab_commands: vec![],
style: collected_style,

Check warning on line 133 in crates/editor_tabs/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L133

Added line #L133 was not covered by tests
}
};

Expand Down Expand Up @@ -218,10 +208,15 @@
let reg = EditorUiReg::ResourceBased {
show_command: show_fn,
title_command: Box::new(|world| {
let sizing = world.resource::<Sizing>().clone();
let text_size = {
let editor = world.resource::<EditorUi>();
let editor_style = (editor.style_getter)(world);
editor_style.text_size(world)
};

Check warning on line 216 in crates/editor_tabs/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L211-L216

Added lines #L211 - L216 were not covered by tests
to_label(
world.resource_mut::<T>().tab_name().title.as_str(),
sizing.text,
text_size,

Check warning on line 219 in crates/editor_tabs/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L219

Added line #L219 was not covered by tests
)
.into()
}),
Expand Down
18 changes: 9 additions & 9 deletions crates/editor_tabs/src/start_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
}

pub enum DoublePanel {
TopPanel,
BottomPanel,
MainPanel,
TopLeft,
BottomLeft,
Main,
}

#[derive(Default, Resource)]
Expand Down Expand Up @@ -51,17 +51,17 @@
impl GroupLayout<DoublePanel> for DoublePanelGroup {
fn push<N: TabName>(&mut self, group: DoublePanel, tab: N) {
match group {
DoublePanel::TopPanel => self.top_panel.push(tab.into()),
DoublePanel::BottomPanel => self.bottom_panel.push(tab.into()),
DoublePanel::MainPanel => self.main_panel.push(tab.into()),
DoublePanel::TopLeft => self.top_panel.push(tab.into()),
DoublePanel::BottomLeft => self.bottom_panel.push(tab.into()),
DoublePanel::Main => self.main_panel.push(tab.into()),

Check warning on line 56 in crates/editor_tabs/src/start_layout.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/start_layout.rs#L54-L56

Added lines #L54 - L56 were not covered by tests
}
}

fn push_front<N: TabName>(&mut self, group: DoublePanel, tab: N) {
match group {
DoublePanel::TopPanel => self.top_panel.insert(0, tab.into()),
DoublePanel::BottomPanel => self.bottom_panel.insert(0, tab.into()),
DoublePanel::MainPanel => self.main_panel.insert(0, tab.into()),
DoublePanel::TopLeft => self.top_panel.insert(0, tab.into()),
DoublePanel::BottomLeft => self.bottom_panel.insert(0, tab.into()),
DoublePanel::Main => self.main_panel.insert(0, tab.into()),

Check warning on line 64 in crates/editor_tabs/src/start_layout.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/start_layout.rs#L62-L64

Added lines #L62 - L64 were not covered by tests
}
}
}
Expand Down
46 changes: 46 additions & 0 deletions crates/editor_tabs/src/tab_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use bevy::prelude::*;
use bevy_egui::egui::{self, RichText};

pub const DEFAULT_STYLE: DefaultStyle = DefaultStyle;

/// This trait use to get tab style
pub trait TabStyle: Resource {
fn error_color(&self) -> egui::Color32;
fn set_egui_style(&self, world: &World, style: &mut egui::Style);
fn text_size(&self, world: &World) -> f32;

fn collect_style(&self, world: &World) -> CollectedStyle {
let error_color = self.error_color();
let text_size = self.text_size(world);
CollectedStyle {
error_color,
text_size,
}
}

Check warning on line 19 in crates/editor_tabs/src/tab_style.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/tab_style.rs#L12-L19

Added lines #L12 - L19 were not covered by tests
}

pub struct CollectedStyle {
pub error_color: egui::Color32,
pub text_size: f32,
}

pub fn to_label(text: &str, size: f32) -> RichText {
RichText::new(text)
.size(size)
.family(egui_dock::egui::FontFamily::Proportional)
}

Check warning on line 31 in crates/editor_tabs/src/tab_style.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/tab_style.rs#L27-L31

Added lines #L27 - L31 were not covered by tests

#[derive(Default, Resource)]
pub struct DefaultStyle;

impl TabStyle for DefaultStyle {
fn error_color(&self) -> egui::Color32 {
egui::Color32::RED
}

Check warning on line 39 in crates/editor_tabs/src/tab_style.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/tab_style.rs#L37-L39

Added lines #L37 - L39 were not covered by tests

fn set_egui_style(&self, _world: &World, _style: &mut egui::Style) {}

Check warning on line 41 in crates/editor_tabs/src/tab_style.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/tab_style.rs#L41

Added line #L41 was not covered by tests

fn text_size(&self, _world: &World) -> f32 {
14.0
}

Check warning on line 45 in crates/editor_tabs/src/tab_style.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/tab_style.rs#L43-L45

Added lines #L43 - L45 were not covered by tests
}
21 changes: 11 additions & 10 deletions crates/editor_tabs/src/tab_viewer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{
prelude::{to_label, Sizing},
schedule_editor_tab::ScheduleEditorTabStorage,
tab_name::TabNameHolder,
EditorTab, EditorUiReg, ERROR_COLOR,
schedule_editor_tab::ScheduleEditorTabStorage, tab_name::TabNameHolder, to_label,
CollectedStyle, EditorTab, EditorUiReg,
};
use bevy::{prelude::*, utils::HashMap};
use bevy_egui::egui;
Expand All @@ -22,6 +20,7 @@
pub registry: &'a mut HashMap<TabNameHolder, EditorUiReg>,
pub visible: Vec<TabNameHolder>,
pub tab_commands: Vec<EditorTabCommand>,
pub style: CollectedStyle,
}

impl<'a, 'w, 's> egui_dock::TabViewer for EditorTabViewer<'a, 'w, 's> {
Expand All @@ -42,19 +41,21 @@
if let Some(tab) = storage.0.get_mut(tab_name) {
tab.ui(ui, self.commands, world);
} else {
ui.colored_label(ERROR_COLOR, "Not implemented schedule tab");
ui.colored_label(
self.style.error_color,
"Not implemented schedule tab",
);

Check warning on line 47 in crates/editor_tabs/src/tab_viewer.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/tab_viewer.rs#L44-L47

Added lines #L44 - L47 were not covered by tests
}
},
);
}
}
} else {
ui.colored_label(ERROR_COLOR, "Not implemented panel");
ui.colored_label(self.style.error_color, "Not implemented panel");

Check warning on line 54 in crates/editor_tabs/src/tab_viewer.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/tab_viewer.rs#L54

Added line #L54 was not covered by tests
}
}

fn title(&mut self, tab: &mut Self::Tab) -> egui::WidgetText {
let sizing = self.world.resource::<Sizing>().clone();
if let Some(reg) = self.registry.get(tab) {
match reg {
EditorUiReg::ResourceBased {
Expand All @@ -67,12 +68,12 @@
.0
.get(tab)
.map_or_else(
|| to_label(&format!("{tab:?}"), sizing.text).into(),
|tab| to_label(&tab.tab_name.title, sizing.text).into(),
|| to_label(&format!("{tab:?}"), self.style.text_size).into(),
|tab| to_label(&tab.tab_name.title, self.style.text_size).into(),

Check warning on line 72 in crates/editor_tabs/src/tab_viewer.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/tab_viewer.rs#L71-L72

Added lines #L71 - L72 were not covered by tests
),
}
} else {
to_label(&format!("{tab:?}"), sizing.text).into()
to_label(&format!("{tab:?}"), self.style.text_size).into()

Check warning on line 76 in crates/editor_tabs/src/tab_viewer.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/tab_viewer.rs#L76

Added line #L76 was not covered by tests
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/editor_ui/src/camera_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use crate::{

use space_editor_tabs::prelude::*;

use crate::colors::*;

pub struct CameraViewTabPlugin;

impl Plugin for CameraViewTabPlugin {
Expand Down
File renamed without changes.
43 changes: 43 additions & 0 deletions crates/editor_ui/src/editor_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use bevy::prelude::*;
use space_editor_tabs::prelude::*;

use crate::{colors::*, sizing::Sizing};
use bevy_egui::egui::FontFamily::{Monospace, Proportional};
use bevy_egui::egui::{FontId, Rounding, TextStyle as ETextStyle};

#[derive(Resource, Default)]
pub struct EditorStyle {}

impl TabStyle for EditorStyle {
fn error_color(&self) -> bevy_egui::egui::Color32 {
ERROR_COLOR
}

Check warning on line 14 in crates/editor_ui/src/editor_style.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_ui/src/editor_style.rs#L12-L14

Added lines #L12 - L14 were not covered by tests

fn set_egui_style(&self, _: &World, stl: &mut bevy_egui::egui::Style) {
stl.spacing.button_padding = bevy_egui::egui::Vec2::new(8., 2.);
stl.spacing.icon_spacing = 4.;
stl.spacing.icon_width = 16.;
stl.spacing.menu_margin = bevy_egui::egui::Margin {
left: 8.,
right: 8.,
top: 4.,
bottom: 8.,
};
stl.visuals.error_fg_color = ERROR_COLOR;
stl.visuals.hyperlink_color = HYPERLINK_COLOR;
stl.visuals.warn_fg_color = WARM_COLOR;
stl.visuals.menu_rounding = Rounding::same(0.5);
stl.text_styles = [
(ETextStyle::Small, FontId::new(10.0, Proportional)),
(ETextStyle::Body, FontId::new(12., Proportional)),
(ETextStyle::Button, FontId::new(14., Proportional)),
(ETextStyle::Heading, FontId::new(20.0, Proportional)),
(ETextStyle::Monospace, FontId::new(12.0, Monospace)),
]
.into()
}

Check warning on line 38 in crates/editor_ui/src/editor_style.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_ui/src/editor_style.rs#L16-L38

Added lines #L16 - L38 were not covered by tests

fn text_size(&self, world: &World) -> f32 {
world.resource::<Sizing>().text
}

Check warning on line 42 in crates/editor_ui/src/editor_style.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_ui/src/editor_style.rs#L40-L42

Added lines #L40 - L42 were not covered by tests
}
2 changes: 2 additions & 0 deletions crates/editor_ui/src/game_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::editor_tab_name::EditorTabName;
use super::tool::EditorTool;
use space_editor_tabs::prelude::*;

use crate::colors::*;

pub struct GameViewPlugin;

impl Plugin for GameViewPlugin {
Expand Down
1 change: 1 addition & 0 deletions crates/editor_ui/src/inspector/events_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bevy::{prelude::*, utils::HashMap};

use bevy_egui::*;

use crate::colors::*;
use crate::prelude::*;

#[derive(Resource, Default)]
Expand Down
1 change: 1 addition & 0 deletions crates/editor_ui/src/inspector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use self::{
resources::ResourceTab,
runtime_assets::RuntimeAssetsTab,
};
use crate::{colors::*, sizing::Sizing};

/// Entities with this marker will be skipped in inspector
#[derive(Component)]
Expand Down
5 changes: 5 additions & 0 deletions crates/editor_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ pub mod selection;
/// Editor tab name
pub mod editor_tab_name;

pub mod colors;
/// This module contains editor style definitions
pub mod editor_style;
pub mod sizing;

pub mod icons;

use bevy_debug_grid::{Grid, GridAxis, SubGrid, TrackedGrid, DEFAULT_GRID_ALPHA};
Expand Down
3 changes: 3 additions & 0 deletions crates/editor_ui/src/menu_toolbars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ use space_undo::{AddedEntity, NewChange, RemovedEntity};
use crate::{
hierarchy::{HierarchyQueryIter, HierarchyTabState},
icons::{add_bundle_icon, add_entity_icon, delete_entity_icon, prefab_icon},
sizing::{to_colored_richtext, to_richtext},
ui_registration::{BundleReg, EditorBundleUntyped},
ShowEditorUi,
};

use crate::{colors::*, sizing::Sizing};

/// Plugin to activate bottom menu in editor UI
pub struct BottomMenuPlugin;

Expand Down
Loading
Loading