-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a side_panel example for 2D which use the viewport to move the …
…camera
- Loading branch information
Showing
2 changed files
with
132 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}); | ||
} |