Skip to content

Commit b71ea27

Browse files
committed
Implement pen_setPenSizeTo block
1 parent 15f18b2 commit b71ea27

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

src/blocks/penblocks.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void PenBlocks::registerBlocks(IEngine *engine)
2626
engine->addCompileFunction(this, "pen_penDown", &compilePenDown);
2727
engine->addCompileFunction(this, "pen_penUp", &compilePenUp);
2828
engine->addCompileFunction(this, "pen_changePenSizeBy", &compileChangePenSizeBy);
29+
engine->addCompileFunction(this, "pen_setPenSizeTo", &compileSetPenSizeTo);
2930

3031
// Inputs
3132
engine->addInput(this, "SIZE", SIZE);
@@ -52,6 +53,12 @@ void PenBlocks::compileChangePenSizeBy(libscratchcpp::Compiler *compiler)
5253
compiler->addFunctionCall(&changePenSizeBy);
5354
}
5455

56+
void PenBlocks::compileSetPenSizeTo(libscratchcpp::Compiler *compiler)
57+
{
58+
compiler->addInput(SIZE);
59+
compiler->addFunctionCall(&setPenSizeTo);
60+
}
61+
5562
unsigned int PenBlocks::clear(VirtualMachine *vm)
5663
{
5764
IPenLayer *penLayer = PenLayer::getProjectPenLayer(vm->engine());
@@ -94,6 +101,16 @@ unsigned int PenBlocks::changePenSizeBy(libscratchcpp::VirtualMachine *vm)
94101
return 1;
95102
}
96103

104+
unsigned int PenBlocks::setPenSizeTo(libscratchcpp::VirtualMachine *vm)
105+
{
106+
SpriteModel *model = getSpriteModel(vm);
107+
108+
if (model)
109+
model->penAttributes().diameter = std::clamp(vm->getInput(0, 1)->toDouble(), PEN_SIZE_MIN, PEN_SIZE_MAX);
110+
111+
return 1;
112+
}
113+
97114
SpriteModel *PenBlocks::getSpriteModel(libscratchcpp::VirtualMachine *vm)
98115
{
99116
Target *target = vm->target();

src/blocks/penblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ class PenBlocks : public libscratchcpp::IBlockSection
2525
static void compilePenDown(libscratchcpp::Compiler *compiler);
2626
static void compilePenUp(libscratchcpp::Compiler *compiler);
2727
static void compileChangePenSizeBy(libscratchcpp::Compiler *compiler);
28+
static void compileSetPenSizeTo(libscratchcpp::Compiler *compiler);
2829

2930
static unsigned int clear(libscratchcpp::VirtualMachine *vm);
3031
static unsigned int penDown(libscratchcpp::VirtualMachine *vm);
3132
static unsigned int penUp(libscratchcpp::VirtualMachine *vm);
3233
static unsigned int changePenSizeBy(libscratchcpp::VirtualMachine *vm);
34+
static unsigned int setPenSizeTo(libscratchcpp::VirtualMachine *vm);
3335

3436
private:
3537
static SpriteModel *getSpriteModel(libscratchcpp::VirtualMachine *vm);

test/blocks/pen_blocks_test.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ TEST_F(PenBlocksTest, RegisterBlocks)
6565
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_penDown", &PenBlocks::compilePenDown));
6666
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_penUp", &PenBlocks::compilePenUp));
6767
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_changePenSizeBy", &PenBlocks::compileChangePenSizeBy));
68+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_setPenSizeTo", &PenBlocks::compileSetPenSizeTo));
6869

6970
// Inputs
7071
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "SIZE", PenBlocks::SIZE));
@@ -264,3 +265,68 @@ TEST_F(PenBlocksTest, ChangePenSizeByImpl)
264265
ASSERT_EQ(vm.registerCount(), 0);
265266
ASSERT_EQ(model.penAttributes().diameter, 1);
266267
}
268+
269+
TEST_F(PenBlocksTest, SetPenSizeTo)
270+
{
271+
Compiler compiler(&m_engineMock);
272+
273+
// set pen size to (51.46)
274+
auto block1 = std::make_shared<Block>("a", "pen_setPenSizeTo");
275+
addValueInput(block1, "SIZE", PenBlocks::SIZE, 51.46);
276+
277+
// change pen size by (null block)
278+
auto block2 = std::make_shared<Block>("b", "pen_setPenSizeTo");
279+
addObscuredInput(block2, "SIZE", PenBlocks::SIZE, createNullBlock("c"));
280+
281+
compiler.init();
282+
283+
EXPECT_CALL(m_engineMock, functionIndex(&PenBlocks::setPenSizeTo)).WillOnce(Return(2));
284+
compiler.setBlock(block1);
285+
PenBlocks::compileSetPenSizeTo(&compiler);
286+
287+
EXPECT_CALL(m_engineMock, functionIndex(&PenBlocks::setPenSizeTo)).WillOnce(Return(2));
288+
compiler.setBlock(block2);
289+
PenBlocks::compileSetPenSizeTo(&compiler);
290+
291+
compiler.end();
292+
293+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 2, vm::OP_NULL, vm::OP_EXEC, 2, vm::OP_HALT }));
294+
ASSERT_EQ(compiler.constValues().size(), 1);
295+
ASSERT_EQ(compiler.constValues()[0].toDouble(), 51.46);
296+
ASSERT_TRUE(compiler.variables().empty());
297+
ASSERT_TRUE(compiler.lists().empty());
298+
}
299+
300+
TEST_F(PenBlocksTest, SetPenSizeToImpl)
301+
{
302+
static unsigned int bytecode1[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT };
303+
static unsigned int bytecode2[] = { vm::OP_START, vm::OP_CONST, 1, vm::OP_EXEC, 0, vm::OP_HALT };
304+
static unsigned int bytecode3[] = { vm::OP_START, vm::OP_CONST, 2, vm::OP_EXEC, 0, vm::OP_HALT };
305+
static BlockFunc functions[] = { &PenBlocks::setPenSizeTo };
306+
static Value constValues[] = { 511.5, -650.08, 1500 };
307+
308+
SpriteModel model;
309+
Sprite sprite;
310+
sprite.setInterface(&model);
311+
312+
VirtualMachine vm(&sprite, &m_engineMock, nullptr);
313+
vm.setBytecode(bytecode1);
314+
vm.setFunctions(functions);
315+
vm.setConstValues(constValues);
316+
317+
vm.run();
318+
ASSERT_EQ(vm.registerCount(), 0);
319+
ASSERT_EQ(model.penAttributes().diameter, 511.5);
320+
321+
vm.reset();
322+
vm.setBytecode(bytecode2);
323+
vm.run();
324+
ASSERT_EQ(vm.registerCount(), 0);
325+
ASSERT_EQ(model.penAttributes().diameter, 1);
326+
327+
vm.reset();
328+
vm.setBytecode(bytecode3);
329+
vm.run();
330+
ASSERT_EQ(vm.registerCount(), 0);
331+
ASSERT_EQ(model.penAttributes().diameter, 1200);
332+
}

0 commit comments

Comments
 (0)