Skip to content

Commit acd4422

Browse files
committed
Implement pen_penDown block
1 parent 0041827 commit acd4422

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/blocks/penblocks.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ void PenBlocks::registerBlocks(IEngine *engine)
1919
{
2020
// Blocks
2121
engine->addCompileFunction(this, "pen_clear", &compileClear);
22+
engine->addCompileFunction(this, "pen_penDown", &compilePenDown);
2223
}
2324

2425
void PenBlocks::compileClear(Compiler *compiler)
2526
{
2627
compiler->addFunctionCall(&clear);
2728
}
2829

30+
void PenBlocks::compilePenDown(Compiler *compiler)
31+
{
32+
compiler->addFunctionCall(&penDown);
33+
}
34+
2935
unsigned int PenBlocks::clear(VirtualMachine *vm)
3036
{
3137
IPenLayer *penLayer = PenLayer::getProjectPenLayer(vm->engine());
@@ -37,3 +43,20 @@ unsigned int PenBlocks::clear(VirtualMachine *vm)
3743

3844
return 0;
3945
}
46+
47+
unsigned int PenBlocks::penDown(VirtualMachine *vm)
48+
{
49+
Target *target = vm->target();
50+
51+
if (!target || target->isStage())
52+
return 0;
53+
54+
Sprite *sprite = static_cast<Sprite *>(target);
55+
SpriteModel *model = static_cast<SpriteModel *>(sprite->getInterface());
56+
57+
if (model)
58+
model->setPenDown(true);
59+
60+
return 0;
61+
}
62+

src/blocks/penblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ class PenBlocks : public libscratchcpp::IBlockSection
1919
void registerBlocks(libscratchcpp::IEngine *engine) override;
2020

2121
static void compileClear(libscratchcpp::Compiler *compiler);
22+
static void compilePenDown(libscratchcpp::Compiler *compiler);
2223

2324
static unsigned int clear(libscratchcpp::VirtualMachine *vm);
25+
static unsigned int penDown(libscratchcpp::VirtualMachine *vm);
2426
};
2527

2628
} // namespace scratchcpprender

test/blocks/pen_blocks_test.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <scratchcpp/block.h>
33
#include <scratchcpp/input.h>
44
#include <penlayer.h>
5+
#include <spritemodel.h>
56
#include <blocks/penblocks.h>
67
#include <enginemock.h>
78
#include <penlayermock.h>
@@ -44,6 +45,7 @@ TEST_F(PenBlocksTest, RegisterBlocks)
4445
{
4546
// Blocks
4647
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_clear", &PenBlocks::compileClear));
48+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_penDown", &PenBlocks::compilePenDown));
4749

4850
m_section->registerBlocks(&m_engineMock);
4951
}
@@ -84,3 +86,44 @@ TEST_F(PenBlocksTest, ClearImpl)
8486

8587
ASSERT_EQ(vm.registerCount(), 0);
8688
}
89+
90+
TEST_F(PenBlocksTest, PenDown)
91+
{
92+
Compiler compiler(&m_engineMock);
93+
94+
auto block = std::make_shared<Block>("a", "pen_penDown");
95+
96+
EXPECT_CALL(m_engineMock, functionIndex(&PenBlocks::penDown)).WillOnce(Return(2));
97+
compiler.init();
98+
compiler.setBlock(block);
99+
PenBlocks::compilePenDown(&compiler);
100+
compiler.end();
101+
102+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 2, vm::OP_HALT }));
103+
ASSERT_TRUE(compiler.constValues().empty());
104+
ASSERT_TRUE(compiler.variables().empty());
105+
ASSERT_TRUE(compiler.lists().empty());
106+
}
107+
108+
TEST_F(PenBlocksTest, PenDownImpl)
109+
{
110+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
111+
static BlockFunc functions[] = { &PenBlocks::penDown };
112+
113+
SpriteModel model;
114+
Sprite sprite;
115+
sprite.setInterface(&model);
116+
117+
VirtualMachine vm(&sprite, &m_engineMock, nullptr);
118+
vm.setBytecode(bytecode);
119+
vm.setFunctions(functions);
120+
121+
vm.run();
122+
ASSERT_EQ(vm.registerCount(), 0);
123+
ASSERT_TRUE(model.penDown());
124+
125+
vm.reset();
126+
vm.run();
127+
ASSERT_EQ(vm.registerCount(), 0);
128+
ASSERT_TRUE(model.penDown());
129+
}

0 commit comments

Comments
 (0)