Skip to content

Commit

Permalink
Share global animation speed setting between Text and Images, allow f…
Browse files Browse the repository at this point in the history
…or per-widget speed customization
  • Loading branch information
llMBQll committed Jan 6, 2025
1 parent 7dd4cac commit 5a5db78
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
42 changes: 33 additions & 9 deletions omni-led/src/renderer/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,24 @@ use crate::script_handler::script_data_types::{
use crate::script_handler::script_data_types::{Rectangle, Size};
use crate::settings::settings::Settings;

macro_rules! get_animation_settings {
($default:expr, $widget:expr) => {
AnimationSettings {
ticks_at_edge: $widget
.animation_ticks_delay
.unwrap_or($default.ticks_at_edge),
ticks_per_move: $widget
.animation_ticks_rate
.unwrap_or($default.ticks_per_move),
}
};
}

pub struct Renderer {
font_manager: FontManager,
image_cache: ImageCache,
animation_data: AnimationData,
scrolling_text_settings: ScrollingTextSettings,
animation_settings: AnimationSettings,
counter: usize,
}

Expand All @@ -50,7 +63,7 @@ impl Renderer {
font_manager: FontManager::new(font_selector),
image_cache: ImageCache::new(),
animation_data: AnimationData::new(),
scrolling_text_settings: ScrollingTextSettings::new(lua),
animation_settings: AnimationSettings::new(lua),
counter: 0,
}
}
Expand Down Expand Up @@ -138,7 +151,16 @@ impl Renderer {
.animation_data
.get_image_context(key)
.entry(widget.image.hash.unwrap())
.or_insert_with(|| Animation::new(1, 1, image.len(), self.counter));
.or_insert_with(|| {
let settings = get_animation_settings!(self.animation_settings, widget);

Animation::new(
settings.ticks_at_edge,
settings.ticks_per_move,
image.len(),
self.counter,
)
});

let step = animation.step(self.counter);
if step.can_wrap {
Expand Down Expand Up @@ -258,7 +280,7 @@ impl Renderer {
Widget::Text(text) => Self::precalculate_single(
ctx,
&mut self.font_manager,
&self.scrolling_text_settings,
&self.animation_settings,
text,
self.counter,
)
Expand Down Expand Up @@ -301,7 +323,7 @@ impl Renderer {
fn precalculate_single(
ctx: &mut HashMap<String, Animation>,
font_manager: &mut FontManager,
settings: &ScrollingTextSettings,
settings: &AnimationSettings,
text: &Text,
counter: usize,
) -> Option<(bool, Step)> {
Expand All @@ -326,6 +348,8 @@ impl Renderer {
if len <= max_characters {
Animation::new(0, 0, 1, counter)
} else {
let settings = get_animation_settings!(settings, text);

Animation::new(
settings.ticks_at_edge,
settings.ticks_per_move,
Expand All @@ -339,17 +363,17 @@ impl Renderer {
}
}

struct ScrollingTextSettings {
struct AnimationSettings {
ticks_at_edge: usize,
ticks_per_move: usize,
}

impl ScrollingTextSettings {
impl AnimationSettings {
pub fn new(lua: &Lua) -> Self {
let settings = UserDataRef::<Settings>::load(lua);
Self {
ticks_at_edge: settings.get().text_ticks_scroll_delay,
ticks_per_move: settings.get().text_ticks_scroll_rate,
ticks_at_edge: settings.get().animation_ticks_delay,
ticks_per_move: settings.get().animation_ticks_rate,
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions omni-led/src/script_handler/script_data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,14 @@ impl UserData for Bar {}
#[derive(Clone, Debug, FromLuaValue)]
pub struct Image {
pub image: ImageData,
pub position: Point,
pub size: Size,
#[mlua(default(false))]
pub animated: bool,
#[mlua(default(128))]
pub threshold: u8,
pub animation_ticks_delay: Option<usize>,
pub animation_ticks_rate: Option<usize>,
pub position: Point,
pub size: Size,

#[mlua(default)]
pub modifiers: Modifiers,
Expand All @@ -183,6 +185,8 @@ pub struct Text {
pub font_size: Option<usize>,
#[mlua(default(false))]
pub scrolling: bool,
pub animation_ticks_delay: Option<usize>,
pub animation_ticks_rate: Option<usize>,
pub position: Point,
pub size: Size,

Expand Down
12 changes: 6 additions & 6 deletions omni-led/src/settings/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ use crate::renderer::font_selector::FontSelector;

#[derive(Debug, Clone, UniqueUserData, FromLuaValue)]
pub struct Settings {
#[mlua(default(8))]
pub animation_ticks_delay: usize,

#[mlua(default(2))]
pub animation_ticks_rate: usize,

#[mlua(default(FontSelector::Default))]
pub font: FontSelector,

Expand All @@ -43,12 +49,6 @@ pub struct Settings {
#[mlua(default(2))]
pub keyboard_ticks_repeat_rate: usize,

#[mlua(default(8))]
pub text_ticks_scroll_delay: usize,

#[mlua(default(2))]
pub text_ticks_scroll_rate: usize,

#[mlua(default(0))]
pub server_port: u16,

Expand Down

0 comments on commit 5a5db78

Please sign in to comment.