Line data Source code
1 : /* Copyright (C) 2021 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #ifndef INCLUDED_SMOOTHEDVALUE 19 : #define INCLUDED_SMOOTHEDVALUE 20 : 21 : #include "maths/MathUtil.h" 22 : 23 : /** 24 : * A value with exponential decay towards the target value. 25 : */ 26 : class CSmoothedValue 27 : { 28 : public: 29 : CSmoothedValue(float value, float smoothness, float minDelta); 30 : 31 0 : float GetSmoothedValue() const 32 : { 33 0 : return m_Current; 34 : } 35 : 36 0 : void SetValueSmoothly(float value) 37 : { 38 0 : m_Target = value; 39 : } 40 : 41 0 : void AddSmoothly(float value) 42 : { 43 0 : m_Target += value; 44 0 : } 45 : 46 0 : void Add(float value) 47 : { 48 0 : m_Target += value; 49 0 : m_Current += value; 50 0 : } 51 : 52 0 : float GetValue() const 53 : { 54 0 : return m_Target; 55 : } 56 : 57 0 : void SetValue(float value) 58 : { 59 0 : m_Target = value; 60 0 : m_Current = value; 61 : } 62 : 63 0 : float GetSmoothness() const 64 : { 65 0 : return m_Smoothness; 66 : } 67 : 68 0 : void SetSmoothness(float smoothness) 69 : { 70 0 : m_Smoothness = smoothness; 71 : } 72 : 73 0 : void ClampSmoothly(float min, float max) 74 : { 75 0 : m_Target = Clamp<double>(m_Target, min, max); 76 0 : } 77 : 78 : float Update(float time); 79 : 80 : // Wrap so 'target' is in the range [min, max] 81 : void Wrap(float min, float max); 82 : 83 : private: 84 : float m_Smoothness; 85 : 86 : double m_Target; // the value which m_Current is tending towards 87 : double m_Current; 88 : // (We use double because the extra precision is worthwhile here) 89 : 90 : float m_MinDelta; // cutoff where we stop moving (to avoid ugly shimmering effects) 91 : }; 92 : 93 : #endif // INCLUDED_SMOOTHEDVALUE