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 | float GetSmoothedValue() const
|
---|
32 | {
|
---|
33 | return m_Current;
|
---|
34 | }
|
---|
35 |
|
---|
36 | void SetValueSmoothly(float value)
|
---|
37 | {
|
---|
38 | m_Target = value;
|
---|
39 | }
|
---|
40 |
|
---|
41 | void AddSmoothly(float value)
|
---|
42 | {
|
---|
43 | m_Target += value;
|
---|
44 | }
|
---|
45 |
|
---|
46 | void Add(float value)
|
---|
47 | {
|
---|
48 | m_Target += value;
|
---|
49 | m_Current += value;
|
---|
50 | }
|
---|
51 |
|
---|
52 | float GetValue() const
|
---|
53 | {
|
---|
54 | return m_Target;
|
---|
55 | }
|
---|
56 |
|
---|
57 | void SetValue(float value)
|
---|
58 | {
|
---|
59 | m_Target = value;
|
---|
60 | m_Current = value;
|
---|
61 | }
|
---|
62 |
|
---|
63 | float GetSmoothness() const
|
---|
64 | {
|
---|
65 | return m_Smoothness;
|
---|
66 | }
|
---|
67 |
|
---|
68 | void SetSmoothness(float smoothness)
|
---|
69 | {
|
---|
70 | m_Smoothness = smoothness;
|
---|
71 | }
|
---|
72 |
|
---|
73 | void ClampSmoothly(float min, float max)
|
---|
74 | {
|
---|
75 | m_Target = Clamp(m_Target, min, max);
|
---|
76 | }
|
---|
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
|
---|