• R/O
  • HTTP
  • SSH
  • HTTPS

コミット

タグ
未設定

よく使われているワード(クリックで追加)

javaandroidc++linuxc#objective-ccocoa誰得qtrubybathyscaphegamephpguicwindows翻訳pythonomegattwitterframeworkbtronarduinovb.net計画中(planning stage)directxpreviewertestゲームエンジンdom

Simple C++ UI framework and more...


コミットメタ情報

リビジョン66ff3e315a7239560d92c323052827897aa96545 (tree)
日時2018-05-18 07:50:19
作者Starg <starg@user...>
コミッターStarg

ログメッセージ

Simplify animation

変更サマリ

差分

--- a/include/sirius/animation.hpp
+++ b/include/sirius/animation.hpp
@@ -6,6 +6,8 @@
66 #include <cstdint>
77
88 #include <chrono>
9+#include <functional>
10+#include <memory>
911 #include <optional>
1012 #include <utility>
1113 #include <vector>
@@ -26,28 +28,6 @@ enum class AnimationStopReason
2628 Reversed
2729 };
2830
29-enum class AnimationTransitionKind
30-{
31- Instantaneous,
32- Discrete,
33- Linear,
34- SpeedUp,
35- SlowDown,
36- Smooth
37-};
38-
39-struct AnimationKeyFrame
40-{
41- AnimationTransitionKind Transition;
42- float RelativeTime; // [0.0f, 1.0f]
43- float RelativeValue; // [0.0f, 1.0f]
44-};
45-
46-struct AnimationCurve
47-{
48- std::vector<AnimationKeyFrame> KeyFrames;
49-};
50-
5131 class Animatable
5232 {
5333 public:
@@ -74,6 +54,15 @@ public:
7454 }
7555 };
7656
57+using AnimationFunction = std::function<float(float)>;
58+
59+AnimationFunction CreateInstantaneousAnimationFunction();
60+AnimationFunction CreateDiscreteAnimationFunction();
61+AnimationFunction CreateLinearAnimationFunction();
62+AnimationFunction CreateSpeedUpAnimationFunction();
63+AnimationFunction CreateSlowDownAnimationFunction();
64+AnimationFunction CreateSmoothAnimationFunction();
65+
7766 class AnimationVariable : public Animatable
7867 {
7968 public:
@@ -103,16 +92,17 @@ class SIRIUS_DLLEXPORT AnimationManager
10392
10493 public:
10594 void StartAnimation(
106- Animatable* pAnimatable,
107- AnimationCurve animationCurve,
95+ const std::shared_ptr<Animatable>& pAnimatable,
96+ AnimationFunction animationFunction,
10897 float initialValue,
10998 float finalValue,
11099 std::chrono::steady_clock::duration duration
111100 );
112- void AbortAnimation(Animatable* pAnimatable);
101+
102+ void AbortAnimation(const std::shared_ptr<Animatable>& pAnimatable);
113103 void AbortAllAnimations();
114- void ReverseAnimation(Animatable* pAnimatable);
115- bool IsAnimationRunning(Animatable* pAnimatable) const;
104+ void ReverseAnimation(const std::shared_ptr<Animatable>& pAnimatable);
105+ bool IsAnimationRunning(const std::shared_ptr<Animatable>& pAnimatable) const;
116106
117107 void Process();
118108 bool HasRunningAnimations() const;
@@ -124,8 +114,8 @@ public:
124114 private:
125115 struct AnimationInfo
126116 {
127- Animatable* pAnimatable;
128- AnimationCurve Curve;
117+ std::shared_ptr<Animatable> pAnimatable;
118+ AnimationFunction Function;
129119 float InitialValue;
130120 float FinalValue;
131121 std::chrono::steady_clock::duration Duration;
@@ -134,9 +124,9 @@ private:
134124 float RelativeStartTime;
135125 };
136126
137- std::vector<AnimationInfo>::iterator FindAnimationInfoForAnimatable(Animatable* pAnimatable);
138- std::vector<AnimationInfo>::const_iterator FindAnimationInfoForAnimatable(Animatable* pAnimatable) const;
139- std::vector<AnimationInfo>::iterator GetAnimationInfoForAnimatable(Animatable* pAnimatable);
127+ std::vector<AnimationInfo>::iterator FindAnimationInfoForAnimatable(const std::shared_ptr<Animatable>& pAnimatable);
128+ std::vector<AnimationInfo>::const_iterator FindAnimationInfoForAnimatable(const std::shared_ptr<Animatable>& pAnimatable) const;
129+ std::vector<AnimationInfo>::iterator GetAnimationInfoForAnimatable(const std::shared_ptr<Animatable>& pAnimatable);
140130 void AbortAnimation(std::vector<AnimationInfo>::const_iterator it);
141131
142132 // [relativeTime, value]
--- a/src/animation.cpp
+++ b/src/animation.cpp
@@ -13,9 +13,39 @@
1313 namespace Sirius
1414 {
1515
16+AnimationFunction CreateInstantaneousAnimationFunction()
17+{
18+ return [] (float) { return 0.0f; };
19+}
20+
21+AnimationFunction CreateDiscreteAnimationFunction()
22+{
23+ return [] (float relativeTime) { return relativeTime < 0.5f ? 0.0f : 1.0f; };
24+}
25+
26+AnimationFunction CreateLinearAnimationFunction()
27+{
28+ return [] (float relativeTime) { return relativeTime; };
29+}
30+
31+AnimationFunction CreateSpeedUpAnimationFunction()
32+{
33+ return [] (float relativeTime) { return std::cos(relativeTime * std::atan(1.0f) * 2.0f) * -1.0f + 1.0f; };
34+}
35+
36+AnimationFunction CreateSlowDownAnimationFunction()
37+{
38+ return [] (float relativeTime) { return std::sin(relativeTime * std::atan(1.0f) * 2.0f); };
39+}
40+
41+AnimationFunction CreateSmoothAnimationFunction()
42+{
43+ return [] (float relativeTime) { return std::cos(relativeTime * std::atan(1.0f) * 4.0f) * -0.5f + 0.5f; };
44+}
45+
1646 void AnimationManager::StartAnimation(
17- Animatable* pAnimatable,
18- AnimationCurve animationCurve,
47+ const std::shared_ptr<Animatable>& pAnimatable,
48+ AnimationFunction animationFunction,
1949 float initialValue,
2050 float finalValue,
2151 std::chrono::steady_clock::duration duration
@@ -39,7 +69,7 @@ void AnimationManager::StartAnimation(
3969 {
4070 m_Animations.push_back({
4171 pAnimatable,
42- std::move(animationCurve),
72+ std::move(animationFunction),
4373 initialValue,
4474 finalValue,
4575 duration,
@@ -50,7 +80,7 @@ void AnimationManager::StartAnimation(
5080 }
5181 }
5282
53-void AnimationManager::AbortAnimation(Animatable* pAnimatable)
83+void AnimationManager::AbortAnimation(const std::shared_ptr<Animatable>& pAnimatable)
5484 {
5585 AbortAnimation(GetAnimationInfoForAnimatable(pAnimatable));
5686 }
@@ -63,7 +93,7 @@ void AnimationManager::AbortAllAnimations()
6393 }
6494 }
6595
66-void AnimationManager::ReverseAnimation(Animatable* pAnimatable)
96+void AnimationManager::ReverseAnimation(const std::shared_ptr<Animatable>& pAnimatable)
6797 {
6898 auto it = GetAnimationInfoForAnimatable(pAnimatable);
6999 assert(!AreAnimationsBlocked());
@@ -79,7 +109,7 @@ void AnimationManager::ReverseAnimation(Animatable* pAnimatable)
79109 it->pAnimatable->OnStart(it->Direction, currentValue);
80110 }
81111
82-bool AnimationManager::IsAnimationRunning(Animatable* pAnimatable) const
112+bool AnimationManager::IsAnimationRunning(const std::shared_ptr<Animatable>& pAnimatable) const
83113 {
84114 return FindAnimationInfoForAnimatable(pAnimatable) != m_Animations.end();
85115 }
@@ -123,7 +153,7 @@ void AnimationManager::UnblockAllAnimations()
123153 m_BlockingCount--;
124154 }
125155
126-std::vector<AnimationManager::AnimationInfo>::iterator AnimationManager::FindAnimationInfoForAnimatable(Animatable* pAnimatable)
156+std::vector<AnimationManager::AnimationInfo>::iterator AnimationManager::FindAnimationInfoForAnimatable(const std::shared_ptr<Animatable>& pAnimatable)
127157 {
128158 auto it = std::find_if(
129159 m_Animations.rbegin(),
@@ -144,7 +174,7 @@ std::vector<AnimationManager::AnimationInfo>::iterator AnimationManager::FindAni
144174 }
145175 }
146176
147-std::vector<AnimationManager::AnimationInfo>::const_iterator AnimationManager::FindAnimationInfoForAnimatable(Animatable* pAnimatable) const
177+std::vector<AnimationManager::AnimationInfo>::const_iterator AnimationManager::FindAnimationInfoForAnimatable(const std::shared_ptr<Animatable>& pAnimatable) const
148178 {
149179 auto it = std::find_if(
150180 m_Animations.rbegin(),
@@ -165,7 +195,7 @@ std::vector<AnimationManager::AnimationInfo>::const_iterator AnimationManager::F
165195 }
166196 }
167197
168-std::vector<AnimationManager::AnimationInfo>::iterator AnimationManager::GetAnimationInfoForAnimatable(Animatable* pAnimatable)
198+std::vector<AnimationManager::AnimationInfo>::iterator AnimationManager::GetAnimationInfoForAnimatable(const std::shared_ptr<Animatable>& pAnimatable)
169199 {
170200 auto it = FindAnimationInfoForAnimatable(pAnimatable);
171201
@@ -199,49 +229,7 @@ std::pair<float, float> AnimationManager::CalculateCurrentValue(AnimationInfo& i
199229 }
200230 else
201231 {
202- float prevTime = 0.0f;
203- float prevRelValue = 0.0f;
204-
205- for (auto&& i : info.Curve.KeyFrames)
206- {
207- if (prevTime <= relativeTime && relativeTime < i.RelativeTime)
208- {
209- switch (i.Transition)
210- {
211- case AnimationTransitionKind::Instantaneous:
212- return {relativeTime, prevRelValue};
213-
214- case AnimationTransitionKind::Discrete:
215- return {relativeTime, relativeTime < (prevTime + i.RelativeTime) / 2.0f ? prevRelValue : i.RelativeValue};
216-
217- case AnimationTransitionKind::Linear:
218- return {relativeTime, prevRelValue + (relativeTime - prevTime) / (i.RelativeTime - prevTime) * (i.RelativeValue - prevRelValue)};
219-
220- case AnimationTransitionKind::SpeedUp:
221- return {
222- relativeTime,
223- prevRelValue + (std::cos((relativeTime - prevTime) / (i.RelativeTime - prevTime) * std::atan(1.0f) * 2.0f) * -1.0f + 1.0f) * (i.RelativeValue - prevRelValue)
224- };
225-
226- case AnimationTransitionKind::SlowDown:
227- return {
228- relativeTime,
229- prevRelValue + std::sin((relativeTime - prevTime) / (i.RelativeTime - prevTime) * std::atan(1.0f) * 2.0f) * (i.RelativeValue - prevRelValue)
230- };
231-
232- case AnimationTransitionKind::Smooth:
233- return {
234- relativeTime,
235- prevRelValue + (std::cos((relativeTime - prevTime) / (i.RelativeTime - prevTime) * std::atan(1.0f) * 4.0f) * -0.5f + 0.5f) * (i.RelativeValue - prevRelValue)
236- };
237- }
238- }
239-
240- prevTime = i.RelativeTime;
241- prevRelValue = i.RelativeValue;
242- }
243-
244- return {prevTime, prevRelValue};
232+ return {relativeTime, info.InitialValue + info.Function(relativeTime) * (info.FinalValue - info.InitialValue)};
245233 }
246234 }
247235