Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit

Permalink
Clean up src/components/sb code.
Browse files Browse the repository at this point in the history
	* Proper documentation.
	* Reorganize the order of the methods.
	* Opening braces on the same line than if(), while(), for() statements.
	* Suppress unused parameters in SmartBodyGestures::SmartBodyGestures().
  • Loading branch information
Laefy committed Aug 17, 2014
1 parent cbbdf87 commit 134854f
Show file tree
Hide file tree
Showing 22 changed files with 390 additions and 398 deletions.
68 changes: 33 additions & 35 deletions src/components/sb/SmartBodyAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ namespace Ember

SmartBodyAnimation::Type SmartBodyAnimation::getType(Name animationName)
{
switch (animationName)
{
switch (animationName) {

case Name::WALKING:
case Name::RUNNING:
return Type::MOVING;
Expand Down Expand Up @@ -63,15 +63,15 @@ SmartBodyAnimation::Name SmartBodyAnimation::getName() const

float SmartBodyAnimation::getMotionDuration(int motionIndex) const
{
if (motionIndex < 0 || !(motionIndex < getMotionNumber()))
{
if (motionIndex < 0 || !(motionIndex < getMotionNumber())) {

return -1;
}

SmartBody::SBMotion *motion = mAssetManager.getMotion(mMotionNames[motionIndex]);

if (!motion)
{
if (!motion) {

return -1;
}

Expand All @@ -92,27 +92,22 @@ SmartBodyAnimationInstance::SmartBodyAnimationInstance(const SmartBodyAnimation&

SmartBodyAnimationInstance::~SmartBodyAnimationInstance()
{
if (mLastRequestId != "")
{
mBmlProcessor.interruptBML(mCharacter, mLastRequestId, 0);
}
}

int SmartBodyAnimationInstance::getMotionNumber() const
const SmartBodyAnimation& SmartBodyAnimationInstance::getReference() const
{
return mReference.getMotionNumber();
return mReference;
}

void SmartBodyAnimationInstance::notifyUpdate()
int SmartBodyAnimationInstance::getMotionNumber() const
{
specifyReadyTime(false);
specifyStartTime(false);
return mReference.getMotionNumber();
}

void SmartBodyAnimationInstance::specifyStartTime(bool specify, float startTime /*= 0.0f*/)
{
if (specify)
{
if (specify) {

mStartTime = startTime;
}

Expand All @@ -121,42 +116,45 @@ void SmartBodyAnimationInstance::specifyStartTime(bool specify, float startTime

void SmartBodyAnimationInstance::specifyReadyTime(bool specify, float readyTime /*= 0.0f*/)
{
if (specify)
{
if (specify) {

mReadyTime = readyTime;
}

mHasReadyTime = specify;
}

void SmartBodyAnimationInstance::execute(const std::string& characterName)
{
std::string request;
getBmlRequest(request);
mLastRequestId = mBmlProcessor.execBML(characterName, request);

//Notify the animation instance that the request has been sent.
notifyUpdate();
}

void SmartBodyAnimationInstance::convertTimesToBmlStrings(std::vector<std::string>& times) const
{
//In case the vector is not empty.
times.clear();

if (mHasStartTime)
{
if (mHasStartTime) {

times.push_back(" start=\"" + std::to_string(mStartTime) + "\" ");

if (mHasReadyTime)
{
if (mHasReadyTime) {
times.push_back(" ready=\"" + std::to_string(mStartTime + mReadyTime) + "\" ");
}
}
}

const SmartBodyAnimation& SmartBodyAnimationInstance::getReference() const
{
return mReference;
}

void SmartBodyAnimationInstance::execute(const std::string& characterName)
void SmartBodyAnimationInstance::notifyUpdate()
{
std::string request;
getBmlRequest(request);
mLastRequestId = mBmlProcessor.execBML(characterName, request);

//Notify the animation instance that the request has been sent.
notifyUpdate();
//As soon as the new motion is launched, we need to reinitialize mStartTime and mReadyTime.
specifyReadyTime(false);
specifyStartTime(false);
}

}
66 changes: 32 additions & 34 deletions src/components/sb/SmartBodyAnimation.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ namespace Ember
/**
* @brief This class is used to represent an animation: each of them is identified by a name, and is composed of several motions.
*
* An SmartBodyAnimation should not be created directly. It is better to instantiate SmartBodyStaticAnimation, SmartBodyMovingAnimation
* and SmartBodyGestureAnimation. Moreover, the manipulation of this object should be handled by the SmartBodyAnimationManager.
* For each motion that compose an animation, there is a corresponding bml request that the SmartBodyAnimationManager retrieve when a
* character is animated, and then send to SmartBody with the bml processor.
* An SmartBodyAnimation should not be created directly. It is better to instantiate SmartBodyStaticAnimation, SmartBodyMovingAnimation, SmartBodyIntuitiveAnimation and SmartBodyGestureAnimation.
* In addition, the manipulation of this object should be handled by the SmartBodyAnimationManager. For each motion that composes an animation, there is a corresponding bml request that can be executed,
* using the bml processor. A SmartBodyAnimation is a global object, each of them represent the concept of the animation. To bound a SmartBodyAnimation to a character, in order to say, right now, the
* character is animated with motions from this animation state, you need to create a SmartBodyAnimationInstance, which will refer to the SmartBodyAnimation.
*
* @author Céline NOEL <celine.noel.7294@gmail.com>
*
Expand Down Expand Up @@ -72,7 +72,7 @@ class SmartBodyAnimation
};

/**
* @brief Gets what type of class has to be created for the given animation.
* @brief Gets what type of class has to be created for the given animations.
*/
static Type getType(Name animationName);

Expand All @@ -88,12 +88,13 @@ class SmartBodyAnimation
virtual ~SmartBodyAnimation();

/**
* @brief Returns the name for this animation.
* @brief Returns the name of the animation.
*/
SmartBodyAnimation::Name getName() const;

/**
* @brief Gets the request that is to be sent to the bml processor.
*
* @param attributes: the different attributes that will be appended to the request ("start", "ready", "x", "y", "z", etc.).
* @return false if the given index is invalid.
*/
Expand Down Expand Up @@ -135,15 +136,13 @@ class SmartBodyAnimation


/**
* @brief In instance for a SmartBodyAnimation.
* @brief An instance for a SmartBodyAnimation.
*
* Whereas a SmartBodyAnimation is used to describe an animation in general, this class is used into SmartBodyRepresentation to
* bound it to an animation. Everytime we want to animate a character with SmartBody, an object from this class has to be created by the
* SmartBodyAnimationManager and attributed to the character.
*
* In the same way as SmartBodyAnimation, SmartBodyAnimationInstance should be instantiated through children classes (MovindAnimationInstance,
* StaticAnimationInstance, GestureAnimationInstance). Each of them contains details about how the animation is being played on the
* representation they are linked to.
* Whereas a SmartBodyAnimation is used to describe an animation in general, this class is used into SmartBodyRepresentation to bound it to an animation. Everytime we want to animate a character with
* SmartBody, an object from this class has to be created by the SmartBodyAnimationManager and attributed to the character.
* In the same way as SmartBodyAnimation, SmartBodyAnimationInstance should be instantiated through children classes (MovindAnimationInstance, IntuitiveAnimationInstance, StaticAnimationInstance,
* GestureAnimationInstance). Each of them contains information about how the animation is being played on the representation it is linked to.
* The execute method allow you to animate the character with the motion attended and parameterized by this instance.
*
* @author Céline NOEL <celine.noel.7294@gmail.com>
*/
Expand All @@ -161,35 +160,30 @@ class SmartBodyAnimationInstance
*/
virtual ~SmartBodyAnimationInstance();

/**
* @brief Returns the bml request to execute the current motion of this instance.
/**
* @brief Returns a const reference over the corresponding SmartBodyAnimation.
*/
virtual bool getBmlRequest(std::string& request) const = 0;
const SmartBodyAnimation& getReference() const;

/**
* @brief Returns the number of motions constituing mReference.
*/
int getMotionNumber() const;

/**
* @brief Specify the time necessary to move from the previous animation to the new one.
* @brief Specify the time to wait before beginning the new motion.
* @param specify: set to false if the animation should start as soon as the previous one is finished, to true if you want to specify it yourself.
*/
void specifyStartTime(bool specify, float time = 0.0f);

/**
* @brief Specify the time necessary to move from the previous animation to the new one (you need to also specify the start time).
* @brief Specify the time necessary to move from the previous animation to the new one (the start time has to be specified too, or the behaviour could not be the one expected).
* @param specify: set to false if the duration of the blend should be handled by SmartBody, to true if you want to specify it yourself.
*/
void specifyReadyTime(bool specify, float time = 0.0f);

/**
* @brief Returns a const reference over the corresponding SmartBodyAnimation.
*/
const SmartBodyAnimation& getReference() const;

/**
* @brief Sends the bml request to SmartBody.
* @brief Sends the bml request to SmartBody, that will play the active motion with the specified parameters.
*/
void execute(const std::string& characterName);

Expand All @@ -207,14 +201,9 @@ class SmartBodyAnimationInstance
SmartBody::SBBmlProcessor& mBmlProcessor;

/**
* @brief The name of the character this animation is bound to.
* @brief The name of the character this animation instance is bound to.
*/
std::string mCharacter;

/**
* @brief The identifier of the last request sent (can be useful if you need to interrupt it).
*/
std::string mLastRequestId;
std::string mCharacter;

/**
* @brief The time when the animation starts.
Expand All @@ -227,7 +216,7 @@ class SmartBodyAnimationInstance
bool mHasStartTime;

/**
* @brief The time when the animation is fully blended.
* @brief The time taken by the animation to be fully blended.
*/
float mReadyTime;

Expand All @@ -236,12 +225,21 @@ class SmartBodyAnimationInstance
*/
float mHasReadyTime;


/**
* @brief Gets the bml request parts corresponding to start and ready attributes.
*/
void convertTimesToBmlStrings(std::vector<std::string>& times) const;

/**
* @brief Returns the bml request corresponding to this instance.
*/
virtual bool getBmlRequest(std::string& request) const = 0;

/**
* @brief The identifier of the last request sent (can be useful if you need to interrupt it).
*/
std::string mLastRequestId;

/**
* @brief Called just after a bml request has been sent, in order to reinitialize some values (mHasStartTime, mHasReadyTime).
*/
Expand Down
38 changes: 20 additions & 18 deletions src/components/sb/SmartBodyAnimationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ SmartBodyAnimationManager::SmartBodyAnimationManager(SmartBody::SBBmlProcessor&

SmartBodyAnimationManager::~SmartBodyAnimationManager()
{
for (auto& animation : mAnimations)
{
for (auto& animation : mAnimations) {

delete animation.second;
}
}
Expand Down Expand Up @@ -97,8 +97,8 @@ void SmartBodyAnimationManager::initialize(SmartBody::SBAssetManager& assetManag
void SmartBodyAnimationManager::addAnimation(SmartBodyAnimation::Name name, SmartBodyRepresentation& character)
{
//We must find what kind of animation we are looking at.
switch (SmartBodyAnimation::getType(name))
{
switch (SmartBodyAnimation::getType(name)) {

case SmartBodyAnimation::Type::STATIC:
addStaticAnimation(name, character);
break;
Expand Down Expand Up @@ -150,8 +150,8 @@ void SmartBodyAnimationManager::addIntuitiveAnimation(SmartBodyAnimation::Name n
{
SmartBodyAnimationInstance *animation = character.getPosture();

if (!animation || animation->getReference().getName() != name)
{
if (!animation || animation->getReference().getName() != name) {

freePosture(character);

SmartBodyIntuitiveAnimationInstance *animation = new SmartBodyIntuitiveAnimationInstance(dynamic_cast<SmartBodyIntuitiveAnimation&>(*mAnimations[name]), mBmlProcessor, character.getName(), SmartBodyIntuitiveAnimation::Blend::STARTING_RIGHT);
Expand All @@ -169,8 +169,8 @@ void SmartBodyAnimationManager::addGestureAnimation(SmartBodyAnimation::Name nam

void SmartBodyAnimationManager::updateAnimations(SmartBodyRepresentation& character, float timeSlice)
{
switch (SmartBodyAnimation::getType(character.getPosture()->getReference().getName()))
{
switch (SmartBodyAnimation::getType(character.getPosture()->getReference().getName())) {

case SmartBodyAnimation::Type::MOVING:
updateMovingAnimation(character);
break;
Expand All @@ -195,8 +195,8 @@ void SmartBodyAnimationManager::updateMovingAnimation(SmartBodyRepresentation& c
{
SmartBodyMovingAnimationInstance& animation = dynamic_cast<SmartBodyMovingAnimationInstance&>(*character.getPosture());

if (animation.hasDirectionChanged())
{
if (animation.hasDirectionChanged()) {

//Send the update to SmartBody.
animation.execute(character.getName());
}
Expand All @@ -218,12 +218,13 @@ void SmartBodyAnimationManager::updateStaticAnimation(SmartBodyRepresentation& c
SmartBodyStaticAnimationInstance& animation = dynamic_cast<SmartBodyStaticAnimationInstance&>(*character.getPosture());
animation.updateTimers(timeSlice);

if (animation.getTimeSincePostureChange() > MIN_TIME_BEFORE_STATIC_POSE_CHANGE)
{
if (animation.getTimeSincePostureChange() > MIN_TIME_BEFORE_STATIC_POSE_CHANGE) {

//Pick up a number between 0 and RANDOM_STATIC_POSE_CHANGE, if it equals 0, then, we change the posture.
std::uniform_int_distribution<int> range(0, RANDOM_STATIC_POSE_CHANGE - 1);

if (range(mRandGen) == 0)
{
if (range(mRandGen) == 0) {

//Change the posture.
std::uniform_int_distribution<int> postureRange(0, animation.getMotionNumber() - 1);
animation.changePosture(postureRange(mRandGen));
Expand All @@ -233,12 +234,13 @@ void SmartBodyAnimationManager::updateStaticAnimation(SmartBodyRepresentation& c
}
}

if (animation.getTimeSinceGestureEnd() > MIN_TIME_BEFORE_NEW_GESTURE_PLAYED)
{
if (animation.getTimeSinceGestureEnd() > MIN_TIME_BEFORE_NEW_GESTURE_PLAYED) {

//Pick up a number between 0 and MIN_TIME_BEFORE_NEW_GESTURE_PLAYED, if it equals 0, then, we launch a new gesture.
std::uniform_int_distribution<int> range(0, RANDOM_STATIC_GESTURE_PLAY - 1);

if (range(mRandGen) == 0)
{
if (range(mRandGen) == 0) {
//Play a new gesture.
std::uniform_int_distribution<int> gestRange(0, animation.getGestureMotionNumber() - 1);
animation.playGesture(gestRange(mRandGen));
Expand Down
Loading

0 comments on commit 134854f

Please sign in to comment.