Skip to content

Instantly share code, notes, and snippets.

@thomcc
Created February 5, 2021 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomcc/aae69acb5bbd2231f446227437c760a0 to your computer and use it in GitHub Desktop.
Save thomcc/aae69acb5bbd2231f446227437c760a0 to your computer and use it in GitHub Desktop.
//! See https://github.com/ocornut/imgui/issues/3606
use imgui::*;
mod support;
fn main() {
let system = support::init(file!());
system.main_loop(move |_, ui| {
fx_window(ui, 0, |d, a, _b, sz, _mouse, t| {
for n in 0..(((1.0 + (t * 5.7).sin()) * 40.0) as isize).max(0) {
d.add_circle(
[a[0] + sz[0] * 0.5, a[1] + sz[1] * 0.5],
sz[1] * (0.01 + (n as f32) * 0.03),
col32(255, (140 - n * 4) as u8, (n * 3) as u8, 255),
).build();
}
});
fx_window(ui, 1, |d, a, b, sz, _m, t0| {
for t in (0..100).map(|t| (t as f32 + t0) / 100.0) {
let mut cp0 = [a[0], b[1]];
let mut cp1 = b;
let ts = t - t0;
cp0[0] += (0.4 + t.sin() * 0.3) * sz[0];
cp1[0] -= (0.4 + t.cos() * 0.4) * sz[0];
cp0[1] -= (0.5 + (ts * ts).cos() * 0.4) * sz[1];
cp1[1] -= (0.5 + (ts * t).sin() * 0.3) * sz[1];
d.add_bezier_curve([a[0], b[1]], cp0, cp1, b, col32((100f32 + (ts*150f32)) as u8, (255f32 - ts*150f32) as u8, 60, (ts * 200f32) as u8))
.thickness(5.0)
.build();
}
});
});
}
fn col32(r: u8, g: u8, b: u8, a: u8) -> ImColor32 {
ImColor32::from_rgba(r, g, b, a)
}
type V2 = [f32; 2];
fn fx_window(ui: &Ui<'_>, n: usize, fx: impl FnOnce(&DrawListMut<'_>, V2, V2, V2, V2, f32)) {
let pos = [10.0 + (n / 3) as f32 * 350.0, 10.0 + (n % 3) as f32 * 250.0];
Window::new(&im_str!("FX {}", n))
.position(pos, Condition::FirstUseEver)
// .size([320.0, 180.0], Condition::FirstUseEver)
.always_auto_resize(true)
.build(ui, || {
let size = [320.0, 180.0];
ui.invisible_button(im_str!("canvas"), size);
let p0 = ui.item_rect_min();
let p1 = ui.item_rect_max();
let mouse_data = {
let io = ui.io();
[
(io.mouse_pos[0] - p0[0]) / size[0],
(io.mouse_pos[1] - p0[1]) / size[1],
// FIXME: why isn't `mouse_down_duration` pub...
// io.mouse_down_duration[0],
// io.mouse_down_duration[1],
]
};
let draw_list = ui.get_window_draw_list();
draw_list.with_clip_rect(p0, p1, || {
fx(&draw_list, p0, p1, size, mouse_data, ui.time() as f32);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment