Skip to content

Pane Rework and Pane Group Functionality #170

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ unsafe_op_in_unsafe_fn = "warn"
unused_qualifications = "warn"

[workspace.dependencies]
bevy = { git = "https://github.com/bevyengine/bevy.git", rev = "89d094e50f10fc56ec3c4b046c830c650f9f09d5", features = ["wayland"] }
bevy = "0.15.0"
bevy_derive = { git = "https://github.com/bevyengine/bevy.git", rev = "89d094e50f10fc56ec3c4b046c830c650f9f09d5" }
thiserror = "1"
serde = { version = "1", features = ["derive"] }
Expand Down
160 changes: 76 additions & 84 deletions bevy_editor_panes/bevy_2d_viewport/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//! 2d Viewport for Bevy
use bevy::{
ecs::system::{
lifetimeless::{SCommands, SRes, SResMut},
StaticSystemParam,
},
prelude::*,
render::{
camera::RenderTarget,
Expand All @@ -11,28 +15,28 @@ use bevy::{
use bevy_editor_camera::{EditorCamera2d, EditorCamera2dPlugin};
use bevy_editor_styles::Theme;
use bevy_infinite_grid::{InfiniteGrid, InfiniteGridPlugin, InfiniteGridSettings};
use bevy_pane_layout::prelude::*;
use bevy_pane_layout::{pane::Pane, prelude::*};

/// The identifier for the 2D Viewport.
/// This is present on any pane that is a 2D Viewport.
#[derive(Component)]
pub struct Bevy2dViewport {
pub struct Bevy2dViewportPane {
camera_id: Entity,
}

impl Default for Bevy2dViewport {
impl Default for Bevy2dViewportPane {
fn default() -> Self {
Bevy2dViewport {
Bevy2dViewportPane {
camera_id: Entity::PLACEHOLDER,
}
}
}

/// Plugin for the 2D Viewport pane.
pub struct Viewport2dPanePlugin;
impl Pane for Bevy2dViewportPane {
const NAME: &str = "Viewport 2D";
const ID: &str = "viewport_2d";

impl Plugin for Viewport2dPanePlugin {
fn build(&self, app: &mut App) {
fn build(app: &mut App) {
if !app.is_plugin_added::<InfiniteGridPlugin>() {
app.add_plugins(InfiniteGridPlugin);
}
Expand All @@ -43,17 +47,75 @@ impl Plugin for Viewport2dPanePlugin {
update_render_target_size.after(ui_layout_system),
)
.add_observer(
|trigger: Trigger<OnRemove, Bevy2dViewport>,
|trigger: Trigger<OnRemove, Bevy2dViewportPane>,
mut commands: Commands,
query: Query<&Bevy2dViewport>| {
query: Query<&Bevy2dViewportPane>| {
// Despawn the viewport camera
commands
.entity(query.get(trigger.entity()).unwrap().camera_id)
.despawn_recursive();
},
);
}

app.register_pane("Viewport 2D", on_pane_creation);
type Param = (SCommands, SResMut<Assets<Image>>, SRes<Theme>);
fn on_create(structure: In<PaneStructure>, param: StaticSystemParam<Self::Param>) {
let (mut commands, mut images, theme) = param.into_inner();
let mut image = Image::default();

image.texture_descriptor.usage |= TextureUsages::RENDER_ATTACHMENT;
image.texture_descriptor.format = TextureFormat::Bgra8UnormSrgb;

let image_handle = images.add(image);

let image_id = commands
.spawn((
ImageNode::new(image_handle.clone()),
Node {
position_type: PositionType::Absolute,
top: Val::ZERO,
bottom: Val::ZERO,
left: Val::ZERO,
right: Val::ZERO,
..default()
},
))
.set_parent(structure.root)
.id();

let camera_id = commands
.spawn((
Camera2d,
EditorCamera2d {
enabled: false,
..default()
},
Camera {
target: RenderTarget::Image(image_handle),
clear_color: ClearColorConfig::Custom(theme.viewport.background_color),
..default()
},
RenderLayers::from_layers(&[0, 2]),
))
.id();

commands
.entity(image_id)
.observe(
move |_trigger: Trigger<Pointer<Move>>, mut query: Query<&mut EditorCamera2d>| {
let mut editor_camera = query.get_mut(camera_id).unwrap();
editor_camera.enabled = true;
},
)
.observe(
move |_trigger: Trigger<Pointer<Out>>, mut query: Query<&mut EditorCamera2d>| {
query.get_mut(camera_id).unwrap().enabled = false;
},
);

commands
.entity(structure.root)
.insert(Bevy2dViewportPane { camera_id });
}
}

Expand All @@ -74,87 +136,17 @@ fn setup(mut commands: Commands, theme: Res<Theme>) {
));
}

fn on_pane_creation(
structure: In<PaneStructure>,
mut commands: Commands,
mut images: ResMut<Assets<Image>>,
theme: Res<Theme>,
) {
let mut image = Image::default();

image.texture_descriptor.usage |= TextureUsages::RENDER_ATTACHMENT;
image.texture_descriptor.format = TextureFormat::Bgra8UnormSrgb;

let image_handle = images.add(image);

let image_id = commands
.spawn((
ImageNode::new(image_handle.clone()),
Node {
position_type: PositionType::Absolute,
top: Val::ZERO,
bottom: Val::ZERO,
left: Val::ZERO,
right: Val::ZERO,
..default()
},
))
.set_parent(structure.content)
.id();

let camera_id = commands
.spawn((
Camera2d,
EditorCamera2d {
enabled: false,
..default()
},
Camera {
target: RenderTarget::Image(image_handle),
clear_color: ClearColorConfig::Custom(theme.viewport.background_color),
..default()
},
RenderLayers::from_layers(&[0, 2]),
))
.id();

commands
.entity(image_id)
.observe(
move |_trigger: Trigger<Pointer<Move>>, mut query: Query<&mut EditorCamera2d>| {
let mut editor_camera = query.get_mut(camera_id).unwrap();
editor_camera.enabled = true;
},
)
.observe(
move |_trigger: Trigger<Pointer<Out>>, mut query: Query<&mut EditorCamera2d>| {
query.get_mut(camera_id).unwrap().enabled = false;
},
);

commands
.entity(structure.root)
.insert(Bevy2dViewport { camera_id });
}

fn update_render_target_size(
query: Query<(Entity, &Bevy2dViewport)>,
query: Query<(Entity, &Bevy2dViewportPane)>,
mut camera_query: Query<(&Camera, &mut EditorCamera2d)>,
content: Query<&PaneContentNode>,
children_query: Query<&Children>,
pos_query: Query<
(&ComputedNode, &GlobalTransform),
Or<(Changed<ComputedNode>, Changed<GlobalTransform>)>,
>,
mut images: ResMut<Assets<Image>>,
) {
for (pane_root, viewport) in &query {
let content_node_id = children_query
.iter_descendants(pane_root)
.find(|e| content.contains(*e))
.unwrap();

let Ok((computed_node, global_transform)) = pos_query.get(content_node_id) else {
for (pane_root_id, viewport) in &query {
let Ok((computed_node, global_transform)) = pos_query.get(pane_root_id) else {
continue;
};
// TODO Convert to physical pixels
Expand Down
Loading