soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SAnimation.cpp
1
2#include <souistd.h>
3#include <animation/SAnimation.h>
4#include <helper/STime.h>
5
6SNSBEGIN
7
9{
11 if (value.IsEmpty())
12 {
13 d.type = ABSOLUTE_VALUE;
14 d.value = SLayoutSize(0.0f);
15 }
16 else if (value.EndsWith(L"%", true))
17 {
18 d.type = RELATIVE_TO_SELF;
19 d.value = SLayoutSize((float)_wtof(value.Left(value.GetLength() - 1)) / 100, SLayoutSize::px);
20 }
21 else if (value.EndsWith(L"%p", true))
22 {
23 d.type = RELATIVE_TO_PARENT;
24 d.value = SLayoutSize((float)_wtof(value.Left(value.GetLength() - 2)) / 100, SLayoutSize::px);
25 }
26 else
27 {
28 d.type = ABSOLUTE_VALUE;
30 }
31 return d;
32}
33
34////////////////////////////////////////////////////////////////////
36{
37 return false;
38}
39
40int SAnimation::resolveSize(const SValueDescription &value, int size, int parentSize, int nScale)
41{
42 float fValue = 0.0f;
43 if (value.value.unit == SLayoutSize::px)
44 fValue = value.value.fSize;
45 else
46 fValue = value.value.fSize * nScale / 100;
47
48 switch (value.type)
49 {
50 case RELATIVE_TO_SELF:
51 return (int)(size * fValue);
52 case RELATIVE_TO_PARENT:
53 return (int)(parentSize * fValue);
54 case ABSOLUTE_VALUE:
55 default:
56 return (int)fValue;
57 }
58}
59
60void SAnimation::applyTransformation(float interpolatedTime, ITransformation *t)
61{
62}
63
65{
66 return mEnded;
67}
68
70{
71 return mStarted;
72}
73
75{
76 if (mListener != NULL)
77 {
78 mListener->OnAnimationStop(this);
79 }
80}
81
83{
84 if (mListener != NULL)
85 {
86 mListener->OnAnimationRepeat(this);
87 }
88}
89
91{
92 if (mListener != NULL)
93 {
94 mListener->OnAnimationStart(this);
95 }
96}
97
99{
100 return mStartTime == -2;
101}
102
103BOOL SAnimation::getTransformation(uint64_t currentTime, ITransformation *outTransformation)
104{
105 if (mStartTime == -1)
106 {
107 mStartTime = currentTime;
108 }
109
110 int64_t startOffset = getStartOffset();
111 long duration = mDuration;
112 float normalizedTime;
113 if (duration != 0)
114 {
115 normalizedTime = ((float)(currentTime - (mStartTime + startOffset))) / (float)duration;
116 }
117 else
118 {
119 // time is a step-change with a zero duration
120 normalizedTime = currentTime < mStartTime ? 0.0f : 1.0f;
121 }
122
123 BOOL expired = normalizedTime >= 1.0f || isCanceled();
124 BOOL bMore = !expired;
125
126 if (!mFillEnabled || mRepeatCount != 0)
127 normalizedTime = smax(smin(normalizedTime, 1.0f), 0.0f);
128
129 if ((normalizedTime >= 0.0f || mFillBefore) && (normalizedTime <= 1.0f || mFillAfter))
130 {
131 if (!mStarted)
132 {
133 mStarted = true;
135 }
136
137 if (mFillEnabled)
138 normalizedTime = smax(smin(normalizedTime, 1.0f), 0.0f);
139
140 if (mCycleFlip)
141 {
142 normalizedTime = 1.0f - normalizedTime;
143 }
144
145 float interpolatedTime = mInterpolator->getInterpolation(normalizedTime);
146 outTransformation->Clear();
147 applyTransformation(interpolatedTime, outTransformation);
148 }
149
150 if (expired)
151 {
153 {
154 if (!mEnded)
155 {
156 mEnded = true;
158 }
159 }
160 else
161 {
162 if (mRepeatCount > 0)
163 {
164 mRepeated++;
165 }
166 else
167 {
168 mRepeated = 1;
169 }
170
171 if (mRepeatMode == REVERSE)
172 {
174 }
175
176 mStartTime = currentTime;
177 bMore = true;
178
180 }
181 }
182
183 return bMore;
184}
185
186BOOL SAnimation::getTransformation2(uint64_t currentTime, ITransformation *outTransformation, float scale)
187{
188 mScaleFactor = scale;
189 return getTransformation(currentTime, outTransformation);
190}
191
193{
194 if (getRepeatCount() < 0)
195 {
196 return INT_MAX;
197 }
198 return getStartOffset() + getDuration() * (getRepeatCount() + 1);
199}
200
202{
203 if (!mInterpolator)
204 {
206 }
207}
208
209void SAnimation::setAnimationListener(IAnimationListener *listener)
210{
211 mListener = listener;
212}
213
214ZAdjustment SAnimation::getZAdjustment() const
215{
216 return mZAdjustment;
217}
218
220{
221 return mRepeatCount;
222}
223
225{
226 return mRepeatMode;
227}
228
230{
231 return mRepeated == 0 ? mStartOffset : 0;
232}
233
235{
236 return mDuration;
237}
238
240{
241 return mStartTime;
242}
243
244IInterpolator *SAnimation::getInterpolator() const
245{
246 return mInterpolator;
247}
248
250{
251 return mScaleFactor;
252}
253
254void SAnimation::setZAdjustment(ZAdjustment zAdjustment)
255{
256 mZAdjustment = zAdjustment;
257}
258
259void SAnimation::setRepeatCount(int repeatCount)
260{
261 if (repeatCount < 0)
262 {
263 repeatCount = INFINITE;
264 }
265 mRepeatCount = repeatCount;
266}
267
268void SAnimation::setRepeatMode(RepeatMode repeatMode)
269{
270 mRepeatMode = repeatMode;
271}
272
277
279{
280 setStartTime(-1);
281}
282
283void SAnimation::setStartTime(int64_t startTimeMillis)
284{
285 mStartTime = startTimeMillis;
286 mStarted = mEnded = false;
287 mCycleFlip = false;
288 mRepeated = 0;
289}
290
292{
293 mStartOffset = offset;
294}
295
296void SAnimation::setFillEnabled(BOOL fillEnabled)
297{
298 mFillEnabled = fillEnabled;
299}
300
302{
303 return mFillEnabled;
304}
305
307{
308 return mFillAfter;
309}
310
312{
313 mFillAfter = bFill;
314}
315
317{
318 return mFillBefore;
319}
320
322{
323 mFillBefore = bFill;
324}
325
327{
328 mDuration = (long)(mDuration * scale);
329 mStartOffset = (long)(mStartOffset * scale);
330}
331
332void SAnimation::setDuration(long durationMillis)
333{
334 mDuration = durationMillis;
335}
336
337void SAnimation::setInterpolator(IInterpolator *i)
338{
339 mInterpolator = i;
340}
341
343{
344 if (mStarted && !mEnded)
345 {
346 mStartTime = -2;
347 mEnded = true;
349 }
350}
351
353{
354 mEnded = false;
355
356 mStarted = false;
357
358 mPaused = false;
359
360 mStartTime = START_ON_FIRST_FRAME;
361
362 mStartOffset = 0;
363 mDuration = 0;
364 mRepeatCount = 0;
365 mRepeated = 0;
366 mRepeatMode = RESTART;
367 mZAdjustment = ZORDER_NORMAL;
368 mListener = NULL;
369 mScaleFactor = 1.0f;
370
371 mFillBefore = false;
372
373 mFillAfter = true;
374
375 mFillEnabled = true;
376
377 mUserData = 0;
379}
380
381void SAnimation::copy(const IAnimation *src)
382{
383 const SAnimation *src2 = (SAnimation *)src;
385 mDuration = src2->mDuration;
387 mRepeatMode = src2->mRepeatMode;
390 mFillBefore = src2->mFillBefore;
391 mFillAfter = src2->mFillAfter;
394
395 mUserData = src2->mUserData;
396 m_nID = src2->m_nID;
397 m_strName = src2->m_strName;
398}
399
400IAnimation *SAnimation::clone() const
401{
403 if (pRet)
404 {
405 pRet->copy(this);
406 }
407 return pRet;
408}
409
410void SAnimation::initialize(int width, int height, int parentWidth, int parentHeight, int nScale)
411{
412}
413
415{
416 reset();
417}
418
419void SAnimation::setUserData(ULONG_PTR data)
420{
421 mUserData = data;
422}
423
424ULONG_PTR SAnimation::getUserData() const
425{
426 return mUserData;
427}
428
430{
431 if (!mEnded && !mPaused)
432 {
433 mPaused = true;
435 if (mListener)
436 mListener->OnAnimationPauseChange(this, TRUE);
437 }
438}
439
441{
442 if (!mEnded && mPaused)
443 {
444 mPaused = false;
445 if (mStartTime != START_ON_FIRST_FRAME)
446 {
447 uint64_t now = STime::GetCurrentTimeMs();
448 mStartTime += now - mPauseTime;
449 }
450 if (mListener)
451 mListener->OnAnimationPauseChange(this, FALSE);
452 }
453}
454
458
459SNSEND
An interpolator where the rate of change starts and ends slowly, accelerating in the middle.
BOOL getTransformation(uint64_t currentTime, ITransformation *outTransformation) OVERRIDE
Gets the transformation at a specific time.
int resolveSize(const SValueDescription &value, int size, int parentSize, int nScale)
Converts the information in the description of a size to an actual dimension.
bool mStarted
Set by getTransformation(long, STransformation) when the animation starts.
Definition SAnimation.h:79
int64_t getStartTime() SCONST OVERRIDE
Gets the start time of the animation.
BOOL mFillEnabled
Indicates whether fillBefore should be taken into account.
Definition SAnimation.h:151
int mRepeated
Indicates how many times the animation was repeated.
Definition SAnimation.h:110
BOOL mFillAfter
Indicates whether the animation transformation should be applied after the animation ends.
Definition SAnimation.h:146
void pause() OVERRIDE
Pauses the animation.
void scaleCurrentDuration(float scale) OVERRIDE
Scales the current duration of the animation.
void setFillBefore(BOOL bFill) OVERRIDE
Sets whether the animation transformation should be applied before the animation starts.
BOOL getFillAfter() SCONST OVERRIDE
Gets whether the animation transformation should be applied after the animation ends.
void reset() OVERRIDE
Resets the animation to its initial state.
~SAnimation()
Destructor for SAnimation.
void ensureInterpolator()
Ensures that this animation has an interpolator. Will use an AccelerateDecelerateInterpolator if noth...
float mScaleFactor
Scale factor to apply to pivot points, etc. during animation. Subclasses retrieve the value via getSc...
Definition SAnimation.h:135
void initialize(int width, int height, int parentWidth, int parentHeight, int nScale) OVERRIDE
Initializes the animation with the dimensions of the object and its parent.
void copy(const IAnimation *src) OVERRIDE
Copies the properties of another animation to this animation.
BOOL hasStarted() SCONST OVERRIDE
Checks whether the animation has started.
void cancel() OVERRIDE
Cancels the animation.
bool mPaused
Indicates whether the animation is paused.
Definition SAnimation.h:161
void setUserData(ULONG_PTR data) OVERRIDE
Sets user data associated with the animation.
uint64_t mPauseTime
The time at which the animation was paused.
Definition SAnimation.h:166
void fireAnimationStart()
Notifies the animation listener that the animation has started.
void setFillAfter(BOOL bFill) OVERRIDE
Sets whether the animation transformation should be applied after the animation ends.
IAnimation * clone() SCONST OVERRIDE
Creates a new animation with a duration of 0ms, the default interpolator, with fillBefore set to true...
void applyTransformation(float interpolatedTime, ITransformation *t) OVERRIDE
Applies the transformation at a specific interpolated time.
ULONG_PTR getUserData() SCONST OVERRIDE
Gets user data associated with the animation.
IAnimationListener * mListener
The animation listener to be notified when the animation starts, ends, or repeats.
Definition SAnimation.h:125
ZAdjustment mZAdjustment
Desired Z order mode during animation.
Definition SAnimation.h:130
BOOL hasAlpha() SCONST OVERRIDE
Checks whether the animation affects the alpha property.
BOOL isFillEnabled() SCONST OVERRIDE
Checks whether fillBefore should be taken into account.
BOOL mFillBefore
Indicates whether the animation transformation should be applied before the animation starts....
Definition SAnimation.h:141
long getStartOffset() SCONST OVERRIDE
Gets the delay in milliseconds after which the animation must start.
SAnimation()
Default constructor for SAnimation.
long computeDurationHint() SCONST OVERRIDE
Computes the duration hint for the animation.
void setStartOffset(long offset) OVERRIDE
Sets the delay in milliseconds after which the animation must start.
BOOL getTransformation2(uint64_t currentTime, ITransformation *outTransformation, float scale) OVERRIDE
Gets the transformation at a specific time.
void setAnimationListener(IAnimationListener *listener) OVERRIDE
Sets the animation listener to be notified when the animation starts, ends, or repeats.
void startNow() OVERRIDE
Starts the animation immediately.
ZAdjustment getZAdjustment() SCONST OVERRIDE
Gets the desired Z order mode during animation.
long mDuration
The duration of one animation cycle in milliseconds.
Definition SAnimation.h:100
int getRepeatCount() SCONST OVERRIDE
Gets the number of times the animation must repeat.
BOOL hasEnded() SCONST OVERRIDE
Checks whether the animation has ended.
void setDuration(long durationMillis) OVERRIDE
Sets the duration of one animation cycle in milliseconds.
void setStartTime(int64_t startTimeMillis) OVERRIDE
Sets the start time of the animation.
float getScaleFactor()
Gets the scale factor that should be applied to pre-scaled values in an Animation....
void setInterpolator(IInterpolator *i) OVERRIDE
Sets the interpolator used by the animation to smooth the movement.
void start() OVERRIDE
Starts the animation.
void setFillEnabled(BOOL fillEnabled) OVERRIDE
Sets whether fillBefore should be taken into account.
void setZAdjustment(ZAdjustment zAdjustment) OVERRIDE
Sets the desired Z order mode during animation.
void fireAnimationEnd()
Notifies the animation listener that the animation has ended.
int mRepeatCount
The number of times the animation must repeat. By default, an animation repeats indefinitely.
Definition SAnimation.h:105
ULONG_PTR mUserData
User data associated with the animation.
Definition SAnimation.h:156
bool mCycleFlip
Set by getTransformation(long, STransformation) when the animation repeats in REVERSE mode.
Definition SAnimation.h:84
SAutoRefPtr< IInterpolator > mInterpolator
The interpolator used by the animation to smooth the movement.
Definition SAnimation.h:120
void resume() OVERRIDE
Resumes the animation.
void setRepeatCount(int repeatCount) OVERRIDE
Sets the number of times the animation must repeat.
void setRepeatMode(RepeatMode repeatMode) OVERRIDE
Sets the repeat mode of the animation.
void fireAnimationRepeat()
Notifies the animation listener that the animation has repeated.
bool mEnded
Set by getTransformation(long, STransformation) when the animation ends.
Definition SAnimation.h:74
IInterpolator * getInterpolator() SCONST OVERRIDE
Gets the interpolator used by the animation.
uint64_t mStartTime
The time in milliseconds at which the animation must start.
Definition SAnimation.h:89
RepeatMode mRepeatMode
The behavior of the animation when it repeats. The repeat mode is either RESTART or REVERSE.
Definition SAnimation.h:115
BOOL getFillBefore() SCONST OVERRIDE
Gets whether the animation transformation should be applied before the animation starts.
RepeatMode getRepeatMode() SCONST OVERRIDE
Gets the repeat mode of the animation.
long mStartOffset
The delay in milliseconds after which the animation must start. When the start offset is > 0,...
Definition SAnimation.h:95
long getDuration() SCONST OVERRIDE
Gets the duration of one animation cycle.
bool isCanceled()
Checks whether the animation is canceled.
virtual IAnimation * CreateAnimationByName(LPCWSTR pszName) const
Create an animation by name.
Definition SApp.cpp:654
布局大小类
Definition SLayoutSize.h:10
void parseString(const SStringW &strSize)
从字符串解析大小
LPCWSTR GetObjectClass() SCONST OVERRIDE
Definition Sobject.hpp:211
static SApplication * getSingletonPtr(void)
Definition SSingleton.h:73
A class representing an ASCII string.
Definition sstringw.h:96
static uint64_t GetCurrentTimeMs()
获取当前时间的毫秒数
Definition stime.cpp:189
Utility class to parse a string description of a size.
Definition SAnimation.h:24
static SValueDescription parseValue(const SStringW &value)
Parses a string description of a size. Size descriptions can appear in three forms:
Definition SAnimation.cpp:8
SLayoutSize value
The absolute or relative dimension for this Description.
Definition SAnimation.h:45
AniValueType type
One of Animation.ABSOLUTE_VALUE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
Definition SAnimation.h:40
SValueDescription(AniValueType _type=ABSOLUTE_VALUE, float _value=0.0f)
Constructor for SValueDescription.
Definition SAnimation.h:31