-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathtwo_windows.rs
122 lines (110 loc) · 3.58 KB
/
two_windows.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use bevy::{
prelude::*,
render::camera::RenderTarget,
window::{PresentMode, PrimaryWindow, WindowRef, WindowResolution},
};
use bevy_egui::{EguiContext, EguiPlugin, EguiUserTextures};
#[derive(Resource)]
struct Images {
bevy_icon: Handle<Image>,
}
fn main() {
let mut app = App::new();
app.add_plugins(DefaultPlugins)
.add_plugins(EguiPlugin)
.init_resource::<SharedUiState>()
.add_systems(Startup, load_assets_system)
.add_systems(Startup, create_new_window_system)
.add_systems(Update, ui_first_window_system)
.add_systems(Update, ui_second_window_system);
app.run();
}
fn create_new_window_system(mut commands: Commands) {
// Spawn a second window
let second_window_id = commands
.spawn(Window {
title: "Second window".to_owned(),
resolution: WindowResolution::new(800.0, 600.0),
present_mode: PresentMode::AutoVsync,
..Default::default()
})
.id();
// second window camera
commands.spawn((
Camera3d::default(),
Camera {
target: RenderTarget::Window(WindowRef::Entity(second_window_id)),
..Default::default()
},
Transform::from_xyz(6.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
));
}
fn load_assets_system(mut commands: Commands, assets: Res<AssetServer>) {
commands.insert_resource(Images {
bevy_icon: assets.load("icon.png"),
});
}
#[derive(Default)]
struct UiState {
input: String,
}
#[derive(Default, Resource)]
struct SharedUiState {
shared_input: String,
}
fn ui_first_window_system(
mut egui_user_textures: ResMut<EguiUserTextures>,
mut ui_state: Local<UiState>,
mut shared_ui_state: ResMut<SharedUiState>,
images: Res<Images>,
mut egui_ctx: Query<&mut EguiContext, With<PrimaryWindow>>,
) {
let bevy_texture_id = egui_user_textures.add_image(images.bevy_icon.clone_weak());
let Ok(mut ctx) = egui_ctx.get_single_mut() else {
return;
};
egui::Window::new("First Window")
.vscroll(true)
.show(ctx.get_mut(), |ui| {
ui.horizontal(|ui| {
ui.label("Write something: ");
ui.text_edit_singleline(&mut ui_state.input);
});
ui.horizontal(|ui| {
ui.label("Shared input: ");
ui.text_edit_singleline(&mut shared_ui_state.shared_input);
});
ui.add(egui::widgets::Image::new(egui::load::SizedTexture::new(
bevy_texture_id,
[256.0, 256.0],
)));
});
}
fn ui_second_window_system(
mut egui_user_textures: ResMut<EguiUserTextures>,
mut ui_state: Local<UiState>,
mut shared_ui_state: ResMut<SharedUiState>,
images: Res<Images>,
mut egui_ctx: Query<&mut EguiContext, Without<PrimaryWindow>>,
) {
let bevy_texture_id = egui_user_textures.add_image(images.bevy_icon.clone_weak());
let Ok(mut ctx) = egui_ctx.get_single_mut() else {
return;
};
egui::Window::new("Second Window")
.vscroll(true)
.show(ctx.get_mut(), |ui| {
ui.horizontal(|ui| {
ui.label("Write something else: ");
ui.text_edit_singleline(&mut ui_state.input);
});
ui.horizontal(|ui| {
ui.label("Shared input: ");
ui.text_edit_singleline(&mut shared_ui_state.shared_input);
});
ui.add(egui::widgets::Image::new(egui::load::SizedTexture::new(
bevy_texture_id,
[256.0, 256.0],
)));
});
}