Skip to main content

KramaFrame

Struct KramaFrame 

Source
pub struct KramaFrame<CL, FL> {
    pub classlist: CL,
    pub framelist: FL,
}
Expand description

The main animation controller.

KramaFrame manages a collection of animation “classes” and their corresponding animation “frames”. A class defines an animation behavior (e.g., easing function), while a frame represents a specific instance of an animation with its own timing and progress.

  • CL: The type for the class list, typically a map from a class name to a KeyFrameFunction.
  • FL: The type for the frame list, typically a map from a class name to a KeyList.

Fields§

§classlist: CL

A list of animation classes, mapping class names to keyframe functions (e.g., “linear”, “ease-in”).

§framelist: FL

A list of animation frames, mapping class names to KeyLists, which track individual animation instances.

Implementations§

Source§

impl<TRES: TimingResolution + Clone, PRES: ProgressResolution + Eq> KramaFrame<BTclasslist, BTreeMap<&'static str, KeyList<TRES, PRES>>>

Source

pub fn extend_iter_classlist<const N: usize>( &mut self, classlist: [(&'static str, KeyFrameFunction); N], )

Extends the class list with new definitions or replaces existing ones.

§Example
krama.extend_iter_classlist([
    ("button1", KeyFrameFunction::Linear),
    ("fade_in", KeyFrameFunction::EaseIn),
]);
Source

pub fn insert_new_class(&mut self, classname: &'static str)

Inserts a new class with a default KeyFrameFunction.

If the class name already exists, its keyframe function will be updated to the default.

Source

pub fn insert_new_id(&mut self, on_classname: &'static str, id: u32, time: TRES)

Inserts a new animation instance (identified by id) for a given class.

If the class name does not exist in the framelist, a new KeyList will be created for it.

Source

pub fn change_timing( &mut self, on_classname: &'static str, id: u32, new_timing: TRES, )

Changes the total duration (new_timing) for a specific animation instance.

Source

pub fn update_progress(&mut self, delta_time: TRES)

Updates the progress of all active animations.

This function should be called in your application’s main loop (e.g., once per frame). It iterates through all registered classes and updates the progress of their associated animation instances based on the elapsed time (delta_time).

§Arguments
  • delta_time: The time elapsed since the last update, typically in seconds.
Source

pub fn restart_progress(&mut self, classname: &'static str, id: u32)

Restarts the progress of a specific animation instance.

This sets its internal timer back to zero.

Source

pub fn get_progress_f32(&self, classname: &'static str, id: u32) -> f32

Gets the current progress of a specific animation instance as a value between 0.0 and 1.0.

Returns 0.0 if the class name or id is not found. Note: This is the raw progress, not yet modified by a KeyFrameFunction.

Source

pub fn remove_classname(&mut self, classname: &'static str)

Source

pub fn replace_classname( &mut self, old_classname: &'static str, new_classname: &'static str, )

Source

pub fn set_timing(&mut self, classname: &'static str, id: u32, timing: TRES)

Source

pub fn get_timing(&self, classname: &'static str, id: u32) -> TRES

Source

pub fn is_reversed(&self, classname: &'static str, id: u32) -> bool

Source

pub fn is_any_animation_inprogress(&self) -> bool

Source

pub fn get_time_f32(&self, classname: &'static str, id: u32) -> f32

Gets the current elapsed time of a specific animation instance in seconds.

Returns 0.0 if the class name or id is not found.

Source

pub fn from_range<T>( &self, on_classname: &'static str, id: u32, range: impl RangeBounds<T>, ) -> T
where T: Clone + Default, ProgressList<TRES, PRES>: GetValueByRange<T>,

rangebounded interpolated to get value from range bound such as start..end, start..=end, ..end and ..=end but it return default value if range is start.., .., =.. and start=..

Source

pub fn is_classname(&self, classname: &'static str) -> bool

Source

pub fn is_id(&self, on_classname: &'static str, id: u32) -> bool

Source

pub fn get_value_byrange<T>( &self, on_classname: &'static str, id: u32, range: Range<T>, ) -> T
where ProgressList<TRES, PRES>: GetValueByRange<T>,

Calculates and returns an interpolated value for an animation within a given Range.

The interpolation is based on the animation’s current progress and its class’s KeyFrameFunction. This method is suitable for types that support the necessary arithmetic operations (Add, Sub, Mul<f32>).

Returns the range.start value if the class name or id is not found.

Source

pub fn get_value_byrange_inclusive<T>( &self, on_classname: &'static str, id: u32, range: RangeInclusive<T>, ) -> T
where T: Clone, ProgressList<TRES, PRES>: GetValueByRange<T>,

Calculates and returns an interpolated value for an animation within a given RangeInclusive.

The interpolation is based on the animation’s current progress and its class’s KeyFrameFunction. This method is suitable for types that support the necessary arithmetic operations (Add, Sub, Mul<f32>).

Returns the range.start() value if the class name or id is not found.

Source

pub fn from_range_generic<T>( &self, on_classname: &'static str, id: u32, range: impl RangeBounds<T>, ) -> T
where T: Default + Clone + Copy, ProgressList<TRES, PRES>: GetValueByGeneric<T>,

rangebounded interpolated to get value from range bound such as start..end, start..=end, ..end and ..=end but it return default value if range is start.., .., =.. and start=..

Source

pub fn get_generic_byrange<T>( &self, on_classname: &'static str, id: u32, range: Range<T>, ) -> T
where T: Copy, ProgressList<TRES, PRES>: GetValueByGeneric<T>,

Gets a value from a Range based on animation progress, for generic types.

This method is intended for types that might not support arithmetic interpolation (e.g., enums). It relies on the GetValueByGeneric trait to determine the value based on progress.

NOTE: Generic should be implemented with

  • Clone, Copy
  • Add<Output = Self>, Sub<Output = Self>
  • Mul<f32, Output = Self>
§Example of implementation
   use std::ops::{Add, Sub, Mul};
   #[derive(Clone, Copy)]
   struct Point {
       x: f32,
       y: f32,
   }

   impl Add for Point {
       type Output = Self;

       fn add(self, other: Self) -> Self {
           Point {
               x: self.x + other.x,
               y: self.y + other.y,
           }
       }
   }

   impl Sub for Point {
       type Output = Self;

       fn sub(self, other: Self) -> Self {
           Point {
               x: self.x - other.x,
               y: self.y - other.y,
           }
       }
   }

   impl Mul<f32> for Point {
       type Output = Self;

       fn mul(self, scalar: f32) -> Self {
           Point {
               x: self.x * scalar,
               y: self.y * scalar,
           }
       }
   }

Returns range.start if the class name or id is not found.

Source

pub fn get_generic_value_by_rangeinclusive<T>( &self, on_classname: &'static str, id: u32, range: RangeInclusive<T>, ) -> T
where T: Clone, ProgressList<TRES, PRES>: GetValueByGeneric<T>,

Gets a value from a RangeInclusive based on animation progress, for generic types.

This method is intended for types that might not support arithmetic interpolation (e.g., enums). It relies on the GetValueByGeneric trait to determine the value based on progress.

NOTE: Generic should be implemented with

  • Clone, Copy
  • Add<Output = Self>, Sub<Output = Self>
  • Mul<f32, Output = Self>
§Example of implementation
   use std::ops::{Add, Sub, Mul};
   #[derive(Clone, Copy)]
   struct Point {
       x: f32,
       y: f32,
   }

   impl Add for Point {
       type Output = Self;

       fn add(self, other: Self) -> Self {
           Point {
               x: self.x + other.x,
               y: self.y + other.y,
           }
       }
   }

   impl Sub for Point {
       type Output = Self;

       fn sub(self, other: Self) -> Self {
           Point {
               x: self.x - other.x,
               y: self.y - other.y,
           }
       }
   }

   impl Mul<f32> for Point {
       type Output = Self;

       fn mul(self, scalar: f32) -> Self {
           Point {
               x: self.x * scalar,
               y: self.y * scalar,
           }
       }
   }

Returns range.start if the class name or id is not found.

Source

pub fn reverse_animate(&mut self, on_classname: &'static str, id: u32)

Reverses the direction of a specific animation instance.

If it was playing forwards, it will now play backwards, and vice-versa.

Source

pub fn reverse_start(&mut self, on_classname: &'static str, id: u32)

Reverses the direction of a specific animation instance and starts it.

If it was playing forwards, it will now play backwards, and vice-versa.

Source

pub fn is_animating(&self, on_classname: &'static str, id: u32) -> bool

Checks if a specific animation instance is currently playing.

Returns true if the animation is playing, false otherwise.

Source

pub fn animate_by_closure_rangegeneric<T>( &mut self, on_classname: &'static str, id: u32, direction: fn(bool) -> bool, start: fn(bool) -> bool, range: Range<T>, ) -> T
where T: Clone, ProgressList<TRES, PRES>: GetValueByGeneric<T>,

A flexible method to control an animation’s state using closures and retrieve its current value.

This function allows you to dynamically control the start and direction of an animation while also getting its current interpolated value from a Range. It is designed for generic types that may not support arithmetic interpolation.

§Arguments
  • on_classname: The name of the animation class.
  • id: The unique identifier for the animation instance.
  • direction: A closure that receives the current reverse state (true if playing backwards) and returns the desired direction (true for forward, false for reverse).
  • start: A closure that receives the current animating state (true if progress is between 0.0 and 1.0) and returns whether the animation should be restarted (true to restart).
  • range: The Range<T> of values to animate between.
§Returns

The calculated value for the current frame, before state changes from the closures are applied.

Source

pub fn animate_by_closure_range<T>( &mut self, on_classname: &'static str, id: u32, direction: fn(bool) -> bool, start: fn(bool) -> bool, range: Range<T>, ) -> T
where ProgressList<TRES, PRES>: GetValueByRange<T>,

A flexible method to control an animation’s state using closures and retrieve its current value.

This function allows you to dynamically control the start and direction of an animation while also getting its current interpolated value from a Range. It is designed for types that support arithmetic operations for smooth interpolation.

§Arguments
  • on_classname: The name of the animation class.
  • id: The unique identifier for the animation instance.
  • direction: A closure that receives the current reverse state (true if playing backwards) and returns the desired direction (true for forward, false for reverse).
  • start: A closure that receives the current animating state (true if progress is between 0.0 and 1.0) and returns whether the animation should be restarted (true to restart).
  • range: The Range<T> of values to animate between.
§Returns

The calculated value for the current frame, before state changes from the closures are applied.

Source§

impl<'a, const N: usize, UN: Eq, TRES: TimingResolution + Clone, PRES: ProgressResolution + Eq> KramaFrame<UClassList<N>, UFrameList<'a, N, UN, TRES, PRES>>

Source

pub fn change_keyframefunction( &mut self, classname: &'static str, new: KeyFrameFunction, )

Changes the keyframe function (easing behavior) for a specific animation class.

§Example
krama.change_keyframefunction("fade_in", KeyFrameFunction::EaseIn);
Source

pub fn update_progress(&mut self, delta_time: TRES)

Updates the progress of all active animations in the frame list based on the provided delta time.

§Example
krama.update_progress(TRES::from_sec(0.016));
Source

pub fn restart_progress(&mut self, classname: &'static str, id: UN)

Restarts the progress of a specific animation instance identified by class name and ID.

§Example
krama.restart_progress("button_click", 1);
Source

pub fn get_progress_f32(&mut self, classname: &'static str, id: UN) -> f32

Retrieves the current progress of an animation instance as a float between 0.0 and 1.0.

§Example
let p = krama.get_progress_f32("spinner", 0);
Source

pub fn set_timing(&mut self, classname: &'static str, id: UN, timing: TRES)

Updates the total duration (timing) for a specific animation instance.

§Example
krama.set_timing("slow_move", 5, TRES::from_sec(10.0));
Source

pub fn get_timing(&mut self, classname: &'static str, id: UN) -> TRES

Gets the configured total duration (timing) for a specific animation instance.

§Example
let duration = krama.get_timing("move", 1);
Source

pub fn is_reversed(&mut self, classname: &'static str, id: UN) -> bool

Checks if a specific animation instance is currently playing in reverse.

§Example
if krama.is_reversed("door", 1) { println!("Closing..."); }
Source

pub fn is_any_animation_inprogress(&mut self) -> bool

Checks if any animation instance across all classes is currently in progress.

§Example
if krama.is_any_animation_inprogress() {
    request_next_frame();
}
Source

pub fn get_time_f32(&mut self, classname: &'static str, id: UN) -> f32

Gets the current elapsed time in seconds for a specific animation instance.

§Example
let seconds = krama.get_time_f32("timer", 1);
Source

pub fn from_range<T>( &self, on_classname: &'static str, id: UN, range: impl RangeBounds<T>, ) -> T
where T: Clone + Default, ProgressList<TRES, PRES>: GetValueByRange<T>,

rangebounded interpolated to get value from range bound such as start..end, start..=end, ..end and ..=end but it return default value if range is start.., .., =.. and start=..

Source

pub fn get_value_byrange<T>( &self, classname: &'static str, id: UN, range: Range<T>, ) -> T
where ProgressList<TRES, PRES>: GetValueByRange<T>,

Calculates an interpolated value within a given Range based on an animation’s progress.

§Example
let x_pos = krama.get_value_byrange("move", 1, 0.0..500.0);
Source

pub fn get_value_byrange_inclusive<T>( &self, classname: &'static str, id: UN, range: RangeInclusive<T>, ) -> T
where T: Clone, ProgressList<TRES, PRES>: GetValueByRange<T>,

Calculates an interpolated value within a given RangeInclusive based on an animation’s progress.

§Example
let alpha = krama.get_value_byrange_inclusive("fade", 1, 0.0..=1.0);
Source

pub fn from_range_generic<T>( &self, on_classname: &'static str, id: UN, range: impl RangeBounds<T>, ) -> T
where T: Default + Clone + Copy, ProgressList<TRES, PRES>: GetValueByGeneric<T>,

rangebounded interpolated to get generic value from range bound such as start..end, start..=end, ..end and ..=end but it return default value if range is start.., .., =.. and start=..

Source

pub fn get_generic_byrange<T>( &self, classname: &'static str, id: UN, range: Range<T>, ) -> T
where T: Copy, ProgressList<TRES, PRES>: GetValueByGeneric<T>,

Gets an interpolated value for generic types using the GetValueByGeneric trait.

§Example
let color = krama.get_generic_byrange("recolor", 1, RED..BLUE);
Source

pub fn get_generic_value_by_rangeinclusive<T>( &self, classname: &'static str, id: UN, range: RangeInclusive<T>, ) -> T
where T: Clone, ProgressList<TRES, PRES>: GetValueByGeneric<T>,

Gets an interpolated value for generic types within a RangeInclusive.

§Example
let pos = krama.get_generic_value_by_rangeinclusive("path", 1, start..=end);
Source

pub fn reverse_animate(&mut self, classname: &'static str, id: UN)

Reverses the playback direction of a specific animation instance.

§Example
krama.reverse_animate("toggle", 1);
Source

pub fn reverse_start(&mut self, classname: &'static str, id: UN)

Reverses the playback direction and ensures the animation is active.

§Example
krama.reverse_start("menu", 1);
Source

pub fn is_animating(&mut self, classname: &'static str, id: UN) -> bool

Checks if a specific animation instance is currently playing.

§Example
if krama.is_animating("logo_spin", 1) { /* ... */ }

Trait Implementations§

Source§

impl<TRES: TimingResolution + Clone, PRES: ProgressResolution + Eq> Default for KramaFrame<BTreeMap<&'static str, KeyFrameFunction>, BTframelist<TRES, PRES>>

Available on crate features std or alloc only.
Source§

fn default() -> Self

Creates a new, empty KramaFrame with default BTreeMap-based storage.

Auto Trait Implementations§

§

impl<CL, FL> Freeze for KramaFrame<CL, FL>
where CL: Freeze, FL: Freeze,

§

impl<CL, FL> RefUnwindSafe for KramaFrame<CL, FL>

§

impl<CL, FL> Send for KramaFrame<CL, FL>
where CL: Send, FL: Send,

§

impl<CL, FL> Sync for KramaFrame<CL, FL>
where CL: Sync, FL: Sync,

§

impl<CL, FL> Unpin for KramaFrame<CL, FL>
where CL: Unpin, FL: Unpin,

§

impl<CL, FL> UnsafeUnpin for KramaFrame<CL, FL>
where CL: UnsafeUnpin, FL: UnsafeUnpin,

§

impl<CL, FL> UnwindSafe for KramaFrame<CL, FL>
where CL: UnwindSafe, FL: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.