Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

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

121 changes: 121 additions & 0 deletions core/src/animations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use std::hash::Hash;

pub trait EasingFunction {
fn ease(t: f64) -> f64;
}

#[derive(Copy, Clone, Debug)]
pub enum AnimationRepeat {
Once,
Loop,
PingPong,
}

#[derive(Copy, Clone, Debug)]
pub struct Animation<F: EasingFunction> {
start_time: std::time::Instant,
duration: std::time::Duration,
repeat: AnimationRepeat,
_phantom: std::marker::PhantomData<F>,
}

impl<F: EasingFunction> Default for Animation<F> {
fn default() -> Self {
Self::new(std::time::Duration::from_secs(1), AnimationRepeat::Once)
}
}

impl<F: EasingFunction> Hash for Animation<F> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.start_time.hash(state);
self.duration.hash(state);
(((u32::MAX as f64) * self.get_value()) as u32).hash(state);
}
}

impl<F: EasingFunction> Animation<F> {
pub fn new(duration: std::time::Duration, repeat: AnimationRepeat) -> Self {
Self {
start_time: std::time::Instant::now(),
duration,
repeat,
_phantom: std::marker::PhantomData,
}
}

pub fn reset(&mut self) {
self.start_time = std::time::Instant::now();
}

pub fn get_value(&self) -> f64 {
let t = self.start_time.elapsed().as_secs_f64() / self.duration.as_secs_f64();
match self.repeat {
AnimationRepeat::Once => {
if t >= 1.0 {
1.0
} else {
F::ease(t)
}
}
AnimationRepeat::Loop => F::ease(t % 1.0),
AnimationRepeat::PingPong => {
let t = t % 2.0;
if t >= 1.0 {
F::ease(2.0 - t)
} else {
F::ease(t)
}
}
}
}
}

pub mod easing_functions {
use super::EasingFunction;

pub struct Linear;
impl EasingFunction for Linear {
fn ease(t: f64) -> f64 {
t
}
}

pub struct Quadratic;
impl EasingFunction for Quadratic {
fn ease(t: f64) -> f64 {
t * t
}
}

pub struct Cubic;
impl EasingFunction for Cubic {
fn ease(t: f64) -> f64 {
t * t * t
}
}

pub struct EaseOutQuadratic;
impl EasingFunction for EaseOutQuadratic {
fn ease(t: f64) -> f64 {
-t * (t - 2.0)
}
}

pub struct EaseInQuadratic;
impl EasingFunction for EaseInQuadratic {
fn ease(t: f64) -> f64 {
t * t
}
}

pub struct EaseInOutQuadratic;
impl EasingFunction for EaseInOutQuadratic {
fn ease(t: f64) -> f64 {
if t < 0.5 {
2.0 * t * t
} else {
-1.0 + (4.0 - 2.0 * t) * t
}
}
}
}
3 changes: 3 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub mod reexports {
#[macro_use]
pub mod widgets;

#[macro_use]
pub mod animations;

pub mod types;
pub use types::*;

Expand Down
17 changes: 17 additions & 0 deletions examples/animations/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "animations"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
mctk_core = { path = "../../core", version = "0.1.0" }
mctk_smithay = { path = "../../backends/smithay", version = "0.1.0" }
mctk_macros = { path = "../../macros", version = "0.1.0" }
smithay-client-toolkit = "0.18.0"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"]}
anyhow = { version = "1.0.75", features = ["backtrace"]}
tokio = { version = "1.33", features = ["full"] }
lazy_static = "1.5.0"
rand = "0.8.5"
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/animations/src/assets/icons/eye.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading