mirror of
https://github.com/Noratrieb/game-wip-dontplay.git
synced 2026-01-16 20:35:02 +01:00
vendor
This commit is contained in:
parent
12163d1338
commit
550b1644cb
363 changed files with 84081 additions and 16 deletions
94
egui/examples/screenshot/src/main.rs
Normal file
94
egui/examples/screenshot/src/main.rs
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
||||
|
||||
use eframe::egui::{self, ColorImage};
|
||||
|
||||
fn main() -> Result<(), eframe::Error> {
|
||||
let options = eframe::NativeOptions {
|
||||
renderer: eframe::Renderer::Wgpu,
|
||||
..Default::default()
|
||||
};
|
||||
eframe::run_native(
|
||||
"Take screenshots and display with eframe/egui",
|
||||
options,
|
||||
Box::new(|_cc| Box::new(MyApp::default())),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct MyApp {
|
||||
continuously_take_screenshots: bool,
|
||||
texture: Option<egui::TextureHandle>,
|
||||
screenshot: Option<ColorImage>,
|
||||
save_to_file: bool,
|
||||
}
|
||||
|
||||
impl eframe::App for MyApp {
|
||||
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
if let Some(screenshot) = self.screenshot.take() {
|
||||
self.texture = Some(ui.ctx().load_texture(
|
||||
"screenshot",
|
||||
screenshot,
|
||||
Default::default(),
|
||||
));
|
||||
}
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.checkbox(
|
||||
&mut self.continuously_take_screenshots,
|
||||
"continuously take screenshots",
|
||||
);
|
||||
|
||||
if ui.button("save to 'top_left.png'").clicked() {
|
||||
self.save_to_file = true;
|
||||
frame.request_screenshot();
|
||||
}
|
||||
|
||||
ui.with_layout(egui::Layout::top_down(egui::Align::RIGHT), |ui| {
|
||||
if self.continuously_take_screenshots {
|
||||
if ui
|
||||
.add(egui::Label::new("hover me!").sense(egui::Sense::hover()))
|
||||
.hovered()
|
||||
{
|
||||
ctx.set_visuals(egui::Visuals::dark());
|
||||
} else {
|
||||
ctx.set_visuals(egui::Visuals::light());
|
||||
};
|
||||
frame.request_screenshot();
|
||||
} else if ui.button("take screenshot!").clicked() {
|
||||
frame.request_screenshot();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if let Some(texture) = self.texture.as_ref() {
|
||||
ui.image(texture, ui.available_size());
|
||||
} else {
|
||||
ui.spinner();
|
||||
}
|
||||
|
||||
ctx.request_repaint();
|
||||
});
|
||||
}
|
||||
|
||||
fn post_rendering(&mut self, _window_size: [u32; 2], frame: &eframe::Frame) {
|
||||
if let Some(screenshot) = frame.screenshot() {
|
||||
if self.save_to_file {
|
||||
let pixels_per_point = frame.info().native_pixels_per_point;
|
||||
let region =
|
||||
egui::Rect::from_two_pos(egui::Pos2::ZERO, egui::Pos2 { x: 100., y: 100. });
|
||||
let top_left_corner = screenshot.region(®ion, pixels_per_point);
|
||||
image::save_buffer(
|
||||
"top_left.png",
|
||||
top_left_corner.as_raw(),
|
||||
top_left_corner.width() as u32,
|
||||
top_left_corner.height() as u32,
|
||||
image::ColorType::Rgba8,
|
||||
)
|
||||
.unwrap();
|
||||
self.save_to_file = false;
|
||||
}
|
||||
self.screenshot = Some(screenshot);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue