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

Type agnostic tab name and easy startup layout modification for editor_tabs #272

Merged
merged 16 commits into from
Mar 26, 2024

Conversation

rewin123
Copy link
Owner

@rewin123 rewin123 commented Mar 26, 2024

Part of #267

This PR allows you to:

  • use any type to set the name of the editor panel.
  • allows to support several types specifying the name at the same time
  • It is more convenient to specify the panels displayed when the editor is launched

Major changes:

  • EditorTabName is no longer part of editor_tabs, but part of editor_ui
  • Introduced the TabName trait, which should be released by the types that are used to name editor panels
trait TabName: Debug + Any {
    fn clear_background(&self) -> bool;
    fn title(&self) -> String;
}
  • EditorTabName now implement TabName trait
  • The api for registering new tabs has been changed accordingly
Was
app.editor_tab_by_trait(EditorTabName::GameView, GameViewTab::default());
Became
app.editor_tab_by_trait(GameViewTab::default());
Was
app.editor_tab(EditorTabName::Hierarchy, "Hierarchy".into(), show_hierarchy);
Bacame
app.editor_tab(EditorTabName::Hierarchy, show_hierarchy);
  • Added api for setting the start layout of the editor
    Usage:
/// Create layout group, which hide hard logic of egui_dock tree
app.init_layout_group::<DoublePanelGroup, _>();

/// Push tabs to group of layout
app.layout_push::<DoublePanelGroup, _, _>(DoublePanel::MainPanel, EditorTabName::GameView);
app.layout_push::<DoublePanelGroup, _, _>(DoublePanel::TopPanel, EditorTabName::Hierarchy);
app.layout_push::<DoublePanelGroup, _, _>(
    DoublePanel::BottomPanel,
    EditorTabName::Inspector,
);

/// We can push showed tab on front of list if we want
app.layout_push_front::<DoublePanelGroup, _, _>(
    DoublePanel::MainPanel,
    EditorTabName::Resource,
);
  • Added useful example about how add new editor tabs
use bevy::prelude::*;
use space_editor::prelude::*;

/// This example shows how to create custom editor tabs
/// space_editor allows to create tabs by implementing trait EditorTab
/// or by using system based tabs

fn main() {
    App::default()
        .add_plugins(DefaultPlugins)
        .add_plugins(SpaceEditorPlugin)
        .add_systems(Startup, simple_editor_setup)
        // Add trait based tab
        .editor_tab_by_trait(TraitEditorTab)
        // Add system based tab
        .editor_tab(CustomTabName::SystemBased, system_tab)
        // Add trait based tab as first tab in Bottom panel
        .layout_push_front::<DoublePanelGroup, _, _>(
            DoublePanel::BottomPanel,
            CustomTabName::TraitBased,
        )
        // Add system based tab as first tab in Main panel
        .layout_push_front::<DoublePanelGroup, _, _>(
            DoublePanel::MainPanel,
            CustomTabName::SystemBased,
        )
        .run();
}

#[derive(Debug)]
/// A custom tab name
enum CustomTabName {
    /// A trait based tab
    TraitBased,
    /// A system based tab
    SystemBased,
}

impl TabName for CustomTabName {
    /// Set clear_background to true to clear background of the panel
    fn clear_background(&self) -> bool {
        true
    }

    /// Return title of the tab
    fn title(&self) -> String {
        match self {
            CustomTabName::TraitBased => String::from("Trait Based"),
            CustomTabName::SystemBased => String::from("System Based"),
        }
    }
}

#[derive(Resource)]
/// A struct that implements EditorTab trait
/// which allows to create custom tabs in the editor
struct TraitEditorTab;

impl EditorTab for TraitEditorTab {
    /// This function is called when tab needs to be rendered
    /// ui is bevy_egui::egui::Ui and it allows to build ui inside the tab
    fn ui(
        &mut self,
        ui: &mut space_prefab::prelude::ext::bevy_inspector_egui::egui::Ui,
        _commands: &mut Commands,
        _world: &mut World,
    ) {
        ui.label("Trait Based");
    }

    /// Return name of the tab
    fn tab_name(&self) -> TabNameHolder {
        CustomTabName::TraitBased.into()
    }
}

/// This function is a system that will be called every frame and will construct tab ui
fn system_tab(mut commands: Commands, mut ui: NonSendMut<EditorUiRef>) {
    let ui = &mut ui.0;

    ui.label("System Based");

    // If button is clicked spawn a new entity with
    // SpatialBundle and Name components
    if ui.button("Add").clicked() {
        commands.spawn((
            SpatialBundle::default(),
            PrefabMarker,
            Name::new("New Entity".to_string()),
        ));
    }
}

Next steps in next PRs:

  • Move colors and sizing modules out of editor_tabs crate

Copy link

codecov bot commented Mar 26, 2024

Codecov Report

Attention: Patch coverage is 13.58025% with 140 lines in your changes are missing coverage. Please review.

Project coverage is 34.57%. Comparing base (96e8551) to head (353b413).
Report is 3 commits behind head on v0.6.

Files Patch % Lines
crates/editor_tabs/src/start_layout.rs 0.00% 49 Missing ⚠️
crates/editor_tabs/src/lib.rs 0.00% 23 Missing ⚠️
crates/editor_ui/src/editor_tab_name.rs 0.00% 17 Missing ⚠️
crates/editor_tabs/src/tab_viewer.rs 0.00% 8 Missing ⚠️
crates/editor_ui/src/ui_plugin.rs 0.00% 8 Missing ⚠️
crates/editor_ui/src/inspector/mod.rs 0.00% 6 Missing ⚠️
crates/editor_ui/src/settings.rs 0.00% 6 Missing ⚠️
crates/editor_tabs/src/tab_name.rs 88.00% 3 Missing ⚠️
crates/editor_ui/src/camera_view.rs 0.00% 3 Missing ⚠️
crates/editor_ui/src/change_chain.rs 0.00% 3 Missing ⚠️
... and 7 more
Additional details and impacted files
@@            Coverage Diff             @@
##             v0.6     #272      +/-   ##
==========================================
- Coverage   34.78%   34.57%   -0.21%     
==========================================
  Files          62       67       +5     
  Lines        9301     9418     +117     
==========================================
+ Hits         3235     3256      +21     
- Misses       6066     6162      +96     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rewin123
Copy link
Owner Author

image
Test without features CI failed with this error, weird.....

@naomijub
Copy link
Collaborator

image Test without features CI failed with this error, weird.....

Might be a linker problem on CI. Maybe we could run CI on windows or be sure to have it correctly setup for linux https://github.com/bevyengine/bevy/blob/main/docs/linux_dependencies.md

But I see this is passing now

@naomijub
Copy link
Collaborator

I would always recommend adding UI screenshots when changing stuff that touch the UI, besides that, this is a pretty good change and clearly documented.
Nice work

@rewin123
Copy link
Owner Author

rewin123 commented Mar 26, 2024

Might be a linker problem on CI. Maybe we could run CI on windows or be sure to have it correctly setup for linux https://github.com/bevyengine/bevy/blob/main/docs/linux_dependencies.md

Sometimes space_editor folder fills 20-30 gb of my disk space for debug target. So, I switched to release mode to try to fix this

@rewin123
Copy link
Owner Author

I would always recommend adding UI screenshots when changing stuff that touch the UI, besides that, this is a pretty good change and clearly documented. Nice work

UI shouldn't have changed, just the api

@rewin123 rewin123 merged commit 553314c into v0.6 Mar 26, 2024
6 of 8 checks passed
}

pub enum DoublePanel {
TopPanel,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call them TopLeftPanel and BottomLeftPanel? first time I read them I felt confused with the menutoolbars

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe then call TopLeft and BottomLeft? I love reducing words count

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants