2020-12-29 12:40:11 +00:00
|
|
|
use egui::{containers::*, *};
|
2020-11-02 16:53:28 +00:00
|
|
|
|
2021-01-04 00:44:02 +00:00
|
|
|
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
|
|
|
|
#[cfg_attr(feature = "persistence", serde(default))]
|
2020-11-02 16:53:28 +00:00
|
|
|
pub struct DancingStrings {}
|
|
|
|
|
|
|
|
impl Default for DancingStrings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 16:11:05 +00:00
|
|
|
impl super::Demo for DancingStrings {
|
2020-11-02 16:53:28 +00:00
|
|
|
fn name(&self) -> &str {
|
2020-12-12 18:43:12 +00:00
|
|
|
"♫ Dancing Strings"
|
2020-11-02 16:53:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 13:48:04 +00:00
|
|
|
fn show(&mut self, ctx: &CtxRef, open: &mut bool) {
|
2021-01-01 16:11:05 +00:00
|
|
|
use super::View;
|
2020-11-02 16:53:28 +00:00
|
|
|
Window::new(self.name())
|
|
|
|
.open(open)
|
|
|
|
.default_size(vec2(512.0, 256.0))
|
|
|
|
.scroll(false)
|
|
|
|
.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) {
|
|
|
|
Frame::dark_canvas(ui.style()).show(ui, |ui| {
|
|
|
|
ui.ctx().request_repaint();
|
|
|
|
let time = ui.input().time;
|
|
|
|
|
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
|
|
|
|
|
|
|
let mut cmds = vec![];
|
|
|
|
|
|
|
|
for &mode in &[2, 3, 5] {
|
|
|
|
let mode = mode as f32;
|
|
|
|
let n = 120;
|
|
|
|
let speed = 1.5;
|
|
|
|
|
|
|
|
let points: Vec<Pos2> = (0..=n)
|
|
|
|
.map(|i| {
|
|
|
|
let t = i as f32 / (n as f32);
|
|
|
|
let amp = (time as f32 * speed * mode).sin() / mode;
|
2020-12-27 10:24:08 +00:00
|
|
|
let y = amp * (t * std::f32::consts::TAU / 2.0 * mode).sin();
|
2020-11-02 16:53:28 +00:00
|
|
|
|
|
|
|
pos2(
|
|
|
|
lerp(rect.x_range(), t),
|
|
|
|
remap(y, -1.0..=1.0, rect.y_range()),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let thickness = 10.0 / mode;
|
|
|
|
cmds.push(paint::PaintCmd::line(
|
|
|
|
points,
|
2021-01-03 17:03:11 +00:00
|
|
|
Stroke::new(thickness, Color32::from_additive_luminance(196)),
|
2020-11-02 16:53:28 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.painter().extend(cmds);
|
|
|
|
});
|
2020-12-29 12:40:11 +00:00
|
|
|
ui.add(crate::__egui_github_link_file!());
|
2020-11-02 16:53:28 +00:00
|
|
|
}
|
|
|
|
}
|