Skip to content

Commit 5138865

Browse files
committed
Add blockSection property to Monitor
1 parent 8055b66 commit 5138865

File tree

5 files changed

+21
-0
lines changed

5 files changed

+21
-0
lines changed

include/scratchcpp/monitor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace libscratchcpp
1212
class IMonitorHandler;
1313
class Block;
1414
class Script;
15+
class IBlockSection;
1516
class Sprite;
1617
class Value;
1718
class Rect;
@@ -44,6 +45,8 @@ class LIBSCRATCHCPP_EXPORT Monitor : public Entity
4445
std::shared_ptr<Script> script() const;
4546
void setScript(std::shared_ptr<Script> script);
4647

48+
std::shared_ptr<IBlockSection> blockSection() const;
49+
4750
Sprite *sprite() const;
4851
void setSprite(Sprite *sprite);
4952

src/engine/internal/engine.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ void Engine::resolveIds()
138138
if (container)
139139
block->setCompileFunction(container->resolveBlockCompileFunc(block->opcode()));
140140

141+
monitor->impl->blockSection = blockSection(monitor->opcode());
142+
141143
const auto &fields = block->fields();
142144
Target *target;
143145

@@ -1436,6 +1438,8 @@ void Engine::addVarOrListMonitor(std::shared_ptr<Monitor> monitor, Target *targe
14361438
if (!target->isStage())
14371439
monitor->setSprite(dynamic_cast<Sprite *>(target));
14381440

1441+
monitor->impl->blockSection = blockSection(monitor->opcode());
1442+
14391443
// Auto-position the monitor
14401444
Rect rect = Monitor::getInitialPosition(m_monitors, monitor->width(), monitor->height());
14411445
monitor->setX(rect.left());

src/scratch/monitor.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ void Monitor::setScript(std::shared_ptr<Script> script)
6262
impl->script = script;
6363
}
6464

65+
/*
66+
* Returns the block section of this monitor.
67+
* \note This will return nullptr by default. Add the monitor to a project to set this property.
68+
*/
69+
std::shared_ptr<IBlockSection> Monitor::blockSection() const
70+
{
71+
return impl->blockSection;
72+
}
73+
6574
/*! Convenience method which calls block()->target(). */
6675
Sprite *Monitor::sprite() const
6776
{

src/scratch/monitor_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct MonitorPrivate
1919
IMonitorHandler *iface = nullptr;
2020
Monitor::Mode mode = Monitor::Mode::Default;
2121
std::shared_ptr<Script> script;
22+
std::shared_ptr<IBlockSection> blockSection;
2223
std::shared_ptr<Block> block; // Compiler needs shared_ptr
2324
unsigned int width = 0;
2425
unsigned int height = 0;

test/engine/engine_test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ TEST(EngineTest, CompileAndExecuteMonitors)
138138
ASSERT_EQ(script2->target(), sprite.get());
139139
ASSERT_EQ(script2->topBlock(), m2->block());
140140

141+
ASSERT_EQ(m1->blockSection(), section);
142+
ASSERT_EQ(m2->blockSection(), section);
143+
ASSERT_FALSE(m3->blockSection());
144+
141145
// Execute the monitor blocks
142146
MonitorHandlerMock iface1, iface2, iface3;
143147
EXPECT_CALL(iface1, init);

0 commit comments

Comments
 (0)