From a236513381c7a370d23205c2094858883e510336 Mon Sep 17 00:00:00 2001 From: Jeongseok Lee Date: Fri, 13 Mar 2015 14:50:22 -0400 Subject: [PATCH] Add some signals to Entity --- dart/dynamics/Entity.cpp | 87 ++++++++++++++++++++++++++++++++-------- dart/dynamics/Entity.h | 64 ++++++++++++++++++++++++++++- dart/dynamics/Frame.cpp | 9 +++++ dart/dynamics/Frame.h | 15 +++---- 4 files changed, 150 insertions(+), 25 deletions(-) diff --git a/dart/dynamics/Entity.cpp b/dart/dynamics/Entity.cpp index 7c063485f279f..fd2a37f8462f0 100644 --- a/dart/dynamics/Entity.cpp +++ b/dart/dynamics/Entity.cpp @@ -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; } @@ -80,6 +86,8 @@ const std::string& Entity::getName() const void Entity::addVisualizationShape(Shape* _p) { mVizShapes.push_back(_p); + + mVizShapeAddedSignal.raise(this, _p); } //============================================================================== @@ -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); } //============================================================================== @@ -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); } //============================================================================== @@ -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); } //============================================================================== @@ -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); } //============================================================================== diff --git a/dart/dynamics/Entity.h b/dart/dynamics/Entity.h index c9d6a5b9e5c2a..e7e9a093a0dcb 100644 --- a/dart/dynamics/Entity.h +++ b/dart/dynamics/Entity.h @@ -41,6 +41,7 @@ #include #include +#include "dart/common/Signal.h" #include "dart/dynamics/Shape.h" namespace dart { @@ -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; + using FrameChangedSignal + = common::Signal; + using NameChangedSignal + = common::Signal; + using VizShapeAddedSignal + = common::Signal; + + 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); @@ -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; @@ -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); diff --git a/dart/dynamics/Frame.cpp b/dart/dynamics/Frame.cpp index da223d1ccd0db..ae48d4dfbc7ee 100644 --- a/dart/dynamics/Frame.cpp +++ b/dart/dynamics/Frame.cpp @@ -474,6 +474,8 @@ void Frame::notifyTransformUpdate() for(Entity* entity : mChildEntities) entity->notifyTransformUpdate(); + + mTransformUpdatedSignal.raise(this); } //============================================================================== @@ -489,6 +491,8 @@ void Frame::notifyVelocityUpdate() for(Entity* entity : mChildEntities) entity->notifyVelocityUpdate(); + + mVelocityChangedSignal.raise(this); } //============================================================================== @@ -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)) diff --git a/dart/dynamics/Frame.h b/dart/dynamics/Frame.h index 4d90924836887..82d32c88f0b97 100644 --- a/dart/dynamics/Frame.h +++ b/dart/dynamics/Frame.h @@ -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