Skip to content

Commit 646de65

Browse files
committed
Implement pen_setPenShadeToNumber block
1 parent 4b48bcc commit 646de65

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

src/blocks/penblocks.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ void PenBlocks::registerBlocks(IEngine *engine)
3030
engine->addCompileFunction(this, "pen_changePenSizeBy", &compileChangePenSizeBy);
3131
engine->addCompileFunction(this, "pen_setPenSizeTo", &compileSetPenSizeTo);
3232
engine->addCompileFunction(this, "pen_changePenShadeBy", &compileChangePenShadeBy);
33+
engine->addCompileFunction(this, "pen_setPenShadeToNumber", &compileSetPenShadeToNumber);
3334
engine->addCompileFunction(this, "pen_changePenHueBy", &compileChangePenHueBy);
3435
engine->addCompileFunction(this, "pen_setPenHueToNumber", &compileSetPenHueToNumber);
3536

@@ -79,6 +80,12 @@ void PenBlocks::compileChangePenShadeBy(Compiler *compiler)
7980
compiler->addFunctionCall(&changePenShadeBy);
8081
}
8182

83+
void PenBlocks::compileSetPenShadeToNumber(libscratchcpp::Compiler *compiler)
84+
{
85+
compiler->addInput(SHADE);
86+
compiler->addFunctionCall(&setPenShadeToNumber);
87+
}
88+
8289
void PenBlocks::compileChangePenHueBy(libscratchcpp::Compiler *compiler)
8390
{
8491
compiler->addInput(HUE);
@@ -155,6 +162,16 @@ unsigned int PenBlocks::changePenShadeBy(libscratchcpp::VirtualMachine *vm)
155162
return 1;
156163
}
157164

165+
unsigned int PenBlocks::setPenShadeToNumber(libscratchcpp::VirtualMachine *vm)
166+
{
167+
SpriteModel *model = getSpriteModel(vm);
168+
169+
if (model)
170+
setPenShade(vm->getInput(0, 1)->toInt(), model->penState());
171+
172+
return 1;
173+
}
174+
158175
unsigned int PenBlocks::changePenHueBy(libscratchcpp::VirtualMachine *vm)
159176
{
160177
SpriteModel *model = getSpriteModel(vm);

src/blocks/penblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class PenBlocks : public libscratchcpp::IBlockSection
3333
static void compileChangePenSizeBy(libscratchcpp::Compiler *compiler);
3434
static void compileSetPenSizeTo(libscratchcpp::Compiler *compiler);
3535
static void compileChangePenShadeBy(libscratchcpp::Compiler *compiler);
36+
static void compileSetPenShadeToNumber(libscratchcpp::Compiler *compiler);
3637
static void compileChangePenHueBy(libscratchcpp::Compiler *compiler);
3738
static void compileSetPenHueToNumber(libscratchcpp::Compiler *compiler);
3839

@@ -43,6 +44,7 @@ class PenBlocks : public libscratchcpp::IBlockSection
4344
static unsigned int changePenSizeBy(libscratchcpp::VirtualMachine *vm);
4445
static unsigned int setPenSizeTo(libscratchcpp::VirtualMachine *vm);
4546
static unsigned int changePenShadeBy(libscratchcpp::VirtualMachine *vm);
47+
static unsigned int setPenShadeToNumber(libscratchcpp::VirtualMachine *vm);
4648
static unsigned int changePenHueBy(libscratchcpp::VirtualMachine *vm);
4749
static unsigned int setPenHueToNumber(libscratchcpp::VirtualMachine *vm);
4850

test/blocks/pen_blocks_test.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ TEST_F(PenBlocksTest, RegisterBlocks)
6868
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_changePenSizeBy", &PenBlocks::compileChangePenSizeBy));
6969
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_setPenSizeTo", &PenBlocks::compileSetPenSizeTo));
7070
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_changePenShadeBy", &PenBlocks::compileChangePenShadeBy));
71+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_setPenShadeToNumber", &PenBlocks::compileSetPenShadeToNumber));
7172
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_changePenHueBy", &PenBlocks::compileChangePenHueBy));
7273
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_setPenHueToNumber", &PenBlocks::compileSetPenHueToNumber));
7374

@@ -505,6 +506,72 @@ TEST_F(PenBlocksTest, ChangePenShadeByImpl)
505506
ASSERT_EQ(model.penAttributes().color, QColor::fromHsv(240, 255, 55, 150));
506507
}
507508

509+
TEST_F(PenBlocksTest, SetPenShadeToNumber)
510+
{
511+
Compiler compiler(&m_engineMock);
512+
513+
// set pen shade to (4.5)
514+
auto block1 = std::make_shared<Block>("a", "pen_setPenShadeToNumber");
515+
addValueInput(block1, "SHADE", PenBlocks::SHADE, 4.5);
516+
517+
// set pen shade to (null block)
518+
auto block2 = std::make_shared<Block>("b", "pen_setPenShadeToNumber");
519+
addObscuredInput(block2, "SHADE", PenBlocks::SHADE, createNullBlock("c"));
520+
521+
compiler.init();
522+
523+
EXPECT_CALL(m_engineMock, functionIndex(&PenBlocks::setPenShadeToNumber)).WillOnce(Return(2));
524+
compiler.setBlock(block1);
525+
PenBlocks::compileSetPenShadeToNumber(&compiler);
526+
527+
EXPECT_CALL(m_engineMock, functionIndex(&PenBlocks::setPenShadeToNumber)).WillOnce(Return(2));
528+
compiler.setBlock(block2);
529+
PenBlocks::compileSetPenShadeToNumber(&compiler);
530+
531+
compiler.end();
532+
533+
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 }));
534+
ASSERT_EQ(compiler.constValues().size(), 1);
535+
ASSERT_EQ(compiler.constValues()[0].toDouble(), 4.5);
536+
ASSERT_TRUE(compiler.variables().empty());
537+
ASSERT_TRUE(compiler.lists().empty());
538+
}
539+
540+
TEST_F(PenBlocksTest, SetPenShadeToNumberImpl)
541+
{
542+
static unsigned int bytecode1[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT };
543+
static unsigned int bytecode2[] = { vm::OP_START, vm::OP_CONST, 1, vm::OP_EXEC, 0, vm::OP_HALT };
544+
static unsigned int bytecode3[] = { vm::OP_START, vm::OP_CONST, 2, vm::OP_EXEC, 0, vm::OP_HALT };
545+
static BlockFunc functions[] = { &PenBlocks::setPenShadeToNumber };
546+
static Value constValues[] = { 125.7, -114.09, 489.4 };
547+
548+
SpriteModel model;
549+
model.penState().transparency = 100 * (1 - 150 / 255.0);
550+
Sprite sprite;
551+
sprite.setInterface(&model);
552+
553+
VirtualMachine vm(&sprite, &m_engineMock, nullptr);
554+
vm.setBytecode(bytecode1);
555+
vm.setFunctions(functions);
556+
vm.setConstValues(constValues);
557+
558+
vm.run();
559+
ASSERT_EQ(vm.registerCount(), 0);
560+
ASSERT_EQ(model.penAttributes().color, QColor::fromHsv(240, 148, 253, 150));
561+
562+
vm.reset();
563+
vm.setBytecode(bytecode2);
564+
vm.run();
565+
ASSERT_EQ(vm.registerCount(), 0);
566+
ASSERT_EQ(model.penAttributes().color, QColor::fromHsv(240, 102, 255, 150));
567+
568+
vm.reset();
569+
vm.setBytecode(bytecode3);
570+
vm.run();
571+
ASSERT_EQ(vm.registerCount(), 0);
572+
ASSERT_EQ(model.penAttributes().color, QColor::fromHsv(240, 89, 255, 150));
573+
}
574+
508575
TEST_F(PenBlocksTest, ChangePenHueBy)
509576
{
510577
Compiler compiler(&m_engineMock);

0 commit comments

Comments
 (0)