Skip to content

Implement target click event blocks #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 5, 2024
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: 3 additions & 0 deletions include/scratchcpp/iengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ class LIBSCRATCHCPP_EXPORT IEngine
/* Registers the given "when key pressed" script. */
virtual void addKeyPressScript(std::shared_ptr<Block> hatBlock, int fieldId) = 0;

/* Registers the given "when this sprite/stage clicked" script. */
virtual void addTargetClickScript(std::shared_ptr<Block> hatBlock) = 0;

/*! Returns the list of targets. */
virtual const std::vector<std::shared_ptr<Target>> &targets() const = 0;

Expand Down
12 changes: 12 additions & 0 deletions src/blocks/eventblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ void EventBlocks::registerBlocks(IEngine *engine)
{
// Blocks
engine->addCompileFunction(this, "event_whenflagclicked", &compileWhenFlagClicked);
engine->addCompileFunction(this, "event_whenthisspriteclicked", &compileWhenThisSpriteClicked);
engine->addCompileFunction(this, "event_whenstageclicked", &compileWhenStageClicked);
engine->addCompileFunction(this, "event_broadcast", &compileBroadcast);
engine->addCompileFunction(this, "event_broadcastandwait", &compileBroadcastAndWait);
engine->addCompileFunction(this, "event_whenbroadcastreceived", &compileWhenBroadcastReceived);
Expand All @@ -42,6 +44,16 @@ void EventBlocks::compileWhenFlagClicked(Compiler *compiler)
compiler->engine()->addGreenFlagScript(compiler->block());
}

void EventBlocks::compileWhenThisSpriteClicked(Compiler *compiler)
{
compiler->engine()->addTargetClickScript(compiler->block());
}

void EventBlocks::compileWhenStageClicked(Compiler *compiler)
{
compiler->engine()->addTargetClickScript(compiler->block());
}

void EventBlocks::compileBroadcast(Compiler *compiler)
{
auto input = compiler->input(BROADCAST_INPUT);
Expand Down
2 changes: 2 additions & 0 deletions src/blocks/eventblocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class EventBlocks : public IBlockSection
void registerBlocks(IEngine *engine) override;

static void compileWhenFlagClicked(Compiler *compiler);
static void compileWhenThisSpriteClicked(Compiler *compiler);
static void compileWhenStageClicked(Compiler *compiler);
static void compileBroadcast(Compiler *compiler);
static void compileBroadcastAndWait(Compiler *compiler);
static void compileWhenBroadcastReceived(Compiler *compiler);
Expand Down
17 changes: 11 additions & 6 deletions src/engine/internal/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@
using namespace libscratchcpp;

const std::unordered_map<Engine::HatType, bool> Engine::m_hatRestartExistingThreads = {
{ HatType::GreenFlag, true },
{ HatType::BroadcastReceived, true },
{ HatType::BackdropChanged, true },
{ HatType::CloneInit, false },
{ HatType::KeyPressed, false }
{ HatType::GreenFlag, true }, { HatType::BroadcastReceived, true }, { HatType::BackdropChanged, true },
{ HatType::CloneInit, false }, { HatType::KeyPressed, false }, { HatType::TargetClicked, true }
};

Engine::Engine() :
Expand Down Expand Up @@ -686,7 +683,7 @@ void Engine::setMousePressed(bool pressed)

void Engine::clickTarget(Target *target)
{
// TODO: Implement this (#92, #93)
startHats(HatType::TargetClicked, {}, target);
}

unsigned int Engine::stageWidth() const
Expand Down Expand Up @@ -969,6 +966,11 @@ void Engine::addKeyPressScript(std::shared_ptr<Block> hatBlock, int fieldId)
addHatField(script, HatField::KeyOption, fieldId);
}

void Engine::addTargetClickScript(std::shared_ptr<Block> hatBlock)
{
addHatToMap(m_whenTargetClickedHats, m_scripts[hatBlock].get());
}

const std::vector<std::shared_ptr<Target>> &Engine::targets() const
{
return m_targets;
Expand Down Expand Up @@ -1354,6 +1356,9 @@ const std::vector<Script *> &Engine::getHats(Target *target, HatType type)
case HatType::KeyPressed:
return m_whenKeyPressedHats[target];

case HatType::TargetClicked:
return m_whenTargetClickedHats[target];

default: {
static const std::vector<Script *> empty = {};
return empty;
Expand Down
5 changes: 4 additions & 1 deletion src/engine/internal/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class Engine : public IEngine
void addBackdropChangeScript(std::shared_ptr<Block> hatBlock, int fieldId) override;
void addCloneInitScript(std::shared_ptr<Block> hatBlock) override;
void addKeyPressScript(std::shared_ptr<Block> hatBlock, int fieldId) override;
void addTargetClickScript(std::shared_ptr<Block> hatBlock) override;

const std::vector<std::shared_ptr<Target>> &targets() const override;
void setTargets(const std::vector<std::shared_ptr<Target>> &newTargets) override;
Expand Down Expand Up @@ -156,7 +157,8 @@ class Engine : public IEngine
BroadcastReceived,
BackdropChanged,
CloneInit,
KeyPressed
KeyPressed,
TargetClicked
};

enum class HatField
Expand Down Expand Up @@ -222,6 +224,7 @@ class Engine : public IEngine
std::unordered_map<Target *, std::vector<Script *>> m_broadcastHats;
std::unordered_map<Target *, std::vector<Script *>> m_cloneInitHats;
std::unordered_map<Target *, std::vector<Script *>> m_whenKeyPressedHats;
std::unordered_map<Target *, std::vector<Script *>> m_whenTargetClickedHats;

std::unordered_map<Script *, std::unordered_map<HatField, int>> m_scriptHatFields; // HatField, field ID from the block implementation

Expand Down
42 changes: 42 additions & 0 deletions test/blocks/event_blocks_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ TEST_F(EventBlocksTest, RegisterBlocks)
{
// Blocks
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "event_whenflagclicked", &EventBlocks::compileWhenFlagClicked));
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "event_whenthisspriteclicked", &EventBlocks::compileWhenThisSpriteClicked));
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "event_whenstageclicked", &EventBlocks::compileWhenStageClicked));
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "event_broadcast", &EventBlocks::compileBroadcast));
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "event_broadcastandwait", &EventBlocks::compileBroadcastAndWait));
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "event_whenbroadcastreceived", &EventBlocks::compileWhenBroadcastReceived));
Expand Down Expand Up @@ -121,6 +123,46 @@ TEST_F(EventBlocksTest, WhenFlagClicked)
ASSERT_TRUE(compiler.lists().empty());
}

TEST_F(EventBlocksTest, WhenThisSpriteClicked)
{
Compiler compiler(&m_engineMock);

auto block = createEventBlock("a", "event_whenthisspriteclicked");

compiler.init();

EXPECT_CALL(m_engineMock, addTargetClickScript(block));
compiler.setBlock(block);
EventBlocks::compileWhenThisSpriteClicked(&compiler);

compiler.end();

ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_HALT }));
ASSERT_TRUE(compiler.constValues().empty());
ASSERT_TRUE(compiler.variables().empty());
ASSERT_TRUE(compiler.lists().empty());
}

TEST_F(EventBlocksTest, WhenStageClicked)
{
Compiler compiler(&m_engineMock);

auto block = createEventBlock("a", "event_whenstageclicked");

compiler.init();

EXPECT_CALL(m_engineMock, addTargetClickScript(block));
compiler.setBlock(block);
EventBlocks::compileWhenStageClicked(&compiler);

compiler.end();

ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_HALT }));
ASSERT_TRUE(compiler.constValues().empty());
ASSERT_TRUE(compiler.variables().empty());
ASSERT_TRUE(compiler.lists().empty());
}

TEST_F(EventBlocksTest, Broadcast)
{
Compiler compiler(&m_engineMock);
Expand Down
81 changes: 81 additions & 0 deletions test/engine/engine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,87 @@ TEST(EngineTest, Broadcasts)
ASSERT_EQ(engine.findBroadcastById("c"), 2);
}

TEST(EngineTest, TargetClickScripts)
{
Project p("target_click_scripts.sb3");
ASSERT_TRUE(p.load());
p.run();

auto engine = p.engine();

Stage *stage = engine->stage();
ASSERT_TRUE(stage);

// Initial state
ASSERT_VAR(stage, "1");
ASSERT_EQ(GET_VAR(stage, "1")->value().toInt(), 0);
ASSERT_VAR(stage, "2");
ASSERT_EQ(GET_VAR(stage, "2")->value().toInt(), 0);
ASSERT_VAR(stage, "stage");
ASSERT_EQ(GET_VAR(stage, "stage")->value().toInt(), 0);

// Sprite1
Target *sprite = engine->targetAt(engine->findTarget("Sprite1"));
ASSERT_TRUE(sprite);
engine->clickTarget(sprite);
engine->step();
ASSERT_VAR(stage, "1");
ASSERT_EQ(GET_VAR(stage, "1")->value().toInt(), 1);
ASSERT_VAR(stage, "2");
ASSERT_EQ(GET_VAR(stage, "2")->value().toInt(), 0);
ASSERT_VAR(stage, "stage");
ASSERT_EQ(GET_VAR(stage, "stage")->value().toInt(), 0);

engine->clickTarget(sprite);
engine->step();
ASSERT_VAR(stage, "1");
ASSERT_EQ(GET_VAR(stage, "1")->value().toInt(), 2);
ASSERT_VAR(stage, "2");
ASSERT_EQ(GET_VAR(stage, "2")->value().toInt(), 0);
ASSERT_VAR(stage, "stage");
ASSERT_EQ(GET_VAR(stage, "stage")->value().toInt(), 0);

// Sprite2
sprite = engine->targetAt(engine->findTarget("Sprite2"));
ASSERT_TRUE(sprite);
engine->clickTarget(sprite);
engine->step();
ASSERT_VAR(stage, "1");
ASSERT_EQ(GET_VAR(stage, "1")->value().toInt(), 2);
ASSERT_VAR(stage, "2");
ASSERT_EQ(GET_VAR(stage, "2")->value().toInt(), 1);
ASSERT_VAR(stage, "stage");
ASSERT_EQ(GET_VAR(stage, "stage")->value().toInt(), 0);

engine->clickTarget(sprite);
engine->step();
ASSERT_VAR(stage, "1");
ASSERT_EQ(GET_VAR(stage, "1")->value().toInt(), 2);
ASSERT_VAR(stage, "2");
ASSERT_EQ(GET_VAR(stage, "2")->value().toInt(), 2);
ASSERT_VAR(stage, "stage");
ASSERT_EQ(GET_VAR(stage, "stage")->value().toInt(), 0);

// Stage
engine->clickTarget(stage);
engine->step();
ASSERT_VAR(stage, "1");
ASSERT_EQ(GET_VAR(stage, "1")->value().toInt(), 2);
ASSERT_VAR(stage, "2");
ASSERT_EQ(GET_VAR(stage, "2")->value().toInt(), 2);
ASSERT_VAR(stage, "stage");
ASSERT_EQ(GET_VAR(stage, "stage")->value().toInt(), 1);

engine->clickTarget(stage);
engine->step();
ASSERT_VAR(stage, "1");
ASSERT_EQ(GET_VAR(stage, "1")->value().toInt(), 2);
ASSERT_VAR(stage, "2");
ASSERT_EQ(GET_VAR(stage, "2")->value().toInt(), 2);
ASSERT_VAR(stage, "stage");
ASSERT_EQ(GET_VAR(stage, "stage")->value().toInt(), 2);
}

TEST(EngineTest, Targets)
{
Engine engine;
Expand Down
1 change: 1 addition & 0 deletions test/mocks/enginemock.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class EngineMock : public IEngine
MOCK_METHOD(void, addBackdropChangeScript, (std::shared_ptr<Block>, int), (override));
MOCK_METHOD(void, addCloneInitScript, (std::shared_ptr<Block>), (override));
MOCK_METHOD(void, addKeyPressScript, (std::shared_ptr<Block>, int), (override));
MOCK_METHOD(void, addTargetClickScript, (std::shared_ptr<Block>), (override));

MOCK_METHOD(const std::vector<std::shared_ptr<Target>> &, targets, (), (const, override));
MOCK_METHOD(void, setTargets, (const std::vector<std::shared_ptr<Target>> &), (override));
Expand Down
Binary file added test/target_click_scripts.sb3
Binary file not shown.