Skip to content

Commit

Permalink
fix: use right Text type for legend UI (reintroduce legend plugin)
Browse files Browse the repository at this point in the history
  • Loading branch information
carrascomj committed Dec 30, 2024
1 parent 9e208aa commit 2a2a478
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/aesthetics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ fn plot_hover_hist(
..default()
};
let fill = Fill::color(Color::Srgba(Srgba::hex("ffb73388").unwrap()));
let scales = plot_scales(this_dist, 600., font.clone(), 12.);
let scales = plot_scales::<Text2d>(this_dist, 600., font.clone(), 12.);
commands
.spawn((
HistTag {
Expand Down
30 changes: 19 additions & 11 deletions src/funcplot.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Functions for plotting data.
use bevy::prelude::{Color, Component, Font, Handle, Text2d, TextColor, TextFont, Transform, Vec2};
use bevy::{
prelude::{Color, Component, Font, Handle, Text2d, TextColor, TextFont, Transform, Vec2},
text::TextRoot,
};
use bevy_prototype_lyon::{
entity::ShapeBundle,
prelude::{GeometryBuilder, Path, PathBuilder, Stroke},
Expand Down Expand Up @@ -178,17 +181,17 @@ pub fn plot_box_point(n_cond: usize, cond_index: usize) -> Path {
path_builder.build()
}

type TextBundle = (Text2d, TextFont, TextColor, Transform);
type TextBundle<T> = (T, TextFont, TextColor, Transform);

/// Three text tags to be used as Components to build a an axis scale.
#[derive(Clone)]
pub struct ScaleBundle {
pub x_0: TextBundle,
pub y: TextBundle,
pub x_n: TextBundle,
pub struct ScaleBundle<T> {
pub x_0: TextBundle<T>,
pub y: TextBundle<T>,
pub x_n: TextBundle<T>,
}

impl ScaleBundle {
impl<T: TextRoot> ScaleBundle<T> {
/// Build text components from minimum, maximum and mean values.
pub fn new(
minimum: f32,
Expand All @@ -202,21 +205,21 @@ impl ScaleBundle {
) -> Self {
// build x component
let x_0 = (
Text2d::new(format!("{:+.3e}", minimum)),
T::from(format!("{:+.3e}", minimum)),
TextFont::from_font(font.clone()).with_font_size(font_size),
TextColor(color),
// to the left so that it is centered
Transform::from_xyz(-size / 2. - font_size * 2., 0., 0.2),
);
let x_n = (
Text2d::new(format!("{:+.3e}", maximum)),
T::from(format!("{:+.3e}", maximum)),
TextFont::from_font(font.clone()).with_font_size(font_size),
TextColor(color),
// to the left so that it is centered
Transform::from_xyz(size / 2., 0., 0.2),
);
let y = (
Text2d::new(format!("{:+.3e}", mean)),
T::from(format!("{:+.3e}", mean)),
TextFont::from_font(font.clone()).with_font_size(font_size),
TextColor(color),
// to the left so that it is centered
Expand All @@ -242,7 +245,12 @@ pub fn plot_line(size: f32, transform: Transform) -> (ShapeBundle, Stroke) {
}

/// Build and position text tags to indicate the scale of thethe x-axis.
pub fn plot_scales(samples: &[f32], size: f32, font: Handle<Font>, font_size: f32) -> ScaleBundle {
pub fn plot_scales<T: TextRoot>(
samples: &[f32],
size: f32,
font: Handle<Font>,
font_size: f32,
) -> ScaleBundle<T> {
let mean: f32 = samples.iter().sum::<f32>() / samples.len() as f32;
let min = min_f32(samples);
let max = max_f32(samples);
Expand Down
12 changes: 7 additions & 5 deletions src/legend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn color_legend_arrow(
mut img_query: Query<&ImageNode>,
// these two queries are to filter Children of legend_query
text_query: Query<Entity, With<Xmin>>,
text_max_query: Query<Entity, Without<Xmin>>,
text_max_query: Query<Entity, (Without<Xmin>, With<Xmax>)>,
point_query: Query<(&Point<f32>, &Aesthetics), (With<Gcolor>, With<GeomArrow>)>,
mut images: ResMut<Assets<Image>>,
) {
Expand Down Expand Up @@ -121,7 +121,7 @@ fn color_legend_circle(
mut legend_query: Query<(Entity, &mut Node, &Children), With<LegendCircle>>,
mut img_query: Query<&ImageNode>,
text_query: Query<Entity, With<Xmin>>,
text_max_query: Query<Entity, Without<Xmin>>,
text_max_query: Query<Entity, (Without<Xmin>, With<Xmax>)>,
point_query: Query<(&Point<f32>, &Aesthetics), (With<Gcolor>, With<GeomMetabolite>)>,
mut images: ResMut<Assets<Image>>,
) {
Expand Down Expand Up @@ -195,7 +195,7 @@ fn color_legend_histograms(
>,
mut img_query: Query<(&ImageNode, &mut BackgroundColor)>,
text_query: Query<Entity, With<Xmin>>,
text_max_query: Query<Entity, Without<Xmin>>,
text_max_query: Query<Entity, (Without<Xmin>, With<Xmax>)>,
) {
if !ui_state.is_changed() {
// the ui_state always changes on the creation of histograms
Expand Down Expand Up @@ -324,7 +324,7 @@ fn color_legend_box(
mut legend_query: Query<(Entity, &mut Node, &Side, &Children), With<LegendBox>>,
mut img_query: Query<&ImageNode>,
text_query: Query<Entity, With<Xmin>>,
text_max_query: Query<Entity, Without<Xmin>>,
text_max_query: Query<Entity, (Without<Xmin>, With<Xmax>)>,
point_query: Query<(&Point<f32>, &Aesthetics, &GeomHist), (With<Gy>, Without<PopUp>)>,
mut images: ResMut<Assets<Image>>,
) {
Expand Down Expand Up @@ -357,7 +357,9 @@ fn color_legend_box(
*writer.text(*child, 0) = format!("{:.2e}", max_val);
} else if let Ok(img_legend) = img_query.get_mut(*child) {
// modify the image inplace
let image = images.get_mut(&img_legend.image).unwrap();
let image = images
.get_mut(&img_legend.image)
.expect("Image handles should have been initialized for legend.");

let width = image.size().x as f64;
let points = linspace(min_val, max_val, width as u32);
Expand Down
86 changes: 73 additions & 13 deletions src/legend/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn build_image(
/// - box legend, same as histogram but with Rects instead of images.
pub fn spawn_legend(mut commands: Commands, asset_server: Res<AssetServer>) {
let font = asset_server.load("fonts/Assistant-Regular.ttf");
let scales_arrow = ScaleBundle::new(
let scales_arrow = ScaleBundle::<Text>::new(
0.,
0.,
0.,
Expand Down Expand Up @@ -133,7 +133,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res<AssetServer>) {
// left box side
.with_children(|p| {
// TODO: check this works as expected
p.spawn((scales_right_box.x_0.0, Xmin));
p.spawn((
scales_right_box.x_0.0,
scales_right_box.x_0.1,
scales_right_box.x_0.2,
Xmin,
));
})
.with_children(|p| {
p.spawn(build_image(
Expand All @@ -143,7 +148,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res<AssetServer>) {
));
})
.with_children(|p| {
p.spawn((scales_right_box.x_n.0, Xmax));
p.spawn((
scales_right_box.x_n.0,
scales_right_box.x_n.1,
scales_right_box.x_n.2,
Xmax,
));
});
})
// container for right box side with text tags for axis
Expand All @@ -163,7 +173,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res<AssetServer>) {
.insert(Side::Right)
// right box side
.with_children(|p| {
p.spawn((scales_left_box.x_0.0, Xmin));
p.spawn((
scales_left_box.x_0.0,
scales_left_box.x_0.1,
scales_left_box.x_0.2,
Xmin,
));
})
.with_children(|p| {
p.spawn(build_image(
Expand All @@ -173,7 +188,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res<AssetServer>) {
));
})
.with_children(|p| {
p.spawn((scales_left_box.x_n.0, Xmax));
p.spawn((
scales_left_box.x_n.0,
scales_left_box.x_n.1,
scales_left_box.x_n.2,
Xmax,
));
});
});
})
Expand All @@ -192,13 +212,23 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res<AssetServer>) {
))
.insert(LegendArrow)
.with_children(|p| {
p.spawn((scales_arrow.x_0.0, Xmin));
p.spawn((
scales_arrow.x_0.0,
scales_arrow.x_0.1,
scales_arrow.x_0.2,
Xmin,
));
})
.with_children(|p| {
p.spawn(build_image(arrow_handle.clone(), ARROW_WIDTH, ARROW_HEIGHT));
})
.with_children(|p| {
p.spawn((scales_arrow.x_n.0, Xmax));
p.spawn((
scales_arrow.x_n.0,
scales_arrow.x_n.1,
scales_arrow.x_n.2,
Xmax,
));
});
})
// metabolite legend
Expand All @@ -216,7 +246,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res<AssetServer>) {
))
.insert(LegendCircle)
.with_children(|p| {
p.spawn((scales_mets.x_0.0, Xmin));
p.spawn((
scales_mets.x_0.0,
scales_mets.x_0.1,
scales_mets.x_0.2,
Xmin,
));
})
.with_children(|p| {
p.spawn(build_image(
Expand All @@ -226,7 +261,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res<AssetServer>) {
));
})
.with_children(|p| {
p.spawn((scales_mets.x_n.0, Xmax));
p.spawn((
scales_mets.x_n.0,
scales_mets.x_n.1,
scales_mets.x_n.2,
Xmax,
));
});
})
// hist legend
Expand Down Expand Up @@ -282,7 +322,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res<AssetServer>) {
.insert(Side::Left)
// left histogram side
.with_children(|p| {
p.spawn((scales_left.x_0.0, Xmin));
p.spawn((
scales_left.x_0.0,
scales_left.x_0.1,
scales_left.x_0.2,
Xmin,
));
})
.with_children(|p| {
p.spawn(build_image(
Expand All @@ -292,7 +337,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res<AssetServer>) {
));
})
.with_children(|p| {
p.spawn((scales_left.x_n.0, Xmax));
p.spawn((
scales_left.x_n.0,
scales_left.x_n.1,
scales_left.x_n.2,
Xmax,
));
});
})
// container for right histogram side with text tags for axis
Expand All @@ -315,7 +365,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res<AssetServer>) {
.insert(Side::Right)
// right histogram side
.with_children(|p| {
p.spawn((scales_right.x_0.0, Xmin));
p.spawn((
scales_right.x_0.0,
scales_right.x_0.1,
scales_right.x_0.2,
Xmin,
));
})
.with_children(|p| {
p.spawn(build_image(
Expand All @@ -325,7 +380,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res<AssetServer>) {
));
})
.with_children(|p| {
p.spawn((scales_right.x_n.0, Xmax));
p.spawn((
scales_right.x_n.0,
scales_right.x_n.1,
scales_right.x_n.2,
Xmax,
));
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn main() {
.add_systems(Startup, setup_system)
.add_plugins(aesthetics::AesPlugin)
.add_plugins(scale::ZoomPlugin)
// .add_plugins(legend::LegendPlugin)
.add_plugins(legend::LegendPlugin)
.run();
}

Expand Down

0 comments on commit 2a2a478

Please sign in to comment.