Skip to content

Commit

Permalink
Try out animation
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Sep 24, 2023
1 parent 558beaa commit ea2faf2
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ glutin-winit = { version = "0.3.0", optional = true }
raw-window-handle = { version = "0.5.2", optional = true }
skia-safe = { version = "0.64.0", features = ["gl"], optional = true }
winit = { version = "0.28.6", optional = true }
interpolation = "0.3.0"

44 changes: 44 additions & 0 deletions examples/animation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::{
thread::{self, sleep},
time::{Duration, Instant},
};

use skia_safe::Color4f;
use taffy::{
prelude::Size,
style::{AlignItems, JustifyContent},
};
use viewbuilder::{render::UserEvent, Element, Renderer, Tree};

fn main() {
// TODO this is really early stage, an animation frame should be requested

let mut tree = Tree::default();
let root = Element::new()
.align_items(AlignItems::Center)
.justify_content(JustifyContent::Center)
.size(Size::from_points(100., 100.))
.background_color(Color4f::new(0., 0., 1., 1.))
.build(&mut tree);

let renderer = Renderer::new();
let tx = renderer.tx.clone();

thread::spawn(move || {
let start = Instant::now();
loop {
let t = (Instant::now() - start).as_millis() as f32;
let s: f32 = interpolation::lerp(&0., &100., &(t / 1000.));

tx.send(UserEvent(Box::new(move |tree| {
tree.element(root)
.set_size(Size::from_points(s as f32, s as f32))
})))
.unwrap();

sleep(Duration::from_millis(5))
}
});

renderer.run(tree, root)
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use tree::{NodeRef, Tree};
pub mod element;
pub use element::Element;

mod render;
pub mod render;
pub use render::Renderer;

pub mod event;
Expand Down
23 changes: 19 additions & 4 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ use slotmap::DefaultKey;
use std::{
ffi::CString,
num::NonZeroU32,
time::{Duration, Instant},
time::{Duration, Instant}, sync::mpsc,
};
use winit::{
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::{ControlFlow, EventLoop, EventLoopBuilder},
window::{Window, WindowBuilder},
};

use crate::{event, Tree};

pub struct UserEvent(pub Box<dyn FnOnce(&mut Tree) + Send>);

// Guarantee the drop order inside the FnMut closure. `Window` _must_ be dropped after
// `DirectContext`.
//
Expand All @@ -40,15 +42,17 @@ pub struct Renderer {
gr_context: skia_safe::gpu::DirectContext,
gl_context: PossiblyCurrentContext,
window: Window,
event_loop: EventLoop<()>,
event_loop: EventLoop<UserEvent>,
num_samples: usize,
stencil_size: usize,
fb_info: FramebufferInfo,
pub tx: mpsc::Sender<UserEvent>,
rx: mpsc::Receiver<UserEvent>,
}

impl Renderer {
pub fn new() -> Self {
let el = EventLoop::new();
let el = EventLoopBuilder::with_user_event().build();
let winit_window_builder = WindowBuilder::new().with_title("Viewbuilder");

let template = ConfigTemplateBuilder::new()
Expand Down Expand Up @@ -154,6 +158,8 @@ impl Renderer {

let surface = create_surface(&window, fb_info, &mut gr_context, num_samples, stencil_size);

let (tx, rx) = mpsc::channel();

Self {
surface,
gl_surface,
Expand All @@ -164,6 +170,8 @@ impl Renderer {
num_samples,
stencil_size,
fb_info,
tx,
rx,
}
}

Expand All @@ -174,10 +182,16 @@ impl Renderer {
let mut cursor_pos = None;
let mut clicked = None;

let proxy = self.event_loop.create_proxy();

self.event_loop.run(move |event, _, control_flow| {
let frame_start = Instant::now();
let mut draw_frame = false;

if let Ok(event) = self.rx.try_recv() {
proxy.send_event(event).ok().unwrap();
}

#[allow(deprecated)]
match event {
Event::LoopDestroyed => {}
Expand Down Expand Up @@ -297,6 +311,7 @@ impl Renderer {
Event::RedrawRequested(_) => {
draw_frame = true;
}
Event::UserEvent(UserEvent(update)) => update(&mut tree),
_ => (),
}
let expected_frame_length_seconds = 1.0 / 20.0;
Expand Down
7 changes: 7 additions & 0 deletions src/tree/node_ref.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{element::ElementData, node::NodeData, Node, Tree};
use skia_safe::Color4f;
use slotmap::DefaultKey;
use taffy::{prelude::Size, style::Dimension};

/// Reference to an element in a tree.
pub struct NodeRef<'a> {
Expand Down Expand Up @@ -39,6 +40,12 @@ impl<'a> NodeRef<'a> {
self.as_mut().background_color = Some(color);
self.tree.inner.changes.push(self.key);
}

/// Update the background color.
pub fn set_size(&mut self, size: Size<Dimension>) {
self.as_mut().size = Some(size);
self.tree.inner.changes.push(self.key);
}
}

impl<'a> AsMut<ElementData> for NodeRef<'a> {
Expand Down

0 comments on commit ea2faf2

Please sign in to comment.