Skip to content

Commit 98b0ead

Browse files
committed
Implement pen_clear block
1 parent 8a09468 commit 98b0ead

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

src/blocks/penblocks.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
#include <scratchcpp/iengine.h>
2+
#include <scratchcpp/compiler.h>
3+
#include <scratchcpp/executioncontext.h>
4+
15
#include "penblocks.h"
6+
#include "penlayer.h"
27

38
using namespace scratchcpprender;
49
using namespace libscratchcpp;
@@ -20,4 +25,22 @@ Rgb PenBlocks::color() const
2025

2126
void PenBlocks::registerBlocks(IEngine *engine)
2227
{
28+
engine->addCompileFunction(this, "pen_clear", &compileClear);
29+
}
30+
31+
CompilerValue *PenBlocks::compileClear(Compiler *compiler)
32+
{
33+
compiler->addFunctionCallWithCtx("pen_clear");
34+
return nullptr;
35+
}
36+
37+
BLOCK_EXPORT void pen_clear(ExecutionContext *ctx)
38+
{
39+
IEngine *engine = ctx->engine();
40+
IPenLayer *penLayer = PenLayer::getProjectPenLayer(engine);
41+
42+
if (penLayer) {
43+
penLayer->clear();
44+
engine->requestRedraw();
45+
}
2346
}

src/blocks/penblocks.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class PenBlocks : public libscratchcpp::IExtension
1111
libscratchcpp::Rgb color() const override;
1212

1313
void registerBlocks(libscratchcpp::IEngine *engine) override;
14+
15+
private:
16+
static libscratchcpp::CompilerValue *compileClear(libscratchcpp::Compiler *compiler);
1417
};
1518

1619
} // namespace scratchcpprender

src/penlayer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,11 @@ void PenLayer::addPenLayer(libscratchcpp::IEngine *engine, IPenLayer *penLayer)
468468
m_projectPenLayers[engine] = penLayer;
469469
}
470470

471+
void PenLayer::removePenLayer(libscratchcpp::IEngine *engine)
472+
{
473+
m_projectPenLayers.erase(engine);
474+
}
475+
471476
QNanoQuickItemPainter *PenLayer::createItemPainter() const
472477
{
473478
m_glCtx = QOpenGLContext::currentContext();

src/penlayer.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ class PenLayer : public IPenLayer
4747
const libscratchcpp::Rect &getBounds() const override;
4848

4949
static IPenLayer *getProjectPenLayer(libscratchcpp::IEngine *engine);
50-
static void addPenLayer(libscratchcpp::IEngine *engine, IPenLayer *penLayer); // for tests
50+
51+
// For tests
52+
static void addPenLayer(libscratchcpp::IEngine *engine, IPenLayer *penLayer);
53+
static void removePenLayer(libscratchcpp::IEngine *engine);
5154

5255
signals:
5356
void engineChanged();

test/blocks/pen_blocks_test.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
#include <scratchcpp/project.h>
2+
#include <scratchcpp/sprite.h>
3+
#include <scratchcpp/compiler.h>
4+
#include <scratchcpp/script.h>
5+
#include <scratchcpp/thread.h>
6+
#include <scratchcpp/test/scriptbuilder.h>
27
#include <enginemock.h>
8+
#include <penlayermock.h>
39
#include <gtest/gtest.h>
410

11+
#include "penlayer.h"
512
#include "blocks/penblocks.h"
613

714
using namespace scratchcpprender;
815
using namespace libscratchcpp;
16+
using namespace libscratchcpp::test;
17+
18+
using ::testing::Return;
19+
using ::testing::ReturnRef;
920

1021
class PenBlocksTest : public testing::Test
1122
{
@@ -15,10 +26,47 @@ class PenBlocksTest : public testing::Test
1526
m_extension = std::make_unique<PenBlocks>();
1627
m_engine = m_project.engine().get();
1728
m_extension->registerBlocks(m_engine);
29+
30+
PenLayer::addPenLayer(&m_engineMock, &m_penLayer);
31+
32+
EXPECT_CALL(m_engineMock, targets()).WillRepeatedly(ReturnRef(m_engine->targets()));
33+
}
34+
35+
void TearDown() override { PenLayer::removePenLayer(&m_engineMock); }
36+
37+
std::shared_ptr<Thread> buildScript(ScriptBuilder &builder, Target *target)
38+
{
39+
auto block = builder.currentBlock();
40+
41+
m_compiler = std::make_unique<Compiler>(&m_engineMock, target);
42+
auto code = m_compiler->compile(block);
43+
m_script = std::make_unique<Script>(target, block, &m_engineMock);
44+
m_script->setCode(code);
45+
return std::make_shared<Thread>(target, &m_engineMock, m_script.get());
1846
}
1947

2048
std::unique_ptr<IExtension> m_extension;
2149
Project m_project;
2250
IEngine *m_engine = nullptr;
2351
EngineMock m_engineMock;
52+
PenLayerMock m_penLayer;
53+
54+
private:
55+
std::unique_ptr<Compiler> m_compiler;
56+
std::unique_ptr<Script> m_script;
2457
};
58+
59+
TEST_F(PenBlocksTest, Clear)
60+
{
61+
auto sprite = std::make_shared<Sprite>();
62+
sprite->setEngine(&m_engineMock);
63+
64+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
65+
builder.addBlock("pen_clear");
66+
67+
auto thread = buildScript(builder, sprite.get());
68+
69+
EXPECT_CALL(m_penLayer, clear());
70+
EXPECT_CALL(m_engineMock, requestRedraw());
71+
thread->run();
72+
}

0 commit comments

Comments
 (0)