|
| 1 | +use eframe::egui; |
| 2 | +use eframe::egui::Response; |
| 3 | +use egui_plot::MarkerShape; |
| 4 | +use egui_plot::Plot; |
| 5 | +use egui_plot::Points; |
| 6 | + |
| 7 | +/// Simple LCG pseudo-random number generator. Returns a value in [0.0, 1.0]. |
| 8 | +fn rng(state: &mut u64) -> f64 { |
| 9 | + let mut x = *state; |
| 10 | + x ^= x << 13; |
| 11 | + x ^= x >> 7; |
| 12 | + x ^= x << 17; |
| 13 | + *state = x; |
| 14 | + (x as f64) / (u64::MAX as f64) |
| 15 | +} |
| 16 | + |
| 17 | +fn make_markers(target_count: usize) -> Vec<[f64; 2]> { |
| 18 | + let mut state = 42u64; |
| 19 | + (0..target_count).map(|_| [rng(&mut state), rng(&mut state)]).collect() |
| 20 | +} |
| 21 | + |
| 22 | +pub struct PerformanceDemo { |
| 23 | + target_count: usize, |
| 24 | + marker_radius: f32, |
| 25 | + markers: Vec<[f64; 2]>, |
| 26 | + marker_shape: MarkerShape, |
| 27 | +} |
| 28 | + |
| 29 | +impl Default for PerformanceDemo { |
| 30 | + fn default() -> Self { |
| 31 | + Self { |
| 32 | + target_count: 100, |
| 33 | + marker_radius: 1.0, |
| 34 | + markers: make_markers(100), |
| 35 | + marker_shape: MarkerShape::Circle, |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl PerformanceDemo { |
| 41 | + pub fn show_plot(&self, ui: &mut egui::Ui) -> Response { |
| 42 | + Plot::new("performance_demo") |
| 43 | + .data_aspect(1.0) |
| 44 | + .show(ui, |plot_ui| { |
| 45 | + plot_ui.points( |
| 46 | + Points::new("markers", self.markers.clone()) |
| 47 | + .radius(self.marker_radius) |
| 48 | + .shape(self.marker_shape) |
| 49 | + .filled(true), |
| 50 | + ); |
| 51 | + }) |
| 52 | + .response |
| 53 | + } |
| 54 | + |
| 55 | + pub fn show_controls(&mut self, ui: &mut egui::Ui) -> Response { |
| 56 | + ui.ctx().request_repaint(); // Continuous repaint for FPS counter |
| 57 | + let fps = (1.0 / ui.ctx().input(|i| i.stable_dt)).round(); |
| 58 | + |
| 59 | + ui.horizontal(|ui| { |
| 60 | + ui.label("Markers:"); |
| 61 | + if ui |
| 62 | + .add( |
| 63 | + egui::DragValue::new(&mut self.target_count) |
| 64 | + .speed(100) |
| 65 | + .range(100..=10_000_000), |
| 66 | + ) |
| 67 | + .changed() |
| 68 | + { |
| 69 | + self.markers = make_markers(self.target_count); |
| 70 | + } |
| 71 | + |
| 72 | + ui.label("Radius:"); |
| 73 | + ui.add( |
| 74 | + egui::DragValue::new(&mut self.marker_radius) |
| 75 | + .speed(0.1) |
| 76 | + .range(0.5..=5.0), |
| 77 | + ); |
| 78 | + |
| 79 | + ui.label("Shape:"); |
| 80 | + egui::ComboBox::from_id_salt("marker_shape") |
| 81 | + .selected_text(format!("{:?}", self.marker_shape)) |
| 82 | + .show_ui(ui, |ui| { |
| 83 | + for shape in MarkerShape::all() { |
| 84 | + ui.selectable_value(&mut self.marker_shape, shape, format!("{shape:?}")); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + ui.label(format!("FPS: {fps}")); |
| 89 | + }); |
| 90 | + |
| 91 | + ui.label("Note: Less than 100k markers should work fine, beyond that may cause issues."); |
| 92 | + ui.response() |
| 93 | + } |
| 94 | +} |
0 commit comments