Skip to content

Commit 7e8b6bc

Browse files
committed
Add custom blocks section
1 parent ba8b217 commit 7e8b6bc

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

src/blocks/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ target_sources(scratchcpp
1111
operatorblocks.cpp
1212
variableblocks.cpp
1313
listblocks.cpp
14+
customblocks.cpp
1415
PUBLIC
1516
motionblocks.h
1617
looksblocks.h
@@ -21,4 +22,5 @@ target_sources(scratchcpp
2122
operatorblocks.h
2223
variableblocks.h
2324
listblocks.h
25+
customblocks.h
2426
)

src/blocks/customblocks.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "customblocks.h"
4+
#include "../engine/compiler.h"
5+
6+
using namespace libscratchcpp;
7+
8+
CustomBlocks::CustomBlocks()
9+
{
10+
// Blocks
11+
addHatBlock("procedures_definition");
12+
addCompileFunction("procedures_call", &compileCall);
13+
}
14+
15+
std::string CustomBlocks::name() const
16+
{
17+
return "Custom blocks";
18+
}
19+
20+
void CustomBlocks::compileCall(Compiler *compiler)
21+
{
22+
const std::string &code = compiler->block()->mutationPrototype()->procCode();
23+
compiler->addInstruction(vm::OP_CALL_PROCEDURE, { compiler->procedureIndex(code) });
24+
}

src/blocks/customblocks.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "../engine/iblocksection.h"
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class CustomBlocks : public IBlockSection
11+
{
12+
public:
13+
CustomBlocks();
14+
15+
std::string name() const override;
16+
17+
static void compileCall(Compiler *compiler);
18+
};
19+
20+
} // namespace libscratchcpp

src/blocks/standardblocks.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "operatorblocks.h"
1111
#include "variableblocks.h"
1212
#include "listblocks.h"
13+
#include "customblocks.h"
1314

1415
using namespace libscratchcpp;
1516

@@ -39,4 +40,5 @@ void StandardBlocks::registerSections(Engine *engine)
3940
engine->registerSection(std::make_shared<OperatorBlocks>());
4041
engine->registerSection(std::make_shared<VariableBlocks>());
4142
engine->registerSection(std::make_shared<ListBlocks>());
43+
engine->registerSection(std::make_shared<CustomBlocks>());
4244
}

0 commit comments

Comments
 (0)