Skip to content

Commit

Permalink
Adding custom properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi-rosso committed Feb 23, 2017
1 parent c4c6c02 commit 70b088a
Show file tree
Hide file tree
Showing 12 changed files with 475 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Source/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "ActorRootBone.hpp"
#include "ActorIKTarget.hpp"
#include "ActorEvent.hpp"
#include "CustomProperty.hpp"
#include "BinaryReader.hpp"
#include "BlockReader.hpp"
#include "Exceptions/OverflowException.hpp"
Expand Down Expand Up @@ -277,6 +278,15 @@ void Actor::readComponentsBlock(BlockReader* block)
case BlockType::ActorEvent:
component = ActorEvent::read(this, componentBlock);
break;
case BlockType::CustomIntProperty:
component = CustomIntProperty::read(this, componentBlock);
break;
case BlockType::CustomFloatProperty:
component = CustomFloatProperty::read(this, componentBlock);
break;
case BlockType::CustomStringProperty:
component = CustomStringProperty::read(this, componentBlock);
break;
default:
// Not handled/expected block.
break;
Expand Down
7 changes: 5 additions & 2 deletions Source/Actor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ namespace nima
Atlases = 9,
Atlas = 10,
ActorIKTarget = 11,
ActorEvent = 12
ActorEvent = 12,
CustomIntProperty = 13,
CustomFloatProperty = 14,
CustomStringProperty = 15
};

class Actor
{
friend class ActorAnimationInstance;
Expand Down
52 changes: 52 additions & 0 deletions Source/ActorComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ActorComponent.hpp"
#include "ActorNode.hpp"
#include "CustomProperty.hpp"
#include "BlockReader.hpp"
#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -83,4 +84,55 @@ ActorComponent* ActorComponent::read(Actor* actor, BlockReader* reader, ActorCom
component->m_ParentIdx = reader->readUnsignedShort();

return component;
}

void ActorComponent::addCustomIntProperty(CustomIntProperty* property)
{
m_CustomIntProperties.push_back(property);
}

void ActorComponent::addCustomFloatProperty(CustomFloatProperty* property)
{
m_CustomFloatProperties.push_back(property);
}

void ActorComponent::addCustomStringProperty(CustomStringProperty* property)
{
m_CustomStringProperties.push_back(property);
}

CustomIntProperty* ActorComponent::getCustomIntProperty(const std::string& name)
{
for(CustomIntProperty* prop : m_CustomIntProperties)
{
if(prop->name() == name)
{
return prop;
}
}
return nullptr;
}

CustomFloatProperty* ActorComponent::getCustomFloatProperty(const std::string& name)
{
for(CustomFloatProperty* prop : m_CustomFloatProperties)
{
if(prop->name() == name)
{
return prop;
}
}
return nullptr;
}

CustomStringProperty* ActorComponent::getCustomStringProperty(const std::string& name)
{
for(CustomStringProperty* prop : m_CustomStringProperties)
{
if(prop->name() == name)
{
return prop;
}
}
return nullptr;
}
19 changes: 18 additions & 1 deletion Source/ActorComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace nima
class Actor;
class ActorNode;
class BlockReader;
class CustomIntProperty;
class CustomFloatProperty;
class CustomStringProperty;

enum class ComponentType
{
Expand All @@ -19,7 +22,10 @@ namespace nima
ActorRootBone = 4,
ActorImage = 5,
ActorIKTarget = 11,
ActorEvent = 12
ActorEvent = 12,
CustomIntProperty = 13,
CustomFloatProperty = 14,
CustomStringProperty = 15
};


Expand All @@ -30,6 +36,9 @@ namespace nima
std::string m_Name;
ActorNode* m_Parent;
Actor* m_Actor;
std::vector<CustomIntProperty*> m_CustomIntProperties;
std::vector<CustomFloatProperty*> m_CustomFloatProperties;
std::vector<CustomStringProperty*> m_CustomStringProperties;

private:
unsigned short m_ParentIdx;
Expand All @@ -54,6 +63,14 @@ namespace nima
virtual bool isNode() { return false; }

static ActorComponent* read(Actor* actor, BlockReader* reader, ActorComponent* component = NULL);

void addCustomIntProperty(CustomIntProperty* property);
void addCustomFloatProperty(CustomFloatProperty* property);
void addCustomStringProperty(CustomStringProperty* property);

CustomIntProperty* getCustomIntProperty(const std::string& name);
CustomFloatProperty* getCustomFloatProperty(const std::string& name);
CustomStringProperty* getCustomStringProperty(const std::string& name);
};
}
#endif
44 changes: 44 additions & 0 deletions Source/Animation/KeyFrames/KeyFrameCustomProperty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "KeyFrameCustomProperty.hpp"
#include "../../BlockReader.hpp"
#include "../../CustomProperty.hpp"
#include <cmath>

using namespace nima;

void KeyFrameIntProperty::setValue(ActorComponent* component, float value, float mix)
{
CustomIntProperty* property = reinterpret_cast<CustomIntProperty*>(component);
property->value((int)round(property->value() * (1.0f - mix) + value * mix));
}

void KeyFrameFloatProperty::setValue(ActorComponent* component, float value, float mix)
{
CustomFloatProperty* property = reinterpret_cast<CustomFloatProperty*>(component);
property->value(property->value() * (1.0f - mix) + value * mix);
}

bool KeyFrameStringProperty::read(BlockReader* reader, ActorComponent* component)
{
if(!Base::read(reader, component))
{
return false;
}
m_Value = reader->readString();
return true;
}

void KeyFrameStringProperty::setNext(KeyFrame* frame)
{
// Intentionally blank, we do not interpolate.
}

void KeyFrameStringProperty::apply(ActorComponent* component, float mix)
{
CustomStringProperty* property = reinterpret_cast<CustomStringProperty*>(component);
property->value(m_Value);
}

void KeyFrameStringProperty::applyInterpolation(ActorComponent* component, float time, KeyFrame* toFrame, float mix)
{
apply(component, mix);
}
37 changes: 37 additions & 0 deletions Source/Animation/KeyFrames/KeyFrameCustomProperty.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef _NIMA_KEYFRAMEINTPROPERTY_HPP_
#define _NIMA_KEYFRAMEINTPROPERTY_HPP_

#include "KeyFrameNumeric.hpp"
#include <string>

namespace nima
{
class ActorComponent;

class KeyFrameIntProperty : public KeyFrameInt
{
protected:
void setValue(ActorComponent* component, float value, float mix) override;
};

class KeyFrameFloatProperty : public KeyFrameNumeric
{
protected:
void setValue(ActorComponent* component, float value, float mix) override;
};

class KeyFrameStringProperty : public KeyFrame
{
typedef KeyFrame Base;
private:
std::string m_Value;

public:
bool read(BlockReader* reader, ActorComponent* component) override;
void setNext(KeyFrame* frame) override;
void apply(ActorComponent* component, float mix) override;
void applyInterpolation(ActorComponent* component, float time, KeyFrame* toFrame, float mix) override;
};
}

#endif
66 changes: 66 additions & 0 deletions Source/Animation/KeyFrames/KeyFrameNumeric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,72 @@ void KeyFrameNumeric::applyInterpolation(ActorComponent* component, float time,
break;
}

default:
// Unhandled interpolation...
break;
}
}

KeyFrameInt::KeyFrameInt() :
m_Value(0)
{

}

int KeyFrameInt::value() const
{
return m_Value;
}

bool KeyFrameInt::read(BlockReader* reader, ActorComponent* component)
{
if(!Base::read(reader, component))
{
return false;
}

m_Value = reader->readInt();

return true;
}

void KeyFrameInt::apply(ActorComponent* component, float mix)
{
this->setValue(component, m_Value, mix);
}

void KeyFrameInt::applyInterpolation(ActorComponent* component, float time, KeyFrame* toFrame, float mix)
{
switch(m_InterpolationType)
{
case InterpolationType::Mirrored:
case InterpolationType::Asymmetric:
case InterpolationType::Disconnected:
{
ValueTimeCurveInterpolator* interpolator = reinterpret_cast<ValueTimeCurveInterpolator*>(m_Interpolator);
if(interpolator != nullptr)
{
float v = (float)interpolator->get((double)time);
setValue(component, v, mix);
}
break;
}

case InterpolationType::Hold:
{
setValue(component, m_Value, mix);
break;
}

case InterpolationType::Linear:
{
KeyFrameInt* to = reinterpret_cast<KeyFrameInt*>(toFrame);

float f = (time - m_Time)/(to->m_Time-m_Time);
setValue(component, m_Value * (1.0f-f) + to->m_Value * f, mix);
break;
}

default:
// Unhandled interpolation...
break;
Expand Down
18 changes: 18 additions & 0 deletions Source/Animation/KeyFrames/KeyFrameNumeric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ namespace nima
protected:
virtual void setValue(ActorComponent* component, float value, float mix) = 0;
};

class KeyFrameInt : public KeyFrameWithInterpolation
{
typedef KeyFrameWithInterpolation Base;
private:
int m_Value;

public:
KeyFrameInt();
int value() const;

bool read(BlockReader* reader, ActorComponent* component) override;
void apply(ActorComponent* component, float mix) override;
void applyInterpolation(ActorComponent* component, float time, KeyFrame* toFrame, float mix) override;

protected:
virtual void setValue(ActorComponent* component, float value, float mix) = 0;
};
}

#endif
10 changes: 10 additions & 0 deletions Source/Animation/PropertyAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "KeyFrames/KeyFrameVertexDeform.hpp"
#include "KeyFrames/KeyFrameIKStrength.hpp"
#include "KeyFrames/KeyFrameTrigger.hpp"
#include "KeyFrames/KeyFrameCustomProperty.hpp"
#include <cassert>

using namespace nima;
Expand Down Expand Up @@ -98,6 +99,15 @@ void PropertyAnimation::read(BlockReader* reader, ActorComponent* component)
case PropertyType::Trigger:
frame = new KeyFrameTrigger();
break;
case PropertyType::IntProperty:
frame = new KeyFrameIntProperty();
break;
case PropertyType::FloatProperty:
frame = new KeyFrameFloatProperty();
break;
case PropertyType::StringProperty:
frame = new KeyFrameStringProperty();
break;
default:
// This will only happen if the code isn't handling a property type it should handle.
// Check the PropertyType enum and make sure Max is in the right place (and that you're not missing a case).
Expand Down
3 changes: 3 additions & 0 deletions Source/Animation/PropertyAnimation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ namespace nima
VertexDeform = 9,
IKStrength = 10,
Trigger = 11,
IntProperty = 12,
FloatProperty = 13,
StringProperty = 14,
Max
};

Expand Down
Loading

0 comments on commit 70b088a

Please sign in to comment.