rewin123 / space_editor

bevy prefab editor
MIT License
386 stars 49 forks source link

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

Closed rewin123 closed 7 months ago

rewin123 commented 7 months ago

Part of #267

This PR allows you to:

Major changes:

trait TabName: Debug + Any {
    fn clear_background(&self) -> bool;
    fn title(&self) -> String;
}
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);

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

/// We can push showed tab on front of list if we want app.layout_pushfront::<DoublePanelGroup, , _>( DoublePanel::MainPanel, EditorTabName::Resource, );


- Added useful example about how add new editor tabs
```rust
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:

codecov[bot] commented 7 months ago

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 :warning:
crates/editor_tabs/src/lib.rs 0.00% 23 Missing :warning:
crates/editor_ui/src/editor_tab_name.rs 0.00% 17 Missing :warning:
crates/editor_tabs/src/tab_viewer.rs 0.00% 8 Missing :warning:
crates/editor_ui/src/ui_plugin.rs 0.00% 8 Missing :warning:
crates/editor_ui/src/inspector/mod.rs 0.00% 6 Missing :warning:
crates/editor_ui/src/settings.rs 0.00% 6 Missing :warning:
crates/editor_tabs/src/tab_name.rs 88.00% 3 Missing :warning:
crates/editor_ui/src/camera_view.rs 0.00% 3 Missing :warning:
crates/editor_ui/src/change_chain.rs 0.00% 3 Missing :warning:
... and 7 more
Additional details and impacted files ```diff @@ 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 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

rewin123 commented 7 months ago

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

naomijub commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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