Skip to content

Commit 0041827

Browse files
committed
Implement pen_clear block
1 parent 0484ed3 commit 0041827

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

src/blocks/penblocks.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// SPDX-License-Identifier: LGPL-3.0-or-later
22

3+
#include <scratchcpp/compiler.h>
4+
#include <scratchcpp/sprite.h>
5+
36
#include "penblocks.h"
7+
#include "penlayer.h"
8+
#include "spritemodel.h"
49

510
using namespace scratchcpprender;
611
using namespace libscratchcpp;
@@ -12,4 +17,23 @@ std::string PenBlocks::name() const
1217

1318
void PenBlocks::registerBlocks(IEngine *engine)
1419
{
20+
// Blocks
21+
engine->addCompileFunction(this, "pen_clear", &compileClear);
22+
}
23+
24+
void PenBlocks::compileClear(Compiler *compiler)
25+
{
26+
compiler->addFunctionCall(&clear);
27+
}
28+
29+
unsigned int PenBlocks::clear(VirtualMachine *vm)
30+
{
31+
IPenLayer *penLayer = PenLayer::getProjectPenLayer(vm->engine());
32+
33+
if (penLayer) {
34+
penLayer->clear();
35+
vm->engine()->requestRedraw();
36+
}
37+
38+
return 0;
1539
}

src/blocks/penblocks.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class PenBlocks : public libscratchcpp::IBlockSection
1717
std::string name() const override;
1818

1919
void registerBlocks(libscratchcpp::IEngine *engine) override;
20+
21+
static void compileClear(libscratchcpp::Compiler *compiler);
22+
23+
static unsigned int clear(libscratchcpp::VirtualMachine *vm);
2024
};
2125

2226
} // namespace scratchcpprender

test/blocks/pen_blocks_test.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
#include <scratchcpp/compiler.h>
22
#include <scratchcpp/block.h>
33
#include <scratchcpp/input.h>
4+
#include <penlayer.h>
45
#include <blocks/penblocks.h>
56
#include <enginemock.h>
7+
#include <penlayermock.h>
68

79
#include "../common.h"
810

911
using namespace scratchcpprender;
1012
using namespace libscratchcpp;
1113

14+
using ::testing::Return;
15+
1216
class PenBlocksTest : public testing::Test
1317
{
1418
public:
@@ -38,5 +42,45 @@ TEST_F(PenBlocksTest, CategoryVisible)
3842

3943
TEST_F(PenBlocksTest, RegisterBlocks)
4044
{
45+
// Blocks
46+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_clear", &PenBlocks::compileClear));
47+
4148
m_section->registerBlocks(&m_engineMock);
4249
}
50+
51+
TEST_F(PenBlocksTest, Clear)
52+
{
53+
Compiler compiler(&m_engineMock);
54+
55+
auto block = std::make_shared<Block>("a", "pen_clear");
56+
57+
EXPECT_CALL(m_engineMock, functionIndex(&PenBlocks::clear)).WillOnce(Return(2));
58+
compiler.init();
59+
compiler.setBlock(block);
60+
PenBlocks::compileClear(&compiler);
61+
compiler.end();
62+
63+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 2, vm::OP_HALT }));
64+
ASSERT_TRUE(compiler.constValues().empty());
65+
ASSERT_TRUE(compiler.variables().empty());
66+
ASSERT_TRUE(compiler.lists().empty());
67+
}
68+
69+
TEST_F(PenBlocksTest, ClearImpl)
70+
{
71+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
72+
static BlockFunc functions[] = { &PenBlocks::clear };
73+
74+
PenLayerMock penLayer;
75+
PenLayer::addPenLayer(&m_engineMock, &penLayer);
76+
77+
VirtualMachine vm(nullptr, &m_engineMock, nullptr);
78+
vm.setBytecode(bytecode);
79+
vm.setFunctions(functions);
80+
81+
EXPECT_CALL(penLayer, clear());
82+
EXPECT_CALL(m_engineMock, requestRedraw());
83+
vm.run();
84+
85+
ASSERT_EQ(vm.registerCount(), 0);
86+
}

0 commit comments

Comments
 (0)