From 5a5db785ac17b179ce39206d7dbc0689ae1f6492 Mon Sep 17 00:00:00 2001 From: llMBQll Date: Mon, 6 Jan 2025 02:15:12 +0100 Subject: [PATCH] Share global animation speed setting between Text and Images, allow for per-widget speed customization --- omni-led/src/renderer/renderer.rs | 42 +++++++++++++++---- .../src/script_handler/script_data_types.rs | 8 +++- omni-led/src/settings/settings.rs | 12 +++--- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/omni-led/src/renderer/renderer.rs b/omni-led/src/renderer/renderer.rs index b8c944a..ee2d560 100644 --- a/omni-led/src/renderer/renderer.rs +++ b/omni-led/src/renderer/renderer.rs @@ -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, } @@ -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, } } @@ -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 { @@ -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, ) @@ -301,7 +323,7 @@ impl Renderer { fn precalculate_single( ctx: &mut HashMap, font_manager: &mut FontManager, - settings: &ScrollingTextSettings, + settings: &AnimationSettings, text: &Text, counter: usize, ) -> Option<(bool, Step)> { @@ -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, @@ -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::::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, } } } diff --git a/omni-led/src/script_handler/script_data_types.rs b/omni-led/src/script_handler/script_data_types.rs index deca2e8..a17be41 100644 --- a/omni-led/src/script_handler/script_data_types.rs +++ b/omni-led/src/script_handler/script_data_types.rs @@ -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, + pub animation_ticks_rate: Option, + pub position: Point, + pub size: Size, #[mlua(default)] pub modifiers: Modifiers, @@ -183,6 +185,8 @@ pub struct Text { pub font_size: Option, #[mlua(default(false))] pub scrolling: bool, + pub animation_ticks_delay: Option, + pub animation_ticks_rate: Option, pub position: Point, pub size: Size, diff --git a/omni-led/src/settings/settings.rs b/omni-led/src/settings/settings.rs index f0a3bc2..0c8591b 100644 --- a/omni-led/src/settings/settings.rs +++ b/omni-led/src/settings/settings.rs @@ -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, @@ -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,