imgui_animated.h (3160B)
1 // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com> 2 // SPDX-License-Identifier: (GPL-3.0 OR PolyForm-Strict-1.0.0) 3 4 #pragma once 5 6 #include "common/easing.h" 7 8 #include "imgui.h" 9 #include "imgui_internal.h" 10 11 #include <algorithm> 12 13 class ImAnimatedFloat 14 { 15 public: 16 ImAnimatedFloat() = default; 17 18 bool IsActive() const { return (m_current_value != m_end_value); } 19 float GetCurrentValue() const { return m_current_value; } 20 float GetStartValue() const { return m_start_value; } 21 float GetEndValue() const { return m_end_value; } 22 23 void Stop() { m_end_value = m_current_value; } 24 void SetEndValue(float end_value) { m_end_value = end_value; } 25 26 void Reset(float value) 27 { 28 m_current_value = value; 29 m_start_value = value; 30 m_end_value = value; 31 } 32 33 float UpdateAndGetValue() 34 { 35 if (m_current_value == m_end_value) 36 return m_current_value; 37 38 m_elapsed_time += ImGui::GetIO().DeltaTime; 39 40 const float frac = std::min(0.05f + Easing::OutExpo(m_elapsed_time / m_duration), 1.0f); 41 m_current_value = std::clamp(m_start_value + ((m_end_value - m_start_value) * frac), 42 std::min(m_start_value, m_end_value), std::max(m_start_value, m_end_value)); 43 return m_current_value; 44 } 45 46 void Start(float start_value, float end_value, float duration) 47 { 48 m_current_value = start_value; 49 m_start_value = start_value; 50 m_end_value = end_value; 51 m_elapsed_time = 0.0f; 52 m_duration = duration; 53 } 54 55 private: 56 float m_current_value = 0.0f; 57 float m_start_value = 0.0f; 58 float m_end_value = 0.0f; 59 float m_elapsed_time = 0.0f; 60 float m_duration = 1.0f; 61 }; 62 63 class ImAnimatedVec2 64 { 65 public: 66 ImAnimatedVec2() = default; 67 68 bool IsActive() const { return (m_current_value.x != m_end_value.x || m_current_value.y != m_end_value.y); } 69 const ImVec2& GetCurrentValue() const { return m_current_value; } 70 const ImVec2& GetStartValue() const { return m_start_value; } 71 const ImVec2& GetEndValue() const { return m_end_value; } 72 73 void Stop() { m_end_value = m_current_value; } 74 void SetEndValue(const ImVec2& end_value) { m_end_value = end_value; } 75 76 void Reset(const ImVec2& value) 77 { 78 m_current_value = value; 79 m_start_value = value; 80 m_end_value = value; 81 } 82 83 const ImVec2& UpdateAndGetValue() 84 { 85 if (m_current_value.x == m_end_value.x && m_current_value.y == m_end_value.y) 86 return m_current_value; 87 88 m_elapsed_time += ImGui::GetIO().DeltaTime; 89 90 const float frac = std::min(0.05f + Easing::OutExpo(m_elapsed_time / m_duration), 1.0f); 91 m_current_value = ImClamp(ImLerp(m_start_value, m_end_value, frac), ImMin(m_start_value, m_end_value), 92 ImMax(m_start_value, m_end_value)); 93 return m_current_value; 94 } 95 96 void Start(const ImVec2& start_value, const ImVec2& end_value, float duration) 97 { 98 m_current_value = start_value; 99 m_start_value = start_value; 100 m_end_value = end_value; 101 m_elapsed_time = 0.0f; 102 m_duration = duration; 103 } 104 105 private: 106 ImVec2 m_current_value = {}; 107 ImVec2 m_start_value = {}; 108 ImVec2 m_end_value = {}; 109 float m_elapsed_time = 0.0f; 110 float m_duration = 1.0f; 111 };