Skip to content

Commit

Permalink
Added a side_panel example for 2D which use the viewport to move the …
Browse files Browse the repository at this point in the history
…camera
  • Loading branch information
weberja committed Jan 28, 2024
1 parent f095c38 commit fff8406
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 8 deletions.
18 changes: 10 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ required-features = ["render"]
name = "side_panel"
required-features = ["render"]
[[example]]
name = "side_panel_2d"
required-features = ["render"]
[[example]]
name = "simple"
required-features = ["render"]
[[example]]
Expand All @@ -39,9 +42,7 @@ name = "ui"
required-features = ["render"]

[dependencies]
bevy = { version = "0.12", default-features = false, features = [
"bevy_asset",
] }
bevy = { version = "0.12", default-features = false, features = ["bevy_asset"] }
egui = { version = "0.24.0", default-features = false, features = ["bytemuck"] }
webbrowser = { version = "0.8.2", optional = true }

Expand All @@ -53,9 +54,10 @@ thread_local = { version = "1.1.0", optional = true }
once_cell = "1.16.0"
version-sync = "0.9.4"
bevy = { version = "0.12", default-features = false, features = [
"x11",
"png",
"bevy_pbr",
"bevy_core_pipeline",
"tonemapping_luts",
"x11",
"png",
"bevy_pbr",
"bevy_core_pipeline",
"bevy_sprite",
"tonemapping_luts",
] }
122 changes: 122 additions & 0 deletions examples/side_panel_2d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
use bevy::{
prelude::*, render::camera::Viewport, sprite::MaterialMesh2dBundle, window::PrimaryWindow,
};
use bevy_egui::{egui, EguiContexts, EguiPlugin};

#[derive(Resource, Deref, DerefMut)]
struct OriginalCameraTransform(Transform);

fn main() {
App::new()
.insert_resource(ClearColor(Color::GRAY))
.add_plugins(DefaultPlugins)
.add_plugins(EguiPlugin)
.add_systems(Startup, setup_system)
.add_systems(Update, ui_example_system)
.run();
}

fn ui_example_system(
mut contexts: EguiContexts,
mut cameras: Query<&mut Camera>,
q_window: Query<&mut Window, With<PrimaryWindow>>,
) {
let ctx = contexts.ctx_mut();
let mut camera = cameras.get_single_mut().expect("No camera found");
let window = q_window.get_single().expect("No primary window found");

let left = egui::SidePanel::left("left_panel")
.resizable(true)
.show(ctx, |ui| {
ui.label("Left resizeable panel");
ui.allocate_rect(ui.available_rect_before_wrap(), egui::Sense::hover());
})
.response
.rect
.width();
let right = egui::SidePanel::right("right_panel")
.resizable(true)
.show(ctx, |ui| {
ui.label("Right resizeable panel");
ui.allocate_rect(ui.available_rect_before_wrap(), egui::Sense::hover());
})
.response
.rect
.width();
let top = egui::TopBottomPanel::top("top_panel")
.resizable(true)
.show(ctx, |ui| {
ui.label("Top resizeable panel");
ui.allocate_rect(ui.available_rect_before_wrap(), egui::Sense::hover());
})
.response
.rect
.height();
let bottom = egui::TopBottomPanel::bottom("bottom_panel")
.resizable(true)
.show(ctx, |ui| {
ui.label("Bottom resizeable panel");
ui.allocate_rect(ui.available_rect_before_wrap(), egui::Sense::hover());
})
.response
.rect
.height();

let pos = UVec2::new(left as u32, top as u32);
let size = UVec2::new(window.physical_width(), window.physical_height())
- pos
- UVec2::new(right as u32, bottom as u32);

camera.viewport = Some(Viewport {
physical_position: pos,
physical_size: size,
..default()
});
}

fn setup_system(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
// Circle
commands.spawn(MaterialMesh2dBundle {
mesh: meshes.add(shape::Circle::new(50.).into()).into(),
material: materials.add(ColorMaterial::from(Color::PURPLE)),
transform: Transform::from_translation(Vec3::new(-150., 0., 0.)),
..default()
});

// Rectangle
commands.spawn(SpriteBundle {
sprite: Sprite {
color: Color::rgb(0.25, 0.25, 0.75),
custom_size: Some(Vec2::new(50.0, 100.0)),
..default()
},
transform: Transform::from_translation(Vec3::new(-50., 0., 0.)),
..default()
});

// Quad
commands.spawn(MaterialMesh2dBundle {
mesh: meshes
.add(shape::Quad::new(Vec2::new(50., 100.)).into())
.into(),
material: materials.add(ColorMaterial::from(Color::LIME_GREEN)),
transform: Transform::from_translation(Vec3::new(50., 0., 0.)),
..default()
});

// Hexagon
commands.spawn(MaterialMesh2dBundle {
mesh: meshes.add(shape::RegularPolygon::new(50., 6).into()).into(),
material: materials.add(ColorMaterial::from(Color::TURQUOISE)),
transform: Transform::from_translation(Vec3::new(150., 0., 0.)),
..default()
});

commands.spawn(Camera2dBundle {
..Default::default()
});
}

0 comments on commit fff8406

Please sign in to comment.