Skip to content

Commit

Permalink
move markers with middle mouse button
Browse files Browse the repository at this point in the history
	modified:   src/egui_plot_stuff/egui_horizontal_line.rs
	modified:   src/egui_plot_stuff/egui_image.rs
	modified:   src/egui_plot_stuff/egui_line.rs
	modified:   src/egui_plot_stuff/egui_polygon.rs
	modified:   src/egui_plot_stuff/egui_vertical_line.rs
	modified:   src/fitter/fit_markers.rs
	modified:   src/fitter/gaussian.rs
	modified:   src/histoer/histogram1d.rs
	modified:   src/histoer/histogram2d.rs
  • Loading branch information
alconley committed Jun 25, 2024
1 parent 8db2088 commit 718db8d
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 28 deletions.
57 changes: 55 additions & 2 deletions src/egui_plot_stuff/egui_horizontal_line.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use egui::{Color32, DragValue, Slider, Stroke, Ui};
use egui_plot::{HLine, LineStyle, PlotUi};
use egui::{Color32, DragValue, Id, Slider, Stroke, Ui};
use egui_plot::{HLine, LineStyle, PlotResponse, PlotUi};

use crate::egui_plot_stuff::colors::{Rgb, COLOR_OPTIONS};

Expand All @@ -21,6 +21,11 @@ pub struct EguiHorizontalLine {
// Use Rgb struct for custom RGB values
pub color_rgb: Rgb,
pub stroke_rgb: Rgb,

pub interactive_dragging: bool,

#[serde(skip)]
pub is_dragging: bool,
}

impl Default for EguiHorizontalLine {
Expand All @@ -38,6 +43,8 @@ impl Default for EguiHorizontalLine {
y_value: 0.0,
color_rgb: Rgb::from_color32(Color32::BLUE),
stroke_rgb: Rgb::from_color32(Color32::BLUE),
interactive_dragging: true,
is_dragging: false,
}
}
}
Expand Down Expand Up @@ -70,11 +77,54 @@ impl EguiHorizontalLine {
}

plot_ui.hline(line);

if self.interactive_dragging {
let mid_point_pos: Vec<[f64; 2]> = vec![[
(plot_ui.plot_bounds().min()[0] + plot_ui.plot_bounds().max()[0]) / 2.0,
self.y_value,
]];

let mid_point = egui_plot::Points::new(mid_point_pos)
.color(self.color)
.highlight(self.highlighted)
.radius(1.0)
.id(Id::new(self.name.clone()));

plot_ui.points(mid_point);
}
}
}

pub fn interactive_dragging(&mut self, plot_response: &PlotResponse<()>) {
let pointer_state = plot_response.response.ctx.input(|i| i.pointer.clone());
if let Some(pointer_pos) = pointer_state.hover_pos() {
if let Some(hovered_id) = plot_response.hovered_plot_item {
if hovered_id == Id::new(self.name.clone()) {
self.highlighted = true;
if pointer_state.button_pressed(egui::PointerButton::Middle) {
self.is_dragging = true;
}
} else {
self.highlighted = false;
}
} else {
self.highlighted = false;
}

if self.is_dragging {
self.y_value = plot_response.transform.value_from_position(pointer_pos).y;
if pointer_state.button_released(egui::PointerButton::Middle) {
self.is_dragging = false;
}
}
} else if pointer_state.button_released(egui::PointerButton::Middle) {
self.is_dragging = false;
}
}

pub fn menu_button(&mut self, ui: &mut Ui) {
ui.menu_button(format!("{} Line", self.name), |ui| {
ui.label(self.name.to_string());
ui.vertical(|ui| {
ui.checkbox(&mut self.draw, "Draw Line");
ui.add(
Expand Down Expand Up @@ -116,6 +166,9 @@ impl EguiHorizontalLine {
.prefix("Length: "),
);
});

ui.checkbox(&mut self.interactive_dragging, "Interactive Dragging")
.on_hover_text("Enable interactive dragging of the line. Make sure to have a unique name for each line and add the function to the response of the plot.");
});
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/egui_plot_stuff/egui_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ impl EguiImage {

pub fn menu_button(&mut self, ui: &mut Ui) {
ui.menu_button(format!("{} Image", self.name), |ui| {
ui.label(self.name.to_string());

ui.vertical(|ui| {
ui.checkbox(&mut self.draw, "Draw");

Expand Down
4 changes: 3 additions & 1 deletion src/egui_plot_stuff/egui_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ impl EguiLine {
.highlight(self.highlighted)
.stroke(self.stroke)
.width(self.width)
.color(self.color);
.color(self.color)
.id(egui::Id::new(self.name.clone()));

if self.name_in_legend {
line = line.name(self.name.clone());
Expand All @@ -108,6 +109,7 @@ impl EguiLine {

pub fn menu_button(&mut self, ui: &mut Ui) {
ui.menu_button(format!("{} Line", self.name), |ui| {
ui.label(self.name.to_string());
ui.vertical(|ui| {
ui.checkbox(&mut self.draw, "Draw Line");
ui.checkbox(&mut self.name_in_legend, "Name in Legend")
Expand Down
1 change: 1 addition & 0 deletions src/egui_plot_stuff/egui_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl EguiPolygon {

pub fn menu_button(&mut self, ui: &mut Ui) {
ui.menu_button(self.name.to_string(), |ui| {
ui.label(self.name.to_string());
ui.vertical(|ui| {
ui.text_edit_singleline(&mut self.name);
ui.checkbox(&mut self.draw, "Draw Polygon");
Expand Down
57 changes: 55 additions & 2 deletions src/egui_plot_stuff/egui_vertical_line.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use egui::{Color32, DragValue, Slider, Stroke, Ui};
use egui_plot::{LineStyle, PlotUi, VLine};
use egui::{Color32, DragValue, Id, Slider, Stroke, Ui};
use egui_plot::{LineStyle, PlotResponse, PlotUi, VLine};

use crate::egui_plot_stuff::colors::{Rgb, COLOR_OPTIONS};

Expand All @@ -21,6 +21,11 @@ pub struct EguiVerticalLine {
// Use Rgb struct for custom RGB values
pub color_rgb: Rgb,
pub stroke_rgb: Rgb,

pub interactive_dragging: bool,

#[serde(skip)]
pub is_dragging: bool,
}

impl Default for EguiVerticalLine {
Expand All @@ -38,6 +43,8 @@ impl Default for EguiVerticalLine {
x_value: 0.0,
color_rgb: Rgb::from_color32(Color32::BLUE),
stroke_rgb: Rgb::from_color32(Color32::BLUE),
interactive_dragging: true,
is_dragging: false,
}
}
}
Expand Down Expand Up @@ -70,11 +77,54 @@ impl EguiVerticalLine {
}

plot_ui.vline(line);

if self.interactive_dragging {
let mid_point_pos: Vec<[f64; 2]> = vec![[
self.x_value,
(plot_ui.plot_bounds().min()[1] + plot_ui.plot_bounds().max()[1]) / 2.0,
]];

let mid_point = egui_plot::Points::new(mid_point_pos)
.color(self.color)
.highlight(self.highlighted)
.radius(3.0)
.id(Id::new(self.name.clone()));

plot_ui.points(mid_point);
}
}
}

pub fn interactive_dragging(&mut self, plot_response: &PlotResponse<()>) {
let pointer_state = plot_response.response.ctx.input(|i| i.pointer.clone());
if let Some(pointer_pos) = pointer_state.hover_pos() {
if let Some(hovered_id) = plot_response.hovered_plot_item {
if hovered_id == Id::new(self.name.clone()) {
self.highlighted = true;
if pointer_state.button_pressed(egui::PointerButton::Middle) {
self.is_dragging = true;
}
} else {
self.highlighted = false;
}
} else {
self.highlighted = false;
}

if self.is_dragging {
self.x_value = plot_response.transform.value_from_position(pointer_pos).x;
if pointer_state.button_released(egui::PointerButton::Middle) {
self.is_dragging = false;
}
}
} else if pointer_state.button_released(egui::PointerButton::Middle) {
self.is_dragging = false;
}
}

pub fn menu_button(&mut self, ui: &mut Ui) {
ui.menu_button(format!("{} Line", self.name), |ui| {
ui.label(self.name.to_string());
ui.vertical(|ui| {
ui.checkbox(&mut self.draw, "Draw Line");
ui.add(
Expand Down Expand Up @@ -116,6 +166,9 @@ impl EguiVerticalLine {
.prefix("Length: "),
);
});

ui.checkbox(&mut self.interactive_dragging, "Interactive Dragging")
.on_hover_text("Enable interactive dragging of the line. Make sure to have a unique name for each line and add the function to the response of the plot.");
});
});
}
Expand Down
22 changes: 18 additions & 4 deletions src/fitter/fit_markers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ impl EguiFitMarkers {
});
}

pub fn draw_all_markers(&self, plot_ui: &mut PlotUi) {
for marker in &self.background_markers {
pub fn draw_all_markers(&mut self, plot_ui: &mut PlotUi) {
for marker in &mut self.background_markers {
marker.draw(plot_ui);
}

for marker in &self.region_markers {
for marker in &mut self.region_markers {
marker.draw(plot_ui);
}

for marker in &self.peak_markers {
for marker in &mut self.peak_markers {
marker.draw(plot_ui);
}
}
Expand Down Expand Up @@ -173,6 +173,20 @@ impl EguiFitMarkers {
}
}

pub fn interactive_dragging(&mut self, plot_response: &egui_plot::PlotResponse<()>) {
for marker in &mut self.background_markers {
marker.interactive_dragging(plot_response);
}

for marker in &mut self.region_markers {
marker.interactive_dragging(plot_response);
}

for marker in &mut self.peak_markers {
marker.interactive_dragging(plot_response);
}
}

pub fn menu_button(&mut self, ui: &mut egui::Ui) {
ui.menu_button("Markers", |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
Expand Down
19 changes: 16 additions & 3 deletions src/fitter/gaussian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ impl GaussianFitter {
// if peak_marks is empty, find the max of the y data and use that index of the x data as the initial guess
if self.peak_markers.is_empty() {
let max_y = self.y.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
let max_y_index = self.y.iter().position(|&r| r == max_y).unwrap();
let max_y_index = match self.y.iter().position(|&r| r == max_y) {
Some(index) => index,
None => {
log::error!("Max y value not found in y data");
return vec![];
}
};
self.peak_markers.push(self.x[max_y_index]);
}

Expand Down Expand Up @@ -251,7 +257,7 @@ impl GaussianFitter {

log::info!(
"Linear Coefficients: {:?}",
fit_result.linear_coefficients().unwrap()
fit_result.linear_coefficients()
);
log::info!(
"linear coefficients variance: {:?}",
Expand All @@ -261,7 +267,14 @@ impl GaussianFitter {
let nonlinear_parameters = fit_result.nonlinear_parameters();
let nonlinear_variances = fit_statistics.nonlinear_parameters_variance();

let linear_coefficients = fit_result.linear_coefficients().unwrap();
let linear_coefficients = match fit_result.linear_coefficients() {
Some(coefficients) => coefficients,
None => {
log::error!("Failed to get linear coefficients");
return;
}
};

let linear_variances = fit_statistics.linear_coefficients_variance();

let mut params: Vec<GaussianParams> = Vec::new();
Expand Down
15 changes: 11 additions & 4 deletions src/histoer/histogram1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ impl PlotSettings {
self.egui_settings.menu_button(ui);
self.markers.menu_button(ui);
}

fn interactive_response(&mut self, response: &egui_plot::PlotResponse<()>) {
self.markers.interactive_dragging(response);
}
}

#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
Expand Down Expand Up @@ -370,6 +374,7 @@ impl Histogram {
ui.label("R: Add Region Marker");
ui.label("-: Remove Marker Closest to Cursor");
ui.label("Delete: Remove All Markers and Temp Fits");
ui.label("Middle: Move marker").on_hover_text("Markers can be dragged to new positions with the middle mouse button when hovered over center point");
ui.separator();
ui.label("Fitting");
ui.label("G: Fit Background").on_hover_text("Fit a linear background using the background markers");
Expand Down Expand Up @@ -448,13 +453,15 @@ impl Histogram {
ui.vertical(|ui| {
self.fits.fit_stats_ui(ui);

plot.show(ui, |plot_ui| {
let plot_response = plot.show(ui, |plot_ui| {
self.draw(plot_ui);
})
.response
.context_menu(|ui| {
});

plot_response.response.context_menu(|ui| {
self.context_menu(ui);
});

self.plot_settings.interactive_response(&plot_response);
});
}
}
Loading

0 comments on commit 718db8d

Please sign in to comment.