Skip to content

Commit

Permalink
feat: show the settings only for actively plotted data
Browse files Browse the repository at this point in the history
  • Loading branch information
carrascomj committed Oct 30, 2023
1 parent 74b5af6 commit b3951fc
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/aesthetics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::geom::{
AesFilter, AnyTag, Drag, GeomArrow, GeomHist, GeomMetabolite, HistPlot, HistTag, PopUp, Side,
VisCondition, Xaxis,
};
use crate::gui::{or_color, UiState};
use crate::gui::{or_color, ActiveData, UiState};
use itertools::Itertools;
use std::collections::HashMap;

Expand All @@ -31,6 +31,7 @@ impl Plugin for AesPlugin {
.add_systems(Update, unscale_histogram_children)
.add_systems(Update, fill_conditions)
.add_systems(Update, filter_histograms)
.add_systems(Update, activate_settings)
.add_systems(Update, follow_the_axes)
// TODO: check since these were before load_map
.add_systems(PostUpdate, (build_axes, build_hover_axes, build_point_axes))
Expand Down Expand Up @@ -854,3 +855,48 @@ fn follow_the_axes(
}
}
}

/// Set which data is actively plotted in the screen to show its corresponding
/// settings.
fn activate_settings(
ui_state: ResMut<UiState>,
mut active_data: ResMut<ActiveData>,
arrows: Query<(&Aesthetics, &Point<f32>), With<GeomArrow>>,
circles: Query<(&Aesthetics, &Point<f32>), With<GeomMetabolite>>,
hists: Query<(&Aesthetics, &GeomHist), With<Distribution<f32>>>,
) {
active_data.arrow = arrows
.iter()
// this works because data without a condition should always be shown
.any(|(aes, _)| {
aes.condition
.as_ref()
.map(|c| c == &ui_state.condition)
.unwrap_or(true)
});
active_data.circle = circles.iter().any(|(aes, _)| {
aes.condition
.as_ref()
.map(|c| c == &ui_state.condition)
.unwrap_or(true)
});
(
active_data.histogram.left,
active_data.histogram.right,
active_data.histogram.top,
) = hists
.iter()
.filter(|(aes, _)| {
aes.condition
.as_ref()
.map(|c| c == &ui_state.condition)
.unwrap_or(true)
})
.fold((false, false, false), |(left, right, top), (_, geom)| {
(
left | (geom.side == Side::Left),
right | (geom.side == Side::Right),
top | (geom.side == Side::Up),
)
});
}
37 changes: 37 additions & 0 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl Plugin for GuiPlugin {
.add_plugins(EguiPlugin)
.insert_resource(UiState::default())
.insert_resource(AxisMode::Hide)
.insert_resource(ActiveData::default())
.add_event::<SaveEvent>()
.add_systems(Update, ui_settings)
.add_systems(Update, show_hover)
Expand Down Expand Up @@ -180,6 +181,35 @@ impl UiState {
}
}

#[derive(Default)]
pub struct ActiveHists {
pub left: bool,
pub right: bool,
pub top: bool,
}

#[derive(Default, Resource)]
/// Holds state about what data is being plotted, to then only show relevant
/// options in the Settings at [`ui_settings`].
pub struct ActiveData {
pub arrow: bool,
pub circle: bool,
pub histogram: ActiveHists,
}

impl ActiveData {
fn get(&self, key: &str) -> bool {
match key {
"Reaction" => self.arrow,
"Metabolite" => self.circle,
"left" => self.histogram.left,
"right" => self.histogram.right,
"top" => self.histogram.top,
_ => panic!("{key} should never be an ActiveData key!"),
}
}
}

#[derive(Event)]
pub struct SaveEvent(String);

Expand All @@ -188,6 +218,7 @@ pub struct SaveEvent(String);
pub fn ui_settings(
mut egui_context: EguiContexts,
mut state: ResMut<UiState>,
active_set: Res<ActiveData>,
mut save_events: EventWriter<SaveEvent>,
mut load_events: EventWriter<FileDragAndDrop>,
mut screen_events: EventWriter<ScreenshotEvent>,
Expand All @@ -201,6 +232,9 @@ pub fn ui_settings(
.into_iter()
.cartesian_product(["min", "max"])
{
if !active_set.get(geom) {
continue;
}
if "min" == ext {
ui.label(format!("{geom} scale"));
}
Expand All @@ -215,6 +249,9 @@ pub fn ui_settings(
if condition != "ALL" {
ui.label("Histogram scale");
for side in ["left", "right", "top"] {
if !active_set.get(side) {
continue;
}
ui.horizontal(|ui| {
let (color, value) = state.get_geom_params_mut(side, &condition);
color_edit_button_rgba(ui, color, Alpha::BlendOrAdditive);
Expand Down

0 comments on commit b3951fc

Please sign in to comment.