anime: tweak gif animation time types

This commit is contained in:
Luke D Jones
2021-04-09 23:15:56 +12:00
parent eb83d1a835
commit ece565de1c
7 changed files with 124 additions and 77 deletions

View File

@@ -15,7 +15,7 @@ fn main() {
let path = Path::new(&args[1]);
let brightness = args[2].parse::<f32>().unwrap();
let mut seq = Sequences::new();
seq.add_asus_gif(path, None, brightness).unwrap();
seq.add_asus_gif(path, rog_anime::AnimTime::Infinite, brightness).unwrap();
loop {
for action in seq.iter() {

View File

@@ -6,7 +6,7 @@ use std::{
};
use glam::Vec2;
use rog_anime::{Action, Sequences};
use rog_anime::{Action, AnimTime, Sequences};
use rog_dbus::AuraDbusClient;
fn main() {
@@ -32,12 +32,12 @@ fn main() {
),
if let Ok(time) = args[7].parse::<u64>() {
if time != 0 {
Some(Duration::from_secs(time))
AnimTime::Time(Duration::from_secs(time))
} else {
None
AnimTime::Infinite
}
} else {
None
AnimTime::Infinite
},
args[6].parse::<f32>().unwrap(),
)
@@ -54,12 +54,12 @@ fn main() {
),
if let Ok(time) = args[7].parse::<u64>() {
if time != 0 {
Some(Duration::from_secs(time))
AnimTime::Time(Duration::from_secs(time))
} else {
None
AnimTime::Infinite
}
} else {
None
AnimTime::Infinite
},
args[6].parse::<f32>().unwrap(),
)
@@ -69,6 +69,7 @@ fn main() {
loop {
for action in seq.iter() {
if let Action::Animation(frames) = action {
let mut count = 0;
let start = Instant::now();
'outer: loop {
for frame in frames.frames() {
@@ -77,13 +78,18 @@ fn main() {
.anime()
.write(frame.frame().clone())
.unwrap();
if let Some(time) = frames.duration() {
if let AnimTime::Time(time) = frames.duration() {
if Instant::now().duration_since(start) > time {
break 'outer;
}
} else if let AnimTime::Cycles(times) = frames.duration() {
if count == times {
break 'outer;
}
}
sleep(frame.delay());
}
count +=1;
}
}
}

View File

@@ -1,4 +1,4 @@
use rog_anime::Action;
use rog_anime::{Action, AnimTime};
use rog_dbus::AuraDbusClient;
use rog_user::user_config::*;
@@ -27,20 +27,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let start = Instant::now();
match action {
Action::Animation(frames) => 'animation: loop {
for frame in frames.frames() {
client.proxies().anime().write(frame.frame().clone())?;
if let Some(time) = frames.duration() {
if Instant::now().duration_since(start) > time {
Action::Animation(frames) => {
let mut count = 0;
'animation: loop {
for frame in frames.frames() {
client.proxies().anime().write(frame.frame().clone())?;
if let AnimTime::Time(time) = frames.duration() {
if Instant::now().duration_since(start) > time {
break 'animation;
}
}
sleep(frame.delay());
}
if let AnimTime::Cycles(times) = frames.duration() {
count += 1;
if count >= times {
break 'animation;
}
}
sleep(frame.delay());
}
if frames.duration().is_none() {
break 'animation;
}
},
}
Action::Image(image) => {
client.proxies().anime().write(image.as_ref().clone())?;
}

View File

@@ -5,7 +5,7 @@ use std::{
time::Duration,
};
use rog_anime::{Sequences, Vec2};
use rog_anime::{AnimTime, Sequences, Vec2};
use serde_derive::{Deserialize, Serialize};
use crate::error::Error;
@@ -20,7 +20,7 @@ pub enum AnimeAction {
/// Full gif sequence. Immutable.
AsusAnimation {
file: PathBuf,
duration: Option<Duration>,
time: AnimTime,
brightness: f32,
},
/// Basic image, can have properties changed
@@ -29,7 +29,7 @@ pub enum AnimeAction {
scale: f32,
angle: f32,
translation: Vec2,
duration: Option<Duration>,
time: AnimTime,
brightness: f32,
},
Image {
@@ -45,12 +45,12 @@ pub enum AnimeAction {
impl UserConfig {
pub fn new() -> Self {
Self {
let x = Self {
anime: vec![
AnimeAction::AsusAnimation {
file: "/usr/share/asusd/anime/asus/rog/Sunset.gif".into(),
brightness: 0.5,
duration: None,
time: AnimTime::Cycles(1),
},
AnimeAction::ImageAnimation {
file: "/usr/share/asusd/anime/custom/sonic-run.gif".into(),
@@ -58,7 +58,7 @@ impl UserConfig {
angle: 0.65,
translation: Vec2::default(),
brightness: 0.5,
duration: Some(Duration::from_secs(5)),
time: AnimTime::Time(Duration::from_secs(5)),
},
AnimeAction::Image {
file: "/usr/share/asusd/anime/custom/rust.png".into(),
@@ -74,10 +74,12 @@ impl UserConfig {
angle: 0.0,
translation: Vec2::new(3.0, 2.0),
brightness: 0.5,
duration: None,
time: AnimTime::Cycles(2),
},
],
}
};
println!("{}", serde_json::to_string_pretty(&x).unwrap());
x
}
pub fn load_config(&mut self) -> Result<(), Error> {
@@ -122,7 +124,7 @@ impl UserConfig {
match anime {
AnimeAction::AsusAnimation {
file,
duration,
time: duration,
brightness,
} => seq.add_asus_gif(&file, *duration, *brightness)?,
AnimeAction::ImageAnimation {
@@ -130,7 +132,7 @@ impl UserConfig {
scale,
angle,
translation,
duration,
time: duration,
brightness,
} => {
seq.add_image_gif(&file, *scale, *angle, *translation, *duration, *brightness)?

View File

@@ -1,45 +1,64 @@
{
"anime": [
{
"AsusAnimation": {
"file": "/usr/share/asusd/anime/asus/Controller.gif",
"duration": {
"anime": [
{
"AsusAnimation": {
"file": "/usr/share/asusd/anime/asus/rog/Sunset.gif",
"time": {
"Cycles": 1
},
"brightness": 0.5
}
},
{
"ImageAnimation": {
"file": "/usr/share/asusd/anime/custom/sonic-run.gif",
"scale": 0.9,
"angle": 0.65,
"translation": [
0.0,
0.0
],
"time": {
"Time": {
"secs": 5,
"nanos": 0
}
}
},
{
"ImageAnimation": {
"file": "/usr/share/asusd/anime/sonic.gif",
"scale": 0.9,
"angle": 0.65,
"translation": [
0.0,
0.0
],
"duration": {
"secs": 5,
"nanos": 0
},
"brightness": 0.5
}
},
{
"Image": {
"file_path": "/usr/share/asusd/anime/doom.png",
"scale": 0.7,
"angle": 0.0,
"translation": [
0.0,
0.0
],
"duration": {
"secs": 5,
"nanos": 0
},
"brightness": 0.6
}
},
"brightness": 0.5
}
]
}
},
{
"Image": {
"file": "/usr/share/asusd/anime/custom/rust.png",
"scale": 1.0,
"angle": 0.0,
"translation": [
0.0,
0.0
],
"brightness": 0.6
}
},
{
"Pause": {
"secs": 6,
"nanos": 0
}
},
{
"ImageAnimation": {
"file": "/usr/share/asusd/anime/custom/sonic-wait.gif",
"scale": 0.9,
"angle": 0.0,
"translation": [
3.0,
2.0
],
"time": {
"Cycles": 2
},
"brightness": 0.5
}
}
]
}

View File

@@ -22,16 +22,30 @@ impl AniMeFrame {
}
}
#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
pub enum AnimTime {
Time(Duration),
Cycles(u32),
Infinite,
}
impl Default for AnimTime {
fn default() -> Self {
Self::Infinite
}
}
/// A gif animation. This is a collection of frames from the gif, and a duration
/// that the animation should be shown for.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AniMeGif(Vec<AniMeFrame>, Option<Duration>);
pub struct AniMeGif(Vec<AniMeFrame>, AnimTime);
impl AniMeGif {
/// Create an animation using the 74x36 ASUS gif format
pub fn create_diagonal_gif(
file_name: &Path,
duration: Option<Duration>,
duration: AnimTime,
brightness: f32,
) -> Result<Self, AnimeError> {
let mut matrix = AniMeDiagonal::new(None);
@@ -75,7 +89,7 @@ impl AniMeGif {
scale: f32,
angle: f32,
translation: Vec2,
duration: Option<Duration>,
duration: AnimTime,
brightness: f32,
) -> Result<Self, AnimeError> {
let mut frames = Vec::new();
@@ -142,7 +156,7 @@ impl AniMeGif {
&self.0
}
pub fn duration(&self) -> Option<Duration> {
pub fn duration(&self) -> AnimTime {
self.1
}
}

View File

@@ -3,7 +3,7 @@ use std::{path::Path, time::Duration};
use glam::Vec2;
use serde_derive::{Deserialize, Serialize};
use crate::{error::AnimeError, AniMeDataBuffer, AniMeGif, AniMeImage};
use crate::{AniMeDataBuffer, AniMeGif, AniMeImage, AnimTime, error::AnimeError};
///
#[derive(Debug, Deserialize, Serialize)]
@@ -36,7 +36,7 @@ impl Sequences {
pub fn add_asus_gif(
&mut self,
file: &Path,
duration: Option<Duration>,
duration: AnimTime,
brightness: f32,
) -> Result<(), AnimeError> {
let frames = AniMeGif::create_diagonal_gif(file, duration, brightness)?;
@@ -64,7 +64,7 @@ impl Sequences {
scale: f32,
angle: f32,
translation: Vec2,
duration: Option<Duration>,
duration: AnimTime,
brightness: f32,
) -> Result<(), AnimeError> {
let frames =