Skip to content

Commit

Permalink
Allow to create light with the create service (#513)
Browse files Browse the repository at this point in the history
Signed-off-by: ahcorde <[email protected]>

Co-authored-by: Ian Chen <[email protected]>
Signed-off-by: Ian Chen <[email protected]>

Signed-off-by: Louise Poubel <[email protected]>
Co-authored-by: Louise Poubel <[email protected]>
  • Loading branch information
ahcorde and chapulina authored Jan 20, 2021
1 parent bccea90 commit 4dbf229
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
8 changes: 8 additions & 0 deletions examples/worlds/empty.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ ign service -s /world/empty/remove \
--timeout 300 \
--req 'name: "spawned_light" type: LIGHT'
Insert a light using a message and allow_renaming:
ign service -s /world/empty/create \
--reqtype ignition.msgs.EntityFactory \
--reptype ignition.msgs.Boolean \
--timeout 300 \
--req 'allow_renaming: true, light: {name: "spawned_light", type: 2, diffuse: {r: 1}}'
-->
<sdf version="1.6">
<world name="empty">
Expand Down
27 changes: 22 additions & 5 deletions src/systems/user_commands/UserCommands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ bool CreateCommand::Execute()

// Load SDF
sdf::Root root;
sdf::Light lightSdf;
sdf::Errors errors;
switch (createMsg->from_case())
{
Expand All @@ -424,9 +425,8 @@ bool CreateCommand::Execute()
}
case msgs::EntityFactory::kLight:
{
// TODO(louise) Support light msg
ignerr << "light field not yet supported." << std::endl;
return false;
lightSdf = convert<sdf::Light>(createMsg->light());
break;
}
case msgs::EntityFactory::kCloneName:
{
Expand All @@ -451,18 +451,26 @@ bool CreateCommand::Execute()
bool isModel{false};
bool isLight{false};
bool isActor{false};
bool isRoot{false};
if (root.ModelCount() > 0)
{
isRoot = true;
isModel = true;
}
else if (root.LightCount() > 0)
{
isRoot = true;
isLight = true;
}
else if (root.ActorCount() > 0)
{
isRoot = true;
isActor = true;
}
else if (!lightSdf.Name().empty())
{
isLight = true;
}
else
{
ignerr << "Expected exactly one top-level <model>, <light> or <actor> on"
Expand All @@ -486,10 +494,14 @@ bool CreateCommand::Execute()
{
desiredName = root.ModelByIndex(0)->Name();
}
else if (isLight)
else if (isLight && isRoot)
{
desiredName = root.LightByIndex(0)->Name();
}
else if (isLight)
{
desiredName = lightSdf.Name();
}
else if (isActor)
{
desiredName = root.ActorByIndex(0)->Name();
Expand Down Expand Up @@ -528,12 +540,17 @@ bool CreateCommand::Execute()
model.SetName(desiredName);
entity = this->iface->creator->CreateEntities(&model);
}
else if (isLight)
else if (isLight && isRoot)
{
auto light = root.LightByIndex(0);
light->SetName(desiredName);
entity = this->iface->creator->CreateEntities(light);
}
else if (isLight)
{
lightSdf.SetName(desiredName);
entity = this->iface->creator->CreateEntities(&lightSdf);
}
else if (isActor)
{
auto actor = *root.ActorByIndex(0);
Expand Down
19 changes: 19 additions & 0 deletions test/integration/user_commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,25 @@ TEST_F(UserCommandsTest, Create)

EXPECT_NE(nullptr, ecm->Component<components::Light>(light));

// Request entity spawn
req.Clear();
req.mutable_light()->set_name("light_test");
req.mutable_light()->set_parent_id(1);
EXPECT_TRUE(node.Request(service, req, timeout, res, result));
EXPECT_TRUE(result);
EXPECT_TRUE(res.data());

// Run an iteration and check it was created
server.Run(true, 1, false);

EXPECT_EQ(entityCount + 1, ecm->EntityCount());
entityCount = ecm->EntityCount();

light = ecm->EntityByComponents(components::Name("light_test"));
EXPECT_NE(kNullEntity, light);

EXPECT_NE(nullptr, ecm->Component<components::Light>(light));

// Queue commands and check they're all executed in the same iteration
req.Clear();
req.set_sdf(modelStr);
Expand Down

0 comments on commit 4dbf229

Please sign in to comment.