2 Copyright (C) 2005 - 2025
3 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
4 Copyright (C) 2004 by Philippe Plantier <ayin@anathas.org>
5 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY.
14 See the COPYING file for more details.
19 * Template implementation for frame-based animations.
23// ============================================================================
25// ============================================================================
28const T animated<T>::void_value_ = T();
31// ============================================================================
33// ============================================================================
36inline animated<T>::animated(const std::chrono::milliseconds& start_time)
37 : starting_frame_time_(start_time)
38 , max_animation_time_(0)
40 , current_frame_index_(0)
44 , needs_first_update_(false)
46 , uses_acceleration_(false)
47 , animation_start_tick_()
55inline animated<T>::animated(const animated<T>::anim_description& cfg, const std::chrono::milliseconds& start_time, bool force_change)
56 : starting_frame_time_(start_time)
57 , max_animation_time_(0)
59 , current_frame_index_(0)
63 , needs_first_update_(false)
65 , uses_acceleration_(false)
66 , animation_start_tick_()
71 for(const auto& [duration, value] : cfg) {
72 add_frame(duration, value, force_change);
77inline void animated<T>::add_frame(const std::chrono::milliseconds& duration, const T& value, bool force_change)
79 // NOTE: We cannot use emplace_back here, because the value may be a reference into the same vector,
80 // which case emplace_back could invalidate it before the new frame is constructed.
82 if(!frames_.empty()) {
83 const auto& last_frame = frames_.back();
84 frames_.push_back(frame{duration, value, last_frame.start_time_ + last_frame.duration_});
87 is_static_ = !force_change;
88 frames_.push_back(frame{duration, value, starting_frame_time_});
93// ============================================================================
95// ============================================================================
98inline void animated<T>::start_animation(const std::chrono::milliseconds& start_time_offset, bool cycles)
104 current_frame_index_ = 0;
105 const auto current_tick = get_current_animation_tick(uses_acceleration_);
106 animation_start_tick_ = current_tick - start_time_offset;
107 if(!frames_.empty()) {
108 next_frame_tick_ = animation_start_tick_ + frames_[0].start_time_ + frames_[0].duration_;
109 needs_first_update_ = true;
114inline void animated<T>::pause_animation()
116 if(!started_ || paused_) {
120 pause_tick_ = get_current_animation_tick(uses_acceleration_);
124inline void animated<T>::resume_animation()
130 const auto current_tick = get_current_animation_tick(uses_acceleration_);
131 const auto pause_duration = current_tick - pause_tick_;
133 // Shift the timeline forward by the pause duration
134 animation_start_tick_ += pause_duration;
135 next_frame_tick_ += pause_duration;
141inline void animated<T>::advance_to_current_frame()
143 needs_first_update_ = false;
144 if(frames_.empty()) {
148 const auto current_tick = get_current_animation_tick(uses_acceleration_);
150 // If a cycling animation is more than a whole cycle behind (e.g., it went un-updated
151 // while offscreen), skip forward in whole cycles so the loop below never has to walk
152 // more than one cycle of frames.
154 const std::chrono::milliseconds animation_duration = get_animation_duration();
155 if(animation_duration > std::chrono::milliseconds{0} && current_tick > (next_frame_tick_ + animation_duration)) {
156 const auto behind = current_tick - next_frame_tick_;
157 const auto skipped = (behind / animation_duration) * animation_duration; // The int division truncates to whole cycles.
158 next_frame_tick_ += skipped;
159 animation_start_tick_ += skipped;
163 // Advance frame by frame until we reach the frame containing the current time
164 while(current_tick >= next_frame_tick_) {
165 std::size_t next_index = current_frame_index_ + 1;
167 // Non-looping animation finished, stay on last frame
168 if(!cycles_ && next_index == frames_.size()) {
173 // Advance to the next frame, wrapping around when looping
174 current_frame_index_ = next_index % frames_.size();
175 if(current_frame_index_ == 0) {
176 animation_start_tick_ += get_animation_duration();
178 next_frame_tick_ += frames_[current_frame_index_].duration_;
183// ============================================================================
184// Playback State Queries
185// ============================================================================
188inline bool animated<T>::need_update() const
190 if(needs_first_update_) {
193 if(is_static_ || !started_ || paused_ || finished_ || frames_.empty()) {
196 return get_current_animation_tick(uses_acceleration_) >= next_frame_tick_;
200inline bool animated<T>::animation_finished() const
202 return finished_ || frames_.empty() || !started_ || cycles_ || (get_elapsed_time() >= get_end_time());
206// ============================================================================
207// Time Queries and Manipulation
208// ============================================================================
211inline std::chrono::steady_clock::time_point animated<T>::get_playback_tick() const
213 return paused_ ? pause_tick_ : get_current_animation_tick(uses_acceleration_);
217inline std::chrono::milliseconds animated<T>::get_elapsed_time() const
220 return starting_frame_time_;
223 // Calculate elapsed time since animation started
224 const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(get_playback_tick() - animation_start_tick_);
226 // Apply maximum time cap if set
227 if(max_animation_time_ > std::chrono::milliseconds{0} && elapsed > max_animation_time_) {
228 return max_animation_time_;
235inline void animated<T>::apply_time_offset(const std::chrono::milliseconds& time)
237 // Adjust the anchor point to make the current tick correspond to the requested time offset
238 animation_start_tick_ -= time;
240 // Restart animation from beginning; advance_to_current_frame() will sync on next call
241 if(!frames_.empty()) {
242 current_frame_index_ = 0;
244 needs_first_update_ = true;
245 next_frame_tick_ = animation_start_tick_ + frames_[0].start_time_ + frames_[0].duration_;
250inline void animated<T>::set_duration_limit(const std::chrono::milliseconds& time)
252 max_animation_time_ = time;
256inline std::chrono::milliseconds animated<T>::get_animation_duration() const
258 return get_end_time() - get_begin_time();
262inline std::chrono::milliseconds animated<T>::get_begin_time() const
264 return starting_frame_time_;
268inline std::chrono::milliseconds animated<T>::get_end_time() const
270 if(frames_.empty()) {
271 return starting_frame_time_;
273 const auto& last_frame = frames_.back();
274 return last_frame.start_time_ + last_frame.duration_;
278inline void animated<T>::set_begin_time(const std::chrono::milliseconds& new_begin_time)
280 const auto shift = new_begin_time - starting_frame_time_;
281 starting_frame_time_ = new_begin_time;
282 for(auto& frame : frames_) {
283 frame.start_time_ += shift;
287// ============================================================================
289// ============================================================================
292inline const T& animated<T>::get_current_frame() const
294 if(frames_.empty()) {
297 return frames_[current_frame_index_].value_;
301inline std::chrono::milliseconds animated<T>::get_current_frame_begin_time() const
303 if(frames_.empty()) {
304 return starting_frame_time_;
306 return frames_[current_frame_index_].start_time_;
310inline std::chrono::milliseconds animated<T>::get_current_frame_end_time() const
312 if(frames_.empty()) {
313 return starting_frame_time_;
315 const auto& frame = frames_[current_frame_index_];
316 return frame.start_time_ + frame.duration_;
320inline std::chrono::milliseconds animated<T>::get_time_in_current_frame() const
322 if(frames_.empty() || !started_) {
323 return std::chrono::milliseconds{0};
325 const auto current_tick = get_playback_tick();
326 const auto& frame = frames_[current_frame_index_];
328 // How much of this frame has elapsed, clamped to [0, duration].
329 const auto time_until_next = std::chrono::duration_cast<std::chrono::milliseconds>(next_frame_tick_ - current_tick);
330 const auto elapsed_in_frame = frame.duration_ - time_until_next;
331 return std::clamp(elapsed_in_frame, std::chrono::milliseconds{0}, frame.duration_);
335inline const T& animated<T>::get_first_frame() const
337 if(frames_.empty()) {
340 return frames_[0].value_;
344inline const T& animated<T>::get_frame(std::size_t n) const
346 if(n >= frames_.size()) {
349 return frames_[n].value_;
353inline const T& animated<T>::get_last_frame() const
355 if(frames_.empty()) {
358 return frames_.back().value_;
362inline std::size_t animated<T>::get_frames_count() const
364 return frames_.size();
368// ============================================================================
369// Protected Methods (for unit_animation)
370// ============================================================================
373void animated<T>::remove_frames_until(const std::chrono::milliseconds& new_starting_time)
375 // Remove frames from the beginning until we reach the target time
376 while(starting_frame_time_ < new_starting_time && !frames_.empty()) {
377 starting_frame_time_ += frames_[0].duration_;
378 frames_.erase(frames_.begin());
383inline void animated<T>::set_end_time(const std::chrono::milliseconds& new_ending_time)
385 if(frames_.empty()) {
389 // Cut-off before the animation begins: keep nothing but a zero-length first frame
390 if(new_ending_time <= starting_frame_time_) {
391 frames_.erase(std::next(frames_.begin()), frames_.end());
392 frames_.front().duration_ = std::chrono::milliseconds{0};
396 // Cut-off at or past the current end: extend the last frame to reach it
397 const auto current_end_time = get_end_time();
398 if(new_ending_time >= current_end_time) {
399 frames_.back().duration_ += new_ending_time - current_end_time;
403 // The cut-off lands within a frame: walk to it
404 auto current_start_time = starting_frame_time_;
405 auto it = frames_.begin();
407 while(it != frames_.end()) {
408 const auto next_end_time = current_start_time + it->duration_;
410 // Check if the cut-off point is within or at the end of this frame
411 if(next_end_time >= new_ending_time) {
412 // Truncate this frame to time remaining (a no-op if the cut-off is exactly at its end)
413 it->duration_ = new_ending_time - current_start_time;
415 // Erase all SUBSEQUENT frames (keep the current one)
416 frames_.erase(std::next(it), frames_.end());
420 current_start_time = next_end_time;