Skip to content

Commit bfe2c24

Browse files
committed
Add API for top level reporter blocks
1 parent b0ee231 commit bfe2c24

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

include/scratchcpp/block.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class IEngine;
1212
class Target;
1313
class Input;
1414
class Field;
15+
class InputValue;
1516
class BlockPrivate;
1617

1718
/*! \brief The Block class represents a Scratch block. */
@@ -68,6 +69,11 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
6869

6970
BlockPrototype *mutationPrototype();
7071

72+
bool isTopLevelReporter() const;
73+
void setIsTopLevelReporter(bool isTopLevelReporter);
74+
75+
InputValue *topLevelReporterInfo();
76+
7177
private:
7278
spimpl::unique_impl_ptr<BlockPrivate> impl;
7379
};

src/scratch/block.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,41 @@ BlockPrototype *Block::mutationPrototype()
5858
return &impl->mutationPrototype;
5959
}
6060

61+
/*! Returns true if this is a top level reporter block for a variable or a list. */
62+
bool Block::isTopLevelReporter() const
63+
{
64+
return impl->isTopLevelReporter;
65+
}
66+
67+
/*!
68+
* Sets whether this block is a top level reporter block for a variable or a list.
69+
* \note Setting this to true will allow the use topLevelReporterInfo().
70+
*/
71+
void Block::setIsTopLevelReporter(bool isTopLevelReporter)
72+
{
73+
impl->isTopLevelReporter = isTopLevelReporter;
74+
75+
if (isTopLevelReporter && !impl->topLevelReporterInfo)
76+
impl->topLevelReporterInfo = std::make_unique<InputValue>(InputValue::Type::Variable);
77+
else if (impl->topLevelReporterInfo) {
78+
impl->topLevelReporterInfo.reset();
79+
impl->topLevelReporterInfo = nullptr;
80+
}
81+
}
82+
83+
/*!
84+
* Returns the information about this top level reporter block (if this is a top level reporter block).
85+
* \note This function will return nullptr if this isn't a top level reporter block. Use setIsTopLevelReporter(true)
86+
* before using this function.
87+
*/
88+
InputValue *Block::topLevelReporterInfo()
89+
{
90+
if (impl->topLevelReporterInfo)
91+
return impl->topLevelReporterInfo.get();
92+
else
93+
return nullptr;
94+
}
95+
6196
/*! Returns the next block. */
6297
std::shared_ptr<Block> Block::next() const
6398
{

src/scratch/block_p.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#pragma once
44

55
#include <scratchcpp/blockprototype.h>
6+
#include <scratchcpp/inputvalue.h>
67

78
namespace libscratchcpp
89
{
@@ -33,6 +34,8 @@ struct BlockPrivate
3334
Target *target = nullptr;
3435
BlockPrototype mutationPrototype;
3536
bool mutationHasNext = true;
37+
bool isTopLevelReporter = false;
38+
std::unique_ptr<InputValue> topLevelReporterInfo = nullptr;
3639
};
3740

3841
} // namespace libscratchcpp

test/scratch_classes/block_test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,20 @@ TEST_F(BlockTest, MutationPrototype)
241241
Block block("", "");
242242
ASSERT_TRUE(block.mutationPrototype());
243243
}
244+
245+
TEST_F(BlockTest, TopLevelReporter)
246+
{
247+
Block block("", "");
248+
ASSERT_FALSE(block.isTopLevelReporter());
249+
ASSERT_EQ(block.topLevelReporterInfo(), nullptr);
250+
251+
for (int i = 0; i < 2; i++) {
252+
block.setIsTopLevelReporter(true);
253+
ASSERT_TRUE(block.isTopLevelReporter());
254+
ASSERT_TRUE(block.topLevelReporterInfo());
255+
256+
block.setIsTopLevelReporter(false);
257+
ASSERT_FALSE(block.isTopLevelReporter());
258+
ASSERT_EQ(block.topLevelReporterInfo(), nullptr);
259+
}
260+
}

0 commit comments

Comments
 (0)