Skip to content

Commit 5a5723d

Browse files
committed
Implement pen_setPenHueToNumber block
1 parent 2b35a88 commit 5a5723d

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/blocks/penblocks.cpp

Lines changed: 21 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_changePenHueBy", &compileChangePenHueBy);
33+
engine->addCompileFunction(this, "pen_setPenHueToNumber", &compileSetPenHueToNumber);
3334

3435
// Inputs
3536
engine->addInput(this, "COLOR", COLOR);
@@ -76,6 +77,12 @@ void PenBlocks::compileChangePenHueBy(libscratchcpp::Compiler *compiler)
7677
compiler->addFunctionCall(&changePenHueBy);
7778
}
7879

80+
void PenBlocks::compileSetPenHueToNumber(libscratchcpp::Compiler *compiler)
81+
{
82+
compiler->addInput(HUE);
83+
compiler->addFunctionCall(&setPenHueToNumber);
84+
}
85+
7986
unsigned int PenBlocks::clear(VirtualMachine *vm)
8087
{
8188
IPenLayer *penLayer = PenLayer::getProjectPenLayer(vm->engine());
@@ -140,6 +147,20 @@ unsigned int PenBlocks::changePenHueBy(libscratchcpp::VirtualMachine *vm)
140147
return 1;
141148
}
142149

150+
unsigned int PenBlocks::setPenHueToNumber(libscratchcpp::VirtualMachine *vm)
151+
{
152+
SpriteModel *model = getSpriteModel(vm);
153+
154+
if (model) {
155+
const double colorValue = vm->getInput(0, 1)->toDouble() / 2;
156+
setOrChangeColorParam(ColorParam::COLOR, colorValue, model->penState(), false, true);
157+
model->penState().transparency = 0;
158+
model->penState().updateColor();
159+
}
160+
161+
return 1;
162+
}
163+
143164
unsigned int PenBlocks::setPenColorToColor(libscratchcpp::VirtualMachine *vm)
144165
{
145166
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 compileChangePenHueBy(libscratchcpp::Compiler *compiler);
36+
static void compileSetPenHueToNumber(libscratchcpp::Compiler *compiler);
3637

3738
static unsigned int clear(libscratchcpp::VirtualMachine *vm);
3839
static unsigned int penDown(libscratchcpp::VirtualMachine *vm);
@@ -41,6 +42,7 @@ class PenBlocks : public libscratchcpp::IBlockSection
4142
static unsigned int changePenSizeBy(libscratchcpp::VirtualMachine *vm);
4243
static unsigned int setPenSizeTo(libscratchcpp::VirtualMachine *vm);
4344
static unsigned int changePenHueBy(libscratchcpp::VirtualMachine *vm);
45+
static unsigned int setPenHueToNumber(libscratchcpp::VirtualMachine *vm);
4446

4547
private:
4648
enum class ColorParam

test/blocks/pen_blocks_test.cpp

Lines changed: 69 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_changePenHueBy", &PenBlocks::compileChangePenHueBy));
71+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_setPenHueToNumber", &PenBlocks::compileSetPenHueToNumber));
7172

7273
// Inputs
7374
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "COLOR", PenBlocks::COLOR));
@@ -503,3 +504,71 @@ TEST_F(PenBlocksTest, ChangePenHueByImpl)
503504
ASSERT_EQ(vm.registerCount(), 0);
504505
ASSERT_EQ(model.penAttributes().color, QColor::fromHsv(145, 255, 255, 150));
505506
}
507+
508+
TEST_F(PenBlocksTest, SetPenHueToNumber)
509+
{
510+
Compiler compiler(&m_engineMock);
511+
512+
// set pen hue to (54.09)
513+
auto block1 = std::make_shared<Block>("a", "pen_setPenHueToNumber");
514+
addValueInput(block1, "HUE", PenBlocks::HUE, 54.09);
515+
516+
// set pen hue to (null block)
517+
auto block2 = std::make_shared<Block>("b", "pen_setPenHueToNumber");
518+
addObscuredInput(block2, "HUE", PenBlocks::HUE, createNullBlock("c"));
519+
520+
compiler.init();
521+
522+
EXPECT_CALL(m_engineMock, functionIndex(&PenBlocks::setPenHueToNumber)).WillOnce(Return(2));
523+
compiler.setBlock(block1);
524+
PenBlocks::compileSetPenHueToNumber(&compiler);
525+
526+
EXPECT_CALL(m_engineMock, functionIndex(&PenBlocks::setPenHueToNumber)).WillOnce(Return(2));
527+
compiler.setBlock(block2);
528+
PenBlocks::compileSetPenHueToNumber(&compiler);
529+
530+
compiler.end();
531+
532+
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 }));
533+
ASSERT_EQ(compiler.constValues().size(), 1);
534+
ASSERT_EQ(compiler.constValues()[0].toDouble(), 54.09);
535+
ASSERT_TRUE(compiler.variables().empty());
536+
ASSERT_TRUE(compiler.lists().empty());
537+
}
538+
539+
TEST_F(PenBlocksTest, SetPenHueToNumberImpl)
540+
{
541+
static unsigned int bytecode1[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT };
542+
static unsigned int bytecode2[] = { vm::OP_START, vm::OP_CONST, 1, vm::OP_EXEC, 0, vm::OP_HALT };
543+
static unsigned int bytecode3[] = { vm::OP_START, vm::OP_CONST, 2, vm::OP_EXEC, 0, vm::OP_HALT };
544+
static BlockFunc functions[] = { &PenBlocks::setPenHueToNumber };
545+
static Value constValues[] = { 125.7, -114.09, 489.4 };
546+
547+
SpriteModel model;
548+
QColor color = model.penAttributes().color;
549+
color.setAlpha(150);
550+
model.penState().setColor(color);
551+
Sprite sprite;
552+
sprite.setInterface(&model);
553+
554+
VirtualMachine vm(&sprite, &m_engineMock, nullptr);
555+
vm.setBytecode(bytecode1);
556+
vm.setFunctions(functions);
557+
vm.setConstValues(constValues);
558+
559+
vm.run();
560+
ASSERT_EQ(vm.registerCount(), 0);
561+
ASSERT_EQ(model.penAttributes().color, QColor::fromHsv(226, 255, 255, 255));
562+
563+
vm.reset();
564+
vm.setBytecode(bytecode2);
565+
vm.run();
566+
ASSERT_EQ(vm.registerCount(), 0);
567+
ASSERT_EQ(model.penAttributes().color, QColor::fromHsv(158, 255, 255, 255));
568+
569+
vm.reset();
570+
vm.setBytecode(bytecode3);
571+
vm.run();
572+
ASSERT_EQ(vm.registerCount(), 0);
573+
ASSERT_EQ(model.penAttributes().color, QColor::fromHsv(154, 255, 255, 255));
574+
}

0 commit comments

Comments
 (0)