Skip to content

Commit

Permalink
😊(ImageHandling): Add random naming and image item management
Browse files Browse the repository at this point in the history
Introduce the `rand` crate to generate unique names for textures and plots, preventing overwriting. Additionally, implement a new `ImageItem` struct and update the `MyEguiApp` struct to manage multiple images, allowing for better organization and display of images in the side panel.

Signed-off-by: Benign X <[email protected]>
  • Loading branch information
W-Mai committed Dec 8, 2024
1 parent beaff07 commit a86f205
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ log = "0.4.20"
env_logger = "0.11.2"
serde_yaml = "0.9.33"
serde = { version = "1.0.197", features = ["derive"] }
rand = "0.8.5"

[patch.crates-io]
icu_lib = { path = "icu_lib" }
Expand Down
5 changes: 3 additions & 2 deletions src/image_plotter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use eframe::egui::{Color32, ColorImage, PointerButton};
use egui_plot::{CoordinatesFormatter, Corner, PlotImage, PlotPoint};
use std::cell::RefCell;
use std::rc::Rc;
use rand::random;

pub struct ImagePlotter {
anti_alias: bool,
Expand Down Expand Up @@ -60,7 +61,7 @@ impl ImagePlotter {
pixels: image_data.clone(),
};
let texture = ui.ctx().load_texture(
"showing_image",
format!("showing_image_{}", random::<u32>()),
image,
if self.anti_alias {
egui::TextureOptions::LINEAR
Expand All @@ -79,7 +80,7 @@ impl ImagePlotter {
let color_data_2 = color_data.clone();
let cursor_pos_2 = cursor_pos.clone();

let plot = egui_plot::Plot::new("plot")
let plot = egui_plot::Plot::new(format!("plot{}", random::<u32>()))
.data_aspect(1.0)
.y_axis_formatter(move |y, _, _| format!("{:.0}", -y.value))
.coordinates_formatter(
Expand Down
54 changes: 50 additions & 4 deletions src/image_shower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use eframe::egui;
use eframe::egui::Color32;
use icu_lib::midata::MiData;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

pub fn show_image(image: MiData) {
let native_options = eframe::NativeOptions::default();
Expand Down Expand Up @@ -32,12 +33,22 @@ pub fn show_image(image: MiData) {
};
}

struct ImageItem {
path: String,

width: u32,
height: u32,
image_data: Vec<Color32>,
}

#[derive(Default)]
struct MyEguiApp {
width: u32,
height: u32,
image_data: Option<Vec<Color32>>,

image_items: Vec<ImageItem>,

dropped_files: Vec<egui::DroppedFile>,

context: AppContext,
Expand Down Expand Up @@ -76,6 +87,7 @@ impl MyEguiApp {
height,
image_data,

image_items: vec![],
dropped_files: Default::default(),

context,
Expand All @@ -93,6 +105,34 @@ impl eframe::App for MyEguiApp {
ui.toggle_value(&mut self.context.anti_alias, "Anti-Aliasing");
});
});

if !self.image_items.is_empty() {
egui::SidePanel::left("ImagePicker").show(ctx, |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
for image_item in &self.image_items {
egui::containers::Frame::default()
.inner_margin(10.0)
.outer_margin(10.0)
.rounding(10.0)
.show(ui, |ui| {
ui.vertical_centered_justified(|ui| {
let mut image_plotter = ImagePlotter::new()
.anti_alias(self.context.anti_alias)
.show_grid(false)
.show_only(true);

image_plotter.show(
ui,
Some(image_item.image_data.clone()),
[image_item.width as f32, image_item.height as f32].into(),
);
});
});
}
})
});
}

egui::CentralPanel::default().show(ctx, |ui| {
let mut image_plotter = ImagePlotter::new()
.anti_alias(self.context.anti_alias)
Expand Down Expand Up @@ -175,7 +215,7 @@ impl MyEguiApp {
}
}
None => {
let data = std::fs::read(info);
let data = std::fs::read(&info);
match data {
Ok(data) => {
if let Some(coder) = icu_lib::endecoder::find_endecoder(&data) {
Expand Down Expand Up @@ -206,15 +246,21 @@ impl MyEguiApp {

self.width = width;
self.height = height;
self.image_data = image_data;
self.image_data = image_data.clone();

self.dropped_files.clear();
break;
self.image_items.push(ImageItem {
path: info,
width,
height,
image_data: image_data.unwrap(),
});
}
MiData::GRAY(_) => {}
MiData::PATH => {}
};
}

self.dropped_files.clear();
}
}
}

0 comments on commit a86f205

Please sign in to comment.