2021-12-30 21:43:53 +00:00
|
|
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
|
|
|
|
2021-09-04 15:44:01 +00:00
|
|
|
use eframe::{egui, epi};
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct MyApp {
|
2021-09-20 20:36:57 +00:00
|
|
|
texture: Option<(egui::Vec2, egui::TextureId)>,
|
2021-09-04 15:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl epi::App for MyApp {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"Show an image with eframe/egui"
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:13:10 +00:00
|
|
|
fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
|
2021-09-04 15:44:01 +00:00
|
|
|
if self.texture.is_none() {
|
|
|
|
// Load the image:
|
2021-09-20 20:36:57 +00:00
|
|
|
let image_data = include_bytes!("rust-logo-256x256.png");
|
2021-09-04 15:44:01 +00:00
|
|
|
use image::GenericImageView;
|
|
|
|
let image = image::load_from_memory(image_data).expect("Failed to load image");
|
|
|
|
let image_buffer = image.to_rgba8();
|
2021-12-26 20:21:28 +00:00
|
|
|
let size = [image.width() as usize, image.height() as usize];
|
2021-09-04 15:44:01 +00:00
|
|
|
let pixels = image_buffer.into_vec();
|
2021-12-26 20:21:28 +00:00
|
|
|
let image = epi::Image::from_rgba_unmultiplied(size, &pixels);
|
2021-09-04 15:44:01 +00:00
|
|
|
|
|
|
|
// Allocate a texture:
|
2021-12-26 20:21:28 +00:00
|
|
|
let texture = frame.alloc_texture(image);
|
|
|
|
let size = egui::Vec2::new(size[0] as f32, size[1] as f32);
|
2021-09-20 20:36:57 +00:00
|
|
|
self.texture = Some((size, texture));
|
2021-09-04 15:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
egui::CentralPanel::default().show(ctx, |ui| {
|
2021-09-20 20:36:57 +00:00
|
|
|
if let Some((size, texture)) = self.texture {
|
|
|
|
ui.heading("This is an image:");
|
|
|
|
ui.image(texture, size);
|
|
|
|
|
|
|
|
ui.heading("This is an image you can click:");
|
|
|
|
ui.add(egui::ImageButton::new(texture, size));
|
2021-09-04 15:44:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let options = eframe::NativeOptions::default();
|
|
|
|
eframe::run_native(Box::new(MyApp::default()), options);
|
|
|
|
}
|