-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaesthetics.rs
916 lines (880 loc) · 33.6 KB
/
aesthetics.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
use crate::escher::{ArrowTag, CircleTag, Hover, Tag};
use crate::funcplot::{
build_grad, from_grad_clamped, lerp, max_f32, min_f32, path_to_vec, plot_box_point, plot_hist,
plot_kde, plot_line, plot_scales, zero_lerp, IgnoreSave,
};
use crate::geom::{
AesFilter, AnyTag, Drag, GeomArrow, GeomHist, GeomMetabolite, HistPlot, HistTag, PopUp, Side,
VisCondition, Xaxis, YCategory,
};
use crate::gui::{or_color, ActiveData, UiState};
use core::f32;
use itertools::Itertools;
use std::collections::HashMap;
use bevy::prelude::*;
// use bevy_prototype_lyon::prelude::*;
use bevy_prototype_lyon::prelude::*;
pub struct AesPlugin;
impl Plugin for AesPlugin {
fn build(&self, app: &mut App) {
app.add_event::<RestoreEvent>()
.add_systems(Update, plot_arrow_size)
.add_systems(Update, plot_metabolite_size)
.add_systems(Update, plot_arrow_color)
.add_systems(Update, plot_metabolite_color)
.add_systems(Update, restore_geoms::<CircleTag>)
.add_systems(Update, restore_geoms::<ArrowTag>)
.add_systems(Update, normalize_histogram_height)
.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))
.add_systems(Update, (plot_side_hist, plot_hover_hist))
.add_systems(Update, (plot_side_box, change_color.before(plot_side_box)));
}
}
#[derive(Component)]
pub struct Aesthetics {
/// ordered identifers that each aesthetic will be plotted at
pub identifiers: Vec<String>,
/// ordered condition identifiers
pub condition: Option<String>,
}
#[derive(Component)]
pub struct Gy {}
/// Data from the variables is allocated here.
#[derive(Component)]
pub struct Point<T>(pub Vec<T>);
#[derive(Component)]
pub struct Distribution<T>(pub Vec<Vec<T>>);
#[derive(Component)]
pub struct Gsize {}
#[derive(Component)]
pub struct Gcolor {}
/// Marker to avoid scaling some Entities with HistTag.
#[derive(Component)]
pub struct Unscale;
/// Marker for things that need to change the color when UiChanges.
#[derive(Component)]
struct ColorListener {
value: f32,
min_val: f32,
max_val: f32,
}
/// Everytime this is sent, all data and plots are removed, leaving
/// the map as default. This is triggered when new data is added.
#[derive(Event)]
pub struct RestoreEvent;
/// Plot arrow size.
pub fn plot_arrow_size(
ui_state: Res<UiState>,
mut query: Query<(&mut Stroke, &ArrowTag)>,
mut aes_query: Query<(&Point<f32>, &Aesthetics, &GeomArrow), With<Gsize>>,
) {
for (sizes, aes, _geom) in aes_query.iter_mut() {
if let Some(condition) = &aes.condition {
if condition != &ui_state.condition {
continue;
}
}
let min_val = min_f32(&sizes.0);
let max_val = max_f32(&sizes.0);
for (mut stroke, arrow) in query.iter_mut() {
if let Some(index) = aes.identifiers.iter().position(|r| r == &arrow.id) {
let unscaled_width = sizes.0[index];
let f = if ui_state.zero_white { zero_lerp } else { lerp };
stroke.options.line_width = f(
unscaled_width,
min_val,
max_val,
ui_state.min_reaction,
ui_state.max_reaction,
);
} else {
stroke.options.line_width = 10.;
}
}
}
}
/// Plot Color as numerical variable in circles.
pub fn plot_arrow_color(
ui_state: Res<UiState>,
mut query: Query<(&mut Stroke, &ArrowTag), Without<Fill>>,
mut aes_query: Query<(&Point<f32>, &Aesthetics, &GeomArrow), With<Gcolor>>,
) {
for (colors, aes, _) in aes_query.iter_mut() {
if let Some(condition) = &aes.condition {
if condition != &ui_state.condition {
continue;
}
}
let min_val = min_f32(&colors.0);
let max_val = max_f32(&colors.0);
let grad = build_grad(
ui_state.zero_white,
min_val,
max_val,
&ui_state.min_reaction_color,
&ui_state.max_reaction_color,
);
for (mut stroke, tag) in query.iter_mut() {
if let Some(index) = aes.identifiers.iter().position(|r| r == tag.id()) {
stroke.color = from_grad_clamped(&grad, colors.0[index], min_val, max_val);
} else {
stroke.color = Color::srgb(0.85, 0.85, 0.85);
}
}
}
}
/// Plot Color as numerical variable in Circles.
pub fn plot_metabolite_color(
ui_state: Res<UiState>,
mut query: Query<(&mut Fill, &CircleTag)>,
mut aes_query: Query<(&Point<f32>, &Aesthetics, &GeomMetabolite), With<Gcolor>>,
) {
for (colors, aes, _) in aes_query.iter_mut() {
if let Some(condition) = &aes.condition {
if condition != &ui_state.condition {
continue;
}
}
let min_val = min_f32(&colors.0);
let max_val = max_f32(&colors.0);
let grad = build_grad(
ui_state.zero_white,
min_val,
max_val,
&ui_state.min_metabolite_color,
&ui_state.max_metabolite_color,
);
for (mut fill, tag) in query.iter_mut() {
if let Some(index) = aes.identifiers.iter().position(|r| r == tag.id()) {
fill.color = from_grad_clamped(&grad, colors.0[index], min_val, max_val);
} else {
fill.color = Color::srgb(0.85, 0.85, 0.85);
}
}
}
}
/// Plot size as numerical variable in metabolic circles.
pub fn plot_metabolite_size(
ui_state: Res<UiState>,
mut query: Query<(&mut Shape, &CircleTag)>,
mut aes_query: Query<(&Point<f32>, &Aesthetics), (With<Gsize>, With<GeomMetabolite>)>,
) {
for (sizes, aes) in aes_query.iter_mut() {
if let Some(condition) = &aes.condition {
if condition != &ui_state.condition {
continue;
}
}
let min_val = min_f32(&sizes.0);
let max_val = max_f32(&sizes.0);
for (mut path, arrow) in query.iter_mut() {
let radius = if let Some(index) = aes.identifiers.iter().position(|r| r == &arrow.id) {
lerp(
sizes.0[index],
min_val,
max_val,
ui_state.min_metabolite,
ui_state.max_metabolite,
)
} else {
20.
};
let polygon = shapes::RegularPolygon {
sides: 6,
feature: shapes::RegularPolygonFeature::Radius(radius),
..shapes::RegularPolygon::default()
};
*path = ShapePath::build_as(&polygon);
}
}
}
/// Remove colors and sizes from circles and arrows after new data is dropped.
fn restore_geoms<T: Tag>(
mut restore_event: EventReader<RestoreEvent>,
mut query: ParamSet<(
Query<(&mut Fill, &mut Shape), With<T>>,
Query<&mut Stroke, (With<T>, Without<Fill>)>,
)>,
) {
for _ in restore_event.read() {
for (mut fill, mut path) in query.p0().iter_mut() {
// met colors
fill.color = T::default_color();
let polygon = shapes::RegularPolygon {
sides: 6,
feature: shapes::RegularPolygonFeature::Radius(20.),
..shapes::RegularPolygon::default()
};
// met size
*path = ShapePath::build_as(&polygon);
}
for mut stroke in query.p1().iter_mut() {
stroke.color = T::default_color();
stroke.options.line_width = 10.0;
}
}
}
/// Build axes for histograms, summarising all external information.
/// Each Side of an arrow is assigned a different axis, shared across conditions.
fn build_axes(
mut commands: Commands,
mut query: Query<(&Transform, &ArrowTag, &Shape)>,
mut aes_query: Query<
(&Distribution<f32>, &Aesthetics, &mut GeomHist),
(With<Gy>, Without<PopUp>),
>,
) {
let mut axes: HashMap<String, HashMap<Side, (Xaxis, Transform)>> = HashMap::new();
let mut means: HashMap<Side, Vec<f32>> = HashMap::new();
// first gather all x-limits for different conditions and the arrow and side
for (dist, aes, mut geom) in aes_query.iter_mut() {
if geom.in_axis {
continue;
}
means.entry(geom.side.clone()).or_default().push(
dist.0
.iter()
.map(|cloud| cloud.iter().sum::<f32>() / cloud.len() as f32)
.sum::<f32>()
/ dist.0.len() as f32,
);
let xlimits = (
min_f32(&dist.0.iter().map(|x| min_f32(x)).collect::<Vec<f32>>()),
max_f32(&dist.0.iter().map(|x| max_f32(x)).collect::<Vec<f32>>()),
);
for (trans, arrow, path) in query.iter_mut() {
if aes.identifiers.iter().any(|r| r == &arrow.id) {
let size = path_to_vec(path).length();
let (rotation_90, away) = match geom.side {
Side::Right => (-Vec2::Y.angle_to(arrow.direction.perp()), -30.),
Side::Left => (-Vec2::NEG_Y.angle_to(arrow.direction.perp()), 30.),
_ => {
warn!("Tried to plot Up direction for non-popup '{}'", arrow.id);
continue;
}
};
let transform: Transform = if let Some(Some(ser_transform)) =
arrow.hists.as_ref().map(|x| x.get(&geom.side))
{
// there were saved histogram positions
ser_transform.clone().into()
} else {
// histogram perpendicular to the direction of the arrow
// the arrow direction is decided by a fallible heuristic!
let mut transform =
Transform::from_xyz(trans.translation.x, trans.translation.y, 0.5)
.with_rotation(Quat::from_rotation_z(rotation_90));
transform.translation.x += arrow.direction.perp().x * away;
transform.translation.y += arrow.direction.perp().y * away;
transform
};
let axis_entry = axes
.entry(arrow.id.clone())
.or_default()
.entry(geom.side.clone())
.or_insert((
Xaxis {
id: arrow.id.clone(),
arrow_size: size,
xlimits,
side: geom.side.clone(),
node_id: arrow.node_id,
conditions: Vec::new(),
},
transform,
));
axis_entry.0.xlimits = (
f32::min(axis_entry.0.xlimits.0, xlimits.0),
f32::max(axis_entry.0.xlimits.1, xlimits.1),
);
if let Some(cond) = aes.condition.as_ref() {
axis_entry.0.conditions.push(cond.clone());
}
geom.in_axis = true;
}
}
}
for (_, _, mut geom) in aes_query.iter_mut() {
if let Some(side_means) = means.get(&geom.side) {
geom.mean = Some(side_means.iter().sum::<f32>() / side_means.len() as f32);
}
}
for (axis, trans) in axes.into_values().flat_map(|side| side.into_values()) {
let size = axis.arrow_size;
commands.spawn((axis, Drag::default(), plot_line(size, trans)));
}
}
/// Build axis.
fn build_point_axes(
mut commands: Commands,
mut query: Query<(&Transform, &ArrowTag, &Shape)>,
mut aes_query: Query<
(&Aesthetics, &mut GeomHist),
(With<Gy>, Without<PopUp>, With<Point<f32>>),
>,
) {
let mut axes: HashMap<String, HashMap<Side, (Xaxis, Transform)>> = HashMap::new();
// first gather all x-limits for different conditions and the arrow and side
for (aes, mut geom) in aes_query.iter_mut() {
if geom.in_axis {
continue;
}
for (trans, arrow, path) in query.iter_mut() {
if aes.identifiers.iter().any(|r| r == &arrow.id) {
let size = path_to_vec(path).length();
let (rotation_90, rotation_x, away) = match geom.side {
Side::Right => (-Vec2::Y.angle_to(arrow.direction.perp()), 0.0, -30.),
Side::Left => (
-Vec2::NEG_Y.angle_to(arrow.direction.perp()),
f32::consts::PI,
30.,
),
_ => {
warn!("Tried to plot Up direction for non-popup '{}'", arrow.id);
continue;
}
};
let transform: Transform = if let Some(Some(ser_transform)) =
arrow.hists.as_ref().map(|x| x.get(&geom.side))
{
// there were saved histogram positions
ser_transform.clone().into()
} else {
// histogram perpendicular to the direction of the arrow
// the arrow direction is decided by a fallible heuristic!
let mut transform =
Transform::from_xyz(trans.translation.x, trans.translation.y, 0.5)
.with_rotation(Quat::from_rotation_z(rotation_90));
transform.rotate_x(rotation_x);
transform.translation.x += arrow.direction.perp().x * away;
transform.translation.y += arrow.direction.perp().y * away;
transform
};
let axis_entry = axes
.entry(arrow.id.clone())
.or_default()
.entry(geom.side.clone())
.or_insert((
Xaxis {
id: arrow.id.clone(),
arrow_size: size,
xlimits: (0., 0.),
side: geom.side.clone(),
node_id: arrow.node_id,
conditions: Vec::new(),
},
transform,
));
if let Some(cond) = aes.condition.as_ref() {
axis_entry.0.conditions.push(cond.clone());
}
geom.in_axis = true;
}
}
}
for (mut axis, trans) in axes.into_values().flat_map(|side| side.into_values()) {
// conditions are sorted everywhere to be consistent across dropdowns, etc
axis.conditions.sort();
commands.spawn((
axis,
Drag::default(),
trans,
Unscale {},
Visibility::default(),
));
}
}
fn build_hover_axes(
mut query: Query<&mut Hover>,
mut aes_query: Query<(&Distribution<f32>, &Aesthetics, &mut GeomHist), (With<Gy>, With<PopUp>)>,
) {
let mut axes: HashMap<u64, (f32, f32)> = HashMap::new();
// first gather all x-limits for different conditions and the arrow and side
for (dist, aes, mut geom) in aes_query.iter_mut() {
if geom.in_axis {
continue;
}
for hover in query.iter() {
if hover.xlimits.is_some() {
continue;
}
if let Some(index) = aes.identifiers.iter().position(|r| r == &hover.id) {
let this_dist = match dist.0.get(index) {
Some(d) => d,
None => continue,
};
let xlimits = (min_f32(this_dist), max_f32(this_dist));
let axis_entry = axes.entry(hover.node_id).or_insert(xlimits);
*axis_entry = (
f32::min(axis_entry.0, xlimits.0),
f32::max(axis_entry.1, xlimits.1),
);
geom.in_axis = true;
}
}
}
for (node_id, xlimits) in axes {
for mut hover in query.iter_mut().filter(|h| h.node_id == node_id) {
hover.xlimits = Some(xlimits)
}
}
}
/// Plot histogram as numerical variable next to arrows.
fn plot_side_hist(
mut commands: Commands,
mut z_eps: Local<f32>,
mut aes_query: Query<
(&Distribution<f32>, &Aesthetics, &mut GeomHist, &AesFilter),
(With<Gy>, Without<PopUp>),
>,
query: Query<(&Transform, &Xaxis)>,
) {
'outer: for (dist, aes, mut geom, is_met) in aes_query.iter_mut() {
if geom.rendered {
continue;
}
// we only need to differentiate the z-index between aes with different
// conditions that could appear in the same axis
*z_eps += 1e-6;
for (trans, axis) in query.iter() {
if let Some(index) = aes
.identifiers
.iter()
.position(|r| (r == &axis.id) & (geom.side == axis.side))
{
let this_dist = match dist.0.get(index) {
Some(d) => d,
None => continue,
};
let line = match geom.plot {
HistPlot::Hist => plot_hist(this_dist, 160, axis.arrow_size, axis.xlimits),
HistPlot::Kde => plot_kde(this_dist, 100, axis.arrow_size, axis.xlimits),
HistPlot::BoxPoint => {
warn!("Tried to plot a BoxPoint from a Distributions. Not Implemented! Consider using a Point as input");
None
}
};
let Some(line) = line else { continue 'outer };
let hex = match geom.side {
// the color is updated by another system given the settings
Side::Right => "7dce9688",
Side::Left => "DA968788",
_ => {
warn!("Tried to plot Up direction for non-popup '{}'", axis.id);
continue;
}
};
commands.spawn((
GeometryBuilder::build_as(&line),
trans.with_translation(trans.translation + Vec3::new(0., 0., *z_eps)),
Fill::color(Color::Srgba(Srgba::hex(hex).unwrap())),
VisCondition {
condition: aes.condition.clone(),
},
HistTag {
side: geom.side.clone(),
node_id: axis.node_id,
follow_scale: true,
},
(*is_met).clone(),
));
}
geom.rendered = true;
}
}
}
fn plot_side_box(
mut commands: Commands,
asset_server: Res<AssetServer>,
ui_state: Res<UiState>,
mut aes_query: Query<
(
&Point<f32>,
&Aesthetics,
&mut GeomHist,
&AesFilter,
&YCategory,
),
(With<Gy>, Without<PopUp>),
>,
mut query: Query<(&mut Transform, &Xaxis), With<Unscale>>,
) {
let font: Handle<Font> = asset_server.load("fonts/FiraSans-Bold.ttf");
for (colors, aes, mut geom, is_box, ycat) in aes_query.iter_mut() {
if geom.rendered {
continue;
}
let min_val = min_f32(&colors.0);
let max_val = max_f32(&colors.0);
let grad = build_grad(
ui_state.zero_white,
min_val,
max_val,
&ui_state.min_reaction_color,
&ui_state.max_reaction_color,
);
for (mut trans, axis) in query.iter_mut() {
for index in aes
.identifiers
.iter()
.positions(|r| (r == &axis.id) & (geom.side == axis.side))
{
match geom.plot {
HistPlot::Hist | HistPlot::Kde => {
warn!(
"Tried to plot a distribution from one point. Coercing to a Box Point!"
);
}
_ => (),
};
let color = from_grad_clamped(&grad, colors.0[index], min_val, max_val);
trans.translation.z += 10.;
let shape = if f32::abs(colors.0[index]) > 1e-7 {
let cond_idx = axis
.conditions
.iter()
.position(|x| x == aes.condition.as_ref().unwrap_or(&String::from("")))
.unwrap_or(0) as f32;
let line_box =
plot_box_point(axis.conditions.len(), cond_idx, ycat.idx[index] as f32);
(
GeometryBuilder::build_as(&line_box),
trans.with_scale(Vec3::new(1., 1., 1.)),
Fill::color(color),
Stroke::new(Color::BLACK, 2.),
)
} else {
let circle_center = if axis.conditions.is_empty() {
0.
} else {
let center = axis
.conditions
.iter()
.position(|x| x == aes.condition.as_ref().unwrap_or(&String::from("")))
.unwrap_or(0) as f32
* 40.0
* 1.2;
center - axis.conditions.len() as f32 * 40.0 * 1.2 / 2.
};
let shape = shapes::Circle {
radius: 10.,
center: Vec2::new(circle_center, 20.),
};
(
GeometryBuilder::build_as(&shape),
trans.with_scale(Vec3::new(1., 1., 1.)),
Fill::color(color),
Stroke::new(Color::BLACK, 2.),
)
};
let mut ent = commands.spawn((
shape,
VisCondition {
condition: aes.condition.clone(),
},
HistTag {
side: geom.side.clone(),
node_id: axis.node_id,
follow_scale: false,
},
ColorListener {
value: colors.0[index],
min_val,
max_val,
},
Unscale {},
(*is_box).clone(),
));
// add a label to the top of the boxes
if let Some(tag) = &ycat.tags[index] {
let mut text_trans = Transform::from_xyz(
// based y on the box size (40.) and the number of conditions
-40. * axis.conditions.len() as f32,
40.0 * ycat.idx[index] as f32 * 1.2 + 20.,
0.,
)
.with_rotation(Quat::from_rotation_z(f32::consts::PI / 2.));
if matches!(geom.side, Side::Left) {
text_trans.rotate_x(f32::consts::PI);
}
ent.with_child((
Text2d(tag.clone()),
TextFont::from_font(font.clone()).with_font_size(12.0),
TextColor::BLACK,
text_trans,
));
}
}
geom.rendered = true;
}
}
}
/// Plot hovered histograms of both metabolites and reactions.
fn plot_hover_hist(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut z_eps: Local<f32>,
mut query: Query<(&Transform, &Hover)>,
mut aes_query: Query<
(&Distribution<f32>, &Aesthetics, &mut GeomHist, &AesFilter),
(With<Gy>, With<PopUp>),
>,
) {
'outer: for (dist, aes, mut geom, is_met) in aes_query.iter_mut() {
if geom.rendered {
continue;
}
// we only need to differentiate the z-index between aes with different
// conditions that could appear in the same axis
*z_eps += 1e-6;
let font: Handle<Font> = asset_server.load("fonts/FiraSans-Bold.ttf");
for (trans, hover) in query.iter_mut() {
if hover.xlimits.is_none() {
continue;
}
if let Some(index) = aes.identifiers.iter().position(|r| r == &hover.id) {
let this_dist = match dist.0.get(index) {
Some(d) => d,
None => continue,
};
let xlimits = hover.xlimits.as_ref().unwrap();
let line = match geom.plot {
HistPlot::Hist => plot_hist(this_dist, 55, 600., *xlimits),
HistPlot::Kde => plot_kde(this_dist, 80, 600., *xlimits),
HistPlot::BoxPoint => {
warn!("Tried to plot a BoxPoint from a Distributions. Not Implemented! Consider using a Point as input");
None
}
};
let Some(line) = line else { continue 'outer };
let transform = Transform::from_xyz(
trans.translation.x + 150.,
trans.translation.y + 150.,
40. + *z_eps,
);
let geometry = (
GeometryBuilder::build_as(&line),
transform,
Visibility::Hidden,
);
let fill = Fill::color(Color::Srgba(Srgba::hex("ffb73388").unwrap()));
let scales = plot_scales::<Text2d>(this_dist, 600., font.clone(), 12.);
commands
.spawn((
HistTag {
side: geom.side.clone(),
node_id: hover.node_id,
follow_scale: false,
},
VisCondition {
condition: aes.condition.clone(),
},
))
.insert((geometry, fill))
.with_children(|p| {
p.spawn((
Sprite::from_image(asset_server.load("hover.png")),
Transform::from_xyz(0., 0., -0.4),
));
})
.with_children(|parent| {
parent.spawn((scales.x_0, IgnoreSave));
})
.with_children(|parent| {
parent.spawn((scales.x_n, IgnoreSave));
})
.with_children(|parent| {
parent.spawn((scales.y, IgnoreSave));
})
.insert((AnyTag { id: hover.node_id }, (*is_met).clone()));
}
geom.rendered = true;
}
}
}
/// Normalize the height of histograms to be comparable with each other.
/// It treats the two sides independently.
fn normalize_histogram_height(
mut ui_state: ResMut<UiState>,
mut query: Query<
(
&mut Transform,
&mut Shape,
&mut Fill,
&HistTag,
&VisCondition,
),
Without<Unscale>,
>,
) {
for (mut trans, path, mut fill, hist, condition) in query.iter_mut() {
let height = max_f32(&path.0.iter().map(|ev| ev.to().y).collect::<Vec<f32>>());
trans.scale.y = match hist.side {
Side::Left => ui_state.max_left / height,
Side::Right => ui_state.max_right / height,
Side::Up => ui_state.max_top / height,
};
let ui_condition = ui_state.condition.clone();
fill.color = {
let color_ref = match hist.side {
Side::Left => &mut ui_state.color_left,
Side::Right => &mut ui_state.color_right,
Side::Up => &mut ui_state.color_top,
};
let color = match condition.condition.as_ref() {
Some(cond) => or_color(cond, color_ref, true),
None => or_color(&ui_condition, color_ref, false),
};
Color::linear_rgba(color.r(), color.g(), color.b(), color.a())
}
}
}
/// Propagate color from Ui to color component.
fn change_color(
ui_state: Res<UiState>,
mut query: Query<(&mut Fill, &HistTag, &ColorListener), With<Stroke>>,
) {
let mut gradients: HashMap<&Side, colorgrad::Gradient> = HashMap::new();
if ui_state.is_changed() {
for (mut fill, hist, color) in query.iter_mut() {
let grad = gradients.entry(&hist.side).or_insert(build_grad(
ui_state.zero_white,
color.min_val,
color.max_val,
&ui_state.min_reaction_color,
&ui_state.max_reaction_color,
));
fill.color = from_grad_clamped(grad, color.value, color.min_val, color.max_val);
}
}
}
/// Unscale up children of scaled histograms.
fn unscale_histogram_children(
parents: Query<(Entity, &Children), (With<HistTag>, Without<Unscale>)>,
mut query: Query<&mut Transform>,
) {
for (parent, children) in parents.iter() {
let Ok(scale) = query.get_mut(parent).map(|trans| trans.scale.y) else {
continue;
};
for child in children {
let Ok(mut trans) = query.get_mut(*child) else {
continue;
};
trans.scale.y = 1. / scale;
}
}
}
/// Fill conditions menu.
fn fill_conditions(mut ui_state: ResMut<UiState>, aesthetics: Query<&Aesthetics>) {
let conditions = {
let mut conditions = aesthetics
.iter()
.filter_map(|a| a.condition.clone())
.unique()
.collect::<Vec<String>>();
conditions.sort();
conditions
};
if conditions
.iter()
.any(|cond| !ui_state.conditions.contains(cond))
{
if !conditions.is_empty() {
ui_state.conditions = conditions;
if !ui_state.conditions.contains(&String::from("ALL")) {
ui_state.conditions.push(String::from("ALL"));
}
} else {
ui_state.conditions = vec![String::from("")];
ui_state.condition = String::from("");
}
if ui_state.condition.is_empty() {
ui_state.condition = ui_state.conditions[0].clone();
}
}
}
/// Hide histograms that are not in the conditions.
pub fn filter_histograms(
ui_state: Res<UiState>,
mut query: Query<(&mut Visibility, &VisCondition), Without<AnyTag>>,
) {
for (mut vis, cond) in query.iter_mut() {
if let Some(condition) = &cond.condition {
if (condition != &ui_state.condition) & (ui_state.condition != "ALL") {
*vis = Visibility::Hidden;
} else {
*vis = Visibility::Visible;
}
}
}
}
/// Coordinate the position of histograms with their `Xaxis`.
fn follow_the_axes(
axes: Query<(&Transform, &Xaxis), Changed<Transform>>,
mut hists: Query<(&mut Transform, &HistTag), (Without<AnyTag>, Without<Xaxis>)>,
) {
for (axis_trans, axis) in axes.iter() {
for (mut trans, hist) in hists.iter_mut() {
if (axis.node_id == hist.node_id) & (hist.side == axis.side) {
// z has to be maintained per element in the axis to avoid flickering
trans.translation.x = axis_trans.translation.x;
trans.translation.y = axis_trans.translation.y;
trans.rotation = axis_trans.rotation;
if hist.follow_scale {
trans.scale.x = axis_trans.scale.x;
}
}
}
}
}
/// 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_or_boxes: Query<(&Aesthetics, &Point<f32>), Or<(With<GeomArrow>, With<GeomHist>)>>,
circles: Query<(&Aesthetics, &Point<f32>), With<GeomMetabolite>>,
hists: Query<(&Aesthetics, &GeomHist), With<Distribution<f32>>>,
) {
active_data.arrow = arrows_or_boxes
.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),
)
});
}