Skip to content

Commit dd3eb36

Browse files
committed
Show how to swap viewports in the 'viewports' example
1 parent 5b8755e commit dd3eb36

File tree

1 file changed

+60
-19
lines changed

1 file changed

+60
-19
lines changed

examples/window/viewports.rs

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bevy::{
2-
math::clamp,
2+
math::{clamp, Rect},
33
prelude::*,
44
render::{
55
camera::{ActiveCameras, Camera},
@@ -26,22 +26,22 @@ fn setup(
2626
asset_server: Res<AssetServer>,
2727
) {
2828
// add new camera nodes for the secondary viewports
29-
render_graph.add_system_node("top_right_camera", CameraNode::new("TopRight"));
30-
render_graph.add_system_node("bottom_right_camera", CameraNode::new("BottomRight"));
31-
active_cameras.add("TopRight");
32-
active_cameras.add("BottomRight");
29+
render_graph.add_system_node("front_view_camera", CameraNode::new("FrontView"));
30+
render_graph.add_system_node("side_view_camera", CameraNode::new("SideView"));
31+
active_cameras.add("FrontView");
32+
active_cameras.add("SideView");
3333

3434
// add the cameras to the main pass
3535
{
3636
let main_pass: &mut PassNode<&MainPass> = render_graph.get_node_mut("main_pass").unwrap();
37-
main_pass.add_camera("TopRight");
38-
main_pass.add_camera("BottomRight");
37+
main_pass.add_camera("FrontView");
38+
main_pass.add_camera("SideView");
3939
}
4040
render_graph
41-
.add_node_edge("top_right_camera", "main_pass")
41+
.add_node_edge("front_view_camera", "main_pass")
4242
.unwrap();
4343
render_graph
44-
.add_node_edge("bottom_right_camera", "main_pass")
44+
.add_node_edge("side_view_camera", "main_pass")
4545
.unwrap();
4646

4747
// SETUP SCENE
@@ -78,7 +78,7 @@ fn setup(
7878
// top right camera
7979
.spawn(PerspectiveCameraBundle {
8080
camera: Camera {
81-
name: Some("TopRight".to_string()),
81+
name: Some("FrontView".to_string()),
8282
..Default::default()
8383
},
8484
transform: Transform::from_xyz(0.0, 0.3, 1.3)
@@ -88,7 +88,7 @@ fn setup(
8888
// bottom right camera
8989
.spawn(PerspectiveCameraBundle {
9090
camera: Camera {
91-
name: Some("BottomRight".to_string()),
91+
name: Some("SideView".to_string()),
9292
..Default::default()
9393
},
9494
transform: Transform::from_xyz(-1.3, 0.3, 0.0)
@@ -97,7 +97,8 @@ fn setup(
9797
});
9898

9999
// ui
100-
let instructions_text = "use the arrow keys to resize the viewports";
100+
let instructions_text =
101+
"Use the arrow keys to resize the viewports\nPress Enter to swap the rightmost viewports";
101102
commands
102103
.spawn(UiCameraBundle {
103104
// viewports occupy the entire surface by default, and can overlap each other
@@ -124,13 +125,44 @@ fn setup(
124125
struct ViewportLayout {
125126
divide_x: f32,
126127
divide_y: f32,
128+
invert: bool,
129+
}
130+
131+
impl ViewportLayout {
132+
pub fn main_view(&self) -> Rect<SideLocation> {
133+
Rect {
134+
left: SideLocation::Relative(0.0),
135+
right: SideLocation::Relative(self.divide_x),
136+
top: SideLocation::Relative(0.0),
137+
bottom: SideLocation::Relative(1.0),
138+
}
139+
}
140+
141+
pub fn front_view_view(&self) -> Rect<SideLocation> {
142+
Rect {
143+
left: SideLocation::Relative(self.divide_x),
144+
right: SideLocation::Relative(1.0),
145+
top: SideLocation::Relative(0.0),
146+
bottom: SideLocation::Relative(self.divide_y),
147+
}
148+
}
149+
150+
pub fn side_view_view(&self) -> Rect<SideLocation> {
151+
Rect {
152+
left: SideLocation::Relative(self.divide_x),
153+
right: SideLocation::Relative(1.0),
154+
top: SideLocation::Relative(self.divide_y),
155+
bottom: SideLocation::Relative(1.0),
156+
}
157+
}
127158
}
128159

129160
impl Default for ViewportLayout {
130161
fn default() -> Self {
131162
Self {
132163
divide_x: 0.5,
133164
divide_y: 0.5,
165+
invert: false,
134166
}
135167
}
136168
}
@@ -153,6 +185,9 @@ fn viewport_layout_system(
153185
if keyboard_input.just_pressed(KeyCode::Down) {
154186
layout.divide_y += 0.05;
155187
}
188+
if keyboard_input.just_pressed(KeyCode::Return) {
189+
layout.invert = !layout.invert;
190+
}
156191
layout.divide_x = clamp(layout.divide_x, 0.0, 1.0);
157192
layout.divide_y = clamp(layout.divide_y, 0.0, 1.0);
158193

@@ -161,15 +196,21 @@ fn viewport_layout_system(
161196
match camera.name.as_deref() {
162197
// default camera
163198
Some("Camera3d") => {
164-
viewport.sides.right = SideLocation::Relative(layout.divide_x);
199+
viewport.sides = layout.main_view();
165200
}
166-
Some("TopRight") => {
167-
viewport.sides.left = SideLocation::Relative(layout.divide_x);
168-
viewport.sides.bottom = SideLocation::Relative(layout.divide_y);
201+
Some("FrontView") => {
202+
if layout.invert {
203+
viewport.sides = layout.front_view_view();
204+
} else {
205+
viewport.sides = layout.side_view_view();
206+
}
169207
}
170-
Some("BottomRight") => {
171-
viewport.sides.left = SideLocation::Relative(layout.divide_x);
172-
viewport.sides.top = SideLocation::Relative(layout.divide_y);
208+
Some("SideView") => {
209+
if layout.invert {
210+
viewport.sides = layout.side_view_view();
211+
} else {
212+
viewport.sides = layout.front_view_view();
213+
}
173214
}
174215
_ => {}
175216
}

0 commit comments

Comments
 (0)