-
Notifications
You must be signed in to change notification settings - Fork 257
/
ui.rs
263 lines (230 loc) · 8.41 KB
/
ui.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use bevy::prelude::*;
use bevy_egui::{egui, EguiContext, EguiPlugin, EguiSettings};
struct Images {
bevy_icon: Handle<Image>,
bevy_icon_inverted: Handle<Image>,
}
impl FromWorld for Images {
fn from_world(world: &mut World) -> Self {
let asset_server = world.get_resource_mut::<AssetServer>().unwrap();
Self {
bevy_icon: asset_server.load("icon.png"),
bevy_icon_inverted: asset_server.load("icon_inverted.png"),
}
}
}
/// This example demonstrates the following functionality and use-cases of bevy_egui:
/// - rendering loaded assets;
/// - toggling hidpi scaling (by pressing '/' button);
/// - configuring egui contexts during the startup.
fn main() {
App::new()
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
.insert_resource(Msaa { samples: 4 })
.init_resource::<UiState>()
.add_plugins(DefaultPlugins)
.add_plugin(EguiPlugin)
.add_startup_system(configure_visuals)
.add_startup_system(configure_ui_state)
.add_system(update_ui_scale_factor)
.add_system(ui_example)
.run();
}
#[derive(Default)]
struct UiState {
label: String,
value: f32,
painting: Painting,
inverted: bool,
egui_texture_handle: Option<egui::TextureHandle>,
is_window_open: bool,
}
fn configure_visuals(mut egui_ctx: ResMut<EguiContext>) {
egui_ctx.ctx_mut().set_visuals(egui::Visuals {
window_rounding: 0.0.into(),
..Default::default()
});
}
fn configure_ui_state(mut ui_state: ResMut<UiState>) {
ui_state.is_window_open = true;
}
fn update_ui_scale_factor(
keyboard_input: Res<Input<KeyCode>>,
mut toggle_scale_factor: Local<Option<bool>>,
mut egui_settings: ResMut<EguiSettings>,
windows: Res<Windows>,
) {
if keyboard_input.just_pressed(KeyCode::Slash) || toggle_scale_factor.is_none() {
*toggle_scale_factor = Some(!toggle_scale_factor.unwrap_or(true));
if let Some(window) = windows.get_primary() {
let scale_factor = if toggle_scale_factor.unwrap() {
1.0
} else {
1.0 / window.scale_factor()
};
egui_settings.scale_factor = scale_factor;
}
}
}
fn ui_example(
mut egui_ctx: ResMut<EguiContext>,
mut ui_state: ResMut<UiState>,
// You are not required to store Egui texture ids in systems. We store this one here just to
// demonstrate that rendering by using a texture id of a removed image is handled without
// making bevy_egui panic.
mut rendered_texture_id: Local<egui::TextureId>,
mut is_initialized: Local<bool>,
// If you need to access the ids from multiple systems, you can also initialize the `Images`
// resource while building the app and use `Res<Images>` instead.
images: Local<Images>,
) {
let egui_texture_handle = ui_state
.egui_texture_handle
.get_or_insert_with(|| {
egui_ctx
.ctx_mut()
.load_texture("example-image", egui::ColorImage::example())
})
.clone();
let mut load = false;
let mut remove = false;
let mut invert = false;
if !*is_initialized {
*is_initialized = true;
*rendered_texture_id = egui_ctx.add_image(images.bevy_icon.clone_weak());
}
egui::SidePanel::left("side_panel")
.default_width(200.0)
.show(egui_ctx.ctx_mut(), |ui| {
ui.heading("Side Panel");
ui.horizontal(|ui| {
ui.label("Write something: ");
ui.text_edit_singleline(&mut ui_state.label);
});
ui.add(egui::widgets::Image::new(
egui_texture_handle.id(),
egui_texture_handle.size_vec2(),
));
ui.add(egui::Slider::new(&mut ui_state.value, 0.0..=10.0).text("value"));
if ui.button("Increment").clicked() {
ui_state.value += 1.0;
}
ui.allocate_space(egui::Vec2::new(1.0, 100.0));
ui.horizontal(|ui| {
load = ui.button("Load").clicked();
invert = ui.button("Invert").clicked();
remove = ui.button("Remove").clicked();
});
ui.add(egui::widgets::Image::new(
*rendered_texture_id,
[256.0, 256.0],
));
ui.allocate_space(egui::Vec2::new(1.0, 10.0));
ui.checkbox(&mut ui_state.is_window_open, "Window Is Open");
ui.with_layout(egui::Layout::bottom_up(egui::Align::Center), |ui| {
ui.add(egui::Hyperlink::from_label_and_url(
"powered by egui",
"https://github.com/emilk/egui/",
));
});
});
egui::TopBottomPanel::top("top_panel").show(egui_ctx.ctx_mut(), |ui| {
// The top panel is often a good place for a menu bar:
egui::menu::bar(ui, |ui| {
egui::menu::menu_button(ui, "File", |ui| {
if ui.button("Quit").clicked() {
std::process::exit(0);
}
});
});
});
egui::CentralPanel::default().show(egui_ctx.ctx_mut(), |ui| {
ui.heading("Egui Template");
ui.hyperlink("https://github.com/emilk/egui_template");
ui.add(egui::github_link_file_line!(
"https://github.com/mvlabat/bevy_egui/blob/main/",
"Direct link to source code."
));
egui::warn_if_debug_build(ui);
ui.separator();
ui.heading("Central Panel");
ui.label("The central panel the region left after adding TopPanel's and SidePanel's");
ui.label("It is often a great place for big things, like drawings:");
ui.heading("Draw with your mouse to paint:");
ui_state.painting.ui_control(ui);
egui::Frame::dark_canvas(ui.style()).show(ui, |ui| {
ui_state.painting.ui_content(ui);
});
});
egui::Window::new("Window")
.vscroll(true)
.open(&mut ui_state.is_window_open)
.show(egui_ctx.ctx_mut(), |ui| {
ui.label("Windows can be moved by dragging them.");
ui.label("They are automatically sized based on contents.");
ui.label("You can turn on resizing and scrolling if you like.");
ui.label("You would normally chose either panels OR windows.");
});
if invert {
ui_state.inverted = !ui_state.inverted;
}
if load || invert {
// If an image is already added to the context, it'll return an existing texture id.
if ui_state.inverted {
*rendered_texture_id = egui_ctx.add_image(images.bevy_icon_inverted.clone_weak());
} else {
*rendered_texture_id = egui_ctx.add_image(images.bevy_icon.clone_weak());
};
}
if remove {
egui_ctx.remove_image(&images.bevy_icon);
egui_ctx.remove_image(&images.bevy_icon_inverted);
}
}
struct Painting {
lines: Vec<Vec<egui::Vec2>>,
stroke: egui::Stroke,
}
impl Default for Painting {
fn default() -> Self {
Self {
lines: Default::default(),
stroke: egui::Stroke::new(1.0, egui::Color32::LIGHT_BLUE),
}
}
}
impl Painting {
pub fn ui_control(&mut self, ui: &mut egui::Ui) -> egui::Response {
ui.horizontal(|ui| {
egui::stroke_ui(ui, &mut self.stroke, "Stroke");
ui.separator();
if ui.button("Clear Painting").clicked() {
self.lines.clear();
}
})
.response
}
pub fn ui_content(&mut self, ui: &mut egui::Ui) {
let (response, painter) =
ui.allocate_painter(ui.available_size_before_wrap(), egui::Sense::drag());
let rect = response.rect;
if self.lines.is_empty() {
self.lines.push(vec![]);
}
let current_line = self.lines.last_mut().unwrap();
if let Some(pointer_pos) = response.interact_pointer_pos() {
let canvas_pos = pointer_pos - rect.min;
if current_line.last() != Some(&canvas_pos) {
current_line.push(canvas_pos);
}
} else if !current_line.is_empty() {
self.lines.push(vec![]);
}
for line in &self.lines {
if line.len() >= 2 {
let points: Vec<egui::Pos2> = line.iter().map(|p| rect.min + *p).collect();
painter.add(egui::Shape::line(points, self.stroke));
}
}
}
}