Skip to content

Commit d53d1ef

Browse files
committed
Implement pen_penUp block
1 parent acd4422 commit d53d1ef

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/blocks/penblocks.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void PenBlocks::registerBlocks(IEngine *engine)
2020
// Blocks
2121
engine->addCompileFunction(this, "pen_clear", &compileClear);
2222
engine->addCompileFunction(this, "pen_penDown", &compilePenDown);
23+
engine->addCompileFunction(this, "pen_penUp", &compilePenUp);
2324
}
2425

2526
void PenBlocks::compileClear(Compiler *compiler)
@@ -32,6 +33,11 @@ void PenBlocks::compilePenDown(Compiler *compiler)
3233
compiler->addFunctionCall(&penDown);
3334
}
3435

36+
void PenBlocks::compilePenUp(Compiler *compiler)
37+
{
38+
compiler->addFunctionCall(&penUp);
39+
}
40+
3541
unsigned int PenBlocks::clear(VirtualMachine *vm)
3642
{
3743
IPenLayer *penLayer = PenLayer::getProjectPenLayer(vm->engine());
@@ -60,3 +66,18 @@ unsigned int PenBlocks::penDown(VirtualMachine *vm)
6066
return 0;
6167
}
6268

69+
unsigned int PenBlocks::penUp(libscratchcpp::VirtualMachine *vm)
70+
{
71+
Target *target = vm->target();
72+
73+
if (!target || target->isStage())
74+
return 0;
75+
76+
Sprite *sprite = static_cast<Sprite *>(target);
77+
SpriteModel *model = static_cast<SpriteModel *>(sprite->getInterface());
78+
79+
if (model)
80+
model->setPenDown(false);
81+
82+
return 0;
83+
}

src/blocks/penblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ class PenBlocks : public libscratchcpp::IBlockSection
2020

2121
static void compileClear(libscratchcpp::Compiler *compiler);
2222
static void compilePenDown(libscratchcpp::Compiler *compiler);
23+
static void compilePenUp(libscratchcpp::Compiler *compiler);
2324

2425
static unsigned int clear(libscratchcpp::VirtualMachine *vm);
2526
static unsigned int penDown(libscratchcpp::VirtualMachine *vm);
27+
static unsigned int penUp(libscratchcpp::VirtualMachine *vm);
2628
};
2729

2830
} // namespace scratchcpprender

test/blocks/pen_blocks_test.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ TEST_F(PenBlocksTest, RegisterBlocks)
4646
// Blocks
4747
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_clear", &PenBlocks::compileClear));
4848
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_penDown", &PenBlocks::compilePenDown));
49+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_penUp", &PenBlocks::compilePenUp));
4950

5051
m_section->registerBlocks(&m_engineMock);
5152
}
@@ -127,3 +128,45 @@ TEST_F(PenBlocksTest, PenDownImpl)
127128
ASSERT_EQ(vm.registerCount(), 0);
128129
ASSERT_TRUE(model.penDown());
129130
}
131+
132+
TEST_F(PenBlocksTest, PenUp)
133+
{
134+
Compiler compiler(&m_engineMock);
135+
136+
auto block = std::make_shared<Block>("a", "pen_penUp");
137+
138+
EXPECT_CALL(m_engineMock, functionIndex(&PenBlocks::penUp)).WillOnce(Return(2));
139+
compiler.init();
140+
compiler.setBlock(block);
141+
PenBlocks::compilePenUp(&compiler);
142+
compiler.end();
143+
144+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 2, vm::OP_HALT }));
145+
ASSERT_TRUE(compiler.constValues().empty());
146+
ASSERT_TRUE(compiler.variables().empty());
147+
ASSERT_TRUE(compiler.lists().empty());
148+
}
149+
150+
TEST_F(PenBlocksTest, PenUpImpl)
151+
{
152+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
153+
static BlockFunc functions[] = { &PenBlocks::penUp };
154+
155+
SpriteModel model;
156+
model.setPenDown(true);
157+
Sprite sprite;
158+
sprite.setInterface(&model);
159+
160+
VirtualMachine vm(&sprite, &m_engineMock, nullptr);
161+
vm.setBytecode(bytecode);
162+
vm.setFunctions(functions);
163+
164+
vm.run();
165+
ASSERT_EQ(vm.registerCount(), 0);
166+
ASSERT_FALSE(model.penDown());
167+
168+
vm.reset();
169+
vm.run();
170+
ASSERT_EQ(vm.registerCount(), 0);
171+
ASSERT_FALSE(model.penDown());
172+
}

0 commit comments

Comments
 (0)