Skip to content

Commit

Permalink
Implementing event callbacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi-rosso committed Feb 18, 2017
1 parent 025671c commit c4c6c02
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 9 deletions.
19 changes: 19 additions & 0 deletions Source/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ Actor::Actor() :
m_Components(nullptr),
m_Nodes(nullptr),
m_Root(nullptr),
m_EventCallbackUserData(nullptr),
m_EventCallback(nullptr),
m_MaxTextureIndex(0),
m_ImageNodeCount(0),
m_SolverNodeCount(0),
m_AnimationsCount(0),
m_ImageNodes(nullptr),
m_Solvers(nullptr),
m_Animations(nullptr)

{

}
Expand Down Expand Up @@ -93,6 +96,12 @@ ActorComponent* Actor::component(const std::string& name) const
return nullptr;
}

void Actor::eventCallback(ActorAnimationEvent::Callback callback, void* userdata)
{
m_EventCallbackUserData = userdata;
m_EventCallback = callback;
}

ActorAnimation* Actor::animation(const std::string& name) const
{
for(int i = 0; i < m_AnimationsCount; i++)
Expand All @@ -106,6 +115,16 @@ ActorAnimation* Actor::animation(const std::string& name) const
return nullptr;
}

ActorAnimationInstance* Actor::animationInstance(const std::string& name)
{
ActorAnimation* a = animation(name);
if(a == nullptr)
{
return nullptr;
}
return new ActorAnimationInstance(this, a);
}

void Actor::load(unsigned char* bytes, unsigned int length)
{
dispose();
Expand Down
9 changes: 8 additions & 1 deletion Source/Actor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "BlockReader.hpp"
#include "Solver.hpp"
#include "Animation/ActorAnimation.hpp"
#include "Animation/ActorAnimationInstance.hpp"

namespace nima
{
Expand All @@ -25,9 +26,10 @@ namespace nima
ActorIKTarget = 11,
ActorEvent = 12
};

class Actor
{
friend class ActorAnimationInstance;
public:
Actor();
virtual ~Actor();
Expand All @@ -45,6 +47,8 @@ namespace nima
ActorComponent** m_Components;
ActorNode** m_Nodes;
ActorNode* m_Root;
void* m_EventCallbackUserData;
ActorAnimationEvent::Callback m_EventCallback;
void readComponentsBlock(BlockReader* block);
void readAnimationsBlock(BlockReader* block);

Expand All @@ -70,6 +74,8 @@ namespace nima
ActorComponent* component(unsigned int index) const;
ActorComponent* component(unsigned short index) const;
ActorComponent* component(const std::string& name) const;

void eventCallback(ActorAnimationEvent::Callback callback, void* userdata = nullptr);

template<typename T>
T component(const std::string& name) const
Expand All @@ -79,6 +85,7 @@ namespace nima

ActorNode* root() const;
ActorAnimation* animation(const std::string& name) const;
ActorAnimationInstance* animationInstance(const std::string& name);

void copy(const Actor& actor);
const int textureCount() const;
Expand Down
4 changes: 2 additions & 2 deletions Source/Animation/ActorAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ void ActorAnimation::apply(float time, Actor* actor, float mix)
}
}

void ActorAnimation::triggerEvents(ActorComponent** components, float fromTime, float toTime, std::vector<ActorAnimationEvent>& events)
void ActorAnimation::triggerEvents(Actor* actor, float fromTime, float toTime, std::vector<ActorAnimationEvent>& events)
{
for(auto keyedComponent : m_TriggerComponents)
{
keyedComponent->triggerEvents(components, fromTime, toTime, events);
keyedComponent->triggerEvents(actor, fromTime, toTime, events);
}
}

Expand Down
4 changes: 3 additions & 1 deletion Source/Animation/ActorAnimation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <string>
#include <vector>
#include <functional>
#include "ComponentAnimation.hpp"

namespace nima
Expand All @@ -13,6 +14,7 @@ namespace nima

struct ActorAnimationEvent
{
typedef std::function<void(const ActorAnimationEvent&, void*)> Callback;
ActorEvent* actorEvent;
float keyFrameTime;
float elapsedTime;
Expand Down Expand Up @@ -50,7 +52,7 @@ namespace nima

void apply(float time, Actor* actor, float mix);

void triggerEvents(ActorComponent** components, float fromTime, float toTime, std::vector<ActorAnimationEvent>& events);
void triggerEvents(Actor* actor, float fromTime, float toTime, std::vector<ActorAnimationEvent>& events);

};
}
Expand Down
168 changes: 168 additions & 0 deletions Source/Animation/ActorAnimationInstance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#include "ActorAnimationInstance.hpp"
#include "../Actor.hpp"
#include <cmath>

using namespace nima;

ActorAnimationInstance::ActorAnimationInstance(Actor* actor, ActorAnimation* animation) :
m_Actor(actor),
m_Animation(animation),
m_Time(0.0f),
m_Min(0.0f),
m_Max(animation->duration()),
m_Range(animation->duration()),
m_Loop(animation->isLooping()),
m_EventCallbackUserData(nullptr),
m_EventCallback(nullptr)
{

}

ActorAnimationInstance::~ActorAnimationInstance()
{

}

float ActorAnimationInstance::duration() const
{
return m_Range;
}

float ActorAnimationInstance::min() const
{
return m_Min;
}

float ActorAnimationInstance::max() const
{
return m_Max;
}

float ActorAnimationInstance::time() const
{
return m_Time;
}

void ActorAnimationInstance::time(float value)
{
float delta = value - m_Time;
float time = m_Time + std::fmod(delta, m_Range);

if(time < m_Min)
{
if(m_Loop)
{
time = m_Max - (m_Min - time);
}
else
{
time = m_Min;
}
}
else if(time > m_Max)
{
if(m_Loop)
{
time = m_Min + (time - m_Max);
}
else
{
time = m_Max;
}
}
m_Time = time;
}

bool ActorAnimationInstance::isLooping() const
{
return m_Loop;
}

void ActorAnimationInstance::isLooping(bool isIt)
{
m_Loop = isIt;
}

void ActorAnimationInstance::eventCallback(ActorAnimationEvent::Callback callback, void* userdata)
{
m_EventCallbackUserData = userdata;
m_EventCallback = callback;
}

void ActorAnimationInstance::advance(float seconds)
{
float time = m_Time;
time += std::fmod(seconds, m_Range);
if(time < m_Min)
{
if(m_Loop)
{
m_Animation->triggerEvents(m_Actor, time, m_Time, m_Events);
time = m_Max - (m_Min - time);
m_Animation->triggerEvents(m_Actor, time, m_Max, m_Events);
}
else
{
time = m_Min;
if(m_Time != time)
{
m_Animation->triggerEvents(m_Actor, m_Min, m_Time, m_Events);
}
}
}
else if(time > m_Max)
{
if(m_Loop)
{
m_Animation->triggerEvents(m_Actor, time, m_Time, m_Events);
time = m_Min + (time - m_Max);
m_Animation->triggerEvents(m_Actor, m_Min-0.001f, time, m_Events);
}
else
{
time = m_Max;
if(m_Time != time)
{
m_Animation->triggerEvents(m_Actor, m_Time, m_Max, m_Events);
}
}
}
else if(time > m_Time)
{
m_Animation->triggerEvents(m_Actor, m_Time, time, m_Events);
}
else
{
m_Animation->triggerEvents(m_Actor, time, m_Time, m_Events);
}

for(const ActorAnimationEvent& ev : m_Events)
{
if(m_EventCallback != nullptr)
{
m_EventCallback(ev, m_EventCallbackUserData);
}
if(m_Actor->m_EventCallback != nullptr)
{
m_Actor->m_EventCallback(ev, m_Actor->m_EventCallbackUserData);
}
/*if (AnimationEvent != null)
{
AnimationEvent(this, ev);
}
m_Actor.OnAnimationEvent(ev);*/
}
m_Events.clear();
/*for(var i = 0; i < triggeredEvents.length; i++)
{
var event = triggeredEvents[i];
this.dispatch("animationEvent", event);
m_Actor.dispatch("animationEvent", event);
}*/
m_Time = time;
}

void ActorAnimationInstance::apply(float mix)
{
m_Animation->apply(m_Time, m_Actor, mix);
}
44 changes: 44 additions & 0 deletions Source/Animation/ActorAnimationInstance.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef _NIMA_ACTORANIMATIONINSTANCE_HPP_
#define _NIMA_ACTORANIMATIONINSTANCE_HPP_

#include <string>
#include <vector>
#include "ActorAnimation.hpp"

namespace nima
{
class ActorAnimationInstance
{
private:
Actor* m_Actor;
ActorAnimation* m_Animation;
float m_Time;
float m_Min;
float m_Max;
float m_Range;
bool m_Loop;
std::vector<ActorAnimationEvent> m_Events;
void* m_EventCallbackUserData;
ActorAnimationEvent::Callback m_EventCallback;

public:

ActorAnimationInstance(Actor* actor, ActorAnimation* animation);
~ActorAnimationInstance();

float duration() const;
float min() const;
float max() const;
float time() const;
void time(float value);
bool isLooping() const;
void isLooping(bool isIt);

void advance(float seconds);
void apply(float mix);

void eventCallback(ActorAnimationEvent::Callback callback, void* userdata = nullptr);

};
}
#endif
6 changes: 3 additions & 3 deletions Source/Animation/ComponentAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void ComponentAnimation::apply(float time, Actor* actor, float mix)
}
}

void ComponentAnimation::triggerEvents(ActorComponent** components, float fromTime, float toTime, std::vector<ActorAnimationEvent>& events)
void ComponentAnimation::triggerEvents(Actor* actor, float fromTime, float toTime, std::vector<ActorAnimationEvent>& events)
{
for(int i = 0; i < m_PropertiesCount; i++)
{
Expand Down Expand Up @@ -81,7 +81,7 @@ void ComponentAnimation::triggerEvents(ActorComponent** components, float fromTi
{
if(property.keyFrame(0)->time() == toTime)
{
ActorEvent* actorEvent = reinterpret_cast<ActorEvent*>(components[m_ComponentIndex]);
ActorEvent* actorEvent = reinterpret_cast<ActorEvent*>(actor->component(m_ComponentIndex));
events.emplace_back(ActorAnimationEvent(actorEvent, toTime, 0.0f));
//ActorComponent component = components[keyedComponent.ComponentIndex];
//triggerEvents.Add(new AnimationEventArgs(component.Name, component, property.PropertyType, toTime, 0.0f));
Expand All @@ -95,7 +95,7 @@ void ComponentAnimation::triggerEvents(ActorComponent** components, float fromTi

if(frameTime > fromTime)
{
ActorEvent* actorEvent = reinterpret_cast<ActorEvent*>(components[m_ComponentIndex]);
ActorEvent* actorEvent = reinterpret_cast<ActorEvent*>(actor->component(m_ComponentIndex));
events.emplace_back(ActorAnimationEvent(actorEvent, frameTime, toTime-frameTime));

//ActorComponent component = components[keyedComponent.ComponentIndex];
Expand Down
2 changes: 1 addition & 1 deletion Source/Animation/ComponentAnimation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace nima

void read(BlockReader* reader, ActorComponent** components);
void apply(float time, Actor* actor, float mix);
void triggerEvents(ActorComponent** components, float fromTime, float toTime, std::vector<ActorAnimationEvent>& events);
void triggerEvents(Actor* actor, float fromTime, float toTime, std::vector<ActorAnimationEvent>& events);

};
}
Expand Down
1 change: 0 additions & 1 deletion Source/BlockReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace nima
class Vec2D;

class BlockReader;
typedef std::shared_ptr<BlockReader> BlockReaderPtr;

class BlockReader : public BinaryReader
{
Expand Down

0 comments on commit c4c6c02

Please sign in to comment.