anim.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::collections::HashMap;
  19. use super::{BufferId, DrawCall, GfxDrawCall, TextureId};
  20. #[derive(Debug, Clone)]
  21. pub struct Frame {
  22. /// Duration of this frame in ms
  23. duration: u32,
  24. dc: DrawCall,
  25. }
  26. impl Frame {
  27. pub fn new(duration: u32, dc: DrawCall) -> Self {
  28. Self { duration, dc }
  29. }
  30. }
  31. #[derive(Debug, Clone)]
  32. pub(super) struct GfxSeqAnim {
  33. oneshot: bool,
  34. frames: Vec<Option<GfxFrame>>,
  35. /// Timer between frames
  36. timer: std::time::Instant,
  37. current_idx: usize,
  38. /// While `Some` and unexpired, the anim holds `current_idx` instead of
  39. /// advancing. Used for e.g. pausing cursor blink while typing: the app
  40. /// holds the visible frame for an idle duration and the anim resumes
  41. /// ticking on its own afterwards, with no app-side timed commits.
  42. paused_until: Option<std::time::Instant>,
  43. pub(super) is_visible: bool,
  44. }
  45. impl GfxSeqAnim {
  46. pub fn new(frames_len: usize, oneshot: bool) -> Self {
  47. let frames = vec![None; frames_len];
  48. Self {
  49. oneshot,
  50. frames,
  51. timer: std::time::Instant::now(),
  52. current_idx: 0,
  53. paused_until: None,
  54. is_visible: false,
  55. }
  56. }
  57. pub fn set(
  58. &mut self,
  59. frame_idx: usize,
  60. frame: Frame,
  61. textures: &HashMap<TextureId, miniquad::TextureId>,
  62. buffers: &HashMap<BufferId, miniquad::BufferId>,
  63. ) {
  64. assert!(frame_idx < self.frames.len());
  65. let duration = std::time::Duration::from_millis(frame.duration as u64);
  66. let dc = frame.dc.compile(textures, buffers);
  67. self.frames[frame_idx] = Some(GfxFrame { duration, dc });
  68. //t!("got frame {frame_idx}");
  69. }
  70. fn curr_frame(&self) -> Option<&GfxFrame> {
  71. assert!(self.current_idx < self.frames.len());
  72. self.frames[self.current_idx].as_ref()
  73. }
  74. pub fn tick(&mut self) -> Option<GfxDrawCall> {
  75. if self.curr_frame().is_none() {
  76. assert_eq!(self.current_idx, 0);
  77. return None
  78. };
  79. self.increment();
  80. let curr_frame = self.curr_frame().unwrap().clone();
  81. Some(curr_frame.dc)
  82. }
  83. /// Hold `frame_idx` for `duration_ms`, then resume ticking from there.
  84. /// The timer restarts when the pause expires, so the held frame gets a
  85. /// full frame duration before advancing.
  86. pub fn hold(&mut self, frame_idx: usize, duration_ms: u64) {
  87. assert!(frame_idx < self.frames.len());
  88. self.current_idx = frame_idx;
  89. self.timer = std::time::Instant::now();
  90. let until = std::time::Instant::now() + std::time::Duration::from_millis(duration_ms);
  91. self.paused_until = Some(until);
  92. }
  93. fn increment(&mut self) {
  94. if let Some(until) = self.paused_until {
  95. if std::time::Instant::now() < until {
  96. return
  97. }
  98. self.paused_until = None;
  99. }
  100. // One shot anims dont loop
  101. if self.oneshot && self.current_idx + 1 == self.frames.len() {
  102. return
  103. }
  104. let elapsed = self.timer.elapsed();
  105. let frame = self.curr_frame().unwrap();
  106. let curr_duration = frame.duration;
  107. if elapsed >= curr_duration {
  108. let next_idx = (self.current_idx + 1) % self.frames.len();
  109. // Only advance when the next frame is Some
  110. // Otherwise stay on the same frame
  111. if self.frames[next_idx].is_some() {
  112. self.current_idx = next_idx;
  113. // Reset the timer now we changed frame
  114. self.timer = std::time::Instant::now();
  115. }
  116. }
  117. }
  118. }
  119. #[derive(Debug, Clone)]
  120. struct GfxFrame {
  121. duration: std::time::Duration,
  122. dc: GfxDrawCall,
  123. }