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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- name: Install Dependencies
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libwayland-dev
- name: Run cargo test
run: cargo test --workspace --no-default-features
run: cargo test --workspace --no-default-features --release

test_all:
name: Test all features
Expand All @@ -72,7 +72,7 @@ jobs:
- name: Install Dependencies
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libwayland-dev
- name: Run cargo test
run: cargo test --workspace --all-features
run: cargo test --workspace --all-features --release

# Run cargo clippy -- -D warnings
clippy_check:
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repository = "https://github.com/rewin123/space_editor"
members = [
"crates/*",
"modules/bevy_xpbd_plugin"
, "crates/editor_tabs"]
]

[workspace.package]
version = "0.6.0"
Expand All @@ -29,7 +29,7 @@ repository = "https://github.com/rewin123/space_editor"
homepage = "https://github.com/rewin123/space_editor"

[workspace.dependencies]
bevy = "0.13"
bevy = "0.13.1"

# Editor Crates
space_prefab = { version = "0.6.0", path = "crates/prefab" }
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,15 @@ Any pull request is welcome too:)
- PR to main: Bug Fixes and Tests
- New features and others: next version branch **(ex: last released is 0.5, so create PRs for branch v0.6)**

### Branch Policy
* **v0.x Branches:** These branches house versions of the editor that are actively being developed. Currently, the primary focus is on branch v0.6.
* **Main Branch:** The main branch exclusively hosts stable versions of the space_editor without any known bugs. Updates to this branch are limited to bug fixes, documentation improvements, or merging in the v0.x branch once all identified issues have been resolved, and the version is considered complete.

### License
MIT - https://choosealicense.com/licenses/mit/


### Project naming

I'm using the editor to create my own Sci-Fi space game, so the name of the project starts with space_ :)
Space_editor started as part of my prototype space game, which I feel could be useful in development, so I thought I'd share my inbuilt editor. Since the game is about space and the name of the editor starts with space_*:)

18 changes: 2 additions & 16 deletions crates/editor_tabs/src/editor_tab.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::NewTabBehaviour;
use crate::{tab_name::TabNameHolder, NewTabBehaviour};
/// This module contains the implementation of the editor tabs
use bevy::prelude::*;
use bevy_egui::egui;
Expand All @@ -11,19 +11,5 @@ pub const TAB_MODES: [NewTabBehaviour; 3] = [

pub trait EditorTab {
fn ui(&mut self, ui: &mut egui::Ui, commands: &mut Commands, world: &mut World);
fn title(&self) -> egui::WidgetText;
}

#[derive(Clone, Hash, PartialEq, Eq, Debug, PartialOrd, Ord)]
pub enum EditorTabName {
CameraView,
EventDispatcher,
GameView,
Hierarchy,
Inspector,
Resource,
RuntimeAssets,
Settings,
ToolBox,
Other(String),
fn tab_name(&self) -> TabNameHolder;
}
87 changes: 52 additions & 35 deletions crates/editor_tabs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
pub mod editor_tab;
pub mod schedule_editor_tab;
pub mod sizing;
pub mod start_layout;
pub mod tab_name;
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};
Expand All @@ -18,6 +22,8 @@
use egui_dock::DockArea;
use schedule_editor_tab::*;
use sizing::{to_label, Sizing};
use start_layout::StartLayout;
use tab_name::{TabName, TabNameHolder};
use tab_viewer::*;

use bevy_egui::egui::TextStyle as ETextStyle;
Expand All @@ -27,6 +33,8 @@
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_viewer::*;

pub use super::{
Expand Down Expand Up @@ -79,8 +87,8 @@
/// This resource contains registered editor tabs and current dock tree state
#[derive(Resource)]
pub struct EditorUi {
pub registry: HashMap<EditorTabName, EditorUiReg>,
pub tree: egui_dock::DockState<EditorTabName>,
pub registry: HashMap<TabNameHolder, EditorUiReg>,
pub tree: egui_dock::DockState<TabNameHolder>,
}

impl Default for EditorUi {
Expand All @@ -92,21 +100,11 @@
}
}

pub type EditorTabShowFn = Box<dyn Fn(&mut egui::Ui, &mut Commands, &mut World) + Send + Sync>;
pub type EditorTabGetTitleFn = Box<dyn Fn(&mut World) -> egui::WidgetText + Send + Sync>;

/// This enum determine how tab was registered.
/// ResourceBased - tab will be registered as resource
/// Schedule - tab will be registered as system
pub enum EditorUiReg {
ResourceBased {
show_command: EditorTabShowFn,
title_command: EditorTabGetTitleFn,
},
Schedule,
}

impl EditorUi {
pub fn set_layout<T: StartLayout>(&mut self, layout: &T) {
self.tree = layout.build();
}

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

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L104-L106

Added lines #L104 - L106 were not covered by tests

pub fn ui(&mut self, world: &mut World, ctx: &mut egui::Context) {
//collect tab names to vec to detect visible
let mut visible = vec![];
Expand Down Expand Up @@ -192,22 +190,23 @@

/// Trait for registering editor tabs via app.**
pub trait EditorUiAppExt {
fn editor_tab_by_trait<T>(&mut self, tab_id: EditorTabName, tab: T) -> &mut Self
fn editor_tab_by_trait<T>(&mut self, tab: T) -> &mut Self
where
T: EditorTab + Resource + Send + Sync + 'static;
fn editor_tab<T>(
fn editor_tab<T, N: TabName>(
&mut self,
tab_id: EditorTabName,
title: egui::WidgetText,
tab_name: N,
tab_systems: impl IntoSystemConfigs<T>,
) -> &mut Self;
}

impl EditorUiAppExt for App {
fn editor_tab_by_trait<T>(&mut self, tab_id: EditorTabName, tab: T) -> &mut Self
fn editor_tab_by_trait<T>(&mut self, tab: T) -> &mut Self

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

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L204

Added line #L204 was not covered by tests
where
T: EditorTab + Resource + Send + Sync + 'static,
{
let tab_name = tab.tab_name();

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

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L208-L209

Added lines #L208 - L209 were not covered by tests
self.insert_resource(tab);
let show_fn = Box::new(
|ui: &mut egui::Ui, commands: &mut Commands, world: &mut World| {
Expand All @@ -220,42 +219,61 @@
show_command: show_fn,
title_command: Box::new(|world| {
let sizing = world.resource::<Sizing>().clone();
to_label(world.resource_mut::<T>().title().text(), sizing.text).into()
to_label(
world.resource_mut::<T>().tab_name().title.as_str(),
sizing.text,
)
.into()

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

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L222-L226

Added lines #L222 - L226 were not covered by tests
}),
};

self.world
.resource_mut::<EditorUi>()
.registry
.insert(tab_id, reg);
.insert(tab_name, reg);

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

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L233

Added line #L233 was not covered by tests
self
}

fn editor_tab<T>(
fn editor_tab<T, N: TabName>(

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

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L237

Added line #L237 was not covered by tests
&mut self,
tab_id: EditorTabName,
title: egui::WidgetText,
tab_name: N,

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

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L239

Added line #L239 was not covered by tests
tab_systems: impl IntoSystemConfigs<T>,
) -> &mut Self {
let tab_name_holder = TabNameHolder::new(tab_name);

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

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L242-L243

Added lines #L242 - L243 were not covered by tests
let mut tab = ScheduleEditorTab {
schedule: Schedule::default(),
title,
tab_name: tab_name_holder.clone(),

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

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L246

Added line #L246 was not covered by tests
};

tab.schedule.add_systems(tab_systems);

self.world
.resource_mut::<ScheduleEditorTabStorage>()
.0
.insert(tab_id.clone(), tab);
.insert(tab_name_holder.clone(), tab);

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

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L254

Added line #L254 was not covered by tests
self.world
.resource_mut::<EditorUi>()
.registry
.insert(tab_id, EditorUiReg::Schedule);
.insert(tab_name_holder, EditorUiReg::Schedule);

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

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L258

Added line #L258 was not covered by tests
self
}
}

pub type EditorTabShowFn = Box<dyn Fn(&mut egui::Ui, &mut Commands, &mut World) + Send + Sync>;
pub type EditorTabGetTitleFn = Box<dyn Fn(&mut World) -> egui::WidgetText + Send + Sync>;

/// This enum determine how tab was registered.
/// ResourceBased - tab will be registered as resource
/// Schedule - tab will be registered as system
pub enum EditorUiReg {
ResourceBased {
show_command: EditorTabShowFn,
title_command: EditorTabGetTitleFn,
},
Schedule,
}

#[derive(Default, Reflect, PartialEq, Eq, Clone)]
pub enum NewTabBehaviour {
Pop,
Expand All @@ -264,14 +282,13 @@
SplitNode,
}

impl ToString for NewTabBehaviour {
fn to_string(&self) -> String {
impl Display for NewTabBehaviour {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

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

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L286

Added line #L286 was not covered by tests
match self {
Self::Pop => "New window",
Self::SameNode => "Same Node",
Self::SplitNode => "Splits Node",
Self::Pop => write!(f, "New window"),
Self::SameNode => write!(f, "Same Node"),
Self::SplitNode => write!(f, "Splits Node"),

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

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/lib.rs#L288-L290

Added lines #L288 - L290 were not covered by tests
}
.to_string()
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/editor_tabs/src/schedule_editor_tab.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use bevy::{prelude::*, utils::HashMap};
use bevy_egui::egui;

use crate::{EditorTab, EditorTabName};
use crate::{tab_name::TabNameHolder, EditorTab};

/// Temporary resource for pretty system, based tab registration
pub struct EditorUiRef(pub egui::Ui);

pub struct ScheduleEditorTab {
pub schedule: Schedule,
pub title: egui::WidgetText,
pub tab_name: TabNameHolder,
}

impl EditorTab for ScheduleEditorTab {
Expand All @@ -20,10 +20,10 @@
world.remove_non_send_resource::<EditorUiRef>();
}

fn title(&self) -> egui::WidgetText {
self.title.clone()
fn tab_name(&self) -> TabNameHolder {
self.tab_name.clone()

Check warning on line 24 in crates/editor_tabs/src/schedule_editor_tab.rs

View check run for this annotation

Codecov / codecov/patch

crates/editor_tabs/src/schedule_editor_tab.rs#L23-L24

Added lines #L23 - L24 were not covered by tests
}
}

#[derive(Resource, Default)]
pub struct ScheduleEditorTabStorage(pub HashMap<EditorTabName, ScheduleEditorTab>);
pub struct ScheduleEditorTabStorage(pub HashMap<TabNameHolder, ScheduleEditorTab>);
Loading
Loading