Skip to content

Commit

Permalink
Add some signals to Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
jslee02 committed Mar 13, 2015
1 parent 85f534c commit a236513
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 25 deletions.
87 changes: 71 additions & 16 deletions dart/dynamics/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ Entity::~Entity()
}

//==============================================================================
const std::string& Entity::setName(const std::string &_name)
const std::string& Entity::setName(const std::string& _name)
{
if (mName == _name)
return mName;

const std::string oldName = mName;
mName = _name;
mNameChangedSignal.raise(this, oldName, mName);

return mName;
}

Expand All @@ -80,6 +86,8 @@ const std::string& Entity::getName() const
void Entity::addVisualizationShape(Shape* _p)
{
mVizShapes.push_back(_p);

mVizShapeAddedSignal.raise(this, _p);
}

//==============================================================================
Expand Down Expand Up @@ -153,6 +161,10 @@ bool Entity::isQuiet() const
void Entity::notifyTransformUpdate()
{
mNeedTransformUpdate = true;

// The actual transform hasn't updated yet. But when its getter is called,
// the transformation will be updated automatically.
mTransformUpdatedSignal.raise(this);
}

//==============================================================================
Expand All @@ -165,6 +177,10 @@ bool Entity::needsTransformUpdate() const
void Entity::notifyVelocityUpdate()
{
mNeedVelocityUpdate = true;

// The actual velocity hasn't updated yet. But when its getter is called,
// the velocity will be updated automatically.
mVelocityChangedSignal.raise(this);
}

//==============================================================================
Expand All @@ -177,6 +193,10 @@ bool Entity::needsVelocityUpdate() const
void Entity::notifyAccelerationUpdate()
{
mNeedAccelerationUpdate = true;

// The actual acceleration hasn't updated yet. But when its getter is called,
// the acceleration will be updated automatically.
mAccelerationChangedSignal.raise(this);
}

//==============================================================================
Expand All @@ -185,35 +205,70 @@ bool Entity::needsAccelerationUpdate() const
return mNeedAccelerationUpdate;
}

//==============================================================================
common::Connection Entity::connectFrameChanged(const FrameChangedSlot& _slot)
{
return mFrameChangedSignal.connect(_slot);
}

//==============================================================================
common::Connection Entity::connectNameChanged(const NameChangedSlot& _slot)
{
return mNameChangedSignal.connect(_slot);
}

//==============================================================================
common::Connection Entity::connectVizShapeAdded(const VizShapeAddedSlot& _slot)
{
return mVizShapeAddedSignal.connect(_slot);
}

//==============================================================================
common::Connection Entity::connectTransformChanged(const EntitySlot& _slot)
{
return mTransformUpdatedSignal.connect(_slot);
}

//==============================================================================
common::Connection Entity::connectVelocityChanged(const EntitySlot& _slot)
{
return mVelocityChangedSignal.connect(_slot);
}

//==============================================================================
common::Connection Entity::connectAccelerationChanged(const EntitySlot& _slot)
{
return mAccelerationChangedSignal.connect(_slot);
}

//==============================================================================
void Entity::changeParentFrame(Frame* _newParentFrame)
{
if(!mAmQuiet)
if (mParentFrame == _newParentFrame)
return;

const Frame* oldParentFrame = mParentFrame;

if (!mAmQuiet && nullptr != mParentFrame)
{
if(mParentFrame)
EntityPtrSet::iterator it = mParentFrame->mChildEntities.find(this);
if (it != mParentFrame->mChildEntities.end())
{
EntityPtrSet::iterator it = mParentFrame->mChildEntities.find(this);
if(it != mParentFrame->mChildEntities.end())
{
mParentFrame->mChildEntities.erase(it);
mParentFrame->processRemovedEntity(this);
}
mParentFrame->mChildEntities.erase(it);
mParentFrame->processRemovedEntity(this);
}
}

if(NULL == _newParentFrame)
{
mParentFrame = NULL;
return;
}

mParentFrame =_newParentFrame;
if(!mAmQuiet)

if (!mAmQuiet && nullptr != mParentFrame)
{
mParentFrame->mChildEntities.insert(this);
mParentFrame->processNewEntity(this);
notifyTransformUpdate();
}

mFrameChangedSignal.raise(this, oldParentFrame, mParentFrame);
}

//==============================================================================
Expand Down
64 changes: 62 additions & 2 deletions dart/dynamics/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <string>
#include <vector>

#include "dart/common/Signal.h"
#include "dart/dynamics/Shape.h"

namespace dart {
Expand Down Expand Up @@ -128,6 +129,48 @@ class Entity
/// Returns true iff an acceleration update is needed for this Entity
bool needsAccelerationUpdate() const;

//----------------------------------------------------------------------------
/// \{ \name Signals
//----------------------------------------------------------------------------

using EntitySignal = common::Signal<void(const Entity*)>;
using FrameChangedSignal
= common::Signal<void(const Entity*,
const Frame* _oldFrame,
const Frame* _newFrame)>;
using NameChangedSignal
= common::Signal<void(const Entity*,
const std::string& _oldName,
const std::string& _newFrame)>;
using VizShapeAddedSignal
= common::Signal<void(const Entity*,
const Shape* _newVisShape)>;

using EntitySlot = EntitySignal::SlotType;
using FrameChangedSlot = FrameChangedSignal::SlotType;
using NameChangedSlot = NameChangedSignal::SlotType;
using VizShapeAddedSlot = VizShapeAddedSignal::SlotType;

/// Connect slot to frame changed signal
common::Connection connectFrameChanged(const FrameChangedSlot& _slot);

/// Connect slot to name changed signal
common::Connection connectNameChanged(const NameChangedSlot& _slot);

/// Connect slot to visualization changed signal
common::Connection connectVizShapeAdded(const VizShapeAddedSlot& _slot);

/// Connect slot to transform changed signal
common::Connection connectTransformChanged(const EntitySlot& _slot);

/// Connect slot to velocity changed signal
common::Connection connectVelocityChanged(const EntitySlot& _slot);

/// Connect slot to acceleration changed signal
common::Connection connectAccelerationChanged(const EntitySlot& _slot);

/// \}

protected:
/// Used by derived classes to change their parent frames
virtual void changeParentFrame(Frame* _newParentFrame);
Expand All @@ -151,6 +194,24 @@ class Entity
/// Does this Entity need an Acceleration update
mutable bool mNeedAccelerationUpdate;

/// Frame changed signal
FrameChangedSignal mFrameChangedSignal;

/// Name changed signal
NameChangedSignal mNameChangedSignal;

/// Visualization added signal
VizShapeAddedSignal mVizShapeAddedSignal;

/// Transform changed signal
EntitySignal mTransformUpdatedSignal;

/// Velocity changed signal
EntitySignal mVelocityChangedSignal;

/// Acceleration changed signal
EntitySignal mAccelerationChangedSignal;

private:
/// Whether or not this Entity is set to be quiet
const bool mAmQuiet;
Expand All @@ -163,8 +224,7 @@ class Detachable : public virtual Entity
{
public:
/// Constructor
explicit Detachable(Frame* _refFrame, const std::string& _name,
bool _quiet);
explicit Detachable(Frame* _refFrame, const std::string& _name, bool _quiet);

/// Allows the user to change the parent Frame of this Entity
virtual void setParentFrame(Frame* _newParentFrame);
Expand Down
9 changes: 9 additions & 0 deletions dart/dynamics/Frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ void Frame::notifyTransformUpdate()

for(Entity* entity : mChildEntities)
entity->notifyTransformUpdate();

mTransformUpdatedSignal.raise(this);
}

//==============================================================================
Expand All @@ -489,6 +491,8 @@ void Frame::notifyVelocityUpdate()

for(Entity* entity : mChildEntities)
entity->notifyVelocityUpdate();

mVelocityChangedSignal.raise(this);
}

//==============================================================================
Expand All @@ -502,11 +506,16 @@ void Frame::notifyAccelerationUpdate()

for(Entity* entity : mChildEntities)
entity->notifyAccelerationUpdate();

mAccelerationChangedSignal.raise(this);
}

//==============================================================================
void Frame::changeParentFrame(Frame* _newParentFrame)
{
if (mParentFrame == _newParentFrame)
return;

if(_newParentFrame)
{
if(_newParentFrame->descendsFrom(this))
Expand Down
15 changes: 8 additions & 7 deletions dart/dynamics/Frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,22 +232,23 @@ class Frame : public virtual Entity
//--------------------------------------------------------------------------

// Render this Frame as well as any Entities it contains
virtual void draw(renderer::RenderInterface *_ri = NULL,
const Eigen::Vector4d &_color = Eigen::Vector4d::Ones(),
bool _useDefaultColor = true, int _depth = 0) const;
virtual void draw(
renderer::RenderInterface *_ri = NULL,
const Eigen::Vector4d &_color = Eigen::Vector4d::Ones(),
bool _useDefaultColor = true, int _depth = 0) const override;

/// Notify this Frame and all its children that its pose has changed
virtual void notifyTransformUpdate();
virtual void notifyTransformUpdate() override;

/// Notify this Frame and all its children that its velocity has changed
virtual void notifyVelocityUpdate();
virtual void notifyVelocityUpdate() override;

/// Notify this Frame and all its children that its acceleration has changed
virtual void notifyAccelerationUpdate();
virtual void notifyAccelerationUpdate() override;

protected:
// Documentation inherited
virtual void changeParentFrame(Frame* _newParentFrame);
virtual void changeParentFrame(Frame* _newParentFrame) override;

/// Called during a parent Frame change to allow extensions of the Frame class
/// to handle new children in customized ways. This function is a no op unless
Expand Down

0 comments on commit a236513

Please sign in to comment.