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
3 changes: 2 additions & 1 deletion include/scratchcpp/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
void setShadow(bool newShadow);

bool topLevel() const;
void setTopLevel(bool newTopLevel);

void setEngine(IEngine *newEngine);
IEngine *engine() const;

void setTarget(Target *newTarget);
Target *target() const;

BlockComp compileFunction() const;
void setCompileFunction(BlockComp newCompileFunction);
Expand Down
4 changes: 0 additions & 4 deletions src/internal/scratch3reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,6 @@ bool Scratch3Reader::load()
READER_STEP(step, "target -> block -> shadow");
block->setShadow(blockInfo["shadow"]);

// topLevel
READER_STEP(step, "target -> block -> topLevel");
block->setTopLevel(blockInfo["topLevel"]);

target->addBlock(block);
}

Expand Down
61 changes: 43 additions & 18 deletions src/scratch/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Block::Block(const std::string &id, const std::string &opcode) :
/*! Calls the compile function. */
void Block::compile(Compiler *compiler)
{
return impl->compileFunction(compiler);
if (impl->compileFunction)
return impl->compileFunction(compiler);
}

/*! Returns the opcode. */
Expand Down Expand Up @@ -66,16 +67,18 @@ std::shared_ptr<Block> Block::next() const
/*! Returns the ID of the next block. */
std::string Block::nextId() const
{
if (impl->next)
return impl->next->id();
else
return impl->nextId;
return impl->nextId;
}

/*! Sets the next block. */
void Block::setNext(std::shared_ptr<Block> block)
{
impl->next = block;

if (block)
impl->nextId = block->id();
else
impl->nextId = "";
}

/*! Sets the next block by ID. */
Expand All @@ -94,16 +97,18 @@ std::shared_ptr<Block> Block::parent() const
/*! Returns the ID of the parent block. */
std::string Block::parentId() const
{
if (impl->parent)
return impl->parent->id();
else
return impl->parentId;
return impl->parentId;
}

/*! Sets the parent block. */
void Block::setParent(std::shared_ptr<Block> block)
{
impl->parent = block;

if (block)
impl->parentId = block->id();
else
impl->parentId = "";
}

/*! Sets the parent block by ID. */
Expand All @@ -122,13 +127,21 @@ std::vector<std::shared_ptr<Input>> Block::inputs() const
/*! Adds an input and returns its index. */
int Block::addInput(std::shared_ptr<Input> input)
{
auto it = std::find(impl->inputs.begin(), impl->inputs.end(), input);

if (it != impl->inputs.end())
return it - impl->inputs.begin();

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

/*! Returns the input at index. */
std::shared_ptr<Input> Block::inputAt(int index) const
{
if (index < 0 || index >= impl->inputs.size())
return nullptr;

return impl->inputs[index];
}

Expand Down Expand Up @@ -169,13 +182,21 @@ std::vector<std::shared_ptr<Field>> Block::fields() const
/*! Adds a field and returns its index. */
int Block::addField(std::shared_ptr<Field> field)
{
auto it = std::find(impl->fields.begin(), impl->fields.end(), field);

if (it != impl->fields.end())
return it - impl->fields.begin();

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

/*! Returns the field at index. */
std::shared_ptr<Field> Block::fieldAt(int index) const
{
if (index < 0 || index >= impl->fields.size())
return nullptr;

return impl->fields[index];
}

Expand Down Expand Up @@ -222,15 +243,7 @@ void Block::setShadow(bool newShadow)
/*! Returns true if this is a top level block. */
bool Block::topLevel() const
{
// TODO: Return true if parentId() == ""
// and remove the setter
return impl->topLevel;
}

/*! Toggles whether this block is a top level block. */
void Block::setTopLevel(bool newTopLevel)
{
impl->topLevel = newTopLevel;
return (impl->parentId == "" && !impl->parent);
}

/*! Sets the Engine. */
Expand All @@ -239,8 +252,20 @@ void Block::setEngine(IEngine *newEngine)
impl->engine = newEngine;
}

/*! Returns the Engine. */
IEngine *Block::engine() const
{
return impl->engine;
}

/*! Sets the Target. */
void Block::setTarget(Target *newTarget)
{
impl->target = newTarget;
}

/*! Returns the Target. */
Target *Block::target() const
{
return impl->target;
}
1 change: 0 additions & 1 deletion src/scratch/block_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ struct BlockPrivate
std::vector<std::shared_ptr<Field>> fields;
std::unordered_map<int, Field *> fieldMap;
bool shadow = false;
bool topLevel = false;
IEngine *engine = nullptr;
Target *target = nullptr;
BlockPrototype mutationPrototype;
Expand Down
16 changes: 16 additions & 0 deletions test/scratch_classes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,19 @@ target_link_libraries(
)

gtest_discover_tests(field_test)

# block_test
add_executable(
block_test
block_test.cpp
)

target_link_libraries(
block_test
GTest::gtest_main
GTest::gmock_main
scratchcpp
scratchcpp_mocks
)

gtest_discover_tests(block_test)
Loading