soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SAnimationSet.cpp
1
2#include <souistd.h>
3#include <animation/SAnimationSet.h>
4#include <SApp.h>
5#include <helper/STime.h>
6
7SNSBEGIN
8
10{
11 SAnimation::InitFromXml(pNode);
12 SXmlNode xmlNode(pNode);
13 SXmlNode xmlChild = xmlNode.first_child();
14 while (xmlChild)
15 {
16 IAnimation *ani = SApplication::getSingletonPtr()->CreateAnimationByName(xmlChild.name());
17 if (ani)
18 {
19 ani->InitFromXml(&xmlChild);
20 addAnimation(ani);
21 ani->Release();
22 }
23 xmlChild = xmlChild.next_sibling();
24 }
25 return TRUE;
26}
27
28HRESULT SAnimationSet::OnAttrDuration(const SStringW &value, BOOL bLoading)
29{
30 long iValue = _wtoi(value);
31 setDuration(iValue);
32 return S_FALSE;
33}
34
35HRESULT SAnimationSet::OnAttrFillBefore(const SStringW &value, BOOL bLoading)
36{
37 bool bValue = STRINGASBOOL(value);
38 setFillBefore(bValue);
39 return S_FALSE;
40}
41
42HRESULT SAnimationSet::OnAttrFillAfter(const SStringW &value, BOOL bLoading)
43{
44 bool bValue = STRINGASBOOL(value);
45 setFillAfter(bValue);
46 return S_FALSE;
47}
48
49HRESULT SAnimationSet::OnAttrStartOffset(const SStringW &value, BOOL bLoading)
50{
51 long iValue = _wtoi(value);
52 setStartOffset(iValue);
53 return S_FALSE;
54}
55
56void SAnimationSet::initialize(int width, int height, int parentWidth, int parentHeight, int nScale)
57{
58 bool durationSet = (mFlags & PROPERTY_DURATION_MASK) == PROPERTY_DURATION_MASK;
59 bool fillAfterSet = (mFlags & PROPERTY_FILL_AFTER_MASK) == PROPERTY_FILL_AFTER_MASK;
60 bool fillBeforeSet = (mFlags & PROPERTY_FILL_BEFORE_MASK) == PROPERTY_FILL_BEFORE_MASK;
61 bool shareInterpolator = (mFlags & PROPERTY_SHARE_INTERPOLATOR_MASK) == PROPERTY_SHARE_INTERPOLATOR_MASK;
62
63 if (shareInterpolator)
64 {
66 }
67
68 long duration = mDuration;
69 BOOL fillAfter = mFillAfter;
70 BOOL fillBefore = mFillBefore;
71 RepeatMode repeatMode = mRepeatMode;
72 IInterpolator *interpolator = mInterpolator;
73 long startOffset = mStartOffset;
74
75 for (UINT i = 0; i < mAnimations.GetCount(); i++)
76 {
77 IAnimation *a = mAnimations[i];
78 if (durationSet)
79 {
80 a->setDuration(duration);
81 }
82 if (fillAfterSet)
83 {
84 a->setFillAfter(fillAfter);
85 }
86 if (fillBeforeSet)
87 {
88 a->setFillBefore(fillBefore);
89 }
90 if (shareInterpolator)
91 {
92 a->setInterpolator(interpolator);
93 }
94 a->initialize(width, height, parentWidth, parentHeight, nScale);
95 }
96}
97
98void SAnimationSet::copy(const IAnimation *src)
99{
100 const SAnimationSet *src2 = sobj_cast<const SAnimationSet>(src);
101 if (!src2)
102 return;
103 SAnimation::copy(src);
104
105 mFlags = src2->mFlags;
106 mHasAlpha = src2->mHasAlpha;
107 mLastEnd = src2->mLastEnd;
108 for (UINT i = 0; i < src2->mAnimations.GetCount(); i++)
109 {
111 ani.Attach(src2->mAnimations[i]->clone());
112 mAnimations.Add(ani);
113 }
114}
115
117{
118 int count = (int)mAnimations.GetCount();
119 for (int i = 0; i < count; i++)
120 {
121 mAnimations[i]->scaleCurrentDuration(scale);
122 }
123}
124
125BOOL SAnimationSet::getTransformation(uint64_t currentTime, ITransformation *t)
126{
127 if (mStartTime == -1)
128 {
129 mStartTime = currentTime;
130 }
131
132 int64_t startOffset = getStartOffset();
133 if (currentTime < (mStartTime + startOffset))
134 {
135 return TRUE;
136 }
137 if (!mStarted)
138 {
139 mStarted = TRUE;
141 }
142 t->Clear();
143
144 int count = (int)mAnimations.GetCount();
145 if (!mChildStarted)
146 {
147 mChildStarted = TRUE;
148 for (int i = count - 1; i >= 0; --i)
149 {
150 IAnimation *a = mAnimations[i];
151 a->setStartTime(mStartTime + startOffset);
152 }
153 }
154
155 BOOL more = FALSE;
156 BOOL ended = TRUE;
157
158 for (int i = count - 1; i >= 0; --i)
159 {
160 IAnimation *a = mAnimations[i];
161
162 STransformation temp;
163 more = a->getTransformation2(currentTime, &temp, getScaleFactor()) || more;
164 t->Compose(&temp);
165
166 ended = a->hasEnded() && ended;
167 }
168
169 if (ended || (currentTime - startOffset - mStartTime) >= mLastEnd)
170 {
172 {
173 mEnded = TRUE;
174 mChildStarted = FALSE;
175 more = FALSE;
177 }
178 else
179 {
180 if (mRepeatCount > 0)
181 {
182 mRepeated++;
183 }
184 else
185 {
186 mRepeated = 1;
187 }
188 mChildStarted = FALSE;
189 mStartTime = currentTime;
190 more = TRUE;
192 }
193 }
194
195 return more;
196}
197
199{
200 int count = (int)mAnimations.GetCount();
201 long duration = 0;
202
203 bool durationSet = (mFlags & PROPERTY_DURATION_MASK) == PROPERTY_DURATION_MASK;
204 if (durationSet)
205 {
206 duration = mDuration;
207 }
208 else
209 {
210 for (int i = 0; i < count; i++)
211 {
212 duration = smax(duration, mAnimations[i]->computeDurationHint());
213 }
214 }
215
216 return duration;
217}
218
220{
221 mAnimations.Add(a);
222
223 if ((mFlags & PROPERTY_DURATION_MASK) == PROPERTY_DURATION_MASK)
224 {
225 mLastEnd = mStartOffset + mDuration;
226 }
227 else
228 {
229 long duration = a->computeDurationHint();
230 if (mAnimations.GetCount() == 1)
231 {
232 mDuration = duration;
233 }
234 else
235 {
236 mDuration = smax(mDuration, duration);
237 }
238 mLastEnd = mStartOffset + mDuration;
239 }
240
241 mDirty = true;
242}
243
244void SAnimationSet::setDuration(long durationMillis)
245{
246 mFlags |= PROPERTY_DURATION_MASK;
247 SAnimation::setDuration(durationMillis);
248 mLastEnd = mStartOffset + mDuration;
249}
250
252{
253 if (mDirty)
254 {
255 mDirty = mHasAlpha = false;
256
257 int count = (int)mAnimations.GetCount();
258 for (int i = 0; i < count; i++)
259 {
260 if (mAnimations.GetAt(i)->hasAlpha())
261 {
262 mHasAlpha = true;
263 break;
264 }
265 }
266 }
267
268 return mHasAlpha;
269}
270
271void SAnimationSet::setFillBefore(BOOL fillBefore)
272{
273 mFlags |= PROPERTY_FILL_BEFORE_MASK;
274 SAnimation::setFillBefore(fillBefore);
275}
276
277void SAnimationSet::setFillAfter(BOOL fillAfter)
278{
279 mFlags |= PROPERTY_FILL_AFTER_MASK;
280 SAnimation::setFillAfter(fillAfter);
281}
282
283void SAnimationSet::init()
284{
285 mFlags = 0;
286 mDirty = false;
287 mHasAlpha = false;
288 mChildStarted = false;
289}
290
291void SAnimationSet::setFlag(int mask, bool value)
292{
293 if (value)
294 {
295 mFlags |= mask;
296 }
297 else
298 {
299 mFlags &= ~mask;
300 }
301}
302
303SAnimationSet::SAnimationSet(bool shareInterpolator /*=true*/)
304{
305 setFlag(PROPERTY_SHARE_INTERPOLATOR_MASK, shareInterpolator);
306 init();
307}
308
310{
311 __baseCls::pause();
312 for (UINT i = 0; i < mAnimations.GetCount(); i++)
313 {
314 mAnimations[i]->pause();
315 }
316}
317
319{
320 for (UINT i = 0; i < mAnimations.GetCount(); i++)
321 {
322 mAnimations[i]->resume();
323 }
324 __baseCls::resume();
325}
326
327SNSEND
bool mStarted
Set by getTransformation(long, STransformation) when the animation starts.
Definition SAnimation.h:79
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 setFillBefore(BOOL bFill) OVERRIDE
Sets whether the animation transformation should be applied before the animation starts.
void ensureInterpolator()
Ensures that this animation has an interpolator. Will use an AccelerateDecelerateInterpolator if noth...
void copy(const IAnimation *src) OVERRIDE
Copies the properties of another animation to this animation.
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.
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.
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.
long mDuration
The duration of one animation cycle in milliseconds.
Definition SAnimation.h:100
void setDuration(long durationMillis) OVERRIDE
Sets the duration of one animation cycle in milliseconds.
float getScaleFactor()
Gets the scale factor that should be applied to pre-scaled values in an 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
SAutoRefPtr< IInterpolator > mInterpolator
The interpolator used by the animation to smooth the movement.
Definition SAnimation.h:120
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
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
long mStartOffset
The delay in milliseconds after which the animation must start. When the start offset is > 0,...
Definition SAnimation.h:95
bool isCanceled()
Checks whether the animation is canceled.
void setFillBefore(BOOL bFill) OVERRIDE
Sets whether the animation transformation should be applied before the animation starts.
long getDuration() SCONST OVERRIDE
Gets the duration of the AnimationSet. The duration of an AnimationSet is defined to be the duration ...
void setFillAfter(BOOL bFill) OVERRIDE
Sets whether the animation transformation should be applied after the animation ends.
void pause() OVERRIDE
Pauses the animation.
HRESULT OnAttrDuration(const SStringW &value, BOOL bLoading)
Custom attribute handler for duration.
void addAnimation(IAnimation *a)
Add a child animation to this animation set. The transforms of the child animations are applied in th...
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 resume() OVERRIDE
Resumes the animation.
SAnimationSet(bool shareInterpolator=true)
Constructor to use when building an AnimationSet from code.
HRESULT OnAttrFillBefore(const SStringW &value, BOOL bLoading)
Custom attribute handler for fillBefore.
void setDuration(long durationMillis) OVERRIDE
Sets the duration of every child animation.
void scaleCurrentDuration(float scale) OVERRIDE
Scales the current duration of the animation.
BOOL getTransformation(uint64_t currentTime, ITransformation *outTransformation) OVERRIDE
Gets the transformation at a specific time. The transformation of an animation set is the concatenati...
bool hasAlpha()
Checks if any child animation affects the alpha property.
HRESULT OnAttrFillAfter(const SStringW &value, BOOL bLoading)
Custom attribute handler for fillAfter.
HRESULT OnAttrStartOffset(const SStringW &value, BOOL bLoading)
Custom attribute handler for startOffset.
void copy(const IAnimation *src) OVERRIDE
Copies the properties of another animation to this animation.
BOOL InitFromXml(IXmlNode *pNode) OVERRIDE
Initializes the animation set from an XML node.
virtual IAnimation * CreateAnimationByName(LPCWSTR pszName) const
Create an animation by name.
Definition SApp.cpp:654
Smart pointer class for managing COM-style reference-counted objects.
void Attach(T *p2)
Attaches to an existing object without adding a reference.
static SApplication * getSingletonPtr(void)
Definition SSingleton.h:73
A class representing an ASCII string.
Definition sstringw.h:96
Defines the transformation to be applied at one point in time of an Animation.
Class representing an XML node.
Definition SXml.h:352
SXmlNode next_sibling() const
Gets the next sibling node in the children list of the parent node.
Definition SXml.cpp:393
SXmlNode first_child() const
Gets the first child node of the node.
Definition SXml.cpp:383
const wchar_t * name() const
Gets the name of the node.
Definition SXml.cpp:363
Interface for XML nodes.
Definition sxml-i.h:128