Skip to content

Commit 4f3d6dc

Browse files
committed
ScriptBuilder: Add addEntityInput() method
1 parent c8dd2f3 commit 4f3d6dc

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

include/scratchcpp/dev/test/scriptbuilder.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44

55
#include <vector>
66

7-
#include "../../global.h"
8-
#include "../../spimpl.h"
7+
#include "../../inputvalue.h"
98

109
namespace libscratchcpp
1110
{
1211

1312
class IExtension;
1413
class IEngine;
1514
class Target;
16-
class Entity;
1715
class List;
1816

1917
} // namespace libscratchcpp
@@ -45,6 +43,7 @@ class LIBSCRATCHCPP_EXPORT ScriptBuilder
4543
void addDropdownInput(const std::string &name, const std::string &selectedValue);
4644
void addDropdownField(const std::string &name, const std::string &selectedValue);
4745

46+
void addEntityInput(const std::string &name, const std::string &entityName, InputValue::Type entityType, std::shared_ptr<Entity> entity);
4847
void addEntityField(const std::string &name, std::shared_ptr<Entity> entity);
4948

5049
std::shared_ptr<Block> currentBlock();

src/dev/test/scriptbuilder.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <scratchcpp/dev/test/scriptbuilder.h>
44
#include <scratchcpp/block.h>
55
#include <scratchcpp/input.h>
6+
#include <scratchcpp/inputvalue.h>
67
#include <scratchcpp/field.h>
78
#include <scratchcpp/dev/compilerconstant.h>
89
#include <scratchcpp/dev/executablecode.h>
@@ -152,6 +153,20 @@ void ScriptBuilder::addDropdownField(const std::string &name, const std::string
152153
impl->lastBlock->addField(field);
153154
}
154155

156+
/*! Adds an input pointing to an entity (variable, list, broadcast, etc.) */
157+
void ScriptBuilder::addEntityInput(const std::string &name, const std::string &entityName, InputValue::Type entityType, std::shared_ptr<Entity> entity)
158+
{
159+
if (!impl->lastBlock)
160+
return;
161+
162+
entity->setId(std::to_string(impl->blockId++));
163+
auto input = std::make_shared<Input>(name, Input::Type::Shadow);
164+
input->setPrimaryValue(entityName);
165+
input->primaryValue()->setValuePtr(entity);
166+
input->primaryValue()->setType(entityType);
167+
impl->lastBlock->addInput(input);
168+
}
169+
155170
/*! Adds a field pointing to an entity (variable, list, broadcast, etc.) */
156171
void ScriptBuilder::addEntityField(const std::string &name, std::shared_ptr<Entity> entity)
157172
{

test/dev/test_api/scriptbuilder_test.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,35 @@ TEST_F(ScriptBuilderTest, AddDropdownField)
162162
ASSERT_EQ(testing::internal::GetCapturedStdout(), "hello\n");
163163
}
164164

165+
TEST_F(ScriptBuilderTest, AddEntityInput)
166+
{
167+
auto broadcast = std::make_shared<Broadcast>("", "");
168+
m_engine->setBroadcasts({ broadcast });
169+
170+
m_builder->addBlock("test_simple");
171+
m_builder->addEntityInput("BROADCAST", "test", InputValue::Type::Broadcast, broadcast);
172+
auto block = m_builder->currentBlock();
173+
ASSERT_TRUE(block);
174+
ASSERT_EQ(block->opcode(), "test_simple");
175+
ASSERT_EQ(block->inputs().size(), 1);
176+
ASSERT_EQ(block->inputAt(0)->name(), "BROADCAST");
177+
ASSERT_EQ(block->inputAt(0)->primaryValue()->valuePtr(), broadcast);
178+
ASSERT_EQ(block->inputAt(0)->primaryValue()->type(), InputValue::Type::Broadcast);
179+
}
180+
165181
TEST_F(ScriptBuilderTest, AddEntityField)
166182
{
167183
auto broadcast = std::make_shared<Broadcast>("", "");
168184
m_engine->setBroadcasts({ broadcast });
169185

170-
m_builder->addBlock("test_print_field");
171-
m_builder->addEntityField("STRING", broadcast);
186+
m_builder->addBlock("test_simple");
187+
m_builder->addEntityField("BROADCAST", broadcast);
172188
auto block = m_builder->currentBlock();
173189
ASSERT_TRUE(block);
174-
ASSERT_EQ(block->opcode(), "test_print_field");
190+
ASSERT_EQ(block->opcode(), "test_simple");
175191
ASSERT_TRUE(block->inputs().empty());
176192
ASSERT_EQ(block->fields().size(), 1);
177-
ASSERT_EQ(block->fieldAt(0)->name(), "STRING");
193+
ASSERT_EQ(block->fieldAt(0)->name(), "BROADCAST");
178194
ASSERT_EQ(block->fieldAt(0)->valuePtr(), broadcast);
179195
}
180196

0 commit comments

Comments
 (0)