Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions include/scratchcpp/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class BlockPrivate;
class LIBSCRATCHCPP_EXPORT Block : public Entity
{
public:
friend class Engine;

Block(const std::string &id, const std::string &opcode);
Block(const Block &) = delete;

Expand All @@ -41,14 +43,12 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
std::shared_ptr<Input> inputAt(int index) const;
int findInput(const std::string &inputName) const;
Input *findInputById(int id) const;
void updateInputMap();

std::vector<std::shared_ptr<Field>> fields() const;
int addField(std::shared_ptr<Field> field);
std::shared_ptr<Field> fieldAt(int index) const;
int findField(const std::string &fieldName) const;
Field *findFieldById(int id) const;
void updateFieldMap();

bool shadow() const;
void setShadow(bool newShadow);
Expand All @@ -75,6 +75,9 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
InputValue *topLevelReporterInfo();

private:
void updateInputMap();
void updateFieldMap();

spimpl::unique_impl_ptr<BlockPrivate> impl;
};

Expand Down
18 changes: 16 additions & 2 deletions src/scratch/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ int Block::addInput(std::shared_ptr<Input> input)
return it - impl->inputs.begin();

impl->inputs.push_back(input);
impl->inputMap[input->inputId()] = input.get();

return impl->inputs.size() - 1;
}

Expand Down Expand Up @@ -197,10 +199,15 @@ Input *Block::findInputById(int id) const
{
if (impl->inputMap.count(id) == 1)
return impl->inputMap.at(id);
else {
auto it = std::find_if(impl->inputs.begin(), impl->inputs.end(), [id](std::shared_ptr<Input> input) { return input->inputId() == id; });

if (it != impl->inputs.end())
return it->get();
}
return nullptr;
}

/*! Updates the map that assigns input IDs to input indexes. Used internally by Engine. */
void Block::updateInputMap()
{
impl->inputMap.clear();
Expand All @@ -223,6 +230,8 @@ int Block::addField(std::shared_ptr<Field> field)
return it - impl->fields.begin();

impl->fields.push_back(field);
impl->fieldMap[field->fieldId()] = field.get();

return impl->fields.size() - 1;
}

Expand Down Expand Up @@ -252,10 +261,15 @@ Field *Block::findFieldById(int id) const
{
if (impl->fieldMap.count(id) == 1)
return impl->fieldMap.at(id);
else {
auto it = std::find_if(impl->fields.begin(), impl->fields.end(), [id](std::shared_ptr<Field> field) { return field->fieldId() == id; });

if (it != impl->fields.end())
return it->get();
}
return nullptr;
}

/*! Updates the map that assigns input IDs to input indexes. Used internally by Engine. */
void Block::updateFieldMap()
{
impl->fieldMap.clear();
Expand Down
6 changes: 0 additions & 6 deletions test/blocks/control_blocks_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class ControlBlocksTest : public testing::Test
input->setValueBlock(valueBlock);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();
}

void addObscuredInput(std::shared_ptr<Block> block, const std::string &name, ControlBlocks::Inputs id, std::shared_ptr<Block> valueBlock) const
Expand All @@ -72,7 +71,6 @@ class ControlBlocksTest : public testing::Test
input->setValueBlock(valueBlock);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();
}

void addValueInput(std::shared_ptr<Block> block, const std::string &name, ControlBlocks::Inputs id, const Value &value) const
Expand All @@ -81,15 +79,13 @@ class ControlBlocksTest : public testing::Test
input->setPrimaryValue(value);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();
}

std::shared_ptr<Input> addNullInput(std::shared_ptr<Block> block, const std::string &name, ControlBlocks::Inputs id) const
{
auto input = std::make_shared<Input>(name, Input::Type::Shadow);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();

return input;
}
Expand All @@ -100,15 +96,13 @@ class ControlBlocksTest : public testing::Test
field->setFieldId(id);
field->setSpecialValueId(valueId);
block->addField(field);
block->updateFieldMap();
}

void addVariableField(std::shared_ptr<Block> block, std::shared_ptr<Variable> variable)
{
auto variableField = std::make_shared<Field>("VARIABLE", Value(), variable);
variableField->setFieldId(ControlBlocks::VARIABLE);
block->addField(variableField);
block->updateFieldMap();
}

std::shared_ptr<Block> createSubstack(const std::string &id)
Expand Down
4 changes: 0 additions & 4 deletions test/blocks/custom_blocks_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class CustomBlocksTest : public testing::Test
input->setValueBlock(prototypeBlock);
input->setInputId(CustomBlocks::CUSTOM_BLOCK);
definitionBlock->addInput(input);
definitionBlock->updateInputMap();
}

void addArgumentInput(std::shared_ptr<Block> block, const std::string &argId, const Value &value) const
Expand Down Expand Up @@ -94,7 +93,6 @@ TEST_F(CustomBlocksTest, CustomBlocks)
auto valueField = std::make_shared<Field>("VALUE", "boolean");
valueField->setFieldId(CustomBlocks::VALUE);
argBlock->addField(valueField);
argBlock->updateFieldMap();
input->setValueBlock(argBlock);
input->setInputId(-100);
testBlock->addInput(input);
Expand All @@ -105,12 +103,10 @@ TEST_F(CustomBlocksTest, CustomBlocks)
valueField = std::make_shared<Field>("VALUE", "invalid");
valueField->setFieldId(CustomBlocks::VALUE);
argBlock->addField(valueField);
argBlock->updateFieldMap();
input->setValueBlock(argBlock);
input->setInputId(-101);
testBlock->addInput(input);

testBlock->updateInputMap();
testBlock->setCompileFunction([](Compiler *compiler) {
compiler->addInput(-100);
compiler->addInstruction(vm::OP_PRINT);
Expand Down
4 changes: 0 additions & 4 deletions test/blocks/event_blocks_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class EventBlocksTest : public testing::Test
input->primaryValue()->setType(InputValue::Type::Broadcast);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();
}

void addObscuredInput(std::shared_ptr<Block> block, const std::string &name, EventBlocks::Inputs id, std::shared_ptr<Block> valueBlock) const
Expand All @@ -49,23 +48,20 @@ class EventBlocksTest : public testing::Test
input->setValueBlock(valueBlock);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();
}

void addValueField(std::shared_ptr<Block> block, const std::string &name, EventBlocks::Fields id, const std::string &value) const
{
auto field = std::make_shared<Field>(name, value);
field->setFieldId(id);
block->addField(field);
block->updateFieldMap();
}

void addBroadcastField(std::shared_ptr<Block> block, const std::string &name, EventBlocks::Fields id, std::shared_ptr<Broadcast> broadcast) const
{
auto field = std::make_shared<Field>(name, Value(), broadcast);
field->setFieldId(id);
block->addField(field);
block->updateFieldMap();
}

std::unique_ptr<IBlockSection> m_section;
Expand Down
3 changes: 0 additions & 3 deletions test/blocks/list_blocks_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class ListBlocksTest : public testing::Test
auto listField = std::make_shared<Field>("LIST", Value(), list);
listField->setFieldId(ListBlocks::LIST);
block->addField(listField);
block->updateFieldMap();

return block;
}
Expand All @@ -39,7 +38,6 @@ class ListBlocksTest : public testing::Test
input->setPrimaryValue(item);
input->setInputId(ListBlocks::ITEM);
block->addInput(input);
block->updateInputMap();
}

void addIndexInput(std::shared_ptr<Block> block, const Value &index) const
Expand All @@ -48,7 +46,6 @@ class ListBlocksTest : public testing::Test
input->setPrimaryValue(index);
input->setInputId(ListBlocks::INDEX);
block->addInput(input);
block->updateInputMap();
}

// For add item, item index and list contains item
Expand Down
4 changes: 0 additions & 4 deletions test/blocks/looks_blocks_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class LooksBlocksTest : public testing::Test
input->setPrimaryValue(value);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();
}

void addObscuredInput(std::shared_ptr<Block> block, const std::string &name, LooksBlocks::Inputs id, std::shared_ptr<Block> valueBlock) const
Expand All @@ -54,15 +53,13 @@ class LooksBlocksTest : public testing::Test
input->setValueBlock(valueBlock);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();
}

std::shared_ptr<Input> addNullInput(std::shared_ptr<Block> block, const std::string &name, LooksBlocks::Inputs id) const
{
auto input = std::make_shared<Input>(name, Input::Type::Shadow);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();

return input;
}
Expand All @@ -85,7 +82,6 @@ class LooksBlocksTest : public testing::Test
field->setFieldId(id);
field->setSpecialValueId(valueId);
block->addField(field);
block->updateFieldMap();
}

std::unique_ptr<IBlockSection> m_section;
Expand Down
4 changes: 0 additions & 4 deletions test/blocks/motion_blocks_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class MotionBlocksTest : public testing::Test
input->setPrimaryValue(value);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();
}

void addObscuredInput(std::shared_ptr<Block> block, const std::string &name, MotionBlocks::Inputs id, std::shared_ptr<Block> valueBlock) const
Expand All @@ -54,15 +53,13 @@ class MotionBlocksTest : public testing::Test
input->setValueBlock(valueBlock);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();
}

std::shared_ptr<Input> addNullInput(std::shared_ptr<Block> block, const std::string &name, MotionBlocks::Inputs id) const
{
auto input = std::make_shared<Input>(name, Input::Type::Shadow);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();

return input;
}
Expand All @@ -85,7 +82,6 @@ class MotionBlocksTest : public testing::Test
field->setFieldId(id);
field->setSpecialValueId(valueId);
block->addField(field);
block->updateFieldMap();
}

std::unique_ptr<IBlockSection> m_section;
Expand Down
4 changes: 0 additions & 4 deletions test/blocks/operator_blocks_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class OperatorBlocksTest : public testing::Test
input->setPrimaryValue(value);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();
}

void addObscuredInput(std::shared_ptr<Block> block, const std::string &name, OperatorBlocks::Inputs id, std::shared_ptr<Block> valueBlock) const
Expand All @@ -39,15 +38,13 @@ class OperatorBlocksTest : public testing::Test
input->setValueBlock(valueBlock);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();
}

void addNullInput(std::shared_ptr<Block> block, const std::string &name, OperatorBlocks::Inputs id) const
{
auto input = std::make_shared<Input>(name, Input::Type::Shadow);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();
}

// For the mathop block
Expand All @@ -57,7 +54,6 @@ class OperatorBlocksTest : public testing::Test
field->setFieldId(OperatorBlocks::OPERATOR);
field->setSpecialValueId(operatorId);
block->addField(field);
block->updateFieldMap();
}

std::unique_ptr<IBlockSection> m_section;
Expand Down
4 changes: 0 additions & 4 deletions test/blocks/sensing_blocks_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class SensingBlocksTest : public testing::Test
optionField->setFieldId(SensingBlocks::CURRENTMENU);
optionField->setSpecialValueId(option);
block->addField(optionField);
block->updateFieldMap();

return block;
}
Expand All @@ -54,15 +53,13 @@ class SensingBlocksTest : public testing::Test
input->setValueBlock(valueBlock);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();
}

std::shared_ptr<Input> addNullInput(std::shared_ptr<Block> block, const std::string &name, SensingBlocks::Inputs id) const
{
auto input = std::make_shared<Input>(name, Input::Type::Shadow);
input->setInputId(id);
block->addInput(input);
block->updateInputMap();

return input;
}
Expand All @@ -85,7 +82,6 @@ class SensingBlocksTest : public testing::Test
field->setFieldId(id);
field->setSpecialValueId(valueId);
block->addField(field);
block->updateFieldMap();
}

std::unique_ptr<IBlockSection> m_section;
Expand Down
2 changes: 0 additions & 2 deletions test/blocks/variable_blocks_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ class VariableBlocksTest : public testing::Test
auto variableField = std::make_shared<Field>("VARIABLE", Value(), variable);
variableField->setFieldId(VariableBlocks::VARIABLE);
block->addField(variableField);
block->updateFieldMap();

auto valueInput = std::make_shared<Input>("VALUE", Input::Type::Shadow);
valueInput->setPrimaryValue(value);
valueInput->setInputId(VariableBlocks::VALUE);
block->addInput(valueInput);
block->updateInputMap();

return block;
}
Expand Down
3 changes: 0 additions & 3 deletions test/compiler/compiler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ TEST_F(CompilerTest, ResolveInput)
input->setPrimaryValue("test");
input->setInputId(TestBlockSection::INPUT1);
block->addInput(input);
block->updateInputMap();
block->setCompileFunction(&TestBlockSection::compileTestBlock1);

compiler.compile(block);
Expand All @@ -361,7 +360,6 @@ TEST_F(CompilerTest, ResolveDropdownMenuInput)
input->setValueBlock(menu);
menu->addField(optionField);
block->addInput(input);
block->updateInputMap();
block->setCompileFunction(&TestBlockSection::compileTestBlock1);

compiler.compile(block);
Expand Down Expand Up @@ -402,7 +400,6 @@ TEST_F(CompilerTest, ResolveField)
field->setFieldId(TestBlockSection::FIELD1);
field->setSpecialValueId(TestBlockSection::TestValue);
block->addField(field);
block->updateFieldMap();
block->setCompileFunction(&TestBlockSection::compileTestBlock2);

compiler.compile(block);
Expand Down
Loading