11#include " ComponentAnimation.hpp"
2+ #include " ActorAnimation.hpp"
3+ #include " KeyFrames/KeyFrame.hpp"
24#include " ../Actor.hpp"
35#include " ../BlockReader.hpp"
46
57using namespace nima ;
68
79ComponentAnimation::ComponentAnimation () :
10+ m_ComponentIndex(0 ),
811 m_Properties(NULL ),
912 m_PropertiesCount(0 )
1013{
@@ -30,6 +33,95 @@ void ComponentAnimation::apply(float time, Actor* actor, float mix)
3033 }
3134}
3235
36+ void ComponentAnimation::triggerEvents (ActorComponent** components, float fromTime, float toTime, std::vector<ActorAnimationEvent>& events)
37+ {
38+ for (int i = 0 ; i < m_PropertiesCount; i++)
39+ {
40+ PropertyAnimation& property = m_Properties[i];
41+ switch (property.type ())
42+ {
43+ case PropertyType::Trigger:
44+ {
45+ int keyFramesCount = property.keyFramesCount ();
46+ if (keyFramesCount == 0 )
47+ {
48+ continue ;
49+ }
50+ int idx = 0 ;
51+ // Binary find the keyframe index.
52+ {
53+ int mid = 0 ;
54+ float element = 0 .0f ;
55+ int start = 0 ;
56+ int end = keyFramesCount-1 ;
57+
58+ while (start <= end)
59+ {
60+ mid = ((start + end) >> 1 );
61+ element = property.keyFrame (mid)->time ();
62+ if (element < toTime)
63+ {
64+ start = mid + 1 ;
65+ }
66+ else if (element > toTime)
67+ {
68+ end = mid - 1 ;
69+ }
70+ else
71+ {
72+ start = mid;
73+ break ;
74+ }
75+ }
76+
77+ idx = start;
78+ }
79+
80+ if (idx == 0 )
81+ {
82+ if (property.keyFrame (0 )->time () == toTime)
83+ {
84+ ActorEvent* actorEvent = reinterpret_cast <ActorEvent*>(components[m_ComponentIndex]);
85+ events.emplace_back (ActorAnimationEvent (actorEvent, toTime, 0 .0f ));
86+ // ActorComponent component = components[keyedComponent.ComponentIndex];
87+ // triggerEvents.Add(new AnimationEventArgs(component.Name, component, property.PropertyType, toTime, 0.0f));
88+ }
89+ }
90+ else
91+ {
92+ for (int k = idx-1 ; k >= 0 ; k--)
93+ {
94+ float frameTime = property.keyFrame (k)->time ();
95+
96+ if (frameTime > fromTime)
97+ {
98+ ActorEvent* actorEvent = reinterpret_cast <ActorEvent*>(components[m_ComponentIndex]);
99+ events.emplace_back (ActorAnimationEvent (actorEvent, frameTime, toTime-frameTime));
100+
101+ // ActorComponent component = components[keyedComponent.ComponentIndex];
102+ // triggerEvents.Add(new AnimationEventArgs(component.Name, component, property.PropertyType, frameTime, toTime-frameTime));
103+ /* triggered.push({
104+ name:component._Name,
105+ component:component,
106+ propertyType:property._Type,
107+ keyFrameTime:frame._Time,
108+ elapsed:toTime-frame._Time
109+ });*/
110+ }
111+ else
112+ {
113+ break ;
114+ }
115+ }
116+ }
117+ break ;
118+ }
119+ default :
120+ break ;
121+ }
122+ }
123+ }
124+
33125void ComponentAnimation::read (BlockReader* reader, ActorComponent** components)
34126{
35127 m_ComponentIndex = reader->readUnsignedShort ();
0 commit comments