Replies: 2 comments 4 replies
-
|
I got closer today. I believe However.. Now I can't figure out how to center the frame with the label without it being stretched to fill the whole available space. I tried using layouts but unfortunately the frame always seems to stretch. Here is what I have right now: /// In app.rs
/// Called each time the UI needs repainting, which may be many times per second.
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
CentralPanel::default().frame(Frame::new()).show(ctx, |ui| {
let mut bg_ui = ui.new_child(UiBuilder::new());
self.video.ui(&mut bg_ui);
ui.label(RichText::new("Hello World!").size(24.0));
});
}impl Widget for &mut VideoPlayer {
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
self.ui_no_signal(ui)
}
}
/// impl VideoPlayer
fn ui_no_signal(&mut self, ui: &mut egui::Ui) -> egui::Response {
let size = ui.available_size();
let rect = ui.available_rect_before_wrap();
let image = Image::new(crate::assets::COLOR_BARS)
.maintain_aspect_ratio(false)
.fit_to_exact_size(size);
let mut bg_ui = ui.new_child(egui::UiBuilder::new());
bg_ui.put(rect, image);
let text_frame = Frame::new()
.inner_margin(Margin::symmetric(20, 10))
.fill(egui::Color32::from_black_alpha(180));
let text = RichText::new("NO VIDEO SIGNAL")
.size(40.0)
.strong()
.color(egui::Color32::WHITE);
text_frame.show(ui, |ui| {
ui.label(text);
});
ui.response()
} |
Beta Was this translation helpful? Give feedback.
-
|
Hello. I'm doing a similar project: playing a video stream in the background. I came up with a painter object. So here is a function I have: fn render_texture(&self, ui: &egui::Ui) {
let Some(ref texture) = self.texture else {
return;
};
let frame_aspect_ratio = texture.aspect_ratio();
let mut ui_x = 0.0;
let mut ui_y = 0.0;
let mut ui_width = ui.available_width();
let mut ui_height = ui.available_height();
let ui_aspect_ratio = ui_width / ui_height;
if frame_aspect_ratio > ui_aspect_ratio {
ui_height *= ui_aspect_ratio / frame_aspect_ratio;
ui_y = (ui.available_height() - ui_height) / 2.0;
} else if frame_aspect_ratio < ui_aspect_ratio {
ui_width *= frame_aspect_ratio / ui_aspect_ratio;
ui_x = (ui.available_width() - ui_width) / 2.0;
}
ui.painter()
.image(
texture.id(),
egui::Rect::from_min_max(
egui::pos2(0.0, 0.0),
egui::pos2(ui_width, ui_height)
).translate(egui::vec2(ui_x, ui_y)),
egui::Rect::from_min_max(
egui::pos2(0.0, 0.0),
egui::pos2(1.0, 1.0)
),
egui::Color32::WHITE
);
}And I call it in my egui::CentralPanel::default()
.frame(egui::Frame::default().inner_margin(0.0))
.show(ctx, |ui| {
self.render_texture(ui);
// Render widgets
}); |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to draw an image and a label in a frame in the background that is ignored by the layout and other widgets. And then I want to draw the rest of the UI on top.
So I have this code to draw the background part.
However when I try to draw the normal panels they end up being drawn behind the background image.
If I move the image over slightly we can see the rest is drawn behind it.

Is there a way to achieve this?
Beta Was this translation helpful? Give feedback.
All reactions