Skip to content

Commit 73b4d27

Browse files
committed
Add stage getter to Engine
1 parent c6d832f commit 73b4d27

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

include/scratchcpp/iengine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Broadcast;
1616
class Block;
1717
class Target;
1818
class Sprite;
19+
class Stage;
1920
class Variable;
2021
class List;
2122
class Script;
@@ -215,6 +216,9 @@ class LIBSCRATCHCPP_EXPORT IEngine
215216
/*! Returns the index of the target with the given name. */
216217
virtual int findTarget(const std::string &targetName) const = 0;
217218

219+
/*! Returns the Stage. */
220+
virtual Stage *stage() const = 0;
221+
218222
/*! Returns the target which owns the given variable. If it is the stage, the variable is global. */
219223
virtual Target *variableOwner(Variable *variable) const = 0;
220224

src/engine/internal/engine.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <scratchcpp/iblocksection.h>
55
#include <scratchcpp/script.h>
66
#include <scratchcpp/sprite.h>
7+
#include <scratchcpp/stage.h>
78
#include <scratchcpp/broadcast.h>
89
#include <scratchcpp/compiler.h>
910
#include <scratchcpp/input.h>
@@ -653,6 +654,16 @@ int Engine::findTarget(const std::string &targetName) const
653654
return -1;
654655
}
655656

657+
Stage *Engine::stage() const
658+
{
659+
auto it = std::find_if(m_targets.begin(), m_targets.end(), [](std::shared_ptr<Target> target) { return target && target->isStage(); });
660+
661+
if (it == m_targets.end())
662+
return nullptr;
663+
else
664+
return dynamic_cast<Stage *>((*it).get());
665+
}
666+
656667
Target *Engine::variableOwner(Variable *variable) const
657668
{
658669
auto it = m_variableOwners.find(variable);

src/engine/internal/engine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class Engine : public IEngine
8989
Target *targetAt(int index) const override;
9090
int findTarget(const std::string &targetName) const override;
9191

92+
Stage *stage() const override;
93+
9294
Target *variableOwner(Variable *variable) const override;
9395
Target *listOwner(List *list) const override;
9496

test/engine/engine_test.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <scratchcpp/broadcast.h>
22
#include <scratchcpp/block.h>
33
#include <scratchcpp/project.h>
4+
#include <scratchcpp/sprite.h>
5+
#include <scratchcpp/stage.h>
46
#include <scratchcpp/variable.h>
57
#include <scratchcpp/list.h>
68
#include <timermock.h>
@@ -369,6 +371,33 @@ TEST(EngineTest, Targets)
369371
ASSERT_EQ(block2->engine(), &engine);
370372
}
371373

374+
TEST(EngineTest, Stage)
375+
{
376+
Engine engine;
377+
ASSERT_EQ(engine.stage(), nullptr);
378+
379+
auto t1 = std::make_shared<Sprite>();
380+
t1->setName("Sprite1");
381+
engine.setTargets({ t1 });
382+
ASSERT_EQ(engine.stage(), nullptr);
383+
384+
auto t2 = std::make_shared<Stage>();
385+
t2->setName("Stage");
386+
engine.setTargets({ t1, t2 });
387+
ASSERT_EQ(engine.stage(), t2.get());
388+
389+
auto t3 = std::make_shared<Sprite>();
390+
t3->setName("Sprite2");
391+
engine.setTargets({ t1, t2, t3 });
392+
ASSERT_EQ(engine.stage(), t2.get());
393+
394+
engine.setTargets({ t2, t3 });
395+
ASSERT_EQ(engine.stage(), t2.get());
396+
397+
engine.setTargets({ t1, t3 });
398+
ASSERT_EQ(engine.stage(), nullptr);
399+
}
400+
372401
TEST(EngineTest, VariableOwner)
373402
{
374403
Engine engine;

test/mocks/enginemock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class EngineMock : public IEngine
7575
MOCK_METHOD(Target *, targetAt, (int), (const, override));
7676
MOCK_METHOD(int, findTarget, (const std::string &), (const, override));
7777

78+
MOCK_METHOD(Stage *, stage, (), (const, override));
79+
7880
MOCK_METHOD(Target *, variableOwner, (Variable *), (const, override));
7981
MOCK_METHOD(Target *, listOwner, (List *), (const, override));
8082

0 commit comments

Comments
 (0)