ed
custom_properties_plugin.cpp
Go to the documentation of this file.
2 
3 #include <ed/world_model.h>
4 #include <ed/update_request.h>
5 #include <ed/entity.h>
6 
7 // Property info
8 #include "pose_info.h"
9 #include "counter_info.h"
10 
11 // ----------------------------------------------------------------------------------------------------
12 
14 {
15 }
16 
17 // ----------------------------------------------------------------------------------------------------
18 
20 {
21 }
22 
23 // ----------------------------------------------------------------------------------------------------
24 
26 {
27  // Register the entity property keys you want to use. The first argument is the name of the
28  // property. This name is used to specify properties accross plugins. The second is the property
29  // key, which will be initialized when calling this function
30 
31  init.properties.registerProperty("pose", k_pose_, new PoseInfo);
32  init.properties.registerProperty("counter", k_counter_, new CounterInfo);
33 }
34 
35 // ----------------------------------------------------------------------------------------------------
36 
38 {
39  // Find the entity with id 'test-entity'
40  ed::EntityConstPtr e = world.getEntity("test-entity");
41 
42  if (!e)
43  {
44  // Entity does not yet exist, so add it by setting the pose property. Note that
45  // setting a property will automatically add the entity to the world model
46 
47  // To set a property you need to pass the id of the entity, the key of the property
48  // you want to change, and the value of the property. In our case, we want to
49  // initialize the 'pose' of the entity to the identity pose ((0, 0, 0) and zero rotation).
50  req.setProperty("test-entity", k_pose_, geo::Pose3D::identity());
51  }
52  else
53  {
54  // Entity exists. Try to get his pose. Again, use the appropriate key to access the property
55  const geo::Pose3D* pose = e->property(k_pose_);
56 
57  // The Entity::property function returns a const pointer. If this pointer is 0, the
58  // property was not found OR the type did not match. Therefore, always check if the pointer is not null!
59  if (pose)
60  {
61  std::cout << "Entity " << e->id() << " has pose " << *pose << std::endl;
62 
63  // Add a little offset to the current pose ...
64  geo::Pose3D new_pose = *pose;
65  new_pose.t += geo::Vector3(0.1, 0.2, 0.3);
66 
67  // .. and update it
68  req.setProperty(e->id(), k_pose_, new_pose);
69  }
70 
71  // Now try to get the counter property
72  const int* counter = e->property(k_counter_);
73  if (!counter)
74  {
75  // The first time the plugin tries to access the counter property, the property is not yet there.
76  std::cout << "Counter property does not yet exist. Will be created and initialized to 0" << std::endl;
77  req.setProperty(e->id(), k_counter_, 0);
78  }
79  else
80  {
81  // The second time the counter property will be set, so we can access it and update
82  std::cout << "Entity " << e->id() << " has counter " << *counter << std::endl;
83  req.setProperty(e->id(), k_counter_, *counter + 1);
84  }
85 
87  }
88 }
89 
geo::Vector3
Vec3 Vector3
ed::WorldModel::getEntity
EntityConstPtr getEntity(const ed::UUID &id) const
Definition: world_model.h:76
PoseInfo
Definition: examples/custom_properties/pose_info.h:6
ed::WorldModel
Definition: world_model.h:21
ed::UpdateRequest
Definition: update_request.h:24
custom_properties_plugin.h
entity.h
ED_REGISTER_PLUGIN
#define ED_REGISTER_PLUGIN(Derived)
Definition: plugin.h:5
geo::Transform3T::identity
static Transform3T identity()
CounterInfo
Definition: counter_info.h:6
geo::Transform3T
ed::InitData
Definition: init_data.h:12
CustomProperties::process
void process(const ed::WorldModel &world, ed::UpdateRequest &req)
Definition: custom_properties_plugin.cpp:37
ed::EntityConstPtr
shared_ptr< const Entity > EntityConstPtr
Definition: types.h:37
std::cout
counter_info.h
CustomProperties::k_pose_
ed::PropertyKey< geo::Pose3D > k_pose_
Definition: custom_properties_plugin.h:28
update_request.h
geo::Transform3T::t
Vec3T< T > t
pose_info.h
CustomProperties::k_counter_
ed::PropertyKey< int > k_counter_
Definition: custom_properties_plugin.h:31
CustomProperties::CustomProperties
CustomProperties()
Definition: custom_properties_plugin.cpp:13
std::endl
T endl(T... args)
ed::PropertyKeyDB::registerProperty
void registerProperty(const std::string &name, PropertyKey< T > &key, PropertyInfo *info=0)
Definition: property_key_db.h:43
ed::UpdateRequest::setProperty
void setProperty(const UUID &id, const PropertyKey< T > &key, const T &value)
Definition: update_request.h:170
ed::InitData::properties
ed::PropertyKeyDB & properties
Definition: init_data.h:17
ed::Plugin::initialize
virtual void initialize()
Definition: plugin.h:39
CustomProperties::~CustomProperties
virtual ~CustomProperties()
Definition: custom_properties_plugin.cpp:19
world_model.h
CustomProperties
Definition: custom_properties_plugin.h:8