2020-12-29 12:40:11 +00:00
|
|
|
use egui::{containers::*, *};
|
2020-11-02 16:53:28 +00:00
|
|
|
|
2021-03-31 21:12:42 +00:00
|
|
|
#[derive(Default)]
|
2021-09-29 06:45:13 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
|
|
|
#[cfg_attr(feature = "serde", serde(default))]
|
2020-11-02 16:53:28 +00:00
|
|
|
pub struct DancingStrings {}
|
|
|
|
|
2021-01-01 16:11:05 +00:00
|
|
|
impl super::Demo for DancingStrings {
|
2021-02-20 08:18:23 +00:00
|
|
|
fn name(&self) -> &'static str {
|
2020-12-12 18:43:12 +00:00
|
|
|
"♫ Dancing Strings"
|
2020-11-02 16:53:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 22:13:10 +00:00
|
|
|
fn show(&mut self, ctx: &Context, open: &mut bool) {
|
2021-10-07 19:39:04 +00:00
|
|
|
use super::View as _;
|
2020-11-02 16:53:28 +00:00
|
|
|
Window::new(self.name())
|
|
|
|
.open(open)
|
|
|
|
.default_size(vec2(512.0, 256.0))
|
2021-08-28 11:18:21 +00:00
|
|
|
.vscroll(false)
|
2020-11-02 16:53:28 +00:00
|
|
|
.show(ctx, |ui| self.ui(ui));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 16:11:05 +00:00
|
|
|
impl super::View for DancingStrings {
|
2020-11-02 16:53:28 +00:00
|
|
|
fn ui(&mut self, ui: &mut Ui) {
|
2022-03-14 11:33:17 +00:00
|
|
|
let color = if ui.visuals().dark_mode {
|
|
|
|
Color32::from_additive_luminance(196)
|
|
|
|
} else {
|
|
|
|
Color32::from_black_alpha(240)
|
|
|
|
};
|
|
|
|
|
|
|
|
Frame::canvas(ui.style()).show(ui, |ui| {
|
2020-11-02 16:53:28 +00:00
|
|
|
ui.ctx().request_repaint();
|
2023-01-25 09:24:23 +00:00
|
|
|
let time = ui.input(|i| i.time);
|
2020-11-02 16:53:28 +00:00
|
|
|
|
2020-12-08 20:18:33 +00:00
|
|
|
let desired_size = ui.available_width() * vec2(1.0, 0.35);
|
2020-12-26 00:38:26 +00:00
|
|
|
let (_id, rect) = ui.allocate_space(desired_size);
|
2020-11-02 16:53:28 +00:00
|
|
|
|
2021-02-14 09:33:44 +00:00
|
|
|
let to_screen =
|
2021-02-14 09:44:46 +00:00
|
|
|
emath::RectTransform::from_to(Rect::from_x_y_ranges(0.0..=1.0, -1.0..=1.0), rect);
|
2021-02-14 09:33:44 +00:00
|
|
|
|
2021-01-10 10:43:01 +00:00
|
|
|
let mut shapes = vec![];
|
2020-11-02 16:53:28 +00:00
|
|
|
|
|
|
|
for &mode in &[2, 3, 5] {
|
2021-10-24 16:38:44 +00:00
|
|
|
let mode = mode as f64;
|
2020-11-02 16:53:28 +00:00
|
|
|
let n = 120;
|
|
|
|
let speed = 1.5;
|
|
|
|
|
|
|
|
let points: Vec<Pos2> = (0..=n)
|
|
|
|
.map(|i| {
|
2021-10-24 16:38:44 +00:00
|
|
|
let t = i as f64 / (n as f64);
|
|
|
|
let amp = (time * speed * mode).sin() / mode;
|
|
|
|
let y = amp * (t * std::f64::consts::TAU / 2.0 * mode).sin();
|
|
|
|
to_screen * pos2(t as f32, y as f32)
|
2020-11-02 16:53:28 +00:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2021-10-24 16:38:44 +00:00
|
|
|
let thickness = 10.0 / mode as f32;
|
2022-03-14 11:33:17 +00:00
|
|
|
shapes.push(epaint::Shape::line(points, Stroke::new(thickness, color)));
|
2020-11-02 16:53:28 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 10:43:01 +00:00
|
|
|
ui.painter().extend(shapes);
|
2020-11-02 16:53:28 +00:00
|
|
|
});
|
2021-01-11 22:50:50 +00:00
|
|
|
ui.vertical_centered(|ui| {
|
2022-04-25 20:01:32 +00:00
|
|
|
ui.add(crate::egui_github_link_file!());
|
2021-01-11 22:50:50 +00:00
|
|
|
});
|
2020-11-02 16:53:28 +00:00
|
|
|
}
|
|
|
|
}
|